summaryrefslogtreecommitdiff
path: root/system-profiles/initrd-ssh/default.nix
blob: dca0f125cb6907aa835c4b58083fb80e39085202 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{ hostName, config, pkgs, lib, ... }:

with lib;

{
  imports = [ ./module.nix ];

  config = {
    boot.initrd = {
      network = {
        enable = true;
        ssh = {
          enable = true;
          hostKeys = [ "/etc/ssh/ssh_host_ed25519_key" "/etc/ssh/ssh_host_rsa_key" ];
          authorizedKeys = config.users.users.root.openssh.authorizedKeys.keys ++ map (kF: builtins.readFile kF) config.users.users.root.openssh.authorizedKeys.keyFiles;
        };
      };

      secrets = with config.sops.secrets; {
        "/etc/ssh/ssh_host_ed25519_key" = initrd_ssh_host_ed25519_key.path;
        "/etc/ssh/ssh_host_rsa_key" = initrd_ssh_host_rsa_key.path;
      };

      extraFiles = let
        mkPubkey = typ: pkgs.runCommand "ssh_host_${typ}_key.pub" { buildInputs = with pkgs; [ yq ]; } ''
          yq -r '.${typ}' ${./host-keys + "/${hostName}-public.yaml"} > $out
        '';
      in {
        "/etc/ssh/ssh_host_rsa_key.pub".source = mkPubkey "rsa";
        "/etc/ssh/ssh_host_ed25519_key.pub".source = mkPubkey "ed25519";
      };
    };

    sops.secrets = {
      initrd_ssh_host_rsa_key = {
          key = "rsa";
          sopsFile = ./host-keys + "/${hostName}-private.yaml";
      };
      initrd_ssh_host_ed25519_key = {
          key = "ed25519";
          sopsFile = ./host-keys + "/${hostName}-private.yaml";
      };
    };
  };
}