Skip to content

Commit eb5ff34

Browse files
tiemergify[bot]
authored andcommitted
1 parent 7fb2626 commit eb5ff34

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

docs/howtos/secrets.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,16 @@ In the above example, replace `"my-super-safe-password"` with your actual
6161
encryption password, and `my-disk-encryption-password` with the relevant entry
6262
in your pass password store. Also, ensure to replace `'.#your-host'` and
6363
`root@yourip` with your actual flake and IP address, respectively.
64+
65+
## Example: Using existing SSH host keys
66+
67+
If the system contains existing trusted `/etc/ssh/ssh_host_*` SSH host keys and
68+
certificates, `nixos-anywhere` can copy them in case they are necessary during
69+
installation and system activation.
70+
71+
```
72+
nixos-anywhere --copy-host-keys --flake '.#your-host' root@yourip
73+
```
74+
75+
This would copy `/etc/ssh/ssh_host_*` to `/mnt` after kexec but before
76+
installation, ignoring files that already exist in destination.

src/nixos-anywhere.sh

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Options:
2929
use another kexec tarball to bootstrap NixOS
3030
* --post-kexec-ssh-port <ssh_port>
3131
after kexec is executed, use a custom ssh port to connect. Defaults to 22
32+
* --copy-host-keys
33+
copy over existing /etc/ssh/ssh_host_* host keys to the installation
3234
* --stop-after-disko
3335
exit after disko formatting, you can then proceed to install manually or some other way
3436
* --extra-files <file...>
@@ -119,6 +121,10 @@ while [[ $# -gt 0 ]]; do
119121
post_kexec_ssh_port=$2
120122
shift
121123
;;
124+
--copy-host-keys)
125+
copy_host_keys=y
126+
shift
127+
;;
122128
--debug)
123129
enable_debug="-x"
124130
print_build_logs=y
@@ -450,13 +456,25 @@ fi
450456

451457
step Installing NixOS
452458
ssh_ bash <<SSH
453-
set -efu ${enable_debug}
459+
set -eu ${enable_debug}
454460
# when running not in nixos we might miss this directory, but it's needed in the nixos chroot during installation
455-
export PATH=\$PATH:/run/current-system/sw/bin
461+
export PATH="\$PATH:/run/current-system/sw/bin"
456462
457463
# needed for installation if initrd-secrets are used
458464
mkdir -p /mnt/tmp
459465
chmod 777 /mnt/tmp
466+
if [[ ${copy_host_keys-n} == "y" ]]; then
467+
# NB we copy host keys that are in turn copied by kexec installer.
468+
mkdir -m 755 -p /mnt/etc/ssh
469+
for p in /etc/ssh/ssh_host_*; do
470+
# Skip if the source file does not exist (i.e. glob did not match any files)
471+
# or the destination already exists (e.g. copied with --extra-files).
472+
if [ ! -e "\$p" -o -e "/mnt/\$p" ]; then
473+
continue
474+
end
475+
cp -a "\$p" "/mnt/\$p"
476+
done
477+
fi
460478
nixos-install --no-root-passwd --no-channel-copy --system "$nixos_system"
461479
if command -v zpool >/dev/null; then
462480
zpool export -a || : # we always want to export the zfs pools so people can boot from it without force import

0 commit comments

Comments
 (0)