-
Notifications
You must be signed in to change notification settings - Fork 7
/
module.nix
391 lines (346 loc) Β· 12.5 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
{ config, lib, pkgs, ... }:
let
cfg = config.services.course-management;
settingsType = with lib; types.submodule {
options = {
debug = mkEnableOption "django debug mode";
secretKeyFile = mkOption {
type = types.str;
default = "$STATE_DIRECTORY/secret_key";
description = "The secret key django should use.";
};
adminPassFile = mkOption {
type = types.nullOr types.str;
default = null;
description = "If set, a superuser named `admin` with the given password will be created automatically.";
};
database = mkOption {
type = types.attrsOf types.anything;
description = ''
The database the application should connect to.
See https://docs.djangoproject.com/en/3.2/ref/settings/#databases for details.
Environment variables will be substituted at runtime.
'';
default = {
ENGINE = "django.db.backends.sqlite3";
NAME = "$STATE_DIRECTORY/db.sqlite3";
};
example = {
ENGINE = "django.db.backends.mysql";
NAME = "course_management";
USER = "coursemanagement";
PASSWORD = "$MYSQL_PASSWORD";
HOST = "127.0.0.1";
OPTIONS = {
charset = "utf8mb4";
user_unicode = true;
};
};
};
admins = mkOption {
type = types.listOf (types.submodule {
options = {
name = mkOption {
type = types.str;
};
email = mkOption {
type = types.str;
};
};
});
description = ''
A list of all the people who get code error notifications.
'';
default = [ ];
example = [{
name = "Admin";
email = "[email protected]";
}];
};
allowedHosts = mkOption {
type = types.listOf types.str;
description = "A list of hostnames that may be served.";
default = [ (mkIf (cfg.hostName != null) cfg.hostName) ];
};
email = mkOption {
type = settingsEmailType;
description = "Configuration for sending email.";
default = { };
};
extraConfig = mkOption {
default = "";
type = types.lines;
description = ''
Extra configuration options that will be added verbatim at
the end of `settings.py`.
'';
};
};
};
settingsEmailType = with lib; types.submodule {
options = {
host = mkOption {
type = types.str;
default = "localhost";
};
port = mkOption {
type = types.int;
default = 25;
};
user = mkOption {
type = types.str;
default = "";
};
passwordFile = mkOption {
type = types.str;
default = "/dev/null";
};
fromEmail = mkOption {
type = types.str;
default = "webmaster@localhost";
};
serverEmail = mkOption {
type = types.str;
description = "Email used for error notifications.";
default = "root@localhost";
};
};
};
in
{
options.services.course-management = with lib; {
enable = mkEnableOption "iFSR course management";
package = mkOption {
type = types.package;
default = pkgs.course-management;
description = "The package to use.";
};
user = mkOption {
type = types.str;
default = "course-management";
description = "The user under which the server runs.";
};
group = mkOption {
type = types.str;
default = "course-management";
description = "The group under which the server runs.";
};
hostName = mkOption {
type = types.nullOr types.str;
default = null;
example = "courses.example.com";
description = ''
The hostname the application should be served on.
If it is `null`, nginx will not be automatically configured.
'';
};
listenAddress = mkOption {
type = types.str;
default = "127.0.0.1";
description = "The address the server should listen on.";
};
listenPort = mkOption {
type = types.port;
default = 5000;
description = "The port the server should listen on.";
};
workers = mkOption {
type = types.int;
default = 4;
description = "The number of workers gunicorn should use.";
};
settings = mkOption {
type = settingsType;
};
};
config =
let
python = cfg.package.python;
pythonEnv = python.buildEnv.override {
extraLibs = [ cfg.package ];
};
baseDir = "${cfg.package}/${cfg.package.python.sitePackages}/${cfg.package.pname}";
# Hack to turn a generic nix attrSet/list into a python dict/list.
# This serializes the given value to JSON, writes it to a file and returns the python code to load it again.
cfgToPython = name: value: "load_file('${pkgs.writeText (name + ".json") (builtins.toJSON value)}')";
# Convert admins list to python tuples.
adminsStr = builtins.concatStringsSep "\n" (
builtins.map
(a: "('${a.name}', '${a.email}'),")
cfg.settings.admins
);
settingsFile = pkgs.writeTextDir "${python.sitePackages}/course_management_nixos_settings/__init__.py" /* python */ ''
import os, json
from django.utils.translation import gettext_lazy as _
# function to read a file, substitute env variables and deserialize json
def load_file(file_path):
with open(file_path, 'r') as f:
return json.loads(os.path.expandvars(f.read()))
DEBUG = ${if cfg.settings.debug then "True" else "False"}
if DEBUG:
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
ALLOWED_HOSTS = ${cfgToPython "allowedHosts" cfg.settings.allowedHosts}
BASE_DIR = "${baseDir}"
with open(os.path.expandvars('${cfg.settings.secretKeyFile}')) as f:
SECRET_KEY = f.read().strip()
INSTALLED_APPS = (
'modeltranslation',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'guardian',
'user',
'course',
)
MIDDLEWARE = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'course.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'course.wsgi.application'
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'
AUTHENTICATION_BACKENDS = (
'guardian.backends.ObjectPermissionBackend',
'django.contrib.auth.backends.ModelBackend',
)
ANONYMOUS_USER_ID = -1
ADMINS = [
${adminsStr}
]
DATABASES = {
'default': ${cfgToPython "database" cfg.settings.database}
}
LANGUAGE_CODE = 'en'
LANGUAGES = (
('en', _('English')),
('de', _('German')),
)
TIME_ZONE = 'Europe/Berlin'
USE_TZ = False
USE_I18N = True
USE_L10N = True
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
LOCALE_PATHS = (
os.path.join(BASE_DIR, "locale"),
)
DEFAULT_FROM_EMAIL = '${cfg.settings.email.fromEmail}'
SERVER_EMAIL = '${cfg.settings.email.serverEmail}'
EMAIL_HOST = '${cfg.settings.email.host}'
EMAIL_PORT = ${toString cfg.settings.email.port}
EMAIL_HOST_USER = '${cfg.settings.email.user}'
with open('${cfg.settings.email.passwordFile}', 'r') as f:
EMAIL_HOST_PASSWORD = f.read().strip()
${cfg.settings.extraConfig}
'';
ensureAdminScript = pkgs.writeText "ensureAdminScript.py" /* python */ ''
from django.contrib.auth.models import User
from user.models import UserInformation
with open(os.path.expandvars('${cfg.settings.adminPassFile}')) as f:
password = f.read().strip()
query = User.objects.filter(username="admin")
if not query.exists():
user = User.objects.create_superuser(username="admin", first_name="Admin", password=password)
UserInformation.objects.create(user=user, accepted_privacy_policy=True)
else:
user = query.first()
user.set_password(password)
user.save()
'';
environment = {
PYTHONPATH = "${settingsFile}/${python.sitePackages}:${pythonEnv}/${python.sitePackages}";
DJANGO_SETTINGS_MODULE = "course_management_nixos_settings";
};
manageScript =
let
preserveEnv = "--preserve-env=${lib.concatStringsSep "," (builtins.attrNames environment)}";
exportEnv = lib.concatLines (lib.mapAttrsToList (name: value: "export ${name}='${value}'") environment);
in
pkgs.writeScriptBin "cm-manage" ''
#!${pkgs.runtimeShell}
exec=exec
if [[ "$USER" != ${cfg.user} ]]; then
exec='exec /run/wrappers/bin/sudo -u ${cfg.user} ${preserveEnv}'
fi
${exportEnv}
$exec ${lib.getExe python} ${baseDir}/manage.py "$@"
'';
in
lib.mkIf cfg.enable {
environment.systemPackages = [ manageScript ];
users.users.course-management = lib.mkIf (cfg.user == "course-management") {
group = cfg.group;
isSystemUser = true;
};
users.groups.course-management = lib.mkIf (cfg.group == "course-management") { };
systemd.services.course-management = {
inherit environment;
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
preStart = ''
# Generate secret key if it does not exist
if ! [ -f "${cfg.settings.secretKeyFile}" ]; then
${lib.getExe pkgs.pwgen} -s 50 1 > "${cfg.settings.secretKeyFile}"
fi
# Run migrations
${lib.getExe manageScript} migrate
'' + lib.optionalString (cfg.settings.adminPassFile != null) ''
# Ensure that admin user exists
${lib.getExe manageScript} shell < ${ensureAdminScript}
'';
serviceConfig = {
User = cfg.user;
Group = cfg.group;
StateDirectory = "course-management";
ExecStart = "${lib.getExe' python.pkgs.gunicorn "gunicorn"} course-management.course.wsgi -w ${toString cfg.workers} -b ${cfg.listenAddress}:${toString cfg.listenPort}";
# from systemd-analyze --no-pager security course-management.service
CapabilityBoundingSet = null;
MemoryDenyWriteExecute = true;
PrivateDevices = true;
PrivateUsers = true;
ProtectHome = true;
ProtectKernelLogs = true;
RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_UNIX" ];
RestrictNamespaces = true;
RestrictRealtime = true;
SystemCallArchitectures = "native";
SystemCallFilter = "@system-service";
};
};
services.nginx = lib.mkIf (cfg.hostName != null) {
enable = true;
recommendedProxySettings = lib.mkDefault true;
virtualHosts.${cfg.hostName} = {
locations."/".proxyPass = "http://${cfg.listenAddress}:${toString cfg.listenPort}";
locations."/static".root = baseDir;
locations."/static/admin".root = "${pythonEnv}/${python.sitePackages}/django/contrib/admin";
};
};
};
}