-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.nix
399 lines (359 loc) · 11.7 KB
/
module.nix
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
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.feed-reader;
feed-reader = cfg.package;
migrationScript = pkgs.writeShellScript "feed-reader-migrate" ''
if ${feed-reader.env}/bin/bundle exec rails db:migrate:status 2>&1 | grep "Schema migrations table does not exist yet"
then
${feed-reader.env}/bin/bundle exec rails db:schema:load
else
${feed-reader.env}/bin/bundle exec rails db:migrate
fi
'';
env = {
# General
RAILS_ENV = "production";
RACK_ENV = "production";
RAILS_LOG_TO_STDOUT = "yes";
# RUBY_YJIT_ENABLE = "1";
DATABASE_URL = "postgres://%2Frun%2Fpostgresql/feed_reader";
DATABASE_NAME = "feed_reader";
BOOTSNAP_READONLY = "TRUE";
HOSTNAME = cfg.hostname;
# Puma
PIDFILE = "/run/feed_reader/server.pid";
STATEPATH = "/run/feed_reader/server.state";
SOCKETFILE = "unix:///run/feed_reader/server.sock";
WEB_CONCURRENCY = toString cfg.pumaWorkers;
# Active storage
RAILS_STORAGE_PATH = "${cfg.home}/storage";
# Action mailer
RAILS_MAILER_DEFAULT_FROM = cfg.mailer.defaultFrom;
RAILS_SMTP_ADDRESS = cfg.mailer.smtpAddress;
RAILS_SMTP_DOMAIN = cfg.mailer.smtpDomain;
RAILS_SMTP_USER_NAME = cfg.mailer.smtpUserName;
# Action mailbox
RAILS_INBOUND_EMAIL_DOMAIN = cfg.mailer.inboundDomain;
# Sentry
SENTRY_DSN = cfg.sentry.DSN;
SENTRY_TRACES_SAMPLE_RATE = lib.strings.floatToString cfg.sentry.tracesSampleRate;
};
exports = lib.concatStringsSep "\n"
(lib.mapAttrsToList (name: value: ''export ${name}="${value}"'') env);
console = pkgs.writeShellScriptBin "feed-reader-interactive" ''
${exports}
export $(cat ${cfg.environmentFile} | xargs)
cd ${feed-reader}
${feed-reader.env}/bin/bundle exec rails "$@"
'';
relayMailScript = pkgs.writeShellScript "feed-reader-mail-relay" ''
${exports}
export $(${pkgs.coreutils}/bin/cat ${cfg.environmentFile} | ${pkgs.findutils}/bin/xargs)
cd ${feed-reader}
${feed-reader.env}/bin/bundle exec rails action_mailbox:ingress:postfix URL='https://${cfg.hostname}/rails/action_mailbox/relay/inbound_emails' INGRESS_PASSWORD=$RAILS_INBOUND_EMAIL_PASSWORD
'';
in
{
options.services.feed-reader = {
enable = mkEnableOption "feed-reader";
package = mkOption {
description = ''
Feed reader package to use
'';
default = pkgs.feed-reader;
defaultText = "pkgs.feed-reader";
type = types.package;
};
home = mkOption {
description = "The directory where picture it will run.";
default = "/var/lib/feed_reader";
type = types.path;
};
hostname = mkOption {
description = ''
The virtual hostname on which nginx will host the application.
'';
example = "reader.example.com";
type = types.str;
};
environmentFile = mkOption {
description = ''
Path to a file or a list of filepath containing secret environment variables that
should be passed to feed reader.
Currently this has to contain the `MASTER_KEY` and `SECRET_KEY_BASE`
environment variables, which can be generated using rails secret.
You should also include `RAILS_SMTP_PASSWORD` for the mailer to work.
'';
example = "/run/secrets/feed_reader";
type = types.either types.str (types.listOf types.str);
};
backgroundWorkers = mkOption {
description = ''
A list of workers, with the queues they should use.
Each element in the list will spawn a worker with `QUEUES` set to the listitem.
'';
default = [ "*" ];
example = [ "default" "default" ];
type = types.listOf types.str;
};
pumaWorkers = mkOption {
description = ''
Specifiy the amount of workers that puma should use.
If the value is 0, puma will be booted in non-clustered mode.
'';
default = 0;
example = 2;
type = types.int;
};
backupLocation = mkOption {
description = ''
Location on filesystem where postgres backup can be created.
'';
example = "/mount/backups/smartmonitor";
type = types.str;
default = "";
};
mailer = {
defaultFrom = mkOption {
description = ''
Email address form which all mails will be sent.
'';
example = "[email protected]";
type = types.str;
};
smtpAddress = mkOption {
description = ''
Address of webserver from which mails will be sent.
'';
example = "[email protected]";
default = "localhost";
type = types.str;
};
smtpDomain = mkOption {
description = ''
Specify the HELO domain.
'';
example = "[email protected]";
default = cfg.mailer.smtpAddress;
type = types.str;
};
smtpUserName = mkOption {
description = ''
The username to login on the SMTP server.
Note that a password needs to be supplied through the `environmentFile` option.
'';
example = "[email protected]";
default = cfg.mailer.smtpAddress;
type = types.str;
};
inboundDomain = mkOption {
description = ''
The mail domain on which you want to receive emails. The hostname is used by default.
This will install postfix on mail.DOMAIN.
You need to add an MX record from DOMAIN to mail.DOMAIN to your DNS settings.
'';
example = "mail.example.com";
default = cfg.hostname;
type = types.str;
};
postmasterAlias = mkOption {
description = ''
Set the email on which to reports issues with mail delivery.
'';
example = "[email protected]";
default = "postmaster@${cfg.mailer.inboundDomain}";
type = types.str;
};
};
sentry = {
DSN = mkOption {
description = ''
Set a Sentry Data Source Name to report issues and performance metrics to.
'';
example = "https://abc.ingest.sentry.io/123";
default = "";
type = types.str;
};
tracesSampleRate = mkOption {
description = ''
Set the sample rate for performance metrics.
'';
example = 0.5;
default = 0;
type = types.float;
};
};
nginx = mkOption {
default = { };
example = { basicAuthFile = ./path/to/basic/auth/file; };
description = ''
With this option, you can customize an nginx virtualHost which already
has sensible defaults for feed reader. Set this to {} to just
enable the virtualHost if you don't need any customization. If this is
set to null (the default), no nginx virtualHost will be configured.
'';
};
};
config = mkIf cfg.enable {
environment.systemPackages = [
console
];
services.postgresql = {
enable = true;
package = pkgs.postgresql_15;
ensureDatabases = [ "feed_reader" ];
ensureUsers = [{
name = "feed_reader";
ensureDBOwnership = true;
}];
};
services.postfix =
let
certDir = config.security.acme.certs.${cfg.mailer.inboundDomain}.directory;
in
{
enable = true;
hostname = "mail.${cfg.mailer.inboundDomain}";
postmasterAlias = cfg.mailer.postmasterAlias;
virtual = ''
@${cfg.mailer.inboundDomain} feed_reader@${cfg.mailer.inboundDomain}
'';
transport = ''
${cfg.mailer.inboundDomain} forward_to_feed_reader:
'';
relayDomains = [
cfg.mailer.inboundDomain
];
masterConfig.forward_to_feed_reader = {
command = "pipe";
privileged = true;
args = [
# See pipe manual for details on these settings https://www.postfix.org/pipe.8.html
"flags=Xhq"
"user=feed_reader"
"argv=${relayMailScript}"
];
};
sslCert = "${certDir}/cert.pem";
sslKey = "${certDir}/key.pem";
extraConfig = ''
notify_classes = resource, software, delay, 2bounce, bounce
'';
};
networking.firewall.allowedTCPPorts = [ 25 ];
systemd.tmpfiles.rules = [
"d /run/feed_reader 0755 feed_reader feed_reader -"
"d /var/log/feed_reader 0755 feed_reader feed_reader -"
"d ${cfg.home}/storage 0755 feed_reader feed_reader -"
];
systemd.services = {
feed-reader = {
after = [ "network.target" "postgresql.service" ];
requires = [ "postgresql.service" "feed-reader.socket" ];
wantedBy = [ "multi-user.target" ];
environment = env;
path = [
feed-reader.env
feed-reader.env.wrappedRuby
];
serviceConfig = {
EnvironmentFile = cfg.environmentFile;
Type = "simple";
User = "feed_reader";
Group = "feed_reader";
Restart = "on-failure";
WorkingDirectory = feed-reader;
ExecStartPre = [ migrationScript ];
ExecStart =
"${feed-reader.env}/bin/puma -C ${feed-reader}/config/puma.rb";
};
};
} // (builtins.foldl' (x: y: x // y) { } (builtins.genList
(index: {
"feed-reader-worker-${toString (index + 1)}" = {
after = [
"network.target"
"feed-reader.service"
"postgresql.service"
];
requires = [ "postgresql.service" ];
partOf = [ "feed-reader-workers.service" ];
wantedBy = [ "multi-user.target" ];
environment = env // {
GOOD_JOB_QUEUES = (builtins.elemAt cfg.backgroundWorkers index);
};
path = [ feed-reader.env feed-reader.env.wrappedRuby ];
serviceConfig = {
EnvironmentFile = cfg.environmentFile;
Type = "simple";
User = "feed_reader";
Group = "feed_reader";
Restart = "always";
WorkingDirectory = feed-reader;
ExecStart = "${feed-reader.env}/bin/bundle exec good_job start";
};
};
})
(builtins.length cfg.backgroundWorkers)));
systemd.sockets = {
feed-reader = {
wantedBy = [ "sockets.target" ];
wants = [ "feed-reader.service" ];
listenStreams = [ "0.0.0.0:3000" "/run/feed_reader/server.sock" ];
socketConfig = {
# Socket options matching Puma defaults
NoDelay = true;
ReusePort = true;
Backlog = 1024;
};
};
};
users.users.feed_reader = {
group = "feed_reader";
home = cfg.home;
createHome = true;
isSystemUser = true;
};
users.groups.feed_reader = { };
services.nginx.upstreams = {
"feed_reader_server" = {
servers = {
"${env.SOCKETFILE}" = { };
};
};
};
services.nginx.virtualHosts = mkIf (cfg.nginx != null)
{
"${cfg.hostname}" = mkMerge [
cfg.nginx
{
root = "${feed-reader}/public";
enableACME = true;
forceSSL = true;
locations = {
"/" = { tryFiles = "$uri @rails"; };
"@rails" = {
proxyPass = "http://feed_reader_server";
extraConfig = ''
proxy_set_header X-Forwarded-Ssl on;
'';
};
};
}
];
"mail.${cfg.mailer.inboundDomain}" = {
enableACME = true;
};
} // optionalAttrs (cfg.mailer.inboundDomain != cfg.hostname) {
"${cfg.mailer.inboundDomain}" = {
enableACME = true;
};
};
services.postgresqlBackup = {
enable = (builtins.stringLength cfg.backupLocation) > 0;
location = cfg.backupLocation;
databases = [ "feed_reader" ];
};
};
}