NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
17.46k stars 13.66k forks source link

Document: bridged nixos-container to get IP via DHCP #62131

Open davidak opened 5 years ago

davidak commented 5 years ago

Issue description

I want to spawn a nixos-container that gets an IP via DHCP in the same network as my notebook (as it was connected to the same switch).

This works with VMs in VirtualBox using the "Bridged Adapter" setting.

How can i achieve that using nixos-container?

A documentation would be great!

https://nixos.org/nixos/manual/index.html#ch-containers

It currently only explains NAT, but i want to access the container from the network using it's own IP.

Steps to reproduce

I tried

  networking.bridges = {
    br0 = {
      interfaces = [ "enp0s31f6" ];
    };
  };

  networking.networkmanager.unmanaged = [ "interface-name:vb-*" ];

  containers = {
    target0 = {
      autoStart = true;
      config = targetConfig;
      privateNetwork = true;
      hostBridge = "br0";
      #localAddress = "192.168.0.100/24";
    };
  };

Inside container config:

...
{ boot.isContainer = true;
  networking.useDHCP = mkForce true;
  networking.firewall.enable = false;
...

With this config, my notebooks network stops working and the container has a link local address.

[root@ethmoid:~]# nixos-container run target0 -- ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0@if114: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 3a:0b:9c:e6:ee:8c brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 169.254.129.65/16 brd 169.254.255.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::380b:9cff:fee6:ee8c/64 scope link 
       valid_lft forever preferred_lft forever

Technical details

davidak commented 5 years ago

It works when i set the IP on the bridge!

  networking = {
    ...
    bridges = {
      br0 = {
        interfaces = [ "enp0s25" ];
      };
    };

    interfaces = {
      br0.ipv4.addresses = [
        { address = "192.168.10.214"; prefixLength = 24; }
      ];
    };

    nameservers = [ "192.168.50.253" "192.168.10.254" ];
    defaultGateway = { address = "192.168.10.254"; interface = "br0"; };
};
stale[bot] commented 4 years ago

Thank you for your contributions.

This has been automatically marked as stale because it has had no activity for 180 days.

If this is still important to you, we ask that you leave a comment below. Your comment can be as simple as "still important to me". This lets people see that at least one person still cares about this. Someone will have to do this at most twice a year if there is no other activity.

Here are suggestions that might help resolve this more quickly:

  1. Search for maintainers and people that previously touched the related code and @ mention them in a comment.
  2. Ask on the NixOS Discourse.
  3. Ask on the #nixos channel on irc.freenode.net.
srid commented 1 year ago

@davidak Could you show your full configuration (host & guest) that made it work?

davidak commented 1 year ago

I don't have access to the machine and i think i used another approach in the end.

But the code in the other posts should be enough. What is unclear specifically?