aboutsummaryrefslogtreecommitdiff
path: root/modules/services/nixos/ssh.nix
blob: 34265edd53a9605ee785abe4179b1dbfcf1fea2a (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
46
47
48
49
50
51
52
53
54
55
56
{
  config,
  lib,
  hosts,
  ...
}: let
  cfg = config.collinux.services.networking.sshd;
in {
  config = lib.mkIf cfg.enable {
    services.openssh = {
      enable = true;
      hostKeys = [
        {
          path = "/etc/ssh/ssh_host_ed25519_key";
          type = "ed25519";
        }
      ];

      listenAddresses = [
        {
          addr = cfg.bind_host;
          port = 22;
        }
      ];

      settings = {
        PermitRootLogin = "prohibit-password"; # deploy-rs uses root account
        PasswordAuthentication = false;
      };

      knownHosts = builtins.mapAttrs (_: data: {publicKey = data.host_pubkey;}) hosts;
    };

    users.users.${config.collinux.user.name}.openssh.authorizedKeys.keys =
      hosts
      |> (builtins.mapAttrs (_: data: data.user_pubkey or null))
      |> builtins.attrValues
      |> (builtins.filter (x: x != null));

    users.users."root".openssh.authorizedKeys.keys =
      if config.services.openssh.settings.PermitRootLogin == "prohibit-password"
      then
        hosts
        |> (builtins.mapAttrs (_: data: data.user_pubkey or null))
        |> builtins.attrValues
        |> (builtins.filter (x: x != null))
      else {};

    systemd.services."openssh" = lib.mkIf config.collinux.services.networking.networkd.enable {
      after = lib.mkAfter ["network-online.target"];
      wants = lib.mkAfter ["network-online.target"];
    };

    services.tailscale.extraSetFlags = lib.optional config.services.tailscale.enable "--ssh=true";
  };
}