nixos-{rebuild,container}: Use flakeref#attrpath syntax

This makes them consistent with the 'nix' command line.
This commit is contained in:
Eelco Dolstra 2019-09-20 18:37:17 +02:00
parent 20087bd553
commit fb051f0dec
3 changed files with 27 additions and 38 deletions

View File

@ -83,10 +83,6 @@
<option>--flake</option> <replaceable>flake-uri</replaceable>
</arg>
<arg>
<option>--config</option> <replaceable>name</replaceable>
</arg>
<sbr />
<arg>
@ -525,7 +521,7 @@
<varlistentry>
<term>
<option>--flake</option> <replaceable>flake-uri</replaceable>
<option>--flake</option> <replaceable>flake-uri</replaceable>[<replaceable>name</replaceable>]
</term>
<listitem>
<para>
@ -533,22 +529,9 @@
the directory containing the target of the symlink
<filename>/etc/nixos/flake.nix</filename>, if it exists. The
flake must contain an output named
<literal>nixosConfigurations.<replaceable>name</replaceable></literal>,
where <replaceable>name</replaceable> denotes the name of the
configuration and can be specified using the
<option>--config</option> option.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<option>--config</option> <replaceable>name</replaceable>
</term>
<listitem>
<para>
Specifies which NixOS configuration to use from the
flake. Defaults to the current hostname.
<literal>nixosConfigurations.<replaceable>name</replaceable></literal>. If
<replaceable>name</replaceable> is omitted, it default to the
current host name.
</para>
</listitem>
</varlistentry>

View File

@ -100,10 +100,6 @@ while [ "$#" -gt 0 ]; do
flake="$1"
shift 1
;;
--config)
flakeAttr="nixosConfigurations.$1"
shift 1
;;
*)
echo "$0: unknown option \`$i'"
exit 1
@ -254,12 +250,20 @@ fi
# For convenience, use the hostname as the default configuration to
# build from the flake.
if [[ -n $flake && -z $flakeAttr ]]; then
hostname=$(cat /proc/sys/kernel/hostname)
if [[ -z $hostname ]]; then
hostname=default
if [[ -n $flake ]]; then
if [[ $flake =~ ^(.*)\#([^\#\"]*)$ ]]; then
flake="${BASH_REMATCH[1]}"
flakeAttr="${BASH_REMATCH[2]}"
fi
if [[ -z $flakeAttr ]]; then
hostname=$(cat /proc/sys/kernel/hostname)
if [[ -z $hostname ]]; then
hostname=default
fi
flakeAttr="nixosConfigurations.\"$hostname\""
else
flakeAttr="nixosConfigurations.\"$flakeAttr\""
fi
flakeAttr="nixosConfigurations.\"$hostname\""
fi
# Resolve the flake.
@ -391,17 +395,13 @@ fi
# or "boot"), or just build it and create a symlink "result" in the
# current directory (for "build" and "test").
if [ -z "$rollback" ]; then
if [[ -z $flake ]]; then
echo "building the system configuration..." >&2
else
echo "building the system configuration '$flake:$flakeAttr'..." >&2
fi
echo "building the system configuration..." >&2
if [ "$action" = switch -o "$action" = boot ]; then
if [[ -z $flake ]]; then
pathToConfig="$(nixBuild '<nixpkgs/nixos>' --no-out-link -A system "${extraBuildFlags[@]}")"
else
outLink=$tmpDir/result
nix build "$flake:$flakeAttr.config.system.build.toplevel" --keep-going "${extraBuildFlags[@]}" --out-link $outLink
nix build "$flake#$flakeAttr.config.system.build.toplevel" --keep-going "${extraBuildFlags[@]}" --out-link $outLink
pathToConfig="$(readlink -f $outLink)"
fi
copyToTarget "$pathToConfig"
@ -410,7 +410,7 @@ if [ -z "$rollback" ]; then
if [[ -z $flake ]]; then
pathToConfig="$(nixBuild '<nixpkgs/nixos>' -A system -k "${extraBuildFlags[@]}")"
else
nix build "$flake:$flakeAttr.config.system.build.toplevel" --keep-going "${extraBuildFlags[@]}"
nix build "$flake#$flakeAttr.config.system.build.toplevel" --keep-going "${extraBuildFlags[@]}"
pathToConfig="$(readlink -f ./result)"
fi
elif [ "$action" = build-vm ]; then

View File

@ -64,6 +64,7 @@ my $configFile;
my $hostAddress;
my $localAddress;
my $flake;
my $flakeAttr = "container";
GetOptions(
"help" => sub { showHelp() },
@ -92,6 +93,11 @@ if (defined $configFile and defined $extraConfig) {
"Please define on or the other, but not both";
}
if (defined $flake && $flake =~ /^(.*)#([^#"]+)$/) {
$flake = $1;
$flakeAttr = $2;
}
# Execute the selected action.
mkpath("/etc/containers", 0, 0755);
@ -137,7 +143,7 @@ EOF
sub buildFlake {
system("nix", "build", "-o", "$systemPath.tmp", "--",
"$flake:nixosConfigurations.container.config.system.build.toplevel") == 0
"$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;
unlink("$systemPath.tmp");