gunk-dev/the-valley

web-receiver.nix

source03252b3b26acmain's published tip history
9.9 KiB
1# The web export receiver (web/cmd/valley-web-receive) and valley-web
2# (web/cmd/valley-web) on a NixOS edge host: two long-running units
3# over one state directory.
4#
5# The receiver takes the publisher's uploads and is the state directory's
6# one writer. valley-web serves the activated export to anonymous readers
7# and only reads the directory. Each runs as a user of its own, both in
8# the valley-web group. The directory is the receiver's, mode 0750, so
9# valley-web reads it through the group, and the receiver keeps its
10# staging area to itself (web/export/store.go). Each unit has its own
11# listener and its own memory cap, so an upload that saturates the
12# receiver leaves valley-web serving, and neither unit needs the other to
13# run: valley-web keeps serving while the receiver restarts.
14#
15# The receiver's token is a file outside the Nix store. systemd reads it
16# and hands it to the receiver's unit alone as a credential, so
17# valley-web's user never reads it. The verifier lines are public, and go
18# in the store.
19{
20 config,
21 pkgs,
22 lib,
23 ...
24}:
25
26let
27 cfg = config.services.valley.webReceiver;
28 stateDir = toString cfg.stateDir;
29 stateName = lib.removePrefix "/var/lib/" stateDir;
30
31 verifiersFile = pkgs.writeText "valley-web-verifiers" (
32 lib.concatMapStrings (line: line + "\n") cfg.verifiers
33 );
34
35 receiveCommand = lib.concatStringsSep " " [
36 (lib.getExe cfg.receiverPackage)
37 "--state ${lib.escapeShellArg stateDir}"
38 "--listen ${lib.escapeShellArg cfg.listen}"
39 "--instance ${lib.escapeShellArg cfg.instance}"
40 "--destination ${lib.escapeShellArg cfg.destination}"
41 "--verifiers ${verifiersFile}"
42 # %d is the unit's credentials directory.
43 "--token-file %d/token"
44 "--max-body ${cfg.maxBody}"
45 "--max-upload ${cfg.maxUpload}"
46 "--max-storage ${cfg.maxStorage}"
47 ];
48
49 webCommand = lib.concatStringsSep " " [
50 (lib.getExe cfg.webPackage)
51 "--state ${lib.escapeShellArg stateDir}"
52 "--listen ${lib.escapeShellArg cfg.webListen}"
53 ];
54
55 # What both units are held to. ProtectSystem=strict leaves the whole
56 # filesystem read-only but the receiver's state directory, which systemd
57 # exempts for the unit that names it, and the temporary paths below.
58 # PrivateTmp gives each unit a /tmp and a /var/tmp of its own, writable,
59 # which no other unit sees and systemd removes when the unit stops.
60 # PrivateDevices gives each unit a /dev of its own, read-only, but binds
61 # the host's /dev/shm into it, which stays writable.
62 confinement = {
63 ProtectSystem = "strict";
64 ProtectHome = true;
65 PrivateTmp = true;
66 PrivateDevices = true;
67 NoNewPrivileges = true;
68 ProtectClock = true;
69 ProtectHostname = true;
70 ProtectKernelLogs = true;
71 ProtectKernelModules = true;
72 ProtectKernelTunables = true;
73 ProtectControlGroups = true;
74 ProtectProc = "invisible";
75 RestrictAddressFamilies = [
76 "AF_INET"
77 "AF_INET6"
78 "AF_UNIX"
79 ];
80 RestrictNamespaces = true;
81 RestrictRealtime = true;
82 RestrictSUIDSGID = true;
83 LockPersonality = true;
84 CapabilityBoundingSet = "";
85 SystemCallArchitectures = "native";
86 SystemCallFilter = [ "@system-service" ];
87 UMask = "0077";
88 MemorySwapMax = "0";
89 Restart = "on-failure";
90 RestartSec = "5s";
91 };
92
93 name = lib.types.strMatching "[a-zA-Z0-9][a-zA-Z0-9._-]{0,63}";
94 size = lib.types.strMatching "[1-9][0-9]*[KMGT]?";
95in
96{
97 options.services.valley.webReceiver = {
98 enable = lib.mkEnableOption ''
99 the valley web export receiver and valley-web on this host: the
100 receiver takes a valley host's signed uploads into
101 {option}`services.valley.webReceiver.stateDir`, and valley-web
102 serves what it activates to anonymous readers
103 '';
104
105 instance = lib.mkOption {
106 type = name;
107 example = "valley-host";
108 description = ''
109 The instance every manifest must name: the publishing valley
110 host's {option}`networking.hostName`, which its publisher names.
111 '';
112 };
113
114 destination = lib.mkOption {
115 type = name;
116 example = "example.org";
117 description = ''
118 This receiver's name, which every manifest must name: the
119 publisher's {option}`services.valley.webPublisher.destination`.
120 '';
121 };
122
123 verifiers = lib.mkOption {
124 type = lib.types.listOf lib.types.str;
125 example = [ "valley-host/web-export+1a2b3c4d+AQ..." ];
126 description = ''
127 The publisher's verifier lines. A manifest must open under one of
128 them. The publisher writes its own to `verifier` in its state
129 directory, and prints it whenever it changes.
130 '';
131 };
132
133 tokenFile = lib.mkOption {
134 type = lib.types.strMatching "/.+";
135 example = "/run/keys/valley-web-receiver-token";
136 description = ''
137 The file that holds the token every request to the receiver must
138 carry: one line of 32 to 256 visible ASCII characters. The
139 publisher is given the same token. The path is outside the Nix
140 store, and systemd reads the file as the receiver's credential, so
141 it may be readable by root alone.
142 '';
143 };
144
145 listen = lib.mkOption {
146 type = lib.types.str;
147 example = "192.0.2.10:8081";
148 description = ''
149 The TCP address the receiver listens on, which the publisher
150 reaches. Every request to it must carry the token.
151 '';
152 };
153
154 webListen = lib.mkOption {
155 type = lib.types.str;
156 example = "127.0.0.1:8080";
157 description = ''
158 The TCP address valley-web listens on, which readers reach, or a
159 reverse proxy in front of it.
160 '';
161 };
162
163 stateDir = lib.mkOption {
164 type = lib.types.strMatching "/var/lib/[^/].*";
165 default = "/var/lib/valley-web";
166 description = ''
167 The state directory, under /var/lib: the receiver's, mode 0750,
168 and readable by valley-web through the valley-web group.
169 '';
170 };
171
172 maxBody = lib.mkOption {
173 type = size;
174 default = "1G";
175 description = ''
176 The most bytes one body of an upload may declare: export.json, or
177 one project's bundle (`--max-body`).
178 '';
179 };
180
181 maxUpload = lib.mkOption {
182 type = size;
183 default = "2G";
184 description = ''
185 The most bytes the bodies of one manifest may declare together
186 (`--max-upload`).
187 '';
188 };
189
190 maxStorage = lib.mkOption {
191 type = size;
192 default = "8G";
193 description = ''
194 The most bytes the state directory may take on disk: every
195 generation it holds, the staged upload's bodies, and a repository
196 for each project of the upload (`--max-storage`).
197 '';
198 };
199
200 receiverMemoryMax = lib.mkOption {
201 type = size;
202 default = "2G";
203 description = ''
204 The most memory the receiver may use, with every git it runs to
205 accept a bundle (`MemoryMax`, with no swap).
206 '';
207 };
208
209 webMemoryMax = lib.mkOption {
210 type = size;
211 default = "1G";
212 description = ''
213 The most memory valley-web may use, with every git it runs to
214 browse a repository (`MemoryMax`, with no swap).
215 '';
216 };
217
218 # The programs as this flake packages them. Internal, like the host
219 # module's packages: module-eval sets them to stand-ins.
220 receiverPackage = lib.mkOption {
221 type = lib.types.package;
222 internal = true;
223 default = (import ./packages.nix { inherit pkgs lib; }).valley-web-receive;
224 defaultText = lib.literalExpression "(import ./packages.nix { inherit pkgs lib; }).valley-web-receive";
225 description = "The web export receiver.";
226 };
227 webPackage = lib.mkOption {
228 type = lib.types.package;
229 internal = true;
230 default = (import ./packages.nix { inherit pkgs lib; }).valley-web;
231 defaultText = lib.literalExpression "(import ./packages.nix { inherit pkgs lib; }).valley-web";
232 description = "The web server.";
233 };
234 };
235
236 config = lib.mkIf cfg.enable {
237 assertions = [
238 {
239 assertion = cfg.verifiers != [ ];
240 message = "services.valley.webReceiver.verifiers names no verifier line, and the receiver admits no manifest without one.";
241 }
242 {
243 assertion = !(lib.hasPrefix builtins.storeDir cfg.tokenFile);
244 message = "services.valley.webReceiver.tokenFile is in the Nix store, which every user can read; give a path outside it.";
245 }
246 ];
247
248 users.groups.valley-web = { };
249 users.users.valley-web-receive = {
250 isSystemUser = true;
251 group = "valley-web";
252 description = "valley web export receiver";
253 };
254 users.users.valley-web = {
255 isSystemUser = true;
256 group = "valley-web";
257 description = "valley-web";
258 };
259
260 systemd.services.valley-web-receive = {
261 description = "The valley web export receiver";
262 wantedBy = [ "multi-user.target" ];
263 after = [ "network.target" ];
264 serviceConfig = confinement // {
265 ExecStart = receiveCommand;
266 User = "valley-web-receive";
267 Group = "valley-web";
268 StateDirectory = stateName;
269 StateDirectoryMode = "0750";
270 WorkingDirectory = stateDir;
271 LoadCredential = [ "token:${cfg.tokenFile}" ];
272 MemoryMax = cfg.receiverMemoryMax;
273 };
274 };
275
276 # valley-web starts after the receiver, which makes the state
277 # directory, and needs nothing else of it. It writes nothing: the
278 # state directory is read-only to it, as is everything else. A state
279 # directory not made yet is skipped (the leading -), and valley-web
280 # fails and is restarted until it is there.
281 systemd.services.valley-web = {
282 description = "valley-web, serving the web export receiver's state directory";
283 wantedBy = [ "multi-user.target" ];
284 after = [
285 "network.target"
286 "valley-web-receive.service"
287 ];
288 serviceConfig = confinement // {
289 ExecStart = webCommand;
290 User = "valley-web";
291 Group = "valley-web";
292 ReadOnlyPaths = [ "-${stateDir}" ];
293 MemoryMax = cfg.webMemoryMax;
294 };
295 };
296 };
297}