gunk-dev/the-valley

web-receiver.nix

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