1{ config, lib, pkgs, ... }:
2
3let
4 cfg = config.services.stargazer;
5 globalSection = ''
6 listen = ${lib.concatStringsSep " " cfg.listen}
7 connection-logging = ${lib.boolToString cfg.connectionLogging}
8 log-ip = ${lib.boolToString cfg.ipLog}
9 log-ip-partial = ${lib.boolToString cfg.ipLogPartial}
10 request-timeout = ${toString cfg.requestTimeout}
11 response-timeout = ${toString cfg.responseTimeout}
12
13 [:tls]
14 store = ${toString cfg.store}
15 organization = ${cfg.certOrg}
16 gen-certs = ${lib.boolToString cfg.genCerts}
17 regen-certs = ${lib.boolToString cfg.regenCerts}
18 ${lib.optionalString (cfg.certLifetime != "") "cert-lifetime = ${cfg.certLifetime}"}
19
20 '';
21 genINI = lib.generators.toINI { };
22 configFile = pkgs.writeText "config.ini" (lib.strings.concatStrings (
23 [ globalSection ] ++ (lib.lists.forEach cfg.routes (section:
24 let
25 name = section.route;
26 params = builtins.removeAttrs section [ "route" ];
27 in
28 genINI
29 {
30 "${name}" = params;
31 } + "\n"
32 ))
33 ));
34in
35{
36 options.services.stargazer = {
37 enable = lib.mkEnableOption "Stargazer Gemini server";
38
39 listen = lib.mkOption {
40 type = lib.types.listOf lib.types.str;
41 default = [ "0.0.0.0" ] ++ lib.optional config.networking.enableIPv6 "[::0]";
42 defaultText = lib.literalExpression ''[ "0.0.0.0" ] ++ lib.optional config.networking.enableIPv6 "[::0]"'';
43 example = lib.literalExpression ''[ "10.0.0.12" "[2002:a00:1::]" ]'';
44 description = ''
45 Address and port to listen on.
46 '';
47 };
48
49 connectionLogging = lib.mkOption {
50 type = lib.types.bool;
51 default = true;
52 description = "Whether or not to log connections to stdout.";
53 };
54
55 ipLog = lib.mkOption {
56 type = lib.types.bool;
57 default = false;
58 description = "Log client IP addresses in the connection log.";
59 };
60
61 ipLogPartial = lib.mkOption {
62 type = lib.types.bool;
63 default = false;
64 description = "Log partial client IP addresses in the connection log.";
65 };
66
67 requestTimeout = lib.mkOption {
68 type = lib.types.int;
69 default = 5;
70 description = ''
71 Number of seconds to wait for the client to send a complete
72 request. Set to 0 to disable.
73 '';
74 };
75
76 responseTimeout = lib.mkOption {
77 type = lib.types.int;
78 default = 0;
79 description = ''
80 Number of seconds to wait for the client to send a complete
81 request and for stargazer to finish sending the response.
82 Set to 0 to disable.
83 '';
84 };
85
86 store = lib.mkOption {
87 type = lib.types.path;
88 default = /var/lib/gemini/certs;
89 description = ''
90 Path to the certificate store on disk. This should be a
91 persistent directory writable by Stargazer.
92 '';
93 };
94
95 certOrg = lib.mkOption {
96 type = lib.types.str;
97 default = "stargazer";
98 description = ''
99 The name of the organization responsible for the X.509
100 certificate's /O name.
101 '';
102 };
103
104 genCerts = lib.mkOption {
105 type = lib.types.bool;
106 default = true;
107 description = ''
108 Set to false to disable automatic certificate generation.
109 Use if you want to provide your own certs.
110 '';
111 };
112
113 regenCerts = lib.mkOption {
114 type = lib.types.bool;
115 default = true;
116 description = ''
117 Set to false to turn off automatic regeneration of expired certificates.
118 Use if you want to provide your own certs.
119 '';
120 };
121
122 certLifetime = lib.mkOption {
123 type = lib.types.str;
124 default = "";
125 description = ''
126 How long certs generated by Stargazer should live for.
127 Certs live forever by default.
128 '';
129 example = lib.literalExpression "\"1y\"";
130 };
131
132 debugMode = lib.mkOption {
133 type = lib.types.bool;
134 default = false;
135 description = "Run Stargazer in debug mode.";
136 };
137
138 routes = lib.mkOption {
139 type = lib.types.listOf
140 (lib.types.submodule {
141 freeformType = with lib.types; attrsOf (nullOr
142 (oneOf [
143 bool
144 int
145 float
146 str
147 ]) // {
148 description = "INI atom (null, bool, int, float or string)";
149 });
150 options.route = lib.mkOption {
151 type = lib.types.str;
152 description = "Route section name";
153 };
154 });
155 default = [ ];
156 description = ''
157 Routes that Stargazer should server.
158
159 Expressed as a list of attribute sets. Each set must have a key `route`
160 that becomes the section name for that route in the stargazer ini cofig.
161 The remaining keys and values become the parameters for that route.
162
163 [Refer to upstream docs for other params](https://git.sr.ht/~zethra/stargazer/tree/main/item/doc/stargazer.ini.5.txt)
164 '';
165 example = lib.literalExpression ''
166 [
167 {
168 route = "example.com";
169 root = "/srv/gemini/example.com"
170 }
171 {
172 route = "example.com:/man";
173 root = "/cgi-bin";
174 cgi = true;
175 }
176 {
177 route = "other.org~(.*)";
178 redirect = "gemini://example.com";
179 rewrite = "\1";
180 }
181 ]
182 '';
183 };
184
185 user = lib.mkOption {
186 type = lib.types.str;
187 default = "stargazer";
188 description = "User account under which stargazer runs.";
189 };
190
191 group = lib.mkOption {
192 type = lib.types.str;
193 default = "stargazer";
194 description = "Group account under which stargazer runs.";
195 };
196 };
197
198 config = lib.mkIf cfg.enable {
199 systemd.services.stargazer = {
200 description = "Stargazer gemini server";
201 after = [ "network.target" ];
202 wantedBy = [ "multi-user.target" ];
203 serviceConfig = {
204 ExecStart = "${pkgs.stargazer}/bin/stargazer ${configFile} ${lib.optionalString cfg.debugMode "-D"}";
205 Restart = "always";
206 # User and group
207 User = cfg.user;
208 Group = cfg.group;
209 };
210 };
211
212 # Create default cert store
213 systemd.tmpfiles.rules = lib.mkIf (cfg.store == /var/lib/gemini/certs) [
214 ''d /var/lib/gemini/certs - "${cfg.user}" "${cfg.group}" -''
215 ];
216
217 users.users = lib.optionalAttrs (cfg.user == "stargazer") {
218 stargazer = {
219 group = cfg.group;
220 isSystemUser = true;
221 };
222 };
223
224 users.groups = lib.optionalAttrs (cfg.group == "stargazer") {
225 stargazer = { };
226 };
227 };
228
229 meta.maintainers = with lib.maintainers; [ gaykitty ];
230}