summaryrefslogtreecommitdiff
path: root/modules/nixos/profiles/server.nix
blob: 373dc5dea0d2ced4de4d7ad7483c1003af69f0ce (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
57
58
59
60
61
62
63
64
65
66
67
68
69
{
  config,
  lib,
  inputs',
  ...
}:
let
  cfg = config.profiles.server;

  # 2^30
  # Why doesn't nix have a `pow`???
  gb = 1024 * 1024 * 1024;
  minimumStorageKb = 15 * gb;
in
{
  options.profiles.server = {
    enable = lib.mkEnableOption "the Server profile";

    hostUser = lib.mkEnableOption "a default interactive user" // {
      default = true;
    };
  };

  config = lib.mkIf cfg.enable (
    lib.mkMerge [
      {
        # All servers are most likely on stable, so we want to pull in some newer packages from time to time
        _module.args.unstable = inputs'.nixpkgs.legacyPackages;

        boot.tmp.cleanOnBoot = lib.mkDefault true;

        # We don't need it here
        documentation.enable = false;

        environment.defaultPackages = lib.mkForce [ ];

        nix.gc = {
          dates = "*:0/30"; # Every 30 minutes
          # GC to stay above minimumStorageBytes
          options = toString [
            "--delete-older-than 5d"
            "--max-freed \"$((${toString minimumStorageKb} - 1024 * $(df -k --output=avail /nix/store | tail -n 1)))\""
          ];
        };

        services.comin.enable = true;

        traits = {
          secrets.enable = true;
          tailscale = {
            enable = true;
            ssh.enable = true;
          };
          zram.enable = true;
        };
      }

      (lib.mkIf cfg.hostUser {
        # Hardening access to `nix` as no other users *should* ever really touch it
        nix.settings.allowed-users = [ config.networking.hostName ];

        users.users.${config.networking.hostName} = {
          isNormalUser = true;
          extraGroups = [ "wheel" ];
        };
      })
    ]
  );
}