mirror of
https://github.com/NixOS/nixpkgs.git
synced 2024-11-24 07:53:19 +00:00
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.
This commit is contained in:
parent
f932910323
commit
ddd8f0cf66
@ -17,7 +17,7 @@ umask 0022;
|
||||
sub showHelp {
|
||||
print <<EOF;
|
||||
Usage: nixos-container list
|
||||
nixos-container create <container-name> [--config <string>] [--ensure-unique-name] [--auto-start]
|
||||
nixos-container create <container-name> [--system-path <path>] [--config <string>] [--ensure-unique-name] [--auto-start]
|
||||
nixos-container destroy <container-name>
|
||||
nixos-container start <container-name>
|
||||
nixos-container stop <container-name>
|
||||
@ -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);
|
||||
|
||||
# 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", "<nixpkgs/nixos>",
|
||||
"--set", "-A", "system") == 0
|
||||
or die "$0: failed to build initial container configuration\n";
|
||||
}
|
||||
|
||||
print "$containerName\n" if $ensureUniqueName;
|
||||
exit 0;
|
||||
|
Loading…
Reference in New Issue
Block a user