about summary refs log tree commit diff
path: root/modules/nixos/openssh.nix
blob: c7a144e6b1652d4423b1764e412143fc7aa2078c (plain) (blame)
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
57
58
59
{
  config,
  lib,
  pkgs,
  ...
}:
with lib; let
  cfg = config.nixfiles.modules.openssh;
in {
  options.nixfiles.modules.openssh.server = {
    enable = mkEnableOption "OpenSSH server";

    port = mkOption {
      description = "OpenSSH server port.";
      type = types.port;
      default = 22022; # Port 22 should be occupied by a tarpit.
    };
  };

  config = mkIf cfg.server.enable {
    # FIXME This is mounted after the activation script is launched.
    ark = {
      files = [
        "/etc/ssh/ssh_host_ed25519_key"
        "/etc/ssh/ssh_host_ed25519_key.pub"
        "/etc/ssh/ssh_host_rsa_key"
        "/etc/ssh/ssh_host_rsa_key.pub"
      ];
      # directories = ["/etc/ssh/authorized_keys.d"];
    };

    programs.mosh.enable = true;

    services = {
      openssh = {
        enable = true;
        ports = [cfg.server.port];
        settings = {
          ClientAliveCountMax = 3;
          ClientAliveInterval = 60;
          KbdInteractiveAuthentication = false;
          LogLevel =
            if config.nixfiles.modules.fail2ban.enable
            then "VERBOSE"
            else "ERROR";
          MaxAuthTries = 3;
          PasswordAuthentication = false;
          PermitRootLogin = mkForce "no";
        };
      };

      fail2ban.jails.sshd = ''
        enabled = true
        mode = aggressive
        port = ${toString cfg.server.port}
      '';
    };
  };
}

Consider giving Nix/NixOS a try! <3