-
Notifications
You must be signed in to change notification settings - Fork 0
/
default.nix
269 lines (249 loc) · 8.14 KB
/
default.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
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.accentor;
api = cfg.apiPackage;
gems = api.env;
web = cfg.webPackage;
env = {
BOOTSNAP_READONLY = "TRUE";
DATABASE_URL = "postgresql://%2Frun%2Fpostgresql/accentor";
FFMPEG_LOG_LOCATION = "/var/log/accentor/ffmpeg.log";
PIDFILE = "/run/accentor/server.pid";
STATEPATH = "/run/accentor/server.state";
SOCKETFILE = "unix:///run/accentor/server.socket";
RACK_ENV = "production";
RAILS_ENV = "production";
RAILS_LOG_TO_STDOUT = "yes";
RAILS_STORAGE_PATH = "${cfg.home}/storage";
RAILS_TRANSCODES_PATH = "${cfg.home}/transcodes";
RUBY_ENABLE_YJIT = "1";
};
exports = concatStringsSep
"\n"
(mapAttrsToList (name: value: "export ${name}=\"${value}\"") env);
console = pkgs.writeShellScriptBin "accentor-console" ''
set -ex
${exports}
export $(cat ${cfg.environmentFile} | xargs)
cd ${api}
${gems}/bin/bundle exec rails c
'';
in
{
options.services.accentor = {
enable = mkEnableOption ''Accentor music server.
Accentor provides an API through a Ruby on Rails application which can be
accessed using the Web UI.
'';
home = mkOption {
description = "The directory where Accentor will run.";
default = "/var/lib/accentor";
type = types.path;
};
hostname = mkOption {
description = ''
The virtual hostname on which nginx will host the API and Web UI.
'';
example = "accentor.example.com";
type = types.str;
};
workers = mkOption {
description = ''
A list of background workers with the queues they should use. Each element in the list will spawn a worker with the queues passed.
The available options for queues are:
* `*` (All queues)
* within_30_seconds
* within_5_minutes
* within_30_minutes
* whenever
Queues can be configured in different ways. Please check the good job docs for the possibilities: https://github.com/bensheldon/good_job#optimize-queues-threads-and-processes
'';
default = [ "+within_30_seconds,within_5_minutes,within_30_minutes,whenever" ];
example = [ "within_30_seconds" "within_5_minutes:5;within_30_minutes:2;whenever:2" "+within_30_seconds,within_5_minutes,within_30_minutes,whenever" ];
type = types.listOf types.str;
};
environmentFile = mkOption {
description = ''
Path to a file containing secret environment variables that should be
passed to Accentor. Currently this has to contain the SECRET_KEY_BASE
environment variable which can be generated using rails secret.
'';
example = "/run/secrets/accentor";
type = types.str;
};
rescanTimer = {
enable = mkEnableOption "automatic rescanning of all locations";
dates = mkOption {
type = types.str;
default = "04:44";
description = ''
Specification (in the format described by
<citerefentry><refentrytitle>systemd.time</refentrytitle>
<manvolnum>7</manvolnum></citerefentry>) of the time at
which the rescan will be started.
'';
};
};
nginx = mkOption {
default = {
forceSSL = true;
enableACME = true;
};
example = {
serverAliases = [
"music.\${config.networking.domain}"
];
};
description = ''
With this option, you can customize an nginx virtualHost which already
has sensible defaults for Accentor. 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.
'';
};
apiPackage = mkOption {
description = "Accentor API package to use";
default = pkgs.accentor-api;
defaultText = "pkgs.accentor-api";
type = types.package;
};
webPackage = mkOption {
description = "Accentor web package to use";
default = pkgs.accentor-web;
defaultText = "pkgs.accentor-web";
type = types.package;
};
};
config = mkIf cfg.enable {
environment.systemPackages = [ console ];
services.postgresql = {
ensureUsers = [{
name = "accentor";
ensureDBOwnership = true;
}];
ensureDatabases = [ "accentor" ];
};
systemd.tmpfiles.rules = [
"d /run/accentor 0755 accentor accentor -"
"d /var/log/accentor 0755 accentor accentor -"
"d /var/tmp/accentor/transcode_cache 0755 accentor accentor -"
"d /var/tmp/accentor/bootsnap 0755 accentor accentor -"
"d ${cfg.home}/storage 0755 accentor accentor -"
];
systemd.services = {
accentor-api = {
after = [ "network.target" "postgresql.service" ];
requires = [ "postgresql.service" "accentor-api.socket" ];
wantedBy = [ "multi-user.target" ];
environment = env;
path = [ pkgs.ffmpeg gems gems.wrappedRuby ];
serviceConfig = {
EnvironmentFile = cfg.environmentFile;
Type = "simple";
User = "accentor";
Group = "accentor";
Restart = "on-failure";
WorkingDirectory = api;
ExecStartPre = [
"${gems}/bin/bundle exec rails db:migrate"
];
ExecStart = "${gems}/bin/puma -C ${api}/config/puma.rb";
};
};
} // (builtins.foldl' (x: y: x // y) { } (lib.lists.imap0
(index: value: {
"accentor-worker-${toString (index)}" = {
after = [ "network.target" "accentor-api.service" "postgresql.service" ];
requires = [ "postgresql.service" ];
wantedBy = [ "multi-user.target" ];
environment = env // {
GOOD_JOB_QUEUES = value;
};
path = [ pkgs.ffmpeg gems gems.wrappedRuby ];
serviceConfig = {
EnvironmentFile = cfg.environmentFile;
Type = "simple";
User = "accentor";
Group = "accentor";
Restart = "on-failure";
WorkingDirectory = api;
ExecStart = "${gems}/bin/bundle exec good_job start";
};
};
})
cfg.workers
))
// lib.optionalAttrs cfg.rescanTimer.enable {
accentor-rescan = {
description = "Accentor rescan";
restartIfChanged = false;
unitConfig.X-StopOnRemoval = false;
environment = env;
path = [ gems gems.wrappedRuby ];
serviceConfig = {
EnvironmentFile = cfg.environmentFile;
Type = "oneshot";
User = "accentor";
Group = "accentor";
WorkingDirectory = api;
ExecStart = "${gems}/bin/bundle exec rails rescan:start";
};
startAt = cfg.rescanTimer.dates;
};
};
systemd.sockets = {
accentor-api = {
wantedBy = [ "sockets.target" ];
wants = [ "accentor-api.service" ];
listenStreams = [ "0.0.0.0:3000" "/run/accentor/server.socket" ];
socketConfig = {
Backlog = 1024;
NoDelay = true;
ReusePort = true;
};
};
};
users.users.accentor = {
group = "accentor";
home = cfg.home;
createHome = true;
uid = 314;
};
users.groups.accentor.gid = 314;
services.nginx.upstreams = mkIf (cfg.nginx != null) {
"accentor_api_server" = {
servers = {
"unix:///run/accentor/server.socket" = { };
};
};
};
services.nginx.virtualHosts = mkIf (cfg.nginx != null) {
"${cfg.hostname}" = mkMerge [
cfg.nginx
{
root = web;
locations = {
"/api" = {
proxyPass = "http://accentor_api_server";
extraConfig = ''
proxy_set_header X-Forwarded-Ssl on;
client_max_body_size 40M;
'';
};
"/rails" = {
proxyPass = "http://accentor_api_server";
extraConfig = ''
proxy_set_header X-Forwarded-Ssl on;
'';
};
"/".extraConfig = ''
autoindex on;
try_files $uri $uri/ /index.html =404;
'';
};
}
];
};
};
}