blob: ef6f0a5ea9ca78a43de6f9e917473408abb9b4b4 (
plain)
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
|
{
config,
lib,
pkgs,
...
}:
let
inherit (lib)
mkEnableOption
mkPackageOption
mkOption
mkIf
mkDefault
mkMerge
types
;
cfg = config.services.sstorytime;
dbName = "sstoryline";
configPath = "/etc/sstorytime";
stateDir = "/var/lib/sstorytime";
defaultDatabaseUri = "postgresql://${dbName}@/${dbName}?host=/run/postgresql";
commonServiceConfig = {
User = dbName;
Group = dbName;
StateDirectory = "sstorytime";
WorkingDirectory = "%S/sstorytime";
EnvironmentFile = lib.optional (cfg.environmentFile != null) cfg.environmentFile;
};
# linkding-manage / lasuite-*-manage: same user/state/env as the long-running unit.
systemdRunArgs = lib.escapeShellArgs (
[
"--quiet"
"--collect"
"--pipe"
"--wait"
"--service-type=exec"
"--uid=${commonServiceConfig.User}"
"--gid=${commonServiceConfig.Group}"
"--working-directory=${stateDir}"
"--property=StateDirectory=${commonServiceConfig.StateDirectory}"
]
++ lib.optional (cfg.environmentFile != null) "--property=EnvironmentFile=${cfg.environmentFile}"
++ lib.mapAttrsToList (name: value: "--setenv=${name}=${value}") (
cfg.settings
// {
PATH = lib.makeBinPath [ cfg.package ];
}
)
++ [ "--" ]
);
sstorytime-run = pkgs.writeShellApplication {
name = "sstorytime-run";
text = ''
exec ${lib.getExe' config.systemd.package "systemd-run"} \
${systemdRunArgs} \
"$@"
'';
};
in
{
meta.maintainers = lib.teams.ngi.members;
options.services.sstorytime = {
enable = mkEnableOption "SSTorytime, a unified graph process for mapping knowledge";
package = mkPackageOption pkgs "sstorytime" { };
openFirewall = mkEnableOption "opening the SSTorytime ports in the firewall";
httpPort = mkOption {
type = types.port;
default = 8080;
description = "HTTP listen port (redirects to HTTPS).";
};
httpsPort = mkOption {
type = types.port;
default = 8443;
description = "HTTPS listen port for the web UI and API.";
};
configDir = mkOption {
type = types.path;
default = "${cfg.package}/share/SSTconfig";
defaultText = lib.literalExpression ''"''${config.services.sstorytime.package}/share/SSTconfig"'';
description = ''
Directory that contains configuration files to be installed under
{file}`${configPath}`.
'';
};
settings = mkOption {
type = types.submodule {
freeformType = types.attrsOf (types.nullOr types.str);
options = {
POSTGRESQL_URI = mkOption {
type = types.nullOr types.str;
default = null;
example = "postgresql://sstoryline@/sstoryline?host=/run/postgresql";
description = ''
PostgreSQL connection URI. When
{option}`database.createLocally` is true, defaults to a peer-auth
socket URI. For remote databases prefer
{option}`environmentFile` if the URI contains secrets.
'';
};
SST_CONFIG_PATH = mkOption {
type = types.nullOr types.str;
default = null;
example = configPath;
description = ''
Directory of SSTconfig ontology files (arrows, closures, …).
Defaults to {file}`${configPath}` when the service is enabled.
'';
};
};
};
default = { };
apply = lib.filterAttrs (_: v: v != null);
example = {
POSTGRESQL_URI = "postgresql://sstoryline@/sstoryline?host=/run/postgresql";
};
description = ''
Environment for the service and {command}`sstorytime-run`.
Null values are omitted. Extra freeform keys are passed through as
additional environment variables.
'';
};
environmentFile = mkOption {
type = types.nullOr types.path;
default = null;
example = "/run/secrets/sstorytime.env";
description = ''
Optional EnvironmentFile (e.g. secrets for `POSTGRESQL_URI` via sops).
Used by the service and by {command}`sstorytime-run`.
'';
};
database = {
createLocally = mkOption {
type = types.bool;
default = true;
description = ''
Local PostgreSQL role/database `sstoryline` and default
{option}`settings.POSTGRESQL_URI` using the Unix socket at
{file}`/run/postgresql` (peer auth as user `sstoryline`).
'';
};
};
finalPackage = mkOption {
type = types.package;
visible = false;
readOnly = true;
default = sstorytime-run;
defaultText = lib.literalExpression "sstorytime-run (systemd-run wrapper)";
description = ''
Runs tools in a transient unit with the same user, state directory,
and environment as {option}`systemd.services.sstorytime`.
Example: {command}`sstorytime-run N4L -wipe -u notes.n4l`.
'';
};
};
config = mkIf cfg.enable {
assertions = [
{
assertion =
cfg.database.createLocally || cfg.settings ? POSTGRESQL_URI || cfg.environmentFile != null;
message = ''
services.sstorytime: when database.createLocally is false, set
settings.POSTGRESQL_URI or environmentFile (e.g. sops) so the service
can reach PostgreSQL.
'';
}
];
# `/*` makes etc a directory of links so nested entries can extend it.
environment.etc.sstorytime.source = "${cfg.configDir}/*";
environment.systemPackages = [
cfg.package
cfg.finalPackage
];
users.users.${dbName} = {
isSystemUser = true;
group = dbName;
description = "SSTorytime service and database user";
};
users.groups.${dbName} = { };
services.sstorytime.settings = mkMerge [
{
SST_CONFIG_PATH = mkDefault configPath;
}
(mkIf cfg.database.createLocally {
POSTGRESQL_URI = mkDefault defaultDatabaseUri;
})
];
systemd.services.sstorytime = {
description = "SSTorytime Server";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ] ++ lib.optionals cfg.database.createLocally [ "postgresql.target" ];
path = [ pkgs.openssl ];
environment = cfg.settings;
unitConfig = {
StartLimitBurst = 5;
StartLimitIntervalSec = 100;
};
preStart = ''
mkdir -p cacheroot
if [ ! -f cert.pem ] || [ ! -f key.pem ]; then
openssl req -x509 -newkey rsa:4096 \
-keyout key.pem -out cert.pem \
-days 365 -nodes \
-subj "/CN=localhost"
fi
'';
serviceConfig = commonServiceConfig // {
Restart = "on-failure";
RestartSec = 5;
ExecStart = lib.escapeShellArgs [
(lib.getExe' cfg.package "http_server")
"-http"
":${toString cfg.httpPort}"
"-https"
":${toString cfg.httpsPort}"
"-cert"
"cert.pem"
"-key"
"key.pem"
];
# MemoryDenyWriteExecute breaks Go.
CapabilityBoundingSet = "";
LockPersonality = true;
NoNewPrivileges = true;
PrivateDevices = true;
PrivateTmp = true;
ProtectClock = true;
ProtectControlGroups = true;
ProtectHome = true;
ProtectHostname = true;
ProtectKernelLogs = true;
ProtectKernelModules = true;
ProtectKernelTunables = true;
ProtectSystem = "strict";
RestrictAddressFamilies = [
"AF_INET"
"AF_INET6"
"AF_UNIX"
];
RestrictNamespaces = true;
RestrictRealtime = true;
SystemCallArchitectures = "native";
};
};
networking.firewall.allowedTCPPorts = lib.optionals cfg.openFirewall [
cfg.httpPort
cfg.httpsPort
];
services.postgresql = mkIf cfg.database.createLocally {
enable = true;
ensureUsers = [
{
name = dbName;
ensureDBOwnership = true;
}
];
ensureDatabases = [ dbName ];
};
};
}
|