# Shared activation-script logic to preserve the SSH host key across # nixos-rebuild on platforms that embed the key via environment.etc (lxc and # proxmox). When NIXOS_HOST_KEYS_DIR is not set the key is absent from # environment.etc, and NixOS's etc activation removes any /etc file not in # the new generation — which would destroy the live key and break sops-nix # decryption permanently. These scripts save the key to /run before etc # removes it, then restore it afterward. # # Explicit deps enforce the correct ordering: without them the topological # sort places preserveSshHostKey after etc (confirmed live on lxc-tor-relay: # position 7 vs etc's position 5), so the key is gone before it can be saved. _: { system.activationScripts = { preserveSshHostKey = '' if [ -f /etc/ssh/ssh_host_ed25519_key ]; then cp /etc/ssh/ssh_host_ed25519_key /run/sshd-host-key-preserve.tmp cp /etc/ssh/ssh_host_ed25519_key.pub /run/sshd-host-key-preserve.pub.tmp fi ''; restoreSshHostKey = { deps = [ "etc" ]; text = '' if [ ! -f /etc/ssh/ssh_host_ed25519_key ] && [ -f /run/sshd-host-key-preserve.tmp ]; then install -m 0600 /run/sshd-host-key-preserve.tmp /etc/ssh/ssh_host_ed25519_key install -m 0644 /run/sshd-host-key-preserve.pub.tmp /etc/ssh/ssh_host_ed25519_key.pub fi rm -f /run/sshd-host-key-preserve.tmp /run/sshd-host-key-preserve.pub.tmp ''; }; etc = { deps = [ "preserveSshHostKey" ]; }; setupSecrets = { deps = [ "restoreSshHostKey" ]; }; }; }