From ddd8f0cf661e2c1818eb19476a2d62a417de4873 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 19 Aug 2014 15:47:51 +0200 Subject: nixos-container --create: Add option --system-path This allows creating a container from an existing system store path, which is especially nice for NixOps-deployed hosts because they don't need a Nixpkgs tree anymore. --- nixos/modules/virtualisation/nixos-container.pl | 28 ++++++++++++++++--------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'nixos') diff --git a/nixos/modules/virtualisation/nixos-container.pl b/nixos/modules/virtualisation/nixos-container.pl index 3975e4b62f23..06839874ef02 100644 --- a/nixos/modules/virtualisation/nixos-container.pl +++ b/nixos/modules/virtualisation/nixos-container.pl @@ -17,7 +17,7 @@ umask 0022; sub showHelp { print < [--config ] [--ensure-unique-name] [--auto-start] + nixos-container create [--system-path ] [--config ] [--ensure-unique-name] [--auto-start] nixos-container destroy nixos-container start nixos-container stop @@ -31,6 +31,7 @@ EOF exit 0; } +my $systemPath; my $ensureUniqueName = 0; my $autoStart = 0; my $extraConfig; @@ -39,6 +40,7 @@ GetOptions( "help" => sub { showHelp() }, "ensure-unique-name" => \$ensureUniqueName, "auto-start" => \$autoStart, + "system-path=s" => \$systemPath, "config=s" => \$extraConfig ) or exit 1; @@ -132,11 +134,6 @@ if ($action eq "create") { print STDERR "host IP is $hostAddress, container IP is $localAddress\n"; - mkpath("$root/etc/nixos", 0, 0755); - - my $nixosConfigFile = "$root/etc/nixos/configuration.nix"; - writeNixOSConfig $nixosConfigFile; - # The per-container directory is restricted to prevent users on # the host from messing with guest users who happen to have the # same uid. @@ -145,10 +142,21 @@ if ($action eq "create") { $profileDir = "$profileDir/$containerName"; mkpath($profileDir, 0, 0755); - system("nix-env", "-p", "$profileDir/system", - "-I", "nixos-config=$nixosConfigFile", "-f", "", - "--set", "-A", "system") == 0 - or die "$0: failed to build initial container configuration\n"; + # Build/set the initial configuration. + if (defined $systemPath) { + system("nix-env", "-p", "$profileDir/system", "--set", $systemPath) == 0 + or die "$0: failed to set initial container configuration\n"; + } else { + mkpath("$root/etc/nixos", 0, 0755); + + my $nixosConfigFile = "$root/etc/nixos/configuration.nix"; + writeNixOSConfig $nixosConfigFile; + + system("nix-env", "-p", "$profileDir/system", + "-I", "nixos-config=$nixosConfigFile", "-f", "", + "--set", "-A", "system") == 0 + or die "$0: failed to build initial container configuration\n"; + } print "$containerName\n" if $ensureUniqueName; exit 0; -- cgit 1.4.1