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
|
{ config, lib, pkgs, ... }:
with import <stockholm/lib>;
let
gunicorn = pkgs.python3Packages.gunicorn;
bepasty = pkgs.bepasty;
gevent = pkgs.python3Packages.gevent;
python = pkgs.python3Packages.python;
cfg = config.krebs.bepasty;
out = {
options.krebs.bepasty = api;
config = lib.mkIf cfg.enable (lib.mkMerge [
(lib.mkIf cfg.serveNginx nginx-imp)
imp
]);
};
api = {
enable = mkEnableOption "Bepasty Servers";
serveNginx = mkEnableOption "Serve Bepasty Servers with Nginx";
servers = mkOption {
type = with types; attrsOf optionSet;
example = ''
{
"paste.r" = {
defaultPermissions = "read,delete,create";
};
"paste.krebsco.de" = {
defaultPermissions = "read";
};
}
'';
options = singleton {
nginx = mkOption {
# TODO use the correct type
type = with types; attrsOf unspecified;
description = ''
Additional nginx configuration.
'';
};
secretKey = mkOption {
type = types.str;
description = ''
server secret for safe session cookies, must be set.
'';
default = "";
};
# we create a wsgi socket in $workDir/gunicorn-${name}.wsgi
workDir = mkOption {
type = types.str;
description = ''
Path to the working directory (used for sockets and pidfile).
Defaults to the users home directory. Must be accessible to nginx,
permissions will be set to 755
'';
default = config.users.extraUsers.bepasty.home;
};
dataDir = mkOption {
type = types.str;
description = ''
Defaults to the new users home dir which defaults to
/var/lib/bepasty-server/data
'';
default = "${config.users.extraUsers.bepasty.home}/data";
};
extraConfig = mkOption {
type = types.str;
default = "";
# TODO configure permissions in separate
example = ''
PERMISSIONS = {
'myadminsecret': 'admin,list,create,read,delete',
}
MAX_ALLOWED_FILE_SIZE = 5 * 1000 * 1000
'';
};
defaultPermissions = mkOption {
# TODO: listOf str
type = types.str;
description = ''
default permissions for all unauthenticated users.
'';
example = "read,create,delete";
default = "read";
};
};
default = {};
};
};
imp = {
# Configures systemd services for each configured server
# environment.systemPackages = [ bepasty gunicorn gevent ];
systemd.services = mapAttrs' (name: server:
nameValuePair "bepasty-server-${name}" {
description = "Bepasty Server ${name}";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
restartIfChanged = true;
environment = let
penv = python.buildEnv.override {
extraLibs = [ bepasty gevent ];
};
in {
BEPASTY_CONFIG = "${server.workDir}/bepasty-${name}.conf";
PYTHONPATH= "${penv}/${python.sitePackages}/";
};
serviceConfig = {
Type = "simple";
PrivateTmp = true;
ExecStartPre = assert server.secretKey != ""; pkgs.writeDash "bepasty-server.${name}-init" ''
mkdir -p "${server.dataDir}" "${server.workDir}"
chown bepasty:bepasty "${server.workDir}" "${server.dataDir}"
cat > "${server.workDir}/bepasty-${name}.conf" <<EOF
SITENAME="${name}"
STORAGE_FILESYSTEM_DIRECTORY="${server.dataDir}"
SECRET_KEY="${server.secretKey}"
DEFAULT_PERMISSIONS="${server.defaultPermissions}"
${server.extraConfig}
EOF
'';
ExecStart = ''${gunicorn}/bin/gunicorn bepasty.wsgi --name ${name} \
-u bepasty \
-g bepasty \
--workers 3 --log-level=info \
--bind=unix:${server.workDir}/gunicorn-${name}.sock \
--pid ${server.workDir}/gunicorn-${name}.pid \
-k gevent
'';
};
}
) cfg.servers;
users.extraUsers.bepasty = {
uid = genid_uint31 "bepasty";
group = "bepasty";
home = "/var/lib/bepasty-server";
};
users.extraGroups.bepasty = {
gid = genid_uint31 "bepasty";
};
};
nginx-imp = {
assertions = [{ assertion = config.services.nginx.enable;
message = "services.nginx.enable must be true"; }];
services.nginx.virtualHosts = mapAttrs ( name: server:
(mkMerge [
server.nginx
{
extraConfig = ''
client_max_body_size 32M;
'';
locations = {
"/".extraConfig = "proxy_set_header Host $http_host;";
"/".proxyPass = "http://unix:${server.workDir}/gunicorn-${name}.sock";
"/static/".extraConfig = ''
alias ${bepasty}/lib/${python.libPrefix}/site-packages/bepasty/static/;
'';
};
}])
) cfg.servers ;
};
in
out
|