From ac7edeebfd4ca45d4c6569f36dc5d6dd10aa58e9 Mon Sep 17 00:00:00 2001 From: Martin Bornhold Date: Thu, 16 Nov 2017 13:55:20 +0100 Subject: [PATCH 1/2] nixos-container: Make show-ip work together with ipv4 + netmask The containers local address can be given as ipv4 only or with a subnetmask in CIDR notation in the container configuration, see [1]. This works fine but the 'nixos-container show-ip' only supports plain ipv4 addresses without the netmask suffix. Changed the regex to also match in case of a CIDR netmask suffix. [1] https://github.com/NixOS/nixpkgs/blob/9939032e35bb144bc8f6b57ed6325089910f738c/nixos/modules/virtualisation/containers.nix#L382 --- pkgs/tools/virtualization/nixos-container/nixos-container.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/tools/virtualization/nixos-container/nixos-container.pl b/pkgs/tools/virtualization/nixos-container/nixos-container.pl index 207177133a57..fefdcd614a56 100755 --- a/pkgs/tools/virtualization/nixos-container/nixos-container.pl +++ b/pkgs/tools/virtualization/nixos-container/nixos-container.pl @@ -331,7 +331,7 @@ elsif ($action eq "run") { elsif ($action eq "show-ip") { my $s = read_file($confFile) or die; - $s =~ /^LOCAL_ADDRESS=([0-9\.]+)$/m or die "$0: cannot get IP address\n"; + $s =~ /^LOCAL_ADDRESS=([0-9\.]+)(\/[0-9]+)?$/m or die "$0: cannot get IP address\n"; print "$1\n"; } From 25b71e18f50ed951ada5ad63e32f25d990abec5e Mon Sep 17 00:00:00 2001 From: Martin Bornhold Date: Thu, 16 Nov 2017 14:54:13 +0100 Subject: [PATCH 2/2] nixos-container: Modify existing test to cover show-ip command Modified the existing test to check that the 'nixos-container show-ip' command can handle ipv4 addresses with submask in CIDR notation. --- nixos/tests/containers-bridge.nix | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/nixos/tests/containers-bridge.nix b/nixos/tests/containers-bridge.nix index 598abd22e61b..b8d4759684cc 100644 --- a/nixos/tests/containers-bridge.nix +++ b/nixos/tests/containers-bridge.nix @@ -69,6 +69,12 @@ import ./make-test.nix ({ pkgs, ...} : { $machine->succeed("ping -n -c 1 $ip6"); $machine->succeed("curl --fail http://[$ip6]/ > /dev/null"); + # Check that nixos-container show-ip works in case of an ipv4 address with + # subnetmask in CIDR notation. + my $result = $machine->succeed("nixos-container show-ip webserver"); + chomp $result; + $result eq $ip or die; + # Stop the container. $machine->succeed("nixos-container stop webserver"); $machine->fail("curl --fail --connect-timeout 2 http://$ip/ > /dev/null");