summary refs log tree commit diff
path: root/nixos/modules/services/web-servers/agate.nix
blob: 3afdb561c0b0a819552e0e8a02f66b22c9cc3ad0 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.agate;
in
{
  options = {
    services.agate = {
      enable = mkEnableOption "Agate Server";

      package = mkOption {
        type = types.package;
        default = pkgs.agate;
        defaultText = literalExpression "pkgs.agate";
        description = "The package to use";
      };

      addresses = mkOption {
        type = types.listOf types.str;
        default = [ "0.0.0.0:1965" ];
        description = ''
          Addresses to listen on, IP:PORT, if you haven't disabled forwarding
          only set IPv4.
        '';
      };

      contentDir = mkOption {
        default = "/var/lib/agate/content";
        type = types.path;
        description = "Root of the content directory.";
      };

      certificatesDir = mkOption {
        default = "/var/lib/agate/certificates";
        type = types.path;
        description = "Root of the certificate directory.";
      };

      hostnames = mkOption {
        default = [ ];
        type = types.listOf types.str;
        description = ''
          Domain name of this Gemini server, enables checking hostname and port
          in requests. (multiple occurences means basic vhosts)
        '';
      };

      language = mkOption {
        default = null;
        type = types.nullOr types.str;
        description = "RFC 4646 Language code for text/gemini documents.";
      };

      onlyTls_1_3 = mkOption {
        default = false;
        type = types.bool;
        description = "Only use TLSv1.3 (default also allows TLSv1.2).";
      };

      extraArgs = mkOption {
        type = types.listOf types.str;
        default = [ "" ];
        example = [ "--log-ip" ];
        description = "Extra arguments to use running agate.";
      };
    };
  };

  config = mkIf cfg.enable {
    # available for generating certs by hand
    # it can be a bit arduous with openssl
    environment.systemPackages = [ cfg.package ];

    systemd.services.agate = {
      description = "Agate";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" "network-online.target" ];

      script =
        let
          prefixKeyList = key: list: concatMap (v: [ key v ]) list;
          addresses = prefixKeyList "--addr" cfg.addresses;
          hostnames = prefixKeyList "--hostname" cfg.hostnames;
        in
        ''
          exec ${cfg.package}/bin/agate ${
            escapeShellArgs (
              [
                "--content" "${cfg.contentDir}"
                "--certs" "${cfg.certificatesDir}"
              ] ++
              addresses ++
              (optionals (cfg.hostnames != []) hostnames) ++
              (optionals (cfg.language != null) [ "--lang" cfg.language ]) ++
              (optionals cfg.onlyTls_1_3 [ "--only-tls13" ]) ++
              (optionals (cfg.extraArgs != []) cfg.extraArgs)
            )
          }
        '';

      serviceConfig = {
        Restart = "always";
        RestartSec = "5s";
        DynamicUser = true;
        StateDirectory = "agate";

        # Security options:
        AmbientCapabilities = "";
        CapabilityBoundingSet = "";

        # ProtectClock= adds DeviceAllow=char-rtc r
        DeviceAllow = "";

        LockPersonality = true;

        PrivateTmp = true;
        PrivateDevices = true;
        PrivateUsers = true;

        ProtectClock = true;
        ProtectControlGroups = true;
        ProtectHostname = true;
        ProtectKernelLogs = true;
        ProtectKernelModules = true;
        ProtectKernelTunables = true;

        RestrictNamespaces = true;
        RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ];
        RestrictRealtime = true;

        SystemCallArchitectures = "native";
        SystemCallErrorNumber = "EPERM";
        SystemCallFilter = [
          "@system-service"
          "~@cpu-emulation"
          "~@debug"
          "~@keyring"
          "~@memlock"
          "~@obsolete"
          "~@privileged"
          "~@setuid"
        ];
      };
    };
  };
}