1 { pkgs, lib, config, machineName, ... }:
3 inherit (config.security) gnupg;
5 initrdKey = "initrd/ssh.key";
6 keygrip = "9AA84E6F6D71F9163C46BF396B141A0806219077";
10 <nixpkgs/nixos/modules/profiles/hardened.nix>
12 security.gnupg.store = builtins.getEnv "PASSWORD_STORE_DIR" + "/machines/${machineName}";
13 services.openssh.extraConfig = ''
14 StreamLocalBindUnlink yes
16 installer.ssh-nixos = {
17 PATH = [pkgs.gnupg pkgs.openssh];
19 #"-R" "/var/lib/gnupg/S.gpg-agent.extra:/run/user/1000/gnupg/d.w1sj57hx3zfcwadyxpr6wko9/S.gpg-agent.extra"
20 #"-o" "StreamLocalBindUnlink=yes"
22 script = lib.mkMerge [
24 # Send the SSH key of the initrd
25 gpg --decrypt '${gnupg.store}/${initrdKey}.gpg' |
26 ssh '${config.installer.ssh-nixos.target}' \
27 install -D -m 400 -o root -g root /dev/stdin /root/${initrdKey}
30 ssh '${config.installer.ssh-nixos.target}' \
31 gpg-connect-agent --no-autostart --homedir /var/lib/gnupg "'keyinfo --list'" /bye 2>&1 |
32 grep -qx -e "gpg-connect-agent: no gpg-agent running in this session" \
33 -e "S KEYINFO ${keygrip} . . . 1 .*" || {
35 gpg --decrypt '${gnupg.store}/${rootKey}.pass.gpg' |
36 gpg --batch --pinentry-mode loopback --passphrase-fd 0 --export-secret-subkeys @root@${machineName} |
37 ssh '${config.installer.ssh-nixos.target}' \
38 gpg --no-autostart --homedir /var/lib/gnupg --no-autostart --batch --pinentry-mode loopback --import
40 # Send the rootKey's passphrase
41 gpg --decrypt '${gnupg.store}/${rootKey}.pass.gpg' |
42 ssh '${config.installer.ssh-nixos.target}' \
43 gpg-preset-passphrase --homedir /var/lib/gnupg --preset ${keygrip}
49 gpg --decrypt '${gnupg.store}/${rootKey}.pass.gpg' |
50 gpg --batch --pinentry-mode loopback --passphrase-fd 0 --export-secret-subkeys @root@${machineName} |
51 ssh '${config.installer.ssh-nixos.target}' \
52 gpg --homedir /var/lib/gnupg --no-autostart --batch --pinentry-mode loopback --import
54 gpg --batch --export @root@${machineName} |
55 ssh '${config.installer.ssh-nixos.target}' \
56 gpg --no-autostart --homedir /var/lib/gnupg --no-autostart --batch --pinentry-mode loopback --import
60 boot.initrd.network.ssh.hostKeys = [ "/root/${initrdKey}" ];