Skip to content

Commit 76d4d46

Browse files
committed
nixos-rebuild: set SSHOPTS earlier so that we resolve the correct hostname
Before, when using things like proxy jumps, we would end up with the hostname of either localhost or the jump host (depending on whether you configure the jumphost in your ssh config file or in the SSHOPTS) instead of the hostname of the target host. I was running the following command: ```bash NIX_SSHOPTS='-p 6016 -J username@jumphost' nixos-rebuild --verbose --flake '.#' --target-host localhost --use-remote-sudo --fast build ``` and what was getting built was `nixosConfigurations.<localhost hostname>` instead of `nixosConfigurations.<remote hostname>`, because the SSH connection to determine the hostname didn't have the NIX_SSHOPTS added to it yet. So I simply moved the logic to set up the tmp dir and set the SSHOPTS a bit higher up.
1 parent 5763214 commit 76d4d46

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,25 @@ if [[ -z $flake && -e /etc/nixos/flake.nix && -z $noFlake ]]; then
427427
flake="$(dirname "$(readlink -f /etc/nixos/flake.nix)")"
428428
fi
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.
432451
if [[ -n $flake ]]; then
@@ -450,23 +469,6 @@ if [[ ! -z "$specialisation" && ! "$action" = switch && ! "$action" = test ]]; t
450469
exit 1
451470
fi
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.
472474
if [[ -z $_NIXOS_REBUILD_REEXEC && -n $canRun && -z $fast ]]; then
@@ -510,8 +512,6 @@ if [ "$action" = edit ]; then
510512
exit 1
511513
fi
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.
517517
if [[ -n "$rollback" || "$action" = dry-build ]]; then

0 commit comments

Comments
 (0)