nixos-container: Add flake support
E.g. to create a container that runs the NixOS homepage: $ nixos-container create homepage --flake nixos-homepage And to upgrade it: $ nixos-container update homepage
This commit is contained in:
parent
cfd468adbb
commit
20087bd553
@ -22,13 +22,27 @@ $ENV{"NIXOS_CONFIG"} = "";
|
|||||||
sub showHelp {
|
sub showHelp {
|
||||||
print <<EOF;
|
print <<EOF;
|
||||||
Usage: nixos-container list
|
Usage: nixos-container list
|
||||||
nixos-container create <container-name> [--nixos-path <path>] [--system-path <path>] [--config-file <path>] [--config <string>] [--ensure-unique-name] [--auto-start] [--bridge <iface>] [--port <port>] [--host-address <string>] [--local-address <string>]
|
nixos-container create <container-name>
|
||||||
|
[--nixos-path <path>]
|
||||||
|
[--system-path <path>]
|
||||||
|
[--config <string>]
|
||||||
|
[--config-file <path>]
|
||||||
|
[--flake <flakeref>]
|
||||||
|
[--ensure-unique-name]
|
||||||
|
[--auto-start]
|
||||||
|
[--bridge <iface>]
|
||||||
|
[--port <port>]
|
||||||
|
[--host-address <string>]
|
||||||
|
[--local-address <string>]
|
||||||
nixos-container destroy <container-name>
|
nixos-container destroy <container-name>
|
||||||
nixos-container start <container-name>
|
nixos-container start <container-name>
|
||||||
nixos-container stop <container-name>
|
nixos-container stop <container-name>
|
||||||
nixos-container terminate <container-name>
|
nixos-container terminate <container-name>
|
||||||
nixos-container status <container-name>
|
nixos-container status <container-name>
|
||||||
nixos-container update <container-name> [--config <string>] [--config-file <path>]
|
nixos-container update <container-name>
|
||||||
|
[--config <string>]
|
||||||
|
[--config-file <path>]
|
||||||
|
[--flake <flakeref>]
|
||||||
nixos-container login <container-name>
|
nixos-container login <container-name>
|
||||||
nixos-container root-login <container-name>
|
nixos-container root-login <container-name>
|
||||||
nixos-container run <container-name> -- args...
|
nixos-container run <container-name> -- args...
|
||||||
@ -49,6 +63,7 @@ my $signal;
|
|||||||
my $configFile;
|
my $configFile;
|
||||||
my $hostAddress;
|
my $hostAddress;
|
||||||
my $localAddress;
|
my $localAddress;
|
||||||
|
my $flake;
|
||||||
|
|
||||||
GetOptions(
|
GetOptions(
|
||||||
"help" => sub { showHelp() },
|
"help" => sub { showHelp() },
|
||||||
@ -63,6 +78,7 @@ GetOptions(
|
|||||||
"config-file=s" => \$configFile,
|
"config-file=s" => \$configFile,
|
||||||
"host-address=s" => \$hostAddress,
|
"host-address=s" => \$hostAddress,
|
||||||
"local-address=s" => \$localAddress,
|
"local-address=s" => \$localAddress,
|
||||||
|
"flake=s" => \$flake,
|
||||||
) or exit 1;
|
) or exit 1;
|
||||||
|
|
||||||
if (defined $hostAddress and !defined $localAddress or defined $localAddress and !defined $hostAddress) {
|
if (defined $hostAddress and !defined $localAddress or defined $localAddress and !defined $hostAddress) {
|
||||||
@ -97,8 +113,6 @@ sub writeNixOSConfig {
|
|||||||
|
|
||||||
my $localExtraConfig = "";
|
my $localExtraConfig = "";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if ($extraConfig) {
|
if ($extraConfig) {
|
||||||
$localExtraConfig = $extraConfig
|
$localExtraConfig = $extraConfig
|
||||||
} elsif ($configFile) {
|
} elsif ($configFile) {
|
||||||
@ -121,6 +135,14 @@ EOF
|
|||||||
write_file($nixosConfigFile, $nixosConfig);
|
write_file($nixosConfigFile, $nixosConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub buildFlake {
|
||||||
|
system("nix", "build", "-o", "$systemPath.tmp", "--",
|
||||||
|
"$flake:nixosConfigurations.container.config.system.build.toplevel") == 0
|
||||||
|
or die "$0: failed to build container from flake '$flake'\n";
|
||||||
|
$systemPath = readlink("$systemPath.tmp") or die;
|
||||||
|
unlink("$systemPath.tmp");
|
||||||
|
}
|
||||||
|
|
||||||
if ($action eq "create") {
|
if ($action eq "create") {
|
||||||
# Acquire an exclusive lock to prevent races with other
|
# Acquire an exclusive lock to prevent races with other
|
||||||
# invocations of ‘nixos-container create’.
|
# invocations of ‘nixos-container create’.
|
||||||
@ -176,6 +198,7 @@ if ($action eq "create") {
|
|||||||
push @conf, "HOST_BRIDGE=$bridge\n";
|
push @conf, "HOST_BRIDGE=$bridge\n";
|
||||||
push @conf, "HOST_PORT=$port\n";
|
push @conf, "HOST_PORT=$port\n";
|
||||||
push @conf, "AUTO_START=$autoStart\n";
|
push @conf, "AUTO_START=$autoStart\n";
|
||||||
|
push @conf, "FLAKE=$flake\n" if defined $flake;
|
||||||
write_file($confFile, \@conf);
|
write_file($confFile, \@conf);
|
||||||
|
|
||||||
close($lock);
|
close($lock);
|
||||||
@ -191,6 +214,10 @@ if ($action eq "create") {
|
|||||||
mkpath($profileDir, 0, 0755);
|
mkpath($profileDir, 0, 0755);
|
||||||
|
|
||||||
# Build/set the initial configuration.
|
# Build/set the initial configuration.
|
||||||
|
if (defined $flake) {
|
||||||
|
buildFlake();
|
||||||
|
}
|
||||||
|
|
||||||
if (defined $systemPath) {
|
if (defined $systemPath) {
|
||||||
system("nix-env", "-p", "$profileDir/system", "--set", $systemPath) == 0
|
system("nix-env", "-p", "$profileDir/system", "--set", $systemPath) == 0
|
||||||
or die "$0: failed to set initial container configuration\n";
|
or die "$0: failed to set initial container configuration\n";
|
||||||
@ -326,19 +353,35 @@ elsif ($action eq "status") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
elsif ($action eq "update") {
|
elsif ($action eq "update") {
|
||||||
my $nixosConfigFile = "$root/etc/nixos/configuration.nix";
|
|
||||||
|
|
||||||
# FIXME: may want to be more careful about clobbering the existing
|
# Unless overriden on the command line, rebuild the flake recorded
|
||||||
# configuration.nix.
|
# in the container config file. FIXME: read the container config
|
||||||
if ((defined $extraConfig && $extraConfig ne "") ||
|
# in a more sensible way.
|
||||||
(defined $configFile && $configFile ne "")) {
|
if (!defined $flake && !defined $configFile && !defined $extraConfig) {
|
||||||
writeNixOSConfig $nixosConfigFile;
|
my $s = read_file($confFile);
|
||||||
|
$s =~ /^FLAKE=(.*)$/m;
|
||||||
|
$flake = $1;
|
||||||
}
|
}
|
||||||
|
|
||||||
system("nix-env", "-p", "$profileDir/system",
|
if (defined $flake) {
|
||||||
"-I", "nixos-config=$nixosConfigFile", "-f", "<nixpkgs/nixos>",
|
buildFlake();
|
||||||
"--set", "-A", "system") == 0
|
system("nix-env", "-p", "$profileDir/system", "--set", $systemPath) == 0
|
||||||
or die "$0: failed to build container configuration\n";
|
or die "$0: failed to set container configuration\n";
|
||||||
|
} else {
|
||||||
|
my $nixosConfigFile = "$root/etc/nixos/configuration.nix";
|
||||||
|
|
||||||
|
# FIXME: may want to be more careful about clobbering the existing
|
||||||
|
# configuration.nix.
|
||||||
|
if ((defined $extraConfig && $extraConfig ne "") ||
|
||||||
|
(defined $configFile && $configFile ne "")) {
|
||||||
|
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 container configuration\n";
|
||||||
|
}
|
||||||
|
|
||||||
if (isContainerRunning) {
|
if (isContainerRunning) {
|
||||||
print STDERR "reloading container...\n";
|
print STDERR "reloading container...\n";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user