Skip to content

Commit 5943ffa

Browse files
LassulusMic92
authored andcommitted
nixos-remote.sh: generate temporary ssh-key
we use this so the code has less branches for different ssh-key usecases
1 parent f33a138 commit 5943ffa

File tree

7 files changed

+73
-53
lines changed

7 files changed

+73
-53
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ Options:
4242
* -s, --store-paths
4343
set the store paths to the disko-script and nixos-system directly
4444
if this is give, flake is not needed
45-
* --no-ssh-copy
46-
skip copying ssh-keys to target system
4745
* --no-reboot
4846
do not reboot after installation, allowing further customization of the target installation.
4947
* --kexec url

docs/cli.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ Options:
1010
* -s, --store-paths
1111
set the store paths to the disko-script and nixos-system directly
1212
if this is give, flake is not needed
13-
* --no-ssh-copy
14-
skip copying ssh-keys to target system
1513
* --kexec url
1614
use another kexec tarball to bootstrap NixOS
1715
* --debug

flake.lock

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/nixos-remote.sh

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ Options:
1111
* -s, --store-paths
1212
set the store paths to the disko-script and nixos-system directly
1313
if this is give, flake is not needed
14-
* --no-ssh-copy
15-
skip copying ssh-keys to target system
1614
* --no-reboot
1715
do not reboot after installation, allowing further customization of the target installation.
1816
* --kexec url
@@ -49,9 +47,10 @@ nix_options=(
4947
"--no-write-lock-file"
5048
)
5149
substitute_on_destination=y
52-
nix_copy_options=()
5350

5451
declare -A disk_encryption_keys
52+
declare -a nix_copy_options
53+
declare -a ssh_copy_id_args
5554

5655
while [[ $# -gt 0 ]]; do
5756
case "$1" in
@@ -76,9 +75,6 @@ while [[ $# -gt 0 ]]; do
7675
kexec_url=$2
7776
shift
7877
;;
79-
--no-ssh-copy-id)
80-
no_ssh_copy=y
81-
;;
8278
--debug)
8379
enable_debug="-x"
8480
print_build_logs=y
@@ -126,14 +122,6 @@ while [[ $# -gt 0 ]]; do
126122
shift
127123
done
128124

129-
# ssh wrapper
130-
timeout_ssh_() {
131-
timeout 10 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "$ssh_connection" "$@"
132-
}
133-
ssh_() {
134-
ssh -T -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "$ssh_connection" "$@"
135-
}
136-
137125
if [[ ${print_build_logs-n} == "y" ]]; then
138126
nix_options+=("-L")
139127
fi
@@ -142,8 +130,16 @@ if [[ ${substitute_on_destination-n} == "y" ]]; then
142130
nix_copy_options+=("--substitute-on-destination")
143131
fi
144132

133+
# ssh wrapper
134+
timeout_ssh_() {
135+
timeout 10 ssh -i "$ssh_key_dir"/nixos-remote -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "$ssh_connection" "$@"
136+
}
137+
ssh_() {
138+
ssh -T -i "$ssh_key_dir"/nixos-remote -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "$ssh_connection" "$@"
139+
}
140+
145141
nix_copy() {
146-
NIX_SSHOPTS='-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' nix copy \
142+
NIX_SSHOPTS="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i $ssh_key_dir/nixos-remote" nix copy \
147143
"${nix_options[@]}" \
148144
"${nix_copy_options[@]}" \
149145
"$@"
@@ -160,6 +156,12 @@ if [[ -z ${ssh_connection-} ]]; then
160156
abort "ssh-host must be set"
161157
fi
162158

159+
# we generate a temporary ssh keypair that we can use during nixos-remote
160+
ssh_key_dir=$(mktemp -d)
161+
trap 'rm -rf "$ssh_key_dir"' EXIT
162+
mkdir -p "$ssh_key_dir"
163+
ssh-keygen -t ed25519 -f "$ssh_key_dir"/nixos-remote -P "" -C "nixos-remote" >/dev/null
164+
163165
# parse flake nixos-install style syntax, get the system attr
164166
if [[ -n ${flake-} ]]; then
165167
if [[ $flake =~ ^(.*)\#([^\#\"]*)$ ]]; then
@@ -183,6 +185,30 @@ else
183185
abort "flake must be set"
184186
fi
185187

188+
if [[ -n ${SSH_PRIVATE_KEY-} ]]; then
189+
sshPrivateKeyFile=$(mktemp)
190+
trap 'rm "$sshPrivateKeyFile"' EXIT
191+
(
192+
umask 077
193+
printf '%s' "$SSH_PRIVATE_KEY" >"$sshPrivateKeyFile"
194+
)
195+
unset SSH_AUTH_SOCK # don't use system agent if key was supplied
196+
ssh_copy_id_args+=(-o "IdentityFile=${sshPrivateKeyFile}")
197+
ssh_copy_id_args+=(-f)
198+
fi
199+
200+
until
201+
ssh-copy-id \
202+
-i "$ssh_key_dir"/nixos-remote.pub \
203+
-o ConnectTimeout=10 \
204+
-o UserKnownHostsFile=/dev/null \
205+
-o StrictHostKeyChecking=no \
206+
"${ssh_copy_id_args[@]}" \
207+
"$ssh_connection"
208+
do
209+
sleep 3
210+
done
211+
186212
import_facts() {
187213
local facts filtered_facts
188214
if ! facts=$(
@@ -205,7 +231,7 @@ has_curl=\$(has curl)
205231
FACTS
206232
SSH
207233
); then
208-
return 1
234+
exit 1
209235
fi
210236
filtered_facts=$(echo "$facts" | grep -E '^(has|is)_[a-z0-9_]+=\S+')
211237
if [[ -z $filtered_facts ]]; then
@@ -216,10 +242,7 @@ SSH
216242
export $(echo "$filtered_facts" | xargs)
217243
}
218244

219-
# wait for machine to become reachable (possibly forever)
220-
until import_facts; do
221-
sleep 5
222-
done
245+
import_facts
223246

224247
if [[ ${has_tar-n} == "n" ]]; then
225248
abort "no tar command found, but required to unpack kexec tarball"
@@ -236,10 +259,6 @@ if [[ ${is_arch-n} != "x86_64" ]] && [[ $kexec_url == "$default_kexec_url" ]]; t
236259
abort "The default kexec image only support x86_64 cpus. Checkout https://github.com/numtide/nixos-remote/#using-your-own-kexec-image for more information."
237260
fi
238261

239-
if [[ ${is_kexec-n} != "y" ]] && [[ ${no_ssh_copy-n} != "y" ]]; then
240-
ssh-copy-id -o ConnectTimeout=10 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "$ssh_connection"
241-
fi
242-
243262
if [[ ${is_kexec-n} == "n" ]] && [[ ${is_installer-n} == "n" ]]; then
244263
ssh_ <<SSH
245264
set -efu ${enable_debug}
@@ -279,6 +298,9 @@ nix_copy --to "ssh://$ssh_connection" "$disko_script"
279298
ssh_ "$disko_script"
280299

281300
if [[ ${stop_after_disko-n} == "y" ]]; then
301+
# Should we also do this for `--no-reboot`?
302+
echo "WARNING: leaving temporary ssh key at '$ssh_key_dir/nixos-remote' to login to the machine" >&2
303+
trap - EXIT
282304
exit 0
283305
fi
284306

tests/from-nixos-with-sudo.nix

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@
22
name = "from-nixos-with-sudo";
33
nodes = {
44
installer = ./modules/installer.nix;
5-
installed = ./modules/installed.nix;
5+
installed = {
6+
services.openssh.enable = true;
7+
virtualisation.memorySize = 4096;
8+
9+
users.users.nixos = {
10+
isNormalUser = true;
11+
openssh.authorizedKeys.keyFiles = [ ./modules/ssh-keys/ssh.pub ];
12+
extraGroups = [ "wheel" ];
13+
};
14+
security.sudo.enable = true;
15+
security.sudo.wheelNeedsPassword = false;
16+
};
617
};
718
testScript = ''
819
start_all()
920
installer.succeed("echo super-secret > /tmp/disk-1.key")
1021
output = installer.succeed("""
1122
nixos-remote \
12-
--no-ssh-copy-id \
1323
--debug \
1424
--kexec /etc/nixos-remote/kexec-installer \
1525
--stop-after-disko \

tests/from-nixos.nix

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
name = "from-nixos";
33
nodes = {
44
installer = ./modules/installer.nix;
5-
installed = ./modules/installed.nix;
5+
installed = {
6+
services.openssh.enable = true;
7+
virtualisation.memorySize = 4096;
8+
9+
users.users.root.openssh.authorizedKeys.keyFiles = [ ./modules/ssh-keys/ssh.pub ];
10+
};
611
};
712
testScript = ''
813
def create_test_machine(oldmachine=None, args={}): # taken from <nixpkgs/nixos/tests/installer.nix>
@@ -19,7 +24,6 @@
1924
installer.succeed("echo value > /tmp/extra-files/var/lib/secrets/key")
2025
installer.succeed("""
2126
nixos-remote \
22-
--no-ssh-copy-id \
2327
--debug \
2428
--kexec /etc/nixos-remote/kexec-installer \
2529
--extra-files /tmp/extra-files \

tests/modules/installed.nix

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)