1 { config, lib, pkgs, ... }:
6 cfg = config.services.spacecookie;
14 format = pkgs.formats.json {};
16 configFile = format.generate "spacecookie.json" spacecookieConfig;
20 (mkRenamedOptionModule [ "services" "spacecookie" "root" ] [ "services" "spacecookie" "settings" "root" ])
21 (mkRenamedOptionModule [ "services" "spacecookie" "hostname" ] [ "services" "spacecookie" "settings" "hostname" ])
26 services.spacecookie = {
28 enable = mkEnableOption (lib.mdDoc "spacecookie");
32 default = pkgs.spacecookie;
33 defaultText = literalExpression "pkgs.spacecookie";
34 example = literalExpression "pkgs.haskellPackages.spacecookie";
35 description = lib.mdDoc ''
36 The spacecookie derivation to use. This can be used to
37 override the used package or to use another version.
41 openFirewall = mkOption {
44 description = lib.mdDoc ''
45 Whether to open the necessary port in the firewall for spacecookie.
52 description = lib.mdDoc ''
53 Port the gopher service should be exposed on.
60 description = lib.mdDoc ''
61 Address to listen on. Must be in the
62 `ListenStream=` syntax of
63 [systemd.socket(5)](https://www.freedesktop.org/software/systemd/man/systemd.socket.html).
68 type = types.submodule {
69 freeformType = format.type;
71 options.hostname = mkOption {
73 default = "localhost";
74 description = lib.mdDoc ''
75 The hostname the service is reachable via. Clients
76 will use this hostname for further requests after
77 loading the initial gopher menu.
81 options.root = mkOption {
83 default = "/srv/gopher";
84 description = lib.mdDoc ''
85 The directory spacecookie should serve via gopher.
86 Files in there need to be world-readable since
87 the spacecookie service file sets
93 enable = mkEnableOption (lib.mdDoc "logging for spacecookie")
94 // { default = true; example = false; };
99 description = lib.mdDoc ''
100 If enabled, spacecookie will hide personal
101 information of users like IP addresses from
106 hide-time = mkOption {
108 # since we are starting with systemd anyways
109 # we deviate from the default behavior here:
110 # journald will add timestamps, so no need
113 description = lib.mdDoc ''
114 If enabled, spacecookie will not print timestamps
115 at the beginning of every log line.
126 description = lib.mdDoc ''
127 Log level for the spacecookie service.
133 description = lib.mdDoc ''
134 Settings for spacecookie. The settings set here are
135 directly translated to the spacecookie JSON config
137 [spacecookie.json(5)](https://sternenseemann.github.io/spacecookie/spacecookie.json.5.html)
138 for explanations of all options.
144 config = mkIf cfg.enable {
147 assertion = !(cfg.settings ? user);
149 spacecookie is started as a normal user, so the setuid
150 feature doesn't work. If you want to run spacecookie as
151 a specific user, set:
152 systemd.services.spacecookie.serviceConfig = {
160 assertion = !(cfg.settings ? listen || cfg.settings ? port);
162 The NixOS spacecookie module uses socket activation,
163 so the listen options have no effect. Use the port
164 and address options in services.spacecookie instead.
169 systemd.sockets.spacecookie = {
170 description = "Socket for the Spacecookie Gopher Server";
171 wantedBy = [ "sockets.target" ];
172 listenStreams = [ "${cfg.address}:${toString cfg.port}" ];
174 BindIPv6Only = "both";
178 systemd.services.spacecookie = {
179 description = "Spacecookie Gopher Server";
180 wantedBy = [ "multi-user.target" ];
181 requires = [ "spacecookie.socket" ];
185 ExecStart = "${lib.getBin cfg.package}/bin/spacecookie ${configFile}";
186 FileDescriptorStoreMax = 1;
190 ProtectSystem = "strict";
193 PrivateDevices = true;
194 PrivateMounts = true;
197 ProtectKernelTunables = true;
198 ProtectKernelModules = true;
199 ProtectControlGroups = true;
201 CapabilityBoundingSet = "";
202 NoNewPrivileges = true;
203 LockPersonality = true;
204 RestrictRealtime = true;
206 # AF_UNIX for communication with systemd
207 # AF_INET replaced by BindIPv6Only=both
208 RestrictAddressFamilies = "AF_UNIX AF_INET6";
212 networking.firewall = mkIf cfg.openFirewall {
213 allowedTCPPorts = [ cfg.port ];