forked from walachey/openclonk-lorry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.nix
155 lines (132 loc) · 4.05 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
{ config, lib, pkgs, ... }:
let
user = "lorry";
group = user;
cfg = config.services.lorry;
webserver = config.services.caddy;
lorryConfig = pkgs.writeTextFile {
name = "config.py";
checkPhase = ''
${pkgs.python3}/bin/python -m py_compile "$target"
'';
text = ''
DEBUG = False
SESSION_COOKIE_SAMESITE = "Lax"
SQLALCHEMY_TRACK_MODIFICATIONS = False
CACHE_TYPE = "RedisCache"
CACHE_REDIS_URL = "unix://${config.services.redis.servers.lorry.unixSocket}"
OWN_HOST = "${cfg.hostname}"
RESOURCES_PATH = "${cfg.resourcesPath}"
ALLOWED_FILE_EXTENSIONS = ("ocs", "ocf", "ocd")
${lib.optionalString cfg.enablePostgres ''
SQLALCHEMY_DATABASE_URI = "postgresql://${user}@/${user}?host=/run/postgresql"
''}
${cfg.extraConfig}
${lib.optionalString (cfg.configFilePath != null) ''
import runpy
globals().update(runpy.run_path('${cfg.configFilePath}'))
''}
'';
};
pkg = cfg.package.override { configFile = lorryConfig; };
pythonEnv = pkgs.python3.withPackages (ps: [ ps.gunicorn ps.redis pkg ]);
in
{
options.services.lorry = {
enable = lib.mkEnableOption "lorry";
enablePostgres = lib.mkEnableOption "automatic postgres setup";
package = lib.mkOption {
type = lib.types.package;
default = pkgs.callPackage ./. { };
};
hostname = lib.mkOption {
type = lib.types.str;
description = "hostname lorry will run from";
example = "lorry.example.com";
};
resourcesPath = lib.mkOption {
type = lib.types.path;
description = "path the uploads are stored at";
default = "/var/lib/lorry/resources";
};
extraConfig = lib.mkOption {
type = lib.types.str;
description = "extra configuration (Python)";
default = "";
};
configFilePath = lib.mkOption {
type = lib.types.nullOr lib.types.path;
description = "path to a file with extra configuration";
default = null;
};
socket = lib.mkOption {
type = lib.types.path;
description = "path to the gunicorn listen socket";
default = "/run/lorry.sock";
};
workerCount = lib.mkOption {
type = lib.types.int;
description = "gunicorn worker count";
default = 4;
};
};
config = lib.mkIf cfg.enable {
users.users.${user} = {
inherit group;
isSystemUser = true;
};
users.groups.${group} = {};
systemd.tmpfiles.rules = [
"d ${cfg.resourcesPath} 0750 ${user} ${group} - -"
];
systemd.services.lorry = {
description = "Lorry gunicorn server";
serviceConfig = {
Type = "notify";
NotifyAccess = "main";
ExecStart = "${pythonEnv}/bin/gunicorn --workers ${builtins.toString cfg.workerCount} -m 007 lorryserver.app:app";
User = user;
PrivateTmp = true;
ProtectSystem = "strict";
ReadWritePaths = cfg.resourcesPath;
};
};
systemd.sockets.lorry = {
description = "Lorry listen socket";
wantedBy = [ "sockets.target" ];
socketConfig = {
ListenStream = cfg.socket;
SocketUser = user;
SocketGroup = webserver.group;
SocketMode = 0660;
};
};
services.caddy.enable = true;
services.caddy.virtualHosts.${cfg.hostname}.extraConfig = ''
reverse_proxy unix/${cfg.socket}
'';
services.redis.servers.lorry = {
enable = true;
user = user;
group = user;
};
services.postgresql = lib.mkIf cfg.enablePostgres {
enable = true;
ensureUsers = [{
name = user;
ensureDBOwnership = true;
}];
ensureDatabases = [ user ];
};
systemd.services.lorry-initdb = lib.mkIf cfg.enablePostgres {
description = "init lorry database";
wantedBy = [ "multi-user.target" ];
after = [ "postgresql.service" ];
before = [ "lorry.service" ];
script = ''
${pythonEnv}/bin/python3 -c "from lorryserver.db.init_database import init_database;init_database()"
'';
serviceConfig.User = user;
};
};
}