Skip to content

Commit

Permalink
copy over ssh host keys
Browse files Browse the repository at this point in the history
  • Loading branch information
tie committed Sep 30, 2023
1 parent bb058eb commit b1b0435
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
13 changes: 13 additions & 0 deletions docs/howtos/secrets.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,16 @@ In the above example, replace `"my-super-safe-password"` with your actual
encryption password, and `my-disk-encryption-password` with the relevant entry
in your pass password store. Also, ensure to replace `'.#your-host'` and
`root@yourip` with your actual flake and IP address, respectively.

## Example: Using existing SSH host keys

If the system contains existing trusted `/etc/ssh/ssh_host_*` SSH host keys and
certificates, `nixos-anywhere` can copy them in case they are necessary during
installation and system activation.

```
nixos-anywhere --copy-host-keys --flake '.#your-host' root@yourip
```

This would copy `/etc/ssh/ssh_host_*` to `/mnt` after kexec but before
installation, ignoring files that already exist in destination.
22 changes: 20 additions & 2 deletions src/nixos-anywhere.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Options:
use another kexec tarball to bootstrap NixOS
* --post-kexec-ssh-port <ssh_port>
after kexec is executed, use a custom ssh port to connect. Defaults to 22
* --copy-host-keys
copy over existing /etc/ssh/ssh_host_* host keys to the installation
* --stop-after-disko
exit after disko formatting, you can then proceed to install manually or some other way
* --extra-files <file...>
Expand Down Expand Up @@ -119,6 +121,10 @@ while [[ $# -gt 0 ]]; do
post_kexec_ssh_port=$2
shift
;;
--copy-host-keys)
copy_host_keys=y
shift
;;
--debug)
enable_debug="-x"
print_build_logs=y
Expand Down Expand Up @@ -450,13 +456,25 @@ fi

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

0 comments on commit b1b0435

Please sign in to comment.