Skip to content

Commit 9ba099b

Browse files
Enzimemergify[bot]
authored andcommitted
nixos-anywhere: allow users to specify custom path NixOS configurations
This is useful for users who use a similar `extendModules` pattern like `virtualisation.vmVariant`, where an extended NixOS configuration lives inside an existing configuration: nixos-anywhere --flake .#nixosConfigurations.alpha.config.virtualisation.vmVariant
1 parent d4e91f6 commit 9ba099b

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/nixos-anywhere.sh

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,11 @@ parseArgs() {
313313
echo 'For example, to use the output nixosConfigurations.foo from the flake.nix, append "#foo" to the flake-uri.' >&2
314314
exit 1
315315
fi
316+
317+
# Support .#foo shorthand
318+
if [[ $flakeAttr != nixosConfigurations.* ]]; then
319+
flakeAttr="nixosConfigurations.\"$flakeAttr\".config"
320+
fi
316321
fi
317322

318323
}
@@ -363,7 +368,7 @@ runVmTest() {
363368
--no-link \
364369
-L \
365370
"${nixOptions[@]}" \
366-
"${flake}#nixosConfigurations.\"${flakeAttr}\".config.system.build.installTest"
371+
"${flake}#${flakeAttr}.system.build.installTest"
367372
}
368373

369374
uploadSshKey() {
@@ -539,11 +544,11 @@ runDisko() {
539544
step Building disko script
540545
# We need to do a nix copy first because nix build doesn't have --no-check-sigs
541546
# Use ssh:// here to avoid https://github.com/NixOS/nix/issues/7359
542-
nixCopy --to "ssh://$sshConnection" "${flake}#nixosConfigurations.\"${flakeAttr}\".config.system.build.${diskoMode}Script" \
547+
nixCopy --to "ssh://$sshConnection" "${flake}#${flakeAttr}.system.build.${diskoMode}Script" \
543548
--derivation --no-check-sigs
544549
# If we don't use ssh-ng here, we get `error: operation 'getFSAccessor' is not supported by store`
545550
diskoScript=$(
546-
nixBuild "${flake}#nixosConfigurations.\"${flakeAttr}\".config.system.build.${diskoMode}Script" \
551+
nixBuild "${flake}#${flakeAttr}.system.build.${diskoMode}Script" \
547552
--eval-store auto --store "ssh-ng://$sshConnection?ssh-key=$sshKeyDir/nixos-anywhere"
548553
)
549554
fi
@@ -561,11 +566,11 @@ nixosInstall() {
561566
step Building the system closure
562567
# We need to do a nix copy first because nix build doesn't have --no-check-sigs
563568
# Use ssh:// here to avoid https://github.com/NixOS/nix/issues/7359
564-
nixCopy --to "ssh://$sshConnection?remote-store=local?root=/mnt" "${flake}#nixosConfigurations.\"${flakeAttr}\".config.system.build.toplevel" \
569+
nixCopy --to "ssh://$sshConnection?remote-store=local?root=/mnt" "${flake}#${flakeAttr}.system.build.toplevel" \
565570
--derivation --no-check-sigs
566571
# If we don't use ssh-ng here, we get `error: operation 'getFSAccessor' is not supported by store`
567572
nixosSystem=$(
568-
nixBuild "${flake}#nixosConfigurations.\"${flakeAttr}\".config.system.build.toplevel" \
573+
nixBuild "${flake}#${flakeAttr}.system.build.toplevel" \
569574
--eval-store auto --store "ssh-ng://$sshConnection?ssh-key=$sshKeyDir/nixos-anywhere&remote-store=local?root=/mnt"
570575
)
571576
fi
@@ -626,9 +631,9 @@ main() {
626631
if [[ -n ${flake} ]]; then
627632
if [[ ${buildOnRemote} == "n" ]] && [[ ${hardwareConfigBackend} == "none" ]]; then
628633
if [[ ${phases[disko]} == 1 ]]; then
629-
diskoScript=$(nixBuild "${flake}#nixosConfigurations.\"${flakeAttr}\".config.system.build.${diskoMode}Script")
634+
diskoScript=$(nixBuild "${flake}#${flakeAttr}.system.build.${diskoMode}Script")
630635
fi
631-
nixosSystem=$(nixBuild "${flake}#nixosConfigurations.\"${flakeAttr}\".config.system.build.toplevel")
636+
nixosSystem=$(nixBuild "${flake}#${flakeAttr}.system.build.toplevel")
632637
fi
633638
elif [[ -n ${diskoScript} ]] && [[ -n ${nixosSystem} ]]; then
634639
if [[ ! -e ${diskoScript} ]] || [[ ! -e ${nixosSystem} ]]; then
@@ -684,9 +689,9 @@ main() {
684689

685690
if [[ ${buildOnRemote} == "n" ]] && [[ -n ${flake} ]] && [[ ${hardwareConfigBackend} != "none" ]]; then
686691
if [[ ${phases[disko]} == 1 ]]; then
687-
diskoScript=$(nixBuild "${flake}#nixosConfigurations.\"${flakeAttr}\".config.system.build.${diskoMode}Script")
692+
diskoScript=$(nixBuild "${flake}#${flakeAttr}.system.build.${diskoMode}Script")
688693
fi
689-
nixosSystem=$(nixBuild "${flake}#nixosConfigurations.\"${flakeAttr}\".config.system.build.toplevel")
694+
nixosSystem=$(nixBuild "${flake}#${flakeAttr}.system.build.toplevel")
690695
fi
691696

692697
# Installation will fail if non-root user is used for installer.

0 commit comments

Comments
 (0)