blob: 8a5c4bd927660af34ba9ac0c4fcce892cf898357 (
plain) (
blame)
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
|
{
config,
pkgs,
lib,
...
}:
let
cfg = config.services.misskey;
settingsFormat = pkgs.formats.yaml { };
redisType = lib.types.submodule {
freeformType = lib.types.attrsOf settingsFormat.type;
options = {
host = lib.mkOption {
type = lib.types.str;
default = "localhost";
description = "The Redis host.";
};
port = lib.mkOption {
type = lib.types.port;
default = 6379;
description = "The Redis port.";
};
};
};
settings = lib.mkOption {
description = ''
Configuration for Misskey, see
[`example.yml`](https://github.com/misskey-dev/misskey/blob/develop/.config/example.yml)
for all supported options.
'';
type = lib.types.submodule {
freeformType = lib.types.attrsOf settingsFormat.type;
options = {
url = lib.mkOption {
type = lib.types.str;
example = "https://example.tld/";
description = ''
The final user-facing URL. Do not change after running Misskey for the first time.
This needs to match up with the configured reverse proxy and is automatically configured when using `services.misskey.reverseProxy`.
'';
};
port = lib.mkOption {
type = lib.types.port;
default = 3000;
description = "The port your Misskey server should listen on.";
};
socket = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
example = "/path/to/misskey.sock";
description = "The UNIX socket your Misskey server should listen on.";
};
chmodSocket = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
example = "777";
description = "The file access mode of the UNIX socket.";
};
db = lib.mkOption {
description = "Database settings.";
type = lib.types.submodule {
options = {
host = lib.mkOption {
type = lib.types.str;
default = "/var/run/postgresql";
example = "localhost";
description = "The PostgreSQL host.";
};
port = lib.mkOption {
type = lib.types.port;
default = 5432;
description = "The PostgreSQL port.";
};
db = lib.mkOption {
type = lib.types.str;
default = "misskey";
description = "The database name.";
};
user = lib.mkOption {
type = lib.types.str;
default = "misskey";
description = "The user used for database authentication.";
};
pass = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = "The password used for database authentication.";
};
disableCache = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Whether to disable caching queries.";
};
extra = lib.mkOption {
type = lib.types.nullOr (lib.types.attrsOf settingsFormat.type);
default = null;
example = {
ssl = true;
};
description = "Extra connection options.";
};
};
};
default = { };
};
redis = lib.mkOption {
type = redisType;
default = { };
description = "`ioredis` options. See [`README`](https://github.com/redis/ioredis?tab=readme-ov-file#connect-to-redis) for reference.";
};
redisForPubsub = lib.mkOption {
type = lib.types.nullOr redisType;
default = null;
description = "`ioredis` options for pubsub. See [`README`](https://github.com/redis/ioredis?tab=readme-ov-file#connect-to-redis) for reference.";
};
redisForJobQueue = lib.mkOption {
type = lib.types.nullOr redisType;
default = null;
description = "`ioredis` options for the job queue. See [`README`](https://github.com/redis/ioredis?tab=readme-ov-file#connect-to-redis) for reference.";
};
redisForTimelines = lib.mkOption {
type = lib.types.nullOr redisType;
default = null;
description = "`ioredis` options for timelines. See [`README`](https://github.com/redis/ioredis?tab=readme-ov-file#connect-to-redis) for reference.";
};
meilisearch = lib.mkOption {
description = "Meilisearch connection options.";
type = lib.types.nullOr (
lib.types.submodule {
options = {
host = lib.mkOption {
type = lib.types.str;
default = "localhost";
description = "The Meilisearch host.";
};
port = lib.mkOption {
type = lib.types.port;
default = 7700;
description = "The Meilisearch port.";
};
apiKey = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = "The Meilisearch API key.";
};
ssl = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Whether to connect via SSL.";
};
index = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = "Meilisearch index to use.";
};
scope = lib.mkOption {
type = lib.types.enum [
"local"
"global"
];
default = "local";
description = "The search scope.";
};
};
}
);
default = null;
};
id = lib.mkOption {
type = lib.types.enum [
"aid"
"aidx"
"meid"
"ulid"
"objectid"
];
default = "aidx";
description = "The ID generation method to use. Do not change after starting Misskey for the first time.";
};
};
};
};
in
{
options = {
services.misskey = {
enable = lib.mkEnableOption "misskey";
package = lib.mkPackageOption pkgs "misskey" { };
inherit settings;
database = {
createLocally = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Create the PostgreSQL database locally. Sets `services.misskey.settings.db.{db,host,port,user,pass}`.";
};
passwordFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = "The path to a file containing the database password. Sets `services.misskey.settings.db.pass`.";
};
};
redis = {
createLocally = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Create and use a local Redis instance. Sets `services.misskey.settings.redis.host`.";
};
passwordFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = "The path to a file containing the Redis password. Sets `services.misskey.settings.redis.pass`.";
};
};
meilisearch = {
createLocally = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Create and use a local Meilisearch instance. Sets `services.misskey.settings.meilisearch.{host,port,ssl}`.";
};
keyFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = "The path to a file containing the Meilisearch API key. Sets `services.misskey.settings.meilisearch.apiKey`.";
};
};
reverseProxy = {
enable = lib.mkEnableOption "a HTTP reverse proxy for Misskey";
webserver = lib.mkOption {
type = lib.types.attrTag {
nginx = lib.mkOption {
type = lib.types.submodule (import ../web-servers/nginx/vhost-options.nix);
default = { };
description = ''
Extra configuration for the nginx virtual host of Misskey.
Set to `{ }` to use the default configuration.
'';
};
caddy = lib.mkOption {
type = lib.types.submodule (
import ../web-servers/caddy/vhost-options.nix { cfg = config.services.caddy; }
);
default = { };
description = ''
Extra configuration for the caddy virtual host of Misskey.
Set to `{ }` to use the default configuration.
'';
};
};
description = "The webserver to use as the reverse proxy.";
};
host = lib.mkOption {
type = lib.types.nullOr lib.types.str;
description = ''
The fully qualified domain name to bind to. Sets `services.misskey.settings.url`.
This is required when using `services.misskey.reverseProxy.enable = true`.
'';
example = "misskey.example.com";
default = null;
};
ssl = lib.mkOption {
type = lib.types.nullOr lib.types.bool;
description = ''
Whether to enable SSL for the reverse proxy. Sets `services.misskey.settings.url`.
This is required when using `services.misskey.reverseProxy.enable = true`.
'';
example = true;
default = null;
};
};
};
};
config = lib.mkIf cfg.enable {
assertions = [
{
assertion =
cfg.reverseProxy.enable -> ((cfg.reverseProxy.host != null) && (cfg.reverseProxy.ssl != null));
message = "`services.misskey.reverseProxy.enable` requires `services.misskey.reverseProxy.host` and `services.misskey.reverseProxy.ssl` to be set.";
}
];
services.misskey.settings = lib.mkMerge [
(lib.mkIf cfg.database.createLocally {
db = {
db = lib.mkDefault "misskey";
# Use unix socket instead of localhost to allow PostgreSQL peer authentication,
# required for `services.postgresql.ensureUsers`
host = lib.mkDefault "/var/run/postgresql";
port = lib.mkDefault config.services.postgresql.settings.port;
user = lib.mkDefault "misskey";
pass = lib.mkDefault null;
};
})
(lib.mkIf (cfg.database.passwordFile != null) { db.pass = lib.mkDefault "@DATABASE_PASSWORD@"; })
(lib.mkIf cfg.redis.createLocally { redis.host = lib.mkDefault "localhost"; })
(lib.mkIf (cfg.redis.passwordFile != null) { redis.pass = lib.mkDefault "@REDIS_PASSWORD@"; })
(lib.mkIf cfg.meilisearch.createLocally {
meilisearch = {
host = lib.mkDefault "localhost";
port = lib.mkDefault config.services.meilisearch.listenPort;
ssl = lib.mkDefault false;
};
})
(lib.mkIf (cfg.meilisearch.keyFile != null) {
meilisearch.apiKey = lib.mkDefault "@MEILISEARCH_KEY@";
})
(lib.mkIf cfg.reverseProxy.enable {
url = lib.mkDefault "${
if cfg.reverseProxy.ssl then "https" else "http"
}://${cfg.reverseProxy.host}";
})
];
systemd.services.misskey = {
after = [
"network-online.target"
"postgresql.service"
];
wants = [ "network-online.target" ];
wantedBy = [ "multi-user.target" ];
environment = {
MISSKEY_CONFIG_YML = "/run/misskey/default.yml";
};
preStart =
''
install -m 700 ${settingsFormat.generate "misskey-config.yml" cfg.settings} /run/misskey/default.yml
''
+ (lib.optionalString (cfg.database.passwordFile != null) ''
${pkgs.replace-secret}/bin/replace-secret '@DATABASE_PASSWORD@' "${cfg.database.passwordFile}" /run/misskey/default.yml
'')
+ (lib.optionalString (cfg.redis.passwordFile != null) ''
${pkgs.replace-secret}/bin/replace-secret '@REDIS_PASSWORD@' "${cfg.redis.passwordFile}" /run/misskey/default.yml
'')
+ (lib.optionalString (cfg.meilisearch.keyFile != null) ''
${pkgs.replace-secret}/bin/replace-secret '@MEILISEARCH_KEY@' "${cfg.meilisearch.keyFile}" /run/misskey/default.yml
'');
serviceConfig = {
ExecStart = "${cfg.package}/bin/misskey migrateandstart";
RuntimeDirectory = "misskey";
RuntimeDirectoryMode = "700";
StateDirectory = "misskey";
StateDirectoryMode = "700";
TimeoutSec = 60;
DynamicUser = true;
User = "misskey";
LockPersonality = true;
PrivateDevices = true;
PrivateUsers = true;
ProtectClock = true;
ProtectControlGroups = true;
ProtectHome = true;
ProtectHostname = true;
ProtectKernelLogs = true;
ProtectProc = "invisible";
ProtectKernelModules = true;
ProtectKernelTunables = true;
RestrictAddressFamilies = "AF_INET AF_INET6 AF_UNIX AF_NETLINK";
};
};
services.postgresql = lib.mkIf cfg.database.createLocally {
enable = true;
ensureDatabases = [ "misskey" ];
ensureUsers = [
{
name = "misskey";
ensureDBOwnership = true;
}
];
};
services.redis.servers = lib.mkIf cfg.redis.createLocally {
misskey = {
enable = true;
port = cfg.settings.redis.port;
};
};
services.meilisearch = lib.mkIf cfg.meilisearch.createLocally { enable = true; };
services.caddy = lib.mkIf (cfg.reverseProxy.enable && cfg.reverseProxy.webserver ? caddy) {
enable = true;
virtualHosts.${cfg.settings.url} = lib.mkMerge [
cfg.reverseProxy.webserver.caddy
{
hostName = lib.mkDefault cfg.settings.url;
extraConfig = ''
reverse_proxy localhost:${toString cfg.settings.port}
'';
}
];
};
services.nginx = lib.mkIf (cfg.reverseProxy.enable && cfg.reverseProxy.webserver ? nginx) {
enable = true;
virtualHosts.${cfg.reverseProxy.host} = lib.mkMerge [
cfg.reverseProxy.webserver.nginx
{
locations."/" = {
proxyPass = lib.mkDefault "http://localhost:${toString cfg.settings.port}";
proxyWebsockets = lib.mkDefault true;
recommendedProxySettings = lib.mkDefault true;
};
}
(lib.mkIf (cfg.reverseProxy.ssl != null) { forceSSL = lib.mkDefault cfg.reverseProxy.ssl; })
];
};
};
meta = {
maintainers = [ lib.maintainers.feathecutie ];
};
}
|