@@ -427,6 +427,25 @@ if [[ -z $flake && -e /etc/nixos/flake.nix && -z $noFlake ]]; then
427427 flake=" $( dirname " $( readlink -f /etc/nixos/flake.nix) " ) "
428428fi
429429
430+ tmpDir=$( mktemp -t -d nixos-rebuild.XXXXXX)
431+
432+ if [[ ${# tmpDir} -ge 60 ]]; then
433+ # Very long tmp dirs lead to "too long for Unix domain socket"
434+ # SSH ControlPath errors. Especially macOS sets long TMPDIR paths.
435+ rmdir " $tmpDir "
436+ tmpDir=$( TMPDIR= mktemp -t -d nixos-rebuild.XXXXXX)
437+ fi
438+
439+ cleanup () {
440+ for ctrl in " $tmpDir " /ssh-* ; do
441+ ssh -o ControlPath=" $ctrl " -O exit dummyhost 2> /dev/null || true
442+ done
443+ rm -rf " $tmpDir "
444+ }
445+ trap cleanup EXIT
446+
447+ SSHOPTS=" $NIX_SSHOPTS -o ControlMaster=auto -o ControlPath=$tmpDir /ssh-%n -o ControlPersist=60"
448+
430449# For convenience, use the hostname as the default configuration to
431450# build from the flake.
432451if [[ -n $flake ]]; then
@@ -450,23 +469,6 @@ if [[ ! -z "$specialisation" && ! "$action" = switch && ! "$action" = test ]]; t
450469 exit 1
451470fi
452471
453- tmpDir=$( mktemp -t -d nixos-rebuild.XXXXXX)
454-
455- if [[ ${# tmpDir} -ge 60 ]]; then
456- # Very long tmp dirs lead to "too long for Unix domain socket"
457- # SSH ControlPath errors. Especially macOS sets long TMPDIR paths.
458- rmdir " $tmpDir "
459- tmpDir=$( TMPDIR= mktemp -t -d nixos-rebuild.XXXXXX)
460- fi
461-
462- cleanup () {
463- for ctrl in " $tmpDir " /ssh-* ; do
464- ssh -o ControlPath=" $ctrl " -O exit dummyhost 2> /dev/null || true
465- done
466- rm -rf " $tmpDir "
467- }
468- trap cleanup EXIT
469-
470472
471473# Re-execute nixos-rebuild from the Nixpkgs tree.
472474if [[ -z $_NIXOS_REBUILD_REEXEC && -n $canRun && -z $fast ]]; then
@@ -510,8 +512,6 @@ if [ "$action" = edit ]; then
510512 exit 1
511513fi
512514
513- SSHOPTS=" $NIX_SSHOPTS -o ControlMaster=auto -o ControlPath=$tmpDir /ssh-%n -o ControlPersist=60"
514-
515515# First build Nix, since NixOS may require a newer version than the
516516# current one.
517517if [[ -n " $rollback " || " $action " = dry-build ]]; then
0 commit comments