2 # shellcheck shell=bash
4 if [ -x "@runtimeShell@" ]; then export SHELL
="@runtimeShell@"; fi;
8 shopt -s inherit_errexit
10 export PATH
=@path@
:$PATH
13 exec man nixos-rebuild
18 # Parse the command line.
23 flakeFlags
=(--extra-experimental-features 'nix-command flakes')
30 profile
=/nix
/var
/nix
/profiles
/system
44 # log the given argument to stderr
49 while [ "$#" -gt 0 ]; do
55 switch|boot|
test|build|edit|repl|dry-build|dry-run|dry-activate|build-vm|build-vm-with-bootloader|list-generations
)
56 if [ "$i" = dry-run
]; then i
=dry-build
; fi
57 if [ "$i" = list-generations
]; then
61 # exactly one action mandatory, bail out if multiple are given
62 if [ -n "$action" ]; then showSyntax
; fi
67 log
"$0: '$i' requires an argument"
76 log
"$0: '$i' requires an argument"
84 log
"$0: --install-grub deprecated, use --install-bootloader instead"
103 --use-substitutes|
--substitute-on-destination|
-s)
108 extraBuildFlags
+=("$i" "$j")
110 --max-jobs|
-j|
--cores|
--log-format)
112 extraBuildFlags
+=("$i" "$j")
113 copyFlags
+=("$i" "$j")
115 --accept-flake-config|
-j*|
--quiet|
--print-build-logs|
-L|
--no-build-output|
-Q|
--show-trace|
--refresh|
--impure|
--offline|
--no-net)
116 extraBuildFlags
+=("$i")
118 --keep-going|
-k|
--keep-failed|
-K|
--fallback|
--repair)
119 extraBuildFlags
+=("$i")
122 --verbose|
-v|
-vv|
-vvv|
-vvvv|
-vvvvv)
124 extraBuildFlags
+=("$i")
130 extraBuildFlags
+=("$i" "$j" "$k")
131 copyFlags
+=("$i" "$j" "$k")
139 log
"$0: ‘--profile-name’ requires an argument"
142 if [ "$1" != system
]; then
143 profile
="/nix/var/nix/profiles/system-profiles/$1"
144 (umask 022 && mkdir
-p "$(dirname "$profile")")
150 log
"$0: ‘--specialisation’ requires an argument"
177 --recreate-lock-file|
--no-update-lock-file|
--no-write-lock-file|
--no-registries|
--commit-lock-file)
182 lockFlags
+=("$i" "$j")
187 lockFlags
+=("$i" "$j" "$k")
193 log
"$0: unknown option \`$i'"
199 # log the given argument to stderr if verbose mode is on
201 if [ -n "$verboseScript" ]; then
206 # Run a command, logging it first if verbose mode is on
214 if [[ "${useSudo:-x}" = 1 ]]; then
220 if [ -z "$buildHost" ]; then
222 elif [ -n "$remoteNix" ]; then
223 runCmd
ssh $SSHOPTS "$buildHost" "${c[@]}" env PATH
="$remoteNix":'$PATH' "$@"
225 runCmd
ssh $SSHOPTS "$buildHost" "${c[@]}" "$@"
231 if [[ "${useSudo:-x}" = 1 ]]; then
237 if [ -z "$targetHost" ]; then
238 runCmd
"${c[@]}" "$@"
240 runCmd
ssh $SSHOPTS "$targetHost" "${c[@]}" "$@"
244 targetHostSudoCmd
() {
246 if [[ ! "${noSSHTTY:-x}" = 1 ]]; then
250 if [ -n "$remoteSudo" ]; then
251 useSudo
=1 SSHOPTS
="$SSHOPTS $t" targetHostCmd
"$@"
253 # While a tty might not be necessary, we apply it to be consistent with
254 # sudo usage, and an experience that is more consistent with local deployment.
255 # But if the user really doesn't want it, don't do it.
256 SSHOPTS
="$SSHOPTS $t" targetHostCmd
"$@"
261 if ! [ "$targetHost" = "$buildHost" ]; then
262 if [ -z "$targetHost" ]; then
263 logVerbose
"Running nix-copy-closure with these NIX_SSHOPTS: $SSHOPTS"
264 NIX_SSHOPTS
=$SSHOPTS runCmd nix-copy-closure
"${copyFlags[@]}" --from "$buildHost" "$1"
265 elif [ -z "$buildHost" ]; then
266 logVerbose
"Running nix-copy-closure with these NIX_SSHOPTS: $SSHOPTS"
267 NIX_SSHOPTS
=$SSHOPTS runCmd nix-copy-closure
"${copyFlags[@]}" --to "$targetHost" "$1"
269 buildHostCmd nix-copy-closure
"${copyFlags[@]}" --to "$targetHost" "$1"
275 logVerbose
"Building in legacy (non-flake) mode."
276 if [ -z "$buildHost" ]; then
277 logVerbose
"No --build-host given, running nix-build locally"
278 runCmd nix-build
"$@"
280 logVerbose
"buildHost set to \"$buildHost\", running nix-build remotely"
285 while [ "$#" -gt 0 ]; do
286 local i
="$1"; shift 1
289 local out
="$1"; shift 1
290 buildArgs
+=("--add-root" "$out" "--indirect")
293 local j
="$1"; shift 1
294 instArgs
+=("$i" "$j")
296 -I) # We don't want this in buildArgs
299 --no-out-link) # We don't want this in buildArgs
310 if [[ -z $buildingAttribute ]]; then
311 instArgs
+=("$buildFile")
314 drv
="$(runCmd nix-instantiate "${instArgs[@]}" "${extraBuildFlags[@]}")"
315 if [ -a "$drv" ]; then
316 logVerbose
"Running nix-copy-closure with these NIX_SSHOPTS: $SSHOPTS"
317 NIX_SSHOPTS
=$SSHOPTS runCmd nix-copy-closure
--to "$buildHost" "$drv"
318 buildHostCmd nix-store
-r "$drv" "${buildArgs[@]}"
320 log
"nix-instantiate failed"
327 logVerbose
"Building in flake mode."
328 if [[ -z "$buildHost" && -z "$targetHost" && "$action" != switch
&& "$action" != boot
&& "$action" != test && "$action" != dry-activate
]]
330 runCmd nix
"${flakeFlags[@]}" build
"$@"
332 elif [ -z "$buildHost" ]; then
333 runCmd nix
"${flakeFlags[@]}" build
"$@" --out-link "${tmpDir}/result"
334 readlink
-f "${tmpDir}/result"
342 while [ "$#" -gt 0 ]; do
343 local i
="$1"; shift 1
345 --recreate-lock-file|
--no-update-lock-file|
--no-write-lock-file|
--no-registries|
--commit-lock-file)
349 local j
="$1"; shift 1
350 evalArgs
+=("$i" "$j")
353 local j
="$1"; shift 1
354 local k
="$1"; shift 1
355 evalArgs
+=("$i" "$j" "$k")
357 --impure) # We don't want this in buildArgs, it's only needed at evaluation time, and unsupported during realisation
365 drv
="$(runCmd nix "${flakeFlags[@]}" eval --raw "${attr}.drvPath" "${evalArgs[@]}" "${extraBuildFlags[@]}")"
366 if [ -a "$drv" ]; then
367 logVerbose
"Running nix with these NIX_SSHOPTS: $SSHOPTS"
368 NIX_SSHOPTS
=$SSHOPTS runCmd nix
"${flakeFlags[@]}" copy
"${copyFlags[@]}" --derivation --to "ssh://$buildHost" "$drv"
369 buildHostCmd nix-store
-r "$drv" "${buildArgs[@]}"
371 log
"nix eval failed"
378 if [ -z "$action" ]; then showSyntax
; fi
380 # Only run shell scripts from the Nixpkgs tree if the action is
381 # "switch", "boot", or "test". With other actions (such as "build"),
382 # the user may reasonably expect that no code from the Nixpkgs tree is
383 # executed, so it's safe to run nixos-rebuild against a potentially
386 if [[ "$action" = switch ||
"$action" = boot ||
"$action" = test ]]; then
390 # Verify that user is not trying to use attribute building and flake
392 if [[ -z $buildingAttribute && -n $flake ]]; then
393 log
"error: '--flake' cannot be used with '--file' or '--attr'"
397 # If ‘--upgrade’ or `--upgrade-all` is given,
398 # run ‘nix-channel --update nixos’.
399 if [[ -n $upgrade && -z $_NIXOS_REBUILD_REEXEC && -z $flake ]]; then
400 # If --upgrade-all is passed, or there are other channels that
401 # contain a file called ".update-on-nixos-rebuild", update them as
402 # well. Also upgrade the nixos channel.
404 for channelpath
in /nix
/var
/nix
/profiles
/per-user
/root
/channels
/*; do
405 channel_name
=$
(basename "$channelpath")
407 if [[ "$channel_name" == "nixos" ]]; then
408 runCmd nix-channel
--update "$channel_name"
409 elif [ -e "$channelpath/.update-on-nixos-rebuild" ]; then
410 runCmd nix-channel
--update "$channel_name"
411 elif [[ -n $upgrade_all ]] ; then
412 runCmd nix-channel
--update "$channel_name"
417 # Make sure that we use the Nix package we depend on, not something
418 # else from the PATH for nix-{env,instantiate,build}. This is
419 # important, because NixOS defaults the architecture of the rebuilt
420 # system to the architecture of the nix-* binaries used. So if on an
421 # amd64 system the user has an i686 Nix package in her PATH, then we
422 # would silently downgrade the whole system to be i686 NixOS on the
424 if [ -z "$_NIXOS_REBUILD_REEXEC" ]; then
425 export PATH
=@nix@
/bin
:$PATH
428 # Use /etc/nixos/flake.nix if it exists. It can be a symlink to the
430 if [[ -z $flake && -e /etc
/nixos
/flake.nix
&& -z $noFlake ]]; then
431 flake
="$(dirname "$
(readlink
-f /etc
/nixos
/flake.nix
)")"
434 tmpDir
=$
(mktemp
-t -d nixos-rebuild.XXXXXX
)
436 if [[ ${#tmpDir} -ge 60 ]]; then
437 # Very long tmp dirs lead to "too long for Unix domain socket"
438 # SSH ControlPath errors. Especially macOS sets long TMPDIR paths.
440 tmpDir
=$
(TMPDIR
= mktemp
-t -d nixos-rebuild.XXXXXX
)
444 for ctrl
in "$tmpDir"/ssh-
*; do
445 ssh -o ControlPath
="$ctrl" -O exit dummyhost
2>/dev
/null || true
451 SSHOPTS
="$NIX_SSHOPTS -o ControlMaster=auto -o ControlPath=$tmpDir/ssh-%n -o ControlPersist=60"
453 # For convenience, use the hostname as the default configuration to
454 # build from the flake.
455 if [[ -n $flake ]]; then
456 if [[ $flake =~ ^
(.
*)\
#([^\#\"]*)$ ]]; then
457 flake
="${BASH_REMATCH[1]}"
458 flakeAttr
="${BASH_REMATCH[2]}"
460 if [[ -z $flakeAttr ]]; then
461 hostname
="$(targetHostCmd cat /proc/sys/kernel/hostname)"
462 if [[ -z $hostname ]]; then
465 flakeAttr
="nixosConfigurations.\"$hostname\""
467 flakeAttr
="nixosConfigurations.\"$flakeAttr\""
471 if [[ ! -z "$specialisation" && ! "$action" = switch
&& ! "$action" = test ]]; then
472 log
"error: ‘--specialisation’ can only be used with ‘switch’ and ‘test’"
477 # Re-execute nixos-rebuild from the Nixpkgs tree.
478 if [[ -z $_NIXOS_REBUILD_REEXEC && -n $canRun && -z $fast ]]; then
479 if [[ -z $buildingAttribute ]]; then
480 p
=$
(runCmd nix-build
--no-out-link $buildFile -A "${attr:+$attr.}config.system.build.nixos-rebuild" "${extraBuildFlags[@]}")
482 elif [[ -z $flake ]]; then
483 if p
=$
(runCmd nix-build
--no-out-link --expr 'with import <nixpkgs/nixos> {}; config.system.build.nixos-rebuild' "${extraBuildFlags[@]}"); then
487 runCmd nix
"${flakeFlags[@]}" build --out-link "${tmpDir}/nixos-rebuild" "$flake#$flakeAttr.config.system.build.nixos-rebuild" "${extraBuildFlags[@]}" "${lockFlags[@]}"
488 if p
=$
(readlink
-e "${tmpDir}/nixos-rebuild"); then
493 if [[ -n $SHOULD_REEXEC ]]; then
494 export _NIXOS_REBUILD_REEXEC
=1
495 # Manually call cleanup as the EXIT trap is not triggered when using exec
497 runCmd
exec "$p/bin/nixos-rebuild" "${origArgs[@]}"
502 # Find configuration.nix and open editor instead of building.
503 if [ "$action" = edit
]; then
504 if [[ -z $buildingAttribute ]]; then
505 log
"error: '--file' and '--attr' are not supported with 'edit'"
507 elif [[ -z $flake ]]; then
508 NIXOS_CONFIG
=${NIXOS_CONFIG:-$(runCmd nix-instantiate --find-file nixos-config)}
509 if [[ -d $NIXOS_CONFIG ]]; then
510 NIXOS_CONFIG
=$NIXOS_CONFIG/default.nix
512 runCmd
exec ${EDITOR:-nano} "$NIXOS_CONFIG"
514 runCmd
exec nix
"${flakeFlags[@]}" edit
"${lockFlags[@]}" -- "$flake#$flakeAttr"
519 # First build Nix, since NixOS may require a newer version than the
521 if [[ -n "$rollback" ||
"$action" = dry-build
]]; then
526 machine
="$(uname -m)"
527 if [[ "$machine" =~ i
.86 ]]; then
535 if [ "$machine" = x86_64
]; then
536 echo @nix_x86_64_linux@
537 elif [[ "$machine" =~ i
.86 ]]; then
538 echo @nix_i686_linux@
539 elif [[ "$machine" = aarch64
]]; then
540 echo @nix_aarch64_linux@
542 log
"$0: unsupported platform"
550 if [[ -z $buildingAttribute ]]; then
551 if nixDrv
="$(runCmd nix-instantiate $buildFile --add-root "$tmpDir/nix.drv
" --indirect -A ${attr:+$attr.}config.nix.package.out "${extraBuildFlags[@]}")"; then return; fi
553 if nixDrv
="$(runCmd nix-instantiate '<nixpkgs/nixos>' --add-root "$tmpDir/nix.drv
" --indirect -A config.nix.package.out "${extraBuildFlags[@]}")"; then return; fi
554 if nixDrv
="$(runCmd nix-instantiate '<nixpkgs>' --add-root "$tmpDir/nix.drv
" --indirect -A nix "${extraBuildFlags[@]}")"; then return; fi
556 if ! nixStorePath
="$(runCmd nix-instantiate --eval '<nixpkgs/nixos/modules/installer/tools/nix-fallback-paths.nix>' -A "$
(nixSystem
)" | sed -e 's/^"//' -e 's
/"$//')"; then
557 nixStorePath
="$(prebuiltNix "$
(uname
-m)")"
559 if ! runCmd nix-store
-r "$nixStorePath" --add-root "${tmpDir}/nix" --indirect \
560 --option extra-binary-caches https
://cache.nixos.org
/; then
561 log
"warning: don't know how to get latest Nix"
563 # Older version of nix-store -r don't support --add-root.
564 [ -e "$tmpDir/nix" ] ||
ln -sf "$nixStorePath" "$tmpDir/nix"
565 if [ -n "$buildHost" ]; then
566 remoteNixStorePath
="$(runCmd prebuiltNix "$
(buildHostCmd uname
-m)")"
567 remoteNix
="$remoteNixStorePath/bin"
568 if ! buildHostCmd nix-store
-r "$remoteNixStorePath" \
569 --option extra-binary-caches https
://cache.nixos.org
/ >/dev
/null
; then
571 log
"warning: don't know how to get latest Nix"
579 local gitDir
="$dir/.git"
580 if [ -e "$gitDir" ]; then
581 if [ -z "$(type -P git)" ]; then
582 echo "warning: Git not found; cannot figure out revision of $dir" >&2
586 rev=$
(git
--git-dir="$gitDir" rev-parse
--short HEAD
)
587 if git
--git-dir="$gitDir" describe
--always --dirty |
grep -q dirty
; then
592 if [ -n "$rev" ]; then
598 if [[ -n $buildNix && -z $flake ]]; then
599 log
"building Nix..."
601 if [ -a "$nixDrv" ]; then
602 nix-store
-r "$nixDrv"'!'"out" --add-root "$tmpDir/nix" --indirect >/dev
/null
603 if [ -n "$buildHost" ]; then
604 nix-copy-closure
"${copyFlags[@]}" --to "$buildHost" "$nixDrv"
605 # The nix build produces multiple outputs, we add them all to the remote path
606 for p
in $
(buildHostCmd nix-store
-r "$(readlink "$nixDrv")" "${buildArgs[@]}"); do
607 remoteNix
="$remoteNix${remoteNix:+:}$p/bin"
611 PATH
="$tmpDir/nix/bin:$PATH"
615 # Update the version suffix if we're building from Git (so that
616 # nixos-version shows something useful).
617 if [[ -n $canRun && -z $flake ]]; then
618 if nixpkgs
=$
(runCmd nix-instantiate
--find-file nixpkgs
"${extraBuildFlags[@]}"); then
619 suffix
=$
(getVersion
"$nixpkgs" || true
)
620 if [ -n "$suffix" ]; then
621 echo -n "$suffix" > "$nixpkgs/.version-suffix" || true
627 if [ "$action" = dry-build
]; then
628 extraBuildFlags
+=(--dry-run)
631 if [ "$action" = repl
]; then
632 # This is a very end user command, implemented using sub-optimal means.
633 # You should feel free to improve its behavior, as well as resolve tech
634 # debt in "breaking" ways. Humans adapt quite well.
635 if [[ -z $buildingAttribute ]]; then
636 exec nix repl
--file $buildFile $attr "${extraBuildFlags[@]}"
637 elif [[ -z $flake ]]; then
638 exec nix repl
--file '<nixpkgs/nixos>' "${extraBuildFlags[@]}"
640 if [[ -n "${lockFlags[0]}" ]]; then
641 # nix repl itself does not support locking flags
642 log
"nixos-rebuild repl does not support locking flags yet"
647 bold
="$(echo -e '\033[1m')"
648 blue
="$(echo -e '\033[34;1m')"
649 attention
="$(echo -e '\033[35;1m')"
650 reset="$(echo -e '\033[0m')"
651 if [[ -e $flake ]]; then
652 flakePath
=$
(realpath
"$flake")
656 # This nix repl invocation is impure, because usually the flakeref is.
657 # For a solution that preserves the motd and custom scope, we need
658 # something like https://github.com/NixOS/nix/issues/8679.
659 exec nix repl
--impure --expr "
660 let flake = builtins.getFlake ''$flakePath'';
661 configuration = flake.$flakeAttr;
664 Hello and welcome to the NixOS configuration
668 The following is loaded into nix repl's scope:
670 - ${blue}config${reset} All option values
671 - ${blue}options${reset} Option data and metadata
672 - ${blue}pkgs${reset} Nixpkgs package set
673 - ${blue}lib${reset} Nixpkgs library functions
674 - other module arguments
676 - ${blue}flake${reset} Flake outputs, inputs and source info of $flake
678 Use tab completion to browse around ${blue}config${reset}.
680 Use ${bold}:r${reset} to ${bold}reload${reset} everything after making a change in the flake.
681 (assuming $flake is a mutable flake ref)
683 See ${bold}:?${reset} for more repl commands.
685 ${attention}warning:${reset} nixos-rebuild repl does not currently enforce pure evaluation.
688 assert configuration._type or null == ''configuration'';
689 assert configuration.class or ''nixos'' == ''nixos'';
690 configuration._module.args //
691 configuration._module.specialArgs //
693 inherit (configuration) config options;
694 lib = configuration.lib or configuration.pkgs.lib;
697 in builtins.seq scope builtins.trace motd scope
698 " "${extraBuildFlags[@]}"
702 if [ "$action" = list-generations
]; then
703 if [ ! -L "$profile" ]; then
704 log
"No profile \`$(basename "$profile")' found"
708 generation_from_dir
() {
710 generation_base
="$(basename "$generation_dir")" # Has the format "system-123-link" for generation 123
711 no_link_gen
="${generation_base%-link}" # remove the "-link"
712 echo "${no_link_gen##*-}" # remove everything before the last dash
714 describe_generation
(){
716 generation_number
="$(generation_from_dir "$generation_dir")"
717 nixos_version
="$(cat "$generation_dir/nixos-version
" 2> /dev/null || echo "Unknown
")"
719 kernel_dir
="$(dirname "$
(realpath
"$generation_dir/kernel")")"
720 kernel_version
="$(ls "$kernel_dir/lib
/modules
" || echo "Unknown
")"
722 configurationRevision
="$("$generation_dir/sw
/bin
/nixos-version
" --configuration-revision 2> /dev/null || true)"
724 # Old nixos-version output ignored unknown flags and just printed the version
725 # therefore the following workaround is done not to show the default output
726 nixos_version_default
="$("$generation_dir/sw
/bin
/nixos-version
")"
727 if [ "$configurationRevision" == "$nixos_version_default" ]; then
728 configurationRevision
=""
731 # jq automatically quotes the output => don't try to quote it in output!
732 build_date
="$(stat "$generation_dir" --format=%W | jq 'todate')"
734 pushd "$generation_dir/specialisation/" > /dev
/null ||
:
735 specialisation_list
=(*)
736 popd > /dev
/null ||
:
738 specialisations
="$(jq --compact-output --null-input '$ARGS.positional' --args -- "${specialisation_list[@]}")"
740 if [ "$(basename "$generation_dir")" = "$(readlink "$profile")" ]; then
741 current_generation_tag
="true"
743 current_generation_tag
="false"
746 # Escape userdefined strings
747 nixos_version
="$(jq -aR <<< "$nixos_version")"
748 kernel_version
="$(jq -aR <<< "$kernel_version")"
749 configurationRevision
="$(jq -aR <<< "$configurationRevision")"
752 "generation": $generation_number,
754 "nixosVersion": $nixos_version,
755 "kernelVersion": $kernel_version,
756 "configurationRevision": $configurationRevision,
757 "specialisations": $specialisations,
758 "current": $current_generation_tag
763 find "$(dirname "$profile")" -regex "$profile-[0-9]+-link" |
765 while read -r generation_dir
; do
766 describe_generation
"$generation_dir"
768 if [ -z "$json" ]; then
769 jq
--slurp -r '.[] | [
770 ([.generation, (if .current == true then "current" else "" end)] | join(" ")),
771 (.date | fromdate | strflocaltime("%Y-%m-%d %H:%M:%S")),
772 .nixosVersion, .kernelVersion, .configurationRevision,
773 (.specialisations | join(" "))
775 column --separator $
'\t' --table --table-columns "Generation,Build-date,NixOS version,Kernel,Configuration Revision,Specialisation"
783 # Either upgrade the configuration in the system profile (for "switch"
784 # or "boot"), or just build it and create a symlink "result" in the
785 # current directory (for "build" and "test").
786 if [ -z "$rollback" ]; then
787 log
"building the system configuration..."
788 if [[ "$action" = switch ||
"$action" = boot
]]; then
789 if [[ -z $buildingAttribute ]]; then
790 pathToConfig
="$(nixBuild $buildFile -A "${attr:+$attr.}config.system.build.toplevel
" "${extraBuildFlags[@]}")"
791 elif [[ -z $flake ]]; then
792 pathToConfig
="$(nixBuild '<nixpkgs/nixos>' --no-out-link -A system "${extraBuildFlags[@]}")"
794 pathToConfig
="$(nixFlakeBuild "$flake#$flakeAttr.config.system.build.toplevel
" "${extraBuildFlags[@]}" "${lockFlags[@]}")"
796 copyToTarget
"$pathToConfig"
797 targetHostSudoCmd nix-env
-p "$profile" --set "$pathToConfig"
798 elif [[ "$action" = test ||
"$action" = build ||
"$action" = dry-build ||
"$action" = dry-activate
]]; then
799 if [[ -z $buildingAttribute ]]; then
800 pathToConfig
="$(nixBuild $buildFile -A "${attr:+$attr.}config.system.build.toplevel
" "${extraBuildFlags[@]}")"
801 elif [[ -z $flake ]]; then
802 pathToConfig
="$(nixBuild '<nixpkgs/nixos>' -A system -k "${extraBuildFlags[@]}")"
804 pathToConfig
="$(nixFlakeBuild "$flake#$flakeAttr.config.system.build.toplevel
" "${extraBuildFlags[@]}" "${lockFlags[@]}")"
806 elif [ "$action" = build-vm
]; then
807 if [[ -z $buildingAttribute ]]; then
808 pathToConfig
="$(nixBuild $buildFile -A "${attr:+$attr.}config.system.build.vm
" "${extraBuildFlags[@]}")"
809 elif [[ -z $flake ]]; then
810 pathToConfig
="$(nixBuild '<nixpkgs/nixos>' -A vm -k "${extraBuildFlags[@]}")"
812 pathToConfig
="$(nixFlakeBuild "$flake#$flakeAttr.config.system.build.vm
" "${extraBuildFlags[@]}" "${lockFlags[@]}")"
814 elif [ "$action" = build-vm-with-bootloader
]; then
815 if [[ -z $buildingAttribute ]]; then
816 pathToConfig
="$(nixBuild $buildFile -A "${attr:+$attr.}config.system.build.vmWithBootLoader
" "${extraBuildFlags[@]}")"
817 elif [[ -z $flake ]]; then
818 pathToConfig
="$(nixBuild '<nixpkgs/nixos>' -A vmWithBootLoader -k "${extraBuildFlags[@]}")"
820 pathToConfig
="$(nixFlakeBuild "$flake#$flakeAttr.config.system.build.vmWithBootLoader
" "${extraBuildFlags[@]}" "${lockFlags[@]}")"
825 # Copy build to target host if we haven't already done it
826 if ! [[ "$action" = switch ||
"$action" = boot
]]; then
827 copyToTarget
"$pathToConfig"
829 else # [ -n "$rollback" ]
830 if [[ "$action" = switch ||
"$action" = boot
]]; then
831 targetHostSudoCmd nix-env
--rollback -p "$profile"
832 pathToConfig
="$profile"
833 elif [[ "$action" = test ||
"$action" = build
]]; then
835 targetHostCmd nix-env
-p "$profile" --list-generations |
836 sed -n '/current/ {g; p;}; s/ *\([0-9]*\).*/\1/; h'
838 pathToConfig
="$profile"-${systemNumber}-link
839 if [ -z "$targetHost" ]; then
840 ln -sT "$pathToConfig" .
/result
848 # If we're not just building, then make the new configuration the boot
849 # default and/or activate it now.
850 if [[ "$action" = switch ||
"$action" = boot ||
"$action" = test ||
"$action" = dry-activate
]]; then
851 # Using systemd-run here to protect against PTY failures/network
852 # disconnections during rebuild.
853 # See: https://github.com/NixOS/nixpkgs/issues/39118
856 "-E" "LOCALE_ARCHIVE" # Will be set to new value early in switch-to-configuration script, but interpreter starts out with old value
857 "-E" "NIXOS_INSTALL_BOOTLOADER=$installBootloader"
862 "--service-type=exec"
863 "--unit=nixos-rebuild-switch-to-configuration"
866 # Check if we have a working systemd-run. In chroot environments we may have
867 # a non-working systemd, so we fallback to not using systemd-run.
868 # You may also want to explicitly set NIXOS_SWITCH_USE_DIRTY_ENV environment
869 # variable, since systemd-run runs inside an isolated environment and
870 # this may break some post-switch scripts. However keep in mind that this
871 # may be dangerous in remote access (e.g. SSH).
872 if [[ -n "$NIXOS_SWITCH_USE_DIRTY_ENV" ]]; then
873 log
"warning: skipping systemd-run since NIXOS_SWITCH_USE_DIRTY_ENV is set. This environment variable will be ignored in the future"
874 cmd
=("env" "NIXOS_INSTALL_BOOTLOADER=$installBootloader")
875 elif ! targetHostSudoCmd
"${cmd[@]}" true
; then
876 logVerbose
"Skipping systemd-run to switch configuration since it is not working in target host."
880 "LOCALE_ARCHIVE=$LOCALE_ARCHIVE"
881 "NIXOS_INSTALL_BOOTLOADER=$installBootloader"
884 logVerbose
"Using systemd-run to switch configuration."
886 if [[ -z "$specialisation" ]]; then
887 cmd
+=("$pathToConfig/bin/switch-to-configuration")
889 cmd
+=("$pathToConfig/specialisation/$specialisation/bin/switch-to-configuration")
891 if [ -z "$targetHost" ]; then
892 specialisationExists
=$
(test -f "${cmd[-1]}")
894 specialisationExists
=$
(targetHostCmd
test -f "${cmd[-1]}")
897 if ! $specialisationExists; then
898 log
"error: specialisation not found: $specialisation"
903 if ! targetHostSudoCmd
"${cmd[@]}" "$action"; then
904 log
"warning: error(s) occurred while switching to the new configuration"
910 if [[ "$action" = build-vm ||
"$action" = build-vm-with-bootloader
]]; then
913 Done. The virtual machine can be started by running $(echo "${pathToConfig}/bin/"run-*-vm)