Skip to content

Commit 8428ae7

Browse files
zimbatmMic92
authored andcommitted
add -i to allow passing private key files
Mirror the `-i` option from SSH, so you can run `nixos-anywhere ~/.ssh/other_key`. This commit also fixes an issue where the generated key-pair would stay around when using the SSH_PRIVATE_KEY env var.
1 parent fae3915 commit 8428ae7

File tree

5 files changed

+26
-11
lines changed

5 files changed

+26
-11
lines changed

docs/reference.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ Usage: nixos-anywhere [options] ssh-host
1919
2020
Options:
2121
22-
* -f, --flake flake
23-
set the flake to install the system from
22+
* -f, --flake <flake_uri>
23+
set the flake to install the system from.
24+
* -i <identity_file>
25+
selects which SSH private key file to use.
2426
* -L, --print-build-logs
2527
print full build logs
2628
* -s, --store-paths

src/nixos-anywhere.sh

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ Usage: nixos-anywhere [options] ssh-host
77
88
Options:
99
10-
* -f, --flake flake
11-
set the flake to install the system from
10+
* -f, --flake <flake_uri>
11+
set the flake to install the system from.
12+
* -i <identity_file>
13+
selects which SSH private key file to use.
1214
* -L, --print-build-logs
1315
print full build logs
1416
* -s, --store-paths
@@ -56,6 +58,7 @@ nix_options=(
5658
"--no-write-lock-file"
5759
)
5860
substitute_on_destination=y
61+
ssh_private_key_file=
5962

6063
declare -A disk_encryption_keys
6164
declare -a nix_copy_options
@@ -67,6 +70,10 @@ while [[ $# -gt 0 ]]; do
6770
flake=$2
6871
shift
6972
;;
73+
-i)
74+
ssh_private_key_file=$2
75+
shift
76+
;;
7077
-L | --print-build-logs)
7178
print_build_logs=y
7279
;;
@@ -198,15 +205,19 @@ else
198205
abort "flake must be set"
199206
fi
200207

208+
# overrides -i if passed as an env var
201209
if [[ -n ${SSH_PRIVATE_KEY-} ]]; then
202-
sshPrivateKeyFile=$(mktemp)
203-
trap 'rm "$sshPrivateKeyFile"' EXIT
210+
# $ssh_key_dir is getting deleted on trap EXIT
211+
ssh_private_key_file="$ssh_key_dir/from-env"
204212
(
205213
umask 077
206-
printf '%s\n' "$SSH_PRIVATE_KEY" >"$sshPrivateKeyFile"
214+
printf '%s\n' "$SSH_PRIVATE_KEY" >"$ssh_private_key_file"
207215
)
216+
fi
217+
218+
if [[ -n ${ssh_private_key_file-} ]]; then
208219
unset SSH_AUTH_SOCK # don't use system agent if key was supplied
209-
ssh_copy_id_args+=(-o "IdentityFile=${sshPrivateKeyFile}")
220+
ssh_copy_id_args+=(-o "IdentityFile=${ssh_private_key_file}")
210221
ssh_copy_id_args+=(-f)
211222
fi
212223

tests/from-nixos-with-sudo.nix

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,17 @@
2020
installer.succeed("echo super-secret > /tmp/disk-1.key")
2121
output = installer.succeed("""
2222
nixos-anywhere \
23+
-i /root/.ssh/install_key \
2324
--debug \
2425
--kexec /etc/nixos-anywhere/kexec-installer \
2526
--stop-after-disko \
2627
--disk-encryption-keys /tmp/disk-1.key /tmp/disk-1.key \
2728
--disk-encryption-keys /tmp/disk-2.key <(echo another-secret) \
2829
--store-paths /etc/nixos-anywhere/disko /etc/nixos-anywhere/system-to-install \
2930
nixos@installed >&2
30-
echo "disk-1.key: '$(ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \
31+
echo "disk-1.key: '$(ssh -i /root/.ssh/install_key -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \
3132
root@installed cat /tmp/disk-1.key)'"
32-
echo "disk-2.key: '$(ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \
33+
echo "disk-2.key: '$(ssh -i /root/.ssh/install_key -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \
3334
root@installed cat /tmp/disk-2.key)'"
3435
""")
3536

tests/from-nixos.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
installer.succeed("echo value > /tmp/extra-files/var/lib/secrets/key")
2525
installer.succeed("""
2626
nixos-anywhere \
27+
-i /root/.ssh/install_key \
2728
--debug \
2829
--kexec /etc/nixos-anywhere/kexec-installer \
2930
--extra-files /tmp/extra-files \

tests/modules/installer.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ let
99
in
1010
{
1111
system.activationScripts.rsa-key = ''
12-
${pkgs.coreutils}/bin/install -D -m600 ${./ssh-keys/ssh} /root/.ssh/id_rsa
12+
${pkgs.coreutils}/bin/install -D -m600 ${./ssh-keys/ssh} /root/.ssh/install_key
1313
'';
1414

1515
environment.systemPackages = [ inputs.nixos-anywhere ];

0 commit comments

Comments
 (0)