From ed49c901ddff4677a6ef8ad914c332dfa64af5ae Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 30 Jun 2020 13:42:48 +0200 Subject: [PATCH] nixos-container: Pass through several Nix flags Since nixos-container calls nix-env / nix-build, it's useful to support various evaluation-related options. --- .../nixos-container/nixos-container.pl | 38 +++++++++++++++++-- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/pkgs/tools/virtualization/nixos-container/nixos-container.pl b/pkgs/tools/virtualization/nixos-container/nixos-container.pl index dc05c974bd0..02ad6af90da 100755 --- a/pkgs/tools/virtualization/nixos-container/nixos-container.pl +++ b/pkgs/tools/virtualization/nixos-container/nixos-container.pl @@ -5,7 +5,7 @@ use POSIX; use File::Path; use File::Slurp; use Fcntl ':flock'; -use Getopt::Long qw(:config gnu_getopt); +use Getopt::Long qw(:config gnu_getopt no_bundling); use Cwd 'abs_path'; use Time::HiRes; @@ -68,6 +68,22 @@ my $localAddress; my $flake; my $flakeAttr = "container"; +# Nix passthru flags. +my @nixFlags; +my @nixFlags2; + +sub copyNixFlags0 { push @nixFlags, "--$_[0]"; } +sub copyNixFlags1 { push @nixFlags, "--$_[0]", $_[1]; } + +# Ugly hack to handle flags that take two arguments, like --option. +sub copyNixFlags2 { + if (scalar(@nixFlags2) % 3 == 0) { + push @nixFlags2, "--$_[0]", $_[1]; + } else { + push @nixFlags2, $_[1]; + } +} + GetOptions( "help" => sub { showHelp() }, "ensure-unique-name" => \$ensureUniqueName, @@ -82,8 +98,22 @@ GetOptions( "host-address=s" => \$hostAddress, "local-address=s" => \$localAddress, "flake=s" => \$flake, + # Nix passthru options. + "log-format=s" => \©NixFlags1, + "option=s{2}" => \©NixFlags2, + "impure" => \©NixFlags0, + "update-input=s" => \©NixFlags1, + "override-input=s{2}" => \©NixFlags2, + "commit-lock-file" => \©NixFlags0, + "no-registries" => \©NixFlags0, + "no-update-lock-file" => \©NixFlags0, + "no-write-lock-file" => \©NixFlags0, + "no-allow-dirty" => \©NixFlags0, + "recreate-lock-file" => \©NixFlags0, ) or exit 1; +push @nixFlags, @nixFlags2; + if (defined $hostAddress and !defined $localAddress or defined $localAddress and !defined $hostAddress) { die "With --host-address set, --local-address is required as well!"; } @@ -144,7 +174,7 @@ EOF } sub buildFlake { - system("nix", "build", "-o", "$systemPath.tmp", "--", + system("nix", "build", "-o", "$systemPath.tmp", @nixFlags, "--", "$flake#nixosConfigurations.\"$flakeAttr\".config.system.build.toplevel") == 0 or die "$0: failed to build container from flake '$flake'\n"; $systemPath = readlink("$systemPath.tmp") or die; @@ -251,7 +281,7 @@ if ($action eq "create") { system("nix-env", "-p", "$profileDir/system", "-I", "nixos-config=$nixosConfigFile", "-f", "$nixenvF", - "--set", "-A", "system") == 0 + "--set", "-A", "system", @nixFlags) == 0 or do { clearContainerState($profileDir, "$profileDir/$containerName", $root, $confFile); die "$0: failed to build initial container configuration\n" @@ -400,7 +430,7 @@ elsif ($action eq "update") { my $nixenvF = $nixosPath // ""; system("nix-env", "-p", "$profileDir/system", "-I", "nixos-config=$nixosConfigFile", "-f", $nixenvF, - "--set", "-A", "system") == 0 + "--set", "-A", "system", @nixFlags) == 0 or die "$0: failed to build container configuration\n"; }