Merge pull request #15967 from ericbmerritt/fix/nixos-container

nixos-container: fix allow alternative nixos paths
This commit is contained in:
Joachim F 2016-08-04 16:50:05 +02:00 committed by GitHub
commit 5754eec8f6

View File

@ -6,6 +6,7 @@ use File::Path;
use File::Slurp;
use Fcntl ':flock';
use Getopt::Long qw(:config gnu_getopt);
use Cwd 'abs_path';
my $nsenter = "@utillinux@/bin/nsenter";
my $su = "@su@";
@ -18,13 +19,13 @@ umask 0022;
sub showHelp {
print <<EOF;
Usage: nixos-container list
nixos-container create <container-name> [--system-path <path>] [--config <string>] [--ensure-unique-name] [--auto-start]
nixos-container create <container-name> [--nixos-path <path>] [--system-path <path>] [--config-file <path>] [--config <string>] [--ensure-unique-name] [--auto-start]
nixos-container destroy <container-name>
nixos-container start <container-name>
nixos-container stop <container-name>
nixos-container kill <container-name> [--signal <signal-specifier>]
nixos-container status <container-name>
nixos-container update <container-name> [--config <string>]
nixos-container update <container-name> [--config <string>] [--config-file <path>]
nixos-container login <container-name>
nixos-container root-login <container-name>
nixos-container run <container-name> -- args...
@ -35,22 +36,30 @@ EOF
}
my $systemPath;
my $nixosPath;
my $ensureUniqueName = 0;
my $autoStart = 0;
my $extraConfig;
my $signal;
my $configFile;
GetOptions(
"help" => sub { showHelp() },
"ensure-unique-name" => \$ensureUniqueName,
"auto-start" => \$autoStart,
"system-path=s" => \$systemPath,
"config=s" => \$extraConfig,
"signal=s" => \$signal
"nixos-path=s" => \$nixosPath,
"config=s" => \$extraConfig,
"config-file=s" => \$configFile
) or exit 1;
my $action = $ARGV[0] or die "$0: no action specified\n";
if (defined $configFile and defined $extraConfig) {
die "--config and --config-file are mutually incompatible. " .
"Please define on or the other, but not both";
}
# Execute the selected action.
@ -71,6 +80,17 @@ $containerName =~ /^[a-zA-Z0-9\-]+$/ or die "$0: invalid container name\n";
sub writeNixOSConfig {
my ($nixosConfigFile) = @_;
my $localExtraConfig = "";
if ($extraConfig) {
$localExtraConfig = $extraConfig
} elsif ($configFile) {
my $resolvedFile = abs_path($configFile);
$localExtraConfig = "imports = [ $resolvedFile ];"
}
my $nixosConfig = <<EOF;
{ config, lib, pkgs, ... }:
@ -79,7 +99,7 @@ with lib;
{ boot.isContainer = true;
networking.hostName = mkDefault "$containerName";
networking.useDHCP = false;
$extraConfig
$localExtraConfig
}
EOF
@ -158,11 +178,12 @@ if ($action eq "create") {
} else {
mkpath("$root/etc/nixos", 0, 0755);
my $nixenvF = $nixosPath // "<nixpkgs/nixos>";
my $nixosConfigFile = "$root/etc/nixos/configuration.nix";
writeNixOSConfig $nixosConfigFile;
system("nix-env", "-p", "$profileDir/system",
"-I", "nixos-config=$nixosConfigFile", "-f", "<nixpkgs/nixos>",
"-I", "nixos-config=$nixosConfigFile", "-f", "$nixenvF",
"--set", "-A", "system") == 0
or die "$0: failed to build initial container configuration\n";
}
@ -270,7 +291,10 @@ elsif ($action eq "update") {
# FIXME: may want to be more careful about clobbering the existing
# configuration.nix.
writeNixOSConfig $nixosConfigFile if (defined $extraConfig && $extraConfig ne "");
if ((defined $extraConfig && $extraConfig ne "") ||
(defined $configFile && $configFile ne "")) {
writeNixOSConfig $nixosConfigFile;
}
system("nix-env", "-p", "$profileDir/system",
"-I", "nixos-config=$nixosConfigFile", "-f", "<nixpkgs/nixos>",