1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
# enstate on NixOS (docs/design.md, 5.3): the schema from options, the receptor run at
# boot and after each switch, and the check before a switch.
#
# enstate.enable = true;
# enstate.channels.secrets.slots."retiolum.key" = {
# type = "application/x-openssh-private-key; algorithm=ed25519";
# secret = true;
# reactions.restart = [ "tincr-retiolum.service" ]; # unless it restarts itself
# };
# enstate.channels.fleet.slots.hosts.type = "application/x-enstate-hosts+json";
# enstate.views.ssh-known-hosts = {
# from = [ "fleet/hosts" ];
# renderer = ./render-known-hosts;
# };
#
# systemd.services.tincr-retiolum.serviceConfig.LoadCredential =
# "retiolum.key:${config.enstate.channels.secrets.slots."retiolum.key".path}";
#
# Every slot is its own view, `CHANNEL.SLOT`, unless it says `view = false`; its file
# is the slot's `path`, `/run/enstate/views/CHANNEL.SLOT/SLOT`. Services read views
# as they like; one whose `LoadCredential=` reads a view is ordered after the unit that
# puts the views in place, by itself.
{
config,
lib,
pkgs,
...
}:
let
inherit (lib) mkOption types;
cfg = config.enstate;
reactions = {
restart = mkOption {
type = types.listOf types.str;
default = [ ];
description = "Units to restart when the view changes (`foo.service`).";
};
reload = mkOption {
type = types.listOf types.str;
default = [ ];
description = "Units to reload (or restart, if they can't reload) when the view changes.";
};
};
slot =
channel:
types.submodule (
{ name, config, ... }:
{
options = {
type = mkOption {
type = types.str;
description = "What the slot holds, as a media type.";
example = "application/x-openssh-private-key; algorithm=ed25519";
};
required = mkOption {
type = types.bool;
default = true;
description = "Whether a submission, or a switch, that leaves the slot unfilled is refused.";
};
secret = mkOption {
type = types.bool;
default = false;
description = "Whether the slot's views are readable by root only.";
};
renamedFrom = mkOption {
type = types.nullOr types.str;
default = null;
description = "The slot's name in an earlier generation, so its entry carries over, both ways.";
};
view = mkOption {
type = types.bool;
default = true;
description = "Whether the slot is its own view, `CHANNEL.SLOT`.";
};
path = mkOption {
type = types.nullOr types.str;
readOnly = true;
default = if config.view then "${cfg.runDir}/views/${channel}.${name}/${name}" else null;
defaultText = lib.literalExpression ''"''${config.enstate.runDir}/views/CHANNEL.SLOT/SLOT"'';
description = "Where the slot's own view puts its entry, if it has one, for services to read.";
};
inherit reactions;
};
}
);
# The views of slots that are their own.
slotViews = lib.concatMapAttrs (
channel: channelCfg:
lib.concatMapAttrs (
name: slotCfg:
lib.optionalAttrs slotCfg.view {
"${channel}.${name}" = {
from = [ "${channel}/${name}" ];
inherit (slotCfg) reactions;
};
}
) channelCfg.slots
) cfg.channels;
nonNull = lib.filterAttrs (_: value: value != null);
schema = {
schema = 1;
types = lib.mapAttrs (
_: typeCfg: nonNull { validator = lib.mapNullable toString typeCfg.validator; }
) cfg.types;
channels = lib.mapAttrs (_: channelCfg: {
slots = lib.mapAttrs (
_: slotCfg:
nonNull {
inherit (slotCfg)
type
required
secret
renamedFrom
;
}
) channelCfg.slots;
}) cfg.channels;
validators = lib.mapAttrs (_: validatorCfg: {
inherit (validatorCfg) slots;
program = toString validatorCfg.program;
}) cfg.validators;
views =
slotViews
// lib.mapAttrs (
_: viewCfg:
nonNull {
inherit (viewCfg) from secret;
renderer = lib.mapNullable toString viewCfg.renderer;
reactions = { inherit (viewCfg.reactions) restart reload; };
}
) cfg.views;
};
schemaFile = pkgs.writers.writeJSON "enstate-schema.json" schema;
settings = pkgs.writers.writeJSON "enstate-config.json" {
state-dir = cfg.stateDir;
run-dir = cfg.runDir;
# Started again by --apply if it failed (for want of the state, at boot).
unit = "enstate.service";
# Validators and renderers run in it.
sandbox = "${pkgs.bubblewrap}/bin/bwrap";
};
enstate = lib.getExe cfg.package;
# Whether a `LoadCredential=` (`ID:PATH`) reads a view.
readsView =
credential:
let
parts = lib.splitString ":" credential;
in
lib.length parts > 1
&& lib.hasPrefix "${cfg.runDir}/views/" (lib.concatStringsSep ":" (lib.tail parts));
in
{
options.enstate = {
enable = lib.mkEnableOption "enstate, state next to the Nix store";
package = mkOption {
type = types.package;
default = pkgs.callPackage ./package.nix { };
defaultText = lib.literalExpression "pkgs.callPackage ./package.nix { }";
description = "enstate, the program.";
};
stateDir = mkOption {
type = types.str;
default = "/var/lib/enstate";
description = "Where the snapshots are kept.";
};
runDir = mkOption {
type = types.str;
default = "/run/enstate";
description = "Where the views are put.";
};
types = mkOption {
type = types.attrsOf (
types.submodule {
options.validator = mkOption {
type = types.nullOr types.path;
default = null;
description = "The type's validator (docs/design.md, 5.2).";
};
}
);
default = { };
description = "Types, by media type, and their validators.";
};
channels = mkOption {
type = types.attrsOf (
types.submodule (
{ name, ... }:
{
options.slots = mkOption {
type = types.attrsOf (slot name);
default = { };
description = "The channel's slots.";
};
}
)
);
default = { };
description = "Channels, and the slots this generation offers in them.";
};
validators = mkOption {
type = types.attrsOf (
types.submodule {
options = {
slots = mkOption {
type = types.listOf types.str;
description = "The slots it is given (`CHANNEL/SLOT`).";
};
program = mkOption {
type = types.path;
description = "The validator (docs/design.md, 5.2).";
};
};
}
);
default = { };
description = "Validators across slots.";
};
views = mkOption {
type = types.attrsOf (
types.submodule (
{ name, ... }:
{
options = {
from = mkOption {
type = types.listOf types.str;
description = "The slots it is made from (`CHANNEL/SLOT`).";
};
renderer = mkOption {
type = types.nullOr types.path;
default = null;
description = "The renderer (docs/design.md, 5.2); none for a view of one entry.";
};
secret = mkOption {
type = types.nullOr types.bool;
default = null;
description = "Whether it is readable by root only; by default, if a slot it is made from is secret.";
};
path = mkOption {
type = types.str;
readOnly = true;
default = "${cfg.runDir}/views/${name}";
defaultText = lib.literalExpression ''"''${config.enstate.runDir}/views/NAME"'';
description = "The directory the view's files are in, for services to read.";
};
inherit reactions;
};
}
)
);
default = { };
description = "Views made by renderers.";
};
incomingDir = mkOption {
type = types.str;
readOnly = true;
default = "${cfg.stateDir}/incoming";
defaultText = lib.literalExpression ''"''${config.enstate.stateDir}/incoming"'';
description = ''
Where submissions wait for the next switch, each a directory of its own, taken
in the order of their names: planned against the new generation's schema
before switching, which a refused one refuses, and applied by the switch.
'';
};
checkBeforeSwitch = mkOption {
type = types.bool;
default = true;
description = ''
Whether a switch to a generation whose required slots the state can't fill,
even by migrating and with the submissions waiting in `incomingDir`, or for
which a waiting submission is refused, is refused before switching. Not
checked for `boot` and `dry-activate`.
'';
};
};
# A service whose `LoadCredential=` reads a view is ordered after the unit that puts
# the views in place, and wants it: without saying so itself.
options.systemd.services = mkOption {
type = types.attrsOf (
types.submodule (
{ config, ... }:
let
credentials = lib.concatMap (option: lib.toList (config.serviceConfig.${option} or [ ])) [
"LoadCredential"
"LoadCredentialEncrypted"
];
in
{
config = lib.mkIf (cfg.enable && lib.any readsView credentials) {
after = [ "enstate.service" ];
wants = [ "enstate.service" ];
};
}
)
);
};
config = lib.mkIf cfg.enable {
assertions = [
{
assertion = lib.intersectLists (lib.attrNames slotViews) (lib.attrNames cfg.views) == [ ];
message = "enstate.views: ${toString (lib.intersectLists (lib.attrNames slotViews) (lib.attrNames cfg.views))} is also a slot's own view";
}
];
environment.systemPackages = [ cfg.package ];
environment.etc."enstate/schema.json".source = schemaFile;
environment.etc."enstate/config.json".source = settings;
# Root's, whatever creates it first (a deploy copying into incomingDir, say).
systemd.tmpfiles.rules = [ "d ${cfg.stateDir} 0700 root root -" ];
# At boot, and after a switch that changes the schema: the views of this
# generation, from the snapshots, and the reactions of those that changed.
systemd.services.enstate = {
description = "enstate: the views of this generation";
wantedBy = [ "multi-user.target" ];
after = [ "local-fs.target" ];
restartTriggers = [ schemaFile ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = "${enstate} --migrate";
};
};
# After a switch whose schema didn't change, with a submission waiting in
# incomingDir or staged (--stage leaves a marker, which --migrate removes):
# switch-to-configuration restarts sysinit-reactivation.target on every switch,
# which starts this, and the conditions keep it from running otherwise. After a
# switch whose schema changed, enstate.service has taken them already.
systemd.services.enstate-switch = {
description = "enstate: what waits for the switch";
wantedBy = [ "sysinit-reactivation.target" ];
after = [ "local-fs.target" ];
before = [
"sysinit-reactivation.target"
"shutdown.target"
];
conflicts = [ "shutdown.target" ];
unitConfig = {
DefaultDependencies = false;
# Either ("|").
ConditionDirectoryNotEmpty = "|${cfg.incomingDir}";
ConditionPathExists = "|${cfg.stateDir}/migrate-due";
};
serviceConfig = {
Type = "oneshot";
ExecStart = "${enstate} --migrate";
};
};
# With the new generation's enstate, schema and configuration; on a host without
# enstate yet, too: its first switch needs its state, which can wait in
# incomingDir.
system.preSwitchChecks = lib.mkIf cfg.checkBeforeSwitch {
enstate = ''
case "''${2-}" in
boot|dry-activate) exit 0 ;;
esac
${enstate} --migrate --dry-run --schema "$1/etc/enstate/schema.json" --config "$1/etc/enstate/config.json"
'';
};
};
}
|