summary refs log tree commit diff
path: root/nixos/modules/services/networking/spacecookie.nix
blob: 400f3e26cc9a19cb619918acf860d37356b6809c (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.spacecookie;

  spacecookieConfig = {
    listen = {
      inherit (cfg) port;
    };
  } // cfg.settings;

  format = pkgs.formats.json {};

  configFile = format.generate "spacecookie.json" spacecookieConfig;

in {
  imports = [
    (mkRenamedOptionModule [ "services" "spacecookie" "root" ] [ "services" "spacecookie" "settings" "root" ])
    (mkRenamedOptionModule [ "services" "spacecookie" "hostname" ] [ "services" "spacecookie" "settings" "hostname" ])
  ];

  options = {

    services.spacecookie = {

      enable = mkEnableOption "spacecookie";

      package = mkOption {
        type = types.package;
        default = pkgs.spacecookie;
        defaultText = literalExpression "pkgs.spacecookie";
        example = literalExpression "pkgs.haskellPackages.spacecookie";
        description = ''
          The spacecookie derivation to use. This can be used to
          override the used package or to use another version.
        '';
      };

      openFirewall = mkOption {
        type = types.bool;
        default = false;
        description = ''
          Whether to open the necessary port in the firewall for spacecookie.
        '';
      };

      port = mkOption {
        type = types.port;
        default = 70;
        description = ''
          Port the gopher service should be exposed on.
        '';
      };

      address = mkOption {
        type = types.str;
        default = "[::]";
        description = ''
          Address to listen on. Must be in the
          <literal>ListenStream=</literal> syntax of
          <link xlink:href="https://www.freedesktop.org/software/systemd/man/systemd.socket.html">systemd.socket(5)</link>.
        '';
      };

      settings = mkOption {
        type = types.submodule {
          freeformType = format.type;

          options.hostname = mkOption {
            type = types.str;
            default = "localhost";
            description = ''
              The hostname the service is reachable via. Clients
              will use this hostname for further requests after
              loading the initial gopher menu.
            '';
          };

          options.root = mkOption {
            type = types.path;
            default = "/srv/gopher";
            description = ''
              The directory spacecookie should serve via gopher.
              Files in there need to be world-readable since
              the spacecookie service file sets
              <literal>DynamicUser=true</literal>.
            '';
          };

          options.log = {
            enable = mkEnableOption "logging for spacecookie"
              // { default = true; example = false; };

            hide-ips = mkOption {
              type = types.bool;
              default = true;
              description = ''
                If enabled, spacecookie will hide personal
                information of users like IP addresses from
                log output.
              '';
            };

            hide-time = mkOption {
              type = types.bool;
              # since we are starting with systemd anyways
              # we deviate from the default behavior here:
              # journald will add timestamps, so no need
              # to double up.
              default = true;
              description = ''
                If enabled, spacecookie will not print timestamps
                at the beginning of every log line.
              '';
            };

            level = mkOption {
              type = types.enum [
                "info"
                "warn"
                "error"
              ];
              default = "info";
              description = ''
                Log level for the spacecookie service.
              '';
            };
          };
        };

        description = ''
          Settings for spacecookie. The settings set here are
          directly translated to the spacecookie JSON config
          file. See
          <link xlink:href="https://sternenseemann.github.io/spacecookie/spacecookie.json.5.html">spacecookie.json(5)</link>
          for explanations of all options.
        '';
      };
    };
  };

  config = mkIf cfg.enable {
    assertions = [
      {
        assertion = !(cfg.settings ? user);
        message = ''
          spacecookie is started as a normal user, so the setuid
          feature doesn't work. If you want to run spacecookie as
          a specific user, set:
          systemd.services.spacecookie.serviceConfig = {
            DynamicUser = false;
            User = "youruser";
            Group = "yourgroup";
          }
        '';
      }
      {
        assertion = !(cfg.settings ? listen || cfg.settings ? port);
        message = ''
          The NixOS spacecookie module uses socket activation,
          so the listen options have no effect. Use the port
          and address options in services.spacecookie instead.
        '';
      }
    ];

    systemd.sockets.spacecookie = {
      description = "Socket for the Spacecookie Gopher Server";
      wantedBy = [ "sockets.target" ];
      listenStreams = [ "${cfg.address}:${toString cfg.port}" ];
      socketConfig = {
        BindIPv6Only = "both";
      };
    };

    systemd.services.spacecookie = {
      description = "Spacecookie Gopher Server";
      wantedBy = [ "multi-user.target" ];
      requires = [ "spacecookie.socket" ];

      serviceConfig = {
        Type = "notify";
        ExecStart = "${lib.getBin cfg.package}/bin/spacecookie ${configFile}";
        FileDescriptorStoreMax = 1;

        DynamicUser = true;

        ProtectSystem = "strict";
        ProtectHome = true;
        PrivateTmp = true;
        PrivateDevices = true;
        PrivateMounts = true;
        PrivateUsers = true;

        ProtectKernelTunables = true;
        ProtectKernelModules = true;
        ProtectControlGroups = true;

        CapabilityBoundingSet = "";
        NoNewPrivileges = true;
        LockPersonality = true;
        RestrictRealtime = true;

        # AF_UNIX for communication with systemd
        # AF_INET replaced by BindIPv6Only=both
        RestrictAddressFamilies = "AF_UNIX AF_INET6";
      };
    };

    networking.firewall = mkIf cfg.openFirewall {
      allowedTCPPorts = [ cfg.port ];
    };
  };
}