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
|
{ config, lib, mylib, pkgs, ... }: let
cfg = config.hrm.continuum;
format = pkgs.formats.toml {};
# Where an instance keeps the files it writes, unless its settings say so.
# Only those that apply: continuum refuses the others.
stateFiles = settings: let
dir = "\${env:STATE_DIRECTORY}";
in
lib.optionalAttrs (!(settings.relay-only or false)) {
state-file = "${dir}/state.json";
document-file = "${dir}/document.automerge";
}
// lib.optionalAttrs (settings ? store-id || settings ? store-root) {
store-file = "${dir}/store.json";
}
// lib.optionalAttrs (settings ? store-vote-key) {
store-vote-record = "${dir}/party-record.json";
}
// lib.optionalAttrs (settings.tor.enable or false) {
tor.state-dir = "${dir}/tor";
};
# The socket of an instance with a domain, where nginx reaches its HTTP API.
socket = name: "/run/continuum-${name}/api.sock";
# The settings as written: their own, the defaults under them, and the
# socket for nginx added to bind-addr.
settingsOf = name: instance:
lib.recursiveUpdate (stateFiles instance.settings) instance.settings
// lib.optionalAttrs (instance.domain != null) {
bind-addr =
lib.toList (instance.settings.bind-addr or []) ++ [ "unix:${socket name}" ];
};
withDomain = lib.filterAttrs (_: instance: instance.domain != null) cfg.instances;
in {
options.hrm.continuum.instances = lib.mkOption {
default = {};
description = ''
continuum instances. Each is a service continuum-<name> with the state
directory /var/lib/continuum-<name>.
'';
type = lib.types.attrsOf (lib.types.submodule {
options.credentials = lib.mkOption {
default = {};
description = ''
Secret files by name, like an htpasswd file or a key. The instance
alone can read each as ''${env:CREDENTIALS_DIRECTORY}/<name>, which
is how settings refer to them. It restarts when one changes.
'';
example = { htpasswd = "/var/src/secrets/continuum.htpasswd"; };
type = lib.types.attrsOf mylib.types.absolute-pathname;
};
options.domain = lib.mkOption {
default = null;
description = ''
A name for an nginx virtual host passing requests on to the
instance's HTTP API, over a Unix socket only nginx and the instance
can use. The instance checks who users are itself (htpasswd), so
requests pass on as they come.
'';
example = "kartei.continuum.ne.r";
type = lib.types.nullOr lib.types.str;
};
options.package = lib.mkPackageOption pkgs "continuum" {};
options.user = lib.mkOption {
default = null;
description = ''
The user the instance runs as. By default one of its own, which
exists only while it runs (DynamicUser).
'';
type = lib.types.nullOr lib.types.str;
};
options.settings = lib.mkOption {
default = {};
description = ''
The instance's configuration: continuum's options by their long
names, see docs/configuration.md and docs/example.toml in continuum.
Text may hold placeholders like ''${env:STATE_DIRECTORY}, which
continuum fills in when it starts. In a Nix string, escape them:
"\''${env:STATE_DIRECTORY}/node.key". Unescaped, Nix takes
env:STATE_DIRECTORY for a URL and drops the braces.
The files continuum writes default to the state directory:
state-file, document-file, and where they apply store-file,
store-vote-record and tor.state-dir. Keys have no default.
'';
type = lib.types.submodule {
freeformType = format.type;
};
};
});
};
config.hrm.systemd.services = lib.mapAttrs' (name: instance:
lib.nameValuePair "continuum-${name}" {
restartIfCredentialsChange = true;
}
) (lib.filterAttrs (_: instance: instance.credentials != {}) cfg.instances);
config.systemd.services = lib.mapAttrs' (name: instance:
lib.nameValuePair "continuum-${name}" {
description = "continuum ${name}";
wantedBy = [ "multi-user.target" ];
wants = [ "network-online.target" ];
after = [ "network-online.target" ];
serviceConfig = {
ExecStart = "${instance.package}/bin/continuum --config ${
format.generate "continuum-${name}.toml" (settingsOf name instance)
}";
# continuum exits with status 0 when a transport stops, too.
Restart = "always";
RestartSec = "5s";
DynamicUser = instance.user == null;
User = lib.mkIf (instance.user != null) instance.user;
StateDirectory = "continuum-${name}";
StateDirectoryMode = "0700";
# The socket's directory and group let nginx through.
RuntimeDirectory = lib.mkIf (instance.domain != null) "continuum-${name}";
RuntimeDirectoryMode = lib.mkIf (instance.domain != null) "0750";
Group = lib.mkIf (instance.domain != null) "continuum-${name}";
LoadCredential =
lib.mapAttrsToList (name: path: "${name}:${path}") instance.credentials;
# It only needs the network and its state directory.
ProtectSystem = "strict";
ProtectHome = true;
PrivateTmp = true;
PrivateDevices = true;
NoNewPrivileges = true;
RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_UNIX" "AF_NETLINK" ];
CapabilityBoundingSet = "";
SystemCallFilter = [ "@system-service" ];
};
}
) cfg.instances;
config.users.groups = lib.mapAttrs' (name: _:
lib.nameValuePair "continuum-${name}" {}
) withDomain;
config.users.users = lib.mkIf (withDomain != {}) {
${config.services.nginx.user}.extraGroups =
lib.mapAttrsToList (name: _: "continuum-${name}") withDomain;
};
config.services.nginx = lib.mkIf (withDomain != {}) {
enable = true;
virtualHosts = lib.mapAttrs' (name: instance:
lib.nameValuePair instance.domain {
locations."/" = {
proxyPass = "http://unix:${socket name}";
extraConfig = ''
proxy_set_header X-Forwarded-Proto $scheme;
'';
};
}
) withDomain;
};
}
|