blob: af9c23cb254b56ec77def2eb4ebdb7e16b1c7c9d (
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
|
{
config,
pkgs,
lib,
...
}:
let
cfg = config.services.tandoor-recipes;
pkg = cfg.package;
stateDir = "/var/lib/tandoor-recipes";
useNewMediaRoot = lib.versionAtLeast config.system.stateVersion "26.05";
# SECRET_KEY through an env file
env = {
GUNICORN_CMD_ARGS = "--bind=${cfg.address}:${toString cfg.port}";
DEBUG = "0";
DEBUG_TOOLBAR = "0";
MEDIA_ROOT = "${stateDir}${lib.optionalString useNewMediaRoot "/media"}";
ALLOWED_HOSTS = cfg.address;
}
// lib.optionalAttrs (config.time.timeZone != null) {
TZ = config.time.timeZone;
}
// (lib.mapAttrs (_: toString) cfg.extraConfig);
manage = pkgs.writeShellScript "manage" ''
set -o allexport # Export the following env vars
${lib.toShellVars env}
# UID is a read-only shell variable
eval "$(${config.systemd.package}/bin/systemctl show -pUID,GID,MainPID tandoor-recipes.service | tr '[:upper:]' '[:lower:]')"
exec ${pkgs.util-linux}/bin/nsenter \
-t $mainpid -m -S $uid -G $gid --wdns=${stateDir} \
${pkg}/bin/tandoor-recipes "$@"
'';
in
{
meta = {
maintainers = with lib.maintainers; [ jvanbruegge ];
doc = ./tandoor-recipes.md;
};
options.services.tandoor-recipes = {
enable = lib.mkOption {
type = lib.types.bool;
default = false;
description = ''
Enable Tandoor Recipes.
When started, the Tandoor Recipes database is automatically created if
it doesn't exist and updated if the package has changed. Both tasks are
achieved by running a Django migration.
A script to manage the instance (by wrapping Django's manage.py) is linked to
`/var/lib/tandoor-recipes/tandoor-recipes-manage`.
'';
};
address = lib.mkOption {
type = lib.types.str;
default = "localhost";
description = "Web interface address.";
};
port = lib.mkOption {
type = lib.types.port;
default = 8080;
description = "Web interface port.";
};
extraConfig = lib.mkOption {
type = lib.types.attrs;
default = { };
description = ''
Extra tandoor recipes config options.
See [the example dot-env file](https://raw.githubusercontent.com/vabene1111/recipes/master/.env.template)
for available options.
'';
example = {
ENABLE_SIGNUP = "1";
};
};
user = lib.mkOption {
type = lib.types.str;
default = "tandoor_recipes";
description = "User account under which Tandoor runs.";
};
group = lib.mkOption {
type = lib.types.str;
default = "tandoor_recipes";
description = "Group under which Tandoor runs.";
};
package = lib.mkPackageOption pkgs "tandoor-recipes" { };
database = {
createLocally = lib.mkOption {
type = lib.types.bool;
default = false;
description = ''
Configure local PostgreSQL database server for Tandoor Recipes.
'';
};
};
};
config = lib.mkIf cfg.enable {
warnings = lib.mkIf (!useNewMediaRoot && !(cfg.extraConfig ? MEDIA_ROOT)) [
"`services.tandoor-recipes.extraConfig.MEDIA_ROOT` is unset. This is considered insecure for `system.stateVersion` < 26.05. See https://nixos.org/manual/nixos/unstable/#module-services-tandoor-recipes-migrating-media for migration instructions."
];
users.users = lib.mkIf (cfg.user == "tandoor_recipes") {
tandoor_recipes = {
inherit (cfg) group;
isSystemUser = true;
};
};
users.groups = lib.mkIf (cfg.group == "tandoor_recipes") {
tandoor_recipes = { };
};
systemd.services.tandoor-recipes = {
description = "Tandoor Recipes server";
requires = lib.optional cfg.database.createLocally "postgresql.target";
after = lib.optional cfg.database.createLocally "postgresql.target";
serviceConfig = {
ExecStart = ''
${pkg.python.pkgs.gunicorn}/bin/gunicorn recipes.wsgi
'';
Restart = "on-failure";
User = cfg.user;
Group = cfg.group;
StateDirectory = [
"tandoor-recipes"
]
++ lib.optional (env.MEDIA_ROOT == "/var/lib/tandoor-recipes/media") "tandoor-recipes/media";
WorkingDirectory = stateDir;
RuntimeDirectory = "tandoor-recipes";
BindReadOnlyPaths = [
"${config.security.pki.caBundle}:/etc/ssl/certs/ca-certificates.crt"
builtins.storeDir
"-/etc/resolv.conf"
"-/etc/nsswitch.conf"
"-/etc/hosts"
"-/etc/localtime"
"-/run/postgresql"
];
CapabilityBoundingSet = "";
LockPersonality = true;
MemoryDenyWriteExecute = true;
PrivateDevices = true;
PrivateUsers = true;
ProtectClock = true;
ProtectControlGroups = true;
ProtectHome = true;
ProtectHostname = true;
ProtectKernelLogs = true;
ProtectKernelModules = true;
ProtectKernelTunables = true;
RestrictAddressFamilies = [
"AF_UNIX"
"AF_INET"
"AF_INET6"
];
RestrictNamespaces = true;
RestrictRealtime = true;
SystemCallArchitectures = "native";
# gunicorn needs setuid
SystemCallFilter = [
"@system-service"
"~@privileged"
"@resources"
"@setuid"
"@keyring"
];
UMask = "0066";
};
wantedBy = [ "multi-user.target" ];
preStart = ''
ln -sf ${manage} tandoor-recipes-manage
# Let django migrate the DB as needed
${pkg}/bin/tandoor-recipes migrate
'';
environment = env // {
PYTHONPATH = "${pkg.python.pkgs.makePythonPath pkg.propagatedBuildInputs}:${pkg}/lib/tandoor-recipes";
};
};
services.tandoor-recipes.extraConfig = lib.mkIf cfg.database.createLocally {
DB_ENGINE = "django.db.backends.postgresql";
POSTGRES_HOST = "/run/postgresql";
POSTGRES_USER = "tandoor_recipes";
POSTGRES_DB = "tandoor_recipes";
};
services.postgresql = lib.mkIf cfg.database.createLocally {
enable = true;
ensureDatabases = [ "tandoor_recipes" ];
ensureUsers = [
{
name = "tandoor_recipes";
ensureDBOwnership = true;
}
];
};
};
}
|