Skip to content

Commit 57eab96

Browse files
authored
Merge pull request #522 from nix-community/no-agents
nixos-anywhere: don't prompt SSH agent after deployment key installed
2 parents 1e2882c + 5a24bc2 commit 57eab96

File tree

1 file changed

+15
-22
lines changed

1 file changed

+15
-22
lines changed

src/nixos-anywhere.sh

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ mkdir -p "$sshKeyDir"
6565
declare -A diskEncryptionKeys=()
6666
declare -A extraFilesOwnership=()
6767
declare -a nixCopyOptions=()
68-
declare -a sshArgs=()
68+
declare -a sshArgs=("-o" "IdentitiesOnly=yes" "-i" "$sshKeyDir/nixos-anywhere" "-o" "UserKnownHostsFile=/dev/null" "-o" "StrictHostKeyChecking=no")
6969

7070
showUsage() {
7171
cat <<USAGE
@@ -407,23 +407,27 @@ parseArgs() {
407407

408408
# ssh wrapper
409409
runSshNoTty() {
410-
ssh -i "$sshKeyDir"/nixos-anywhere -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "${sshArgs[@]}" "$sshConnection" "$@"
410+
# shellcheck disable=SC2029
411+
# We want to expand "$@" to get the command to run over SSH
412+
ssh "${sshArgs[@]}" "$sshConnection" "$@"
411413
}
412414
runSshTimeout() {
413-
timeout 10 ssh -i "$sshKeyDir"/nixos-anywhere -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "${sshArgs[@]}" "$sshConnection" "$@"
415+
timeout 10 ssh "${sshArgs[@]}" "$sshConnection" "$@"
414416
}
415417
runSsh() {
416-
ssh "$sshTtyParam" -i "$sshKeyDir"/nixos-anywhere -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "${sshArgs[@]}" "$sshConnection" "$@"
418+
# shellcheck disable=SC2029
419+
# We want to expand "$@" to get the command to run over SSH
420+
ssh "$sshTtyParam" "${sshArgs[@]}" "$sshConnection" "$@"
417421
}
418422

419423
nixCopy() {
420-
NIX_SSHOPTS="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i $sshKeyDir/nixos-anywhere ${sshArgs[*]}" nix copy \
424+
NIX_SSHOPTS="${sshArgs[*]}" nix copy \
421425
"${nixOptions[@]}" \
422426
"${nixCopyOptions[@]}" \
423427
"$@"
424428
}
425429
nixBuild() {
426-
NIX_SSHOPTS="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i $sshKeyDir/nixos-anywhere ${sshArgs[*]}" nix build \
430+
NIX_SSHOPTS="${sshArgs[*]}" nix build \
427431
--print-out-paths \
428432
--no-link \
429433
"${nixBuildFlags[@]}" \
@@ -470,32 +474,21 @@ uploadSshKey() {
470474
ssh-keygen -t ed25519 -f "$sshKeyDir"/nixos-anywhere -P "" -C "nixos-anywhere" >/dev/null
471475
fi
472476

473-
declare -a sshCopyIdArgs
474-
if [[ -n ${sshPrivateKeyFile} ]]; then
475-
unset SSH_AUTH_SOCK # don't use system agent if key was supplied
476-
sshCopyIdArgs+=(-o "IdentityFile=${sshPrivateKeyFile}" -f)
477-
fi
478-
479477
step Uploading install SSH keys
480478
until
481479
if [[ ${envPassword} == y ]]; then
482480
sshpass -e \
483481
ssh-copy-id \
484-
-i "$sshKeyDir"/nixos-anywhere.pub \
485482
-o ConnectTimeout=10 \
486-
-o UserKnownHostsFile=/dev/null \
487-
-o IdentitiesOnly=yes \
488-
-o StrictHostKeyChecking=no \
489-
"${sshCopyIdArgs[@]}" \
490483
"${sshArgs[@]}" \
491484
"$sshConnection"
492485
else
486+
# To override `IdentitiesOnly=yes` set in `sshArgs` we need to set
487+
# `IdentitiesOnly=no` first as the first time an SSH option is
488+
# specified on the command line takes precedence
493489
ssh-copy-id \
494-
-i "$sshKeyDir"/nixos-anywhere.pub \
490+
-o IdentitiesOnly=no \
495491
-o ConnectTimeout=10 \
496-
-o UserKnownHostsFile=/dev/null \
497-
-o StrictHostKeyChecking=no \
498-
"${sshCopyIdArgs[@]}" \
499492
"${sshArgs[@]}" \
500493
"$sshConnection"
501494
fi
@@ -559,7 +552,7 @@ checkBuildLocally() {
559552
-L \
560553
"${nixOptions[@]}" \
561554
--expr \
562-
"derivation { system = \"$system\"; name = \"env-$entropy\"; builder = \"/bin/sh\"; args = [ \"-c\" \"echo > \$out\" ]; }"; then
555+
"derivation { system = \"$machineSystem\"; name = \"env-$entropy\"; builder = \"/bin/sh\"; args = [ \"-c\" \"echo > \$out\" ]; }"; then
563556
# The local build failed
564557
buildOn=local
565558
return

0 commit comments

Comments
 (0)