blob: 9aa5b54665013cecbc261b0190857b30015c8edf (
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
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
|
{
config,
pkgs,
lib,
...
}:
let
cfg = config.services.glitchtip;
pkg = cfg.package;
inherit (pkg.passthru) python;
environment = lib.mapAttrs (
_: value:
if value == true then
"True"
else if value == false then
"False"
else
toString value
) cfg.settings;
in
{
meta.maintainers = with lib.maintainers; [
defelo
felbinger
];
options = {
services.glitchtip = {
enable = lib.mkEnableOption "GlitchTip";
package = lib.mkPackageOption pkgs "glitchtip" { };
user = lib.mkOption {
type = lib.types.str;
description = "The user account under which GlitchTip runs.";
default = "glitchtip";
};
group = lib.mkOption {
type = lib.types.str;
description = "The group under which GlitchTip runs.";
default = "glitchtip";
};
stateDir = lib.mkOption {
type = lib.types.path;
description = "State directory of glitchtip.";
default = "/var/lib/glitchtip";
};
settings = lib.mkOption {
description = ''
Configuration of GlitchTip. See <https://glitchtip.com/documentation/install#configuration> for more information and required settings.
'';
default = { };
defaultText = lib.literalExpression ''
{
DEBUG = 0;
DEBUG_TOOLBAR = 0;
DATABASE_URL = lib.mkIf config.services.glitchtip.database.createLocally "postgresql://@/glitchtip";
GLITCHTIP_DOMAIN = lib.mkIf config.services.glitchtip.nginx.createLocally "https://''${config.services.glitchtip.nginx.domain}";
GLITCHTIP_VERSION = config.services.glitchtip.package.version;
GRANIAN_HOST = "127.0.0.1";
GRANIAN_PORT = 8000;
GRANIAN_STATIC_PATH_MOUNT = "''${config.services.glitchtip.package}/lib/glitchtip/static";
GRANIAN_WORKERS = 1;
PYTHONUNBUFFERED = 1;
REDIS_URL = lib.mkIf config.services.glitchtip.redis.createLocally "unix://''${config.services.redis.servers.glitchtip.unixSocket}";
}
'';
example = {
GLITCHTIP_DOMAIN = "https://glitchtip.example.com";
DATABASE_URL = "postgres://postgres:postgres@postgres/postgres";
};
type = lib.types.submodule {
freeformType =
with lib.types;
attrsOf (oneOf [
str
int
bool
]);
options = {
GLITCHTIP_DOMAIN = lib.mkOption {
type = lib.types.nullOr lib.types.str;
description = "The URL under which GlitchTip is externally reachable.";
example = "https://glitchtip.example.com";
default = null;
};
GLITCHTIP_ENABLE_MCP = lib.mkOption {
type = lib.types.bool;
description = "Whether to enable the MCP api.";
default = false;
};
GRANIAN_WORKERS = lib.mkOption {
type = lib.types.ints.positive;
description = "Number of granian workers to start";
default = 1;
};
ENABLE_OBSERVABILITY_API = lib.mkOption {
type = lib.types.bool;
description = "Whether to enable the Prometheus metrics endpoint.";
default = false;
};
ENABLE_USER_REGISTRATION = lib.mkOption {
type = lib.types.bool;
description = ''
When true, any user will be able to register. When false, user self-signup is disabled after the first user is registered. Subsequent users must be created by a superuser on the backend and organization invitations may only be sent to existing users.
'';
default = false;
};
ENABLE_ORGANIZATION_CREATION = lib.mkOption {
type = lib.types.bool;
description = ''
When false, only superusers will be able to create new organizations after the first. When true, any user can create a new organization.
'';
default = false;
};
};
};
};
environmentFiles = lib.mkOption {
type = lib.types.listOf lib.types.path;
default = [ ];
example = [ "/run/secrets/glitchtip.env" ];
description = ''
Files to load environment variables from in addition to [](#opt-services.glitchtip.settings).
This is useful to avoid putting secrets into the nix store.
See <https://glitchtip.com/documentation/install#configuration> for more information.
'';
};
database.createLocally = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Whether to enable and configure a local PostgreSQL database server.";
};
nginx = {
createLocally = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Whether to enable and configure a local Nginx server.";
};
domain = lib.mkOption {
type = lib.types.str;
example = "glitchtip.example.com";
description = ''
Domain under which GlitchTip will be reachable.
In contrast to `settings.GLITCHTIP_DOMAIN` this option has no protocol.
It will also set `settings.GLITCHTIP_DOMAIN` with the `https://` protocol.
'';
};
};
redis.createLocally = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Whether to enable and configure a local Redis instance.";
};
};
};
imports = [
(lib.mkRenamedOptionModule
[ "services" "glitchtip" "listenAddress" ]
[ "services" "glitchtip" "settings" "GRANIAN_HOST" ]
)
(lib.mkRenamedOptionModule
[ "services" "glitchtip" "port" ]
[ "services" "glitchtip" "settings" "GRANIAN_PORT" ]
)
(lib.mkRemovedOptionModule [ "services" "glitchtip" "celery" "extraArgs" ]
"GlitchTip 6 migrated away from celery. Please check the upstream docs how to handle your usecase now."
)
(lib.mkRemovedOptionModule [ "services" "glitchtip" "gunicorn" "extraArgs" ]
"GlitchTip 6 migrated away from gunicorn. Please check the upstream docs how to handle your usecase now."
)
];
config = lib.mkIf cfg.enable {
services.glitchtip.settings = {
DEBUG = lib.mkDefault 0;
DEBUG_TOOLBAR = lib.mkDefault 0;
GLITCHTIP_VERSION = pkg.version;
GRANIAN_HOST = lib.mkDefault "127.0.0.1";
GRANIAN_PORT = lib.mkDefault 8000;
GRANIAN_STATIC_PATH_MOUNT = "${pkg}/lib/glitchtip/static";
GRANIAN_WORKERS = lib.mkDefault 1;
PYTHONPATH = "${python.pkgs.makePythonPath pkg.propagatedBuildInputs}:${pkg}/lib/glitchtip";
PYTHONUNBUFFERED = lib.mkDefault 1;
}
// lib.optionalAttrs cfg.database.createLocally { DATABASE_URL = "postgresql://@/glitchtip"; }
// lib.optionalAttrs cfg.nginx.createLocally { GLITCHTIP_DOMAIN = "https://${cfg.nginx.domain}"; }
// lib.optionalAttrs cfg.redis.createLocally {
REDIS_URL = "unix://${config.services.redis.servers.glitchtip.unixSocket}";
};
systemd.services =
let
commonService = {
wantedBy = [ "multi-user.target" ];
wants = [ "network-online.target" ];
requires =
lib.optional cfg.database.createLocally "postgresql.target"
++ lib.optional cfg.redis.createLocally "redis-glitchtip.service";
after = [
"network-online.target"
]
++ lib.optional cfg.database.createLocally "postgresql.target"
++ lib.optional cfg.redis.createLocally "redis-glitchtip.service";
inherit environment;
};
commonServiceConfig = {
User = cfg.user;
Group = cfg.group;
RuntimeDirectory = "glitchtip";
StateDirectory = "glitchtip";
EnvironmentFile = cfg.environmentFiles;
WorkingDirectory = "${pkg}/lib/glitchtip";
BindPaths = [ "${cfg.stateDir}/uploads:${pkg}/lib/glitchtip/uploads" ];
# hardening
AmbientCapabilities = "";
CapabilityBoundingSet = [ "" ];
DevicePolicy = "closed";
LockPersonality = true;
MemoryDenyWriteExecute = true;
NoNewPrivileges = true;
PrivateDevices = true;
PrivateTmp = true;
PrivateUsers = true;
ProcSubset = "pid";
ProtectClock = true;
ProtectControlGroups = true;
ProtectHome = true;
ProtectHostname = true;
ProtectKernelLogs = true;
ProtectKernelModules = true;
ProtectKernelTunables = true;
ProtectProc = "invisible";
ProtectSystem = "strict";
RemoveIPC = true;
RestrictAddressFamilies = [ "AF_INET AF_INET6 AF_UNIX" ];
RestrictNamespaces = true;
RestrictRealtime = true;
RestrictSUIDSGID = true;
SystemCallArchitectures = "native";
SystemCallFilter = [
"@system-service"
"~@privileged"
"~@resources"
"@chown"
];
UMask = "0077";
};
in
{
glitchtip = commonService // {
description = "GlitchTip";
environment =
environment
// lib.optionalAttrs (cfg.settings.ENABLE_OBSERVABILITY_API && cfg.settings.WORKERS > 1) {
PROMETHEUS_MULTIPROC_DIR = "/tmp/prometheus_multiproc";
};
bindsTo = [ "glitchtip-worker.service" ];
before = [ "glitchtip-worker.service" ];
serviceConfig = commonServiceConfig // {
ExecStartPre = [
"${lib.getExe pkg} migrate"
"${lib.getExe pkg} createcachetable"
"${lib.getExe pkg} maintain_partitions"
];
ExecStart = ''
${lib.getExe python.pkgs.granian} \
--interface ${if cfg.settings.GLITCHTIP_ENABLE_MCP then "asgi" else "asginl"} \
glitchtip.asgi:application \
--host ${cfg.settings.GRANIAN_HOST} \
--port ${toString cfg.settings.GRANIAN_PORT} \
--workers ${toString cfg.settings.GRANIAN_WORKERS} \
--no-ws
'';
};
};
glitchtip-worker = commonService // {
description = "GlitchTip Job Runner";
environment = environment // {
IS_WORKER = "1";
};
serviceConfig = commonServiceConfig // {
ExecStart = "${lib.getExe pkg} runworker --scheduler";
};
};
};
services.nginx = lib.mkIf cfg.nginx.createLocally {
enable = true;
virtualHosts.${cfg.nginx.domain} = {
forceSSL = lib.mkDefault true;
locations = {
"/".proxyPass = "http://${cfg.settings.GRANIAN_HOST}:${toString cfg.settings.GRANIAN_PORT}";
"/static/".root = "${pkg}/lib/glitchtip";
};
};
};
services.postgresql = lib.mkIf cfg.database.createLocally {
enable = true;
ensureDatabases = [ "glitchtip" ];
ensureUsers = [
{
name = "glitchtip";
ensureDBOwnership = true;
}
];
};
services.redis.servers.glitchtip.enable = cfg.redis.createLocally;
users.users = lib.mkIf (cfg.user == "glitchtip") {
glitchtip = {
group = cfg.group;
extraGroups = lib.optionals cfg.redis.createLocally [ "redis-glitchtip" ];
isSystemUser = true;
};
};
users.groups = lib.mkIf (cfg.group == "glitchtip") { glitchtip = { }; };
systemd.tmpfiles.settings.glitchtip."${cfg.stateDir}/uploads".d = { inherit (cfg) user group; };
environment.systemPackages = [
(pkgs.writeShellScriptBin "glitchtip-manage" ''
set -o allexport
${lib.toShellVars environment}
${lib.concatMapStringsSep "\n" (f: "source ${f}") cfg.environmentFiles}
${config.security.wrapperDir}/sudo -E -u ${cfg.user} ${lib.getExe pkg} "$@"
'')
];
};
}
|