@@ -0,0 +1,83 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+
5+let
6+ cfg = config.services.spacecookie;
7+ configFile = pkgs.writeText "spacecookie.json" (lib.generators.toJSON {} {
8+ inherit (cfg) hostname port root;
9+ });
10+in {
11+
12+ options = {
13+
14+ services.spacecookie = {
15+
16+ enable = mkEnableOption "spacecookie";
17+
18+ hostname = mkOption {
19+ type = types.str;
20+ default = "localhost";
21+ description = "The hostname the service is reachable via. Clients will use this hostname for further requests after loading the initial gopher menu.";
22+ };
23+
24+ port = mkOption {
25+ type = types.port;
26+ default = 70;
27+ description = "Port the gopher service should be exposed on.";
28+ };
29+
30+ root = mkOption {
31+ type = types.path;
32+ default = "/srv/gopher";
33+ description = "The root directory spacecookie serves via gopher.";
34+ };
35+ };
36+ };
37+
38+ config = mkIf cfg.enable {
39+
40+ systemd.sockets.spacecookie = {
41+ description = "Socket for the Spacecookie Gopher Server";
42+ wantedBy = [ "sockets.target" ];
43+ listenStreams = [ "[::]:${toString cfg.port}" ];
44+ socketConfig = {
45+ BindIPv6Only = "both";
46+ };
47+ };
48+
49+ systemd.services.spacecookie = {
50+ description = "Spacecookie Gopher Server";
51+ wantedBy = [ "multi-user.target" ];
52+ requires = [ "spacecookie.socket" ];
53+
54+ serviceConfig = {
55+ Type = "notify";
56+ ExecStart = "${pkgs.haskellPackages.spacecookie}/bin/spacecookie ${configFile}";
57+ FileDescriptorStoreMax = 1;
58+
59+ DynamicUser = true;
60+
61+ ProtectSystem = "strict";
62+ ProtectHome = true;
63+ PrivateTmp = true;
64+ PrivateDevices = true;
65+ PrivateMounts = true;
66+ PrivateUsers = true;
67+
68+ ProtectKernelTunables = true;
69+ ProtectKernelModules = true;
70+ ProtectControlGroups = true;
71+
72+ CapabilityBoundingSet = "";
73+ NoNewPrivileges = true;
74+ LockPersonality = true;
75+ RestrictRealtime = true;
76+
77+ # AF_UNIX for communication with systemd
78+ # AF_INET replaced by BindIPv6Only=both
79+ RestrictAddressFamilies = "AF_UNIX AF_INET6";
80+ };
81+ };
82+ };
83+}