diff --git a/nixos/modules/virtualisation/containers.nix b/nixos/modules/virtualisation/containers.nix index 637a8713904..7d445fa0951 100644 --- a/nixos/modules/virtualisation/containers.nix +++ b/nixos/modules/virtualisation/containers.nix @@ -676,7 +676,7 @@ in # Generate /etc/hosts entries for the containers. networking.extraHosts = concatStrings (mapAttrsToList (name: cfg: optionalString (cfg.localAddress != null) '' - ${cfg.localAddress} ${name}.containers + ${head (splitString "/" cfg.localAddress)} ${name}.containers '') config.containers); networking.dhcpcd.denyInterfaces = [ "ve-*" "vb-*" ]; diff --git a/nixos/tests/containers-hosts.nix b/nixos/tests/containers-hosts.nix new file mode 100644 index 00000000000..c7a85f190a5 --- /dev/null +++ b/nixos/tests/containers-hosts.nix @@ -0,0 +1,52 @@ +# Test for NixOS' container support. + +import ./make-test.nix ({ pkgs, ...} : { + name = "containers-hosts"; + meta = with pkgs.stdenv.lib.maintainers; { + maintainers = [ montag451 ]; + }; + + machine = + { config, pkgs, lib, ... }: + { + virtualisation.memorySize = 256; + virtualisation.vlans = []; + + networking.bridges.br0.interfaces = []; + networking.interfaces.br0 = { + ip4 = [ { address = "10.11.0.254"; prefixLength = 24; } ]; + }; + + # Force /etc/hosts to be the only source for host name resolution + environment.etc."nsswitch.conf".text = lib.mkForce '' + hosts: files + ''; + + containers.simple = { + autoStart = true; + privateNetwork = true; + localAddress = "10.10.0.1"; + hostAddress = "10.10.0.254"; + + config = {}; + }; + + containers.netmask = { + autoStart = true; + privateNetwork = true; + hostBridge = "br0"; + localAddress = "10.11.0.1/24"; + + config = {}; + }; + }; + + testScript = '' + startAll; + $machine->waitForUnit("default.target"); + + # Ping the containers using the entries added in /etc/hosts + $machine->succeed("ping -n -c 1 simple.containers"); + $machine->succeed("ping -n -c 1 netmask.containers"); + ''; +})