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
|
{ config, pkgs, lib, ... }:
let
inherit (lib)
concatMapStrings concatMapStringsSep concatStringsSep
filterAttrs genAttrs hasPrefix mapAttrs mapAttrsToList mkDefault
mkEnableOption mkIf mkOption optionalAttrs optionalString
recursiveUpdate types;
inherit (types)
attrsOf bool either enum int lines listOf nullOr path str
submodule unspecified;
inherit (builtins)
attrNames elem filter isAttrs isBool isList isString toString;
cfg = config.nixsap.apps.mediawiki;
user = config.nixsap.apps.mediawiki.user;
defaultPool = {
listen.owner = config.nixsap.apps.nginx.user;
pm.max_children = 10;
pm.max_requests = 1000;
pm.max_spare_servers = 5;
pm.min_spare_servers = 3;
pm.strategy = "dynamic";
env.MEDIAWIKI_LOCAL_SETTINGS = "${localSettings}";
php_value = optionalAttrs (cfg.maxUploadSize != null) {
post_max_size = 2 * cfg.maxUploadSize;
upload_max_filesize = cfg.maxUploadSize;
};
};
explicit = filterAttrs (n: v: n != "_module" && v != null);
concatMapAttrsSep = s: f: attrs: concatStringsSep s (mapAttrsToList f attrs);
enabledExtentions = attrNames (filterAttrs (_: enabled: enabled) (explicit cfg.extensions));
keys = filter (hasPrefix "/run/keys/") (mapAttrsToList (_: o: o.password-file) cfg.users);
settings =
let
show = s: n: v:
if isBool v then (if v then "TRUE" else "FALSE")
else if isString v then "'${v}'"
else if isList v then "array(${concatMapStringsSep "," (i: "\n${s}'${toString i}'") v})"
else if isAttrs v then "array(${concatMapAttrsSep "," (p: q: "\n${s}'${p}' => ${show "${s} " p q}") (explicit v)})"
else toString v;
in pkgs.writePHPFile "LocalSettings.inc.php" ''
<?php
${concatMapAttrsSep "\n"
(n: v: if isAttrs v
# XXX This will preserve or replace defaults,
# but would give odd result if any element were a list:
then "\$${n} = array_replace_recursive (\$${n}, ${show " " n v});"
else "\$${n} = ${show " " n v};")
(explicit cfg.localSettings)}
?>
'';
localSettings = pkgs.writePHPFile "LocalSettings.php" ''
<?php
${concatMapStringsSep "\n " (e:
"require_once ('${pkgs.mediawikiExtensions.${e}}/${e}.php');"
) enabledExtentions
}
${optionalString (elem "GraphViz" enabledExtentions)
"$wgGraphVizSettings->execPath = '${pkgs.graphviz}/bin/';"
}
${optionalString (elem "MathJax" enabledExtentions) ''
# MathJax 0.7:
MathJax_Parser::$MathJaxJS = '${pkgs.mathJax}/MathJax.js?config=TeX-AMS-MML_HTMLorMML-full';
''}
$wgDiff = '${pkgs.diffutils}/bin/diff';
$wgDiff3 = '${pkgs.diffutils}/bin/diff3';
$wgImageMagickConvertCommand = '${pkgs.imagemagick}/bin/convert';
${optionalString (cfg.logo != null) ''
$wgLogo = '${cfg.logo}';
''}
${optionalString (cfg.maxUploadSize != null)
"$wgMaxUploadSize = ${toString cfg.maxUploadSize};"
}
require_once ('${settings}');
$wgDirectoryMode = 0750;
?>
'';
mediawiki-db =
let
psql = pkgs.writeBashScript "mw-psql" ''
set -euo pipefail
exec ${pkgs.postgresql}/bin/psql -t -w \
-v ON_ERROR_STOP=1 \
${optionalString (cfg.localSettings.wgDBserver != "")
"-h '${cfg.localSettings.wgDBserver}'"} \
-p ${toString cfg.localSettings.wgDBport} \
-U ${toString cfg.localSettings.wgDBuser} \
-d '${cfg.localSettings.wgDBname}' \
"$@"
'';
mysql = pkgs.writeBashScript "mw-mysql" ''
set -euo pipefail
exec ${pkgs.mysql}/bin/mysql -N \
${optionalString (cfg.localSettings.wgDBserver != "")
"-h '${cfg.localSettings.wgDBserver}'"} \
-u ${toString cfg.localSettings.wgDBuser} \
-D '${cfg.localSettings.wgDBname}' \
"$@"
'';
in pkgs.writeBashScriptBin "mediawiki-db" ''
set -euo pipefail
${if cfg.localSettings.wgDBtype == "postgres" then ''
while ! ${psql} -c ';'; do
sleep 5s
done
exist=$(${psql} -c "SELECT COUNT(1) FROM pg_class WHERE relname = 'mwuser';")
if [ "''${exist//[[:space:]]/}" -eq 0 ]; then
{
# XXX this script has BEGIN, but no COMMIT:
cat ${pkgs.mediawiki}/maintenance/postgres/tables.sql
echo 'COMMIT;'
} | ${psql}
fi
'' else ''
while ! ${mysql} -e ';'; do
sleep 5s
done
exist=$(${mysql} -e "SELECT COUNT(1) FROM information_schema.tables
WHERE table_schema='${cfg.localSettings.wgDBname}'
AND table_name='mwuser'")
if [ "''${exist//[[:space:]]/}" -eq 0 ]; then
{
cat ${pkgs.mediawiki}/maintenance/tables.sql
} | ${mysql}
fi
''}
export MEDIAWIKI_LOCAL_SETTINGS='${localSettings}'
${pkgs.php}/bin/php ${pkgs.mediawiki}/maintenance/update.php
${concatMapAttrsSep "" (n: o: ''
pw=$(cat '${o.password-file}')
if [ -z "$pw" ]; then
echo 'WARNING: Using random password, because ${o.password-file} is empty or cannot be read' >&2
pw=$(${pkgs.pwgen}/bin/pwgen -1 13)
fi
${pkgs.php}/bin/php ${pkgs.mediawiki}/maintenance/createAndPromote.php \
--force --${o.role} '${n}' "$pw"
'') cfg.users}
'';
mediawiki-upload = pkgs.writeBashScriptBin "mediawiki-upload" ''
set -euo pipefail
mkdir -v -p '${cfg.localSettings.wgUploadDirectory}'
${optionalString (elem "GraphViz" enabledExtentions)
# XXX: GraphViz::getUploadSubdir: mkdir(/mediawiki/graphviz/images/, 16872) failed
# GraphViz fails to create the directory until you create the first graph.
"mkdir -v -p '${cfg.localSettings.wgUploadDirectory}/graphviz'"
}
chmod -Rc u=rwX,g=rX,o= '${cfg.localSettings.wgUploadDirectory}'
chown -Rc '${user}:${user}' '${cfg.localSettings.wgUploadDirectory}'
'';
nginx = ''
${cfg.nginxServer}
${optionalString (cfg.maxUploadSize != null)
"client_max_body_size ${toString cfg.maxUploadSize};"
}
root ${pkgs.mediawiki};
index index.php;
${optionalString (cfg.logo != null) ''
location = ${cfg.logo} {
alias ${cfg.logo};
}
''}
${optionalString
(cfg.localSettings.wgEnableUploads
&& hasPrefix "/" cfg.localSettings.wgUploadPath) ''
location ${cfg.localSettings.wgUploadPath} {
alias ${cfg.localSettings.wgUploadDirectory};
}
''}
${concatMapStrings (e: ''
location /extensions/${e} {
alias ${pkgs.mediawikiExtensions.${e}};
}
'') enabledExtentions
}
${optionalString (elem "MathJax" enabledExtentions) ''
location ${pkgs.mathJax} {
alias ${pkgs.mathJax};
}
''}
location / {
try_files $uri $uri/ @rewrite;
}
location @rewrite {
rewrite ^/(.*)$ /index.php?title=$1&$args;
}
location ^~ /maintenance/ {
return 403;
}
location ~ \.php$ {
fastcgi_pass unix:${config.nixsap.apps.php-fpm.mediawiki.pool.listen.socket};
include ${pkgs.nginx}/conf/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
'';
in {
options.nixsap.apps.mediawiki = {
enable = mkEnableOption "Mediawiki";
user = mkOption {
description = ''
The user the PHP-FPM pool runs as. And the owner of uploaded files.
'';
default = "mediawiki";
type = str;
};
nginxServer = mkOption {
type = lines;
default = "";
example = ''
listen 8080;
server_name wiki.example.net;
'';
};
fpmPool = mkOption {
description = "Options for the PHP FPM pool";
type = attrsOf unspecified;
default = {};
};
logo = mkOption {
description = "The site logo (the image displayed in the upper-left corner of the page)";
type = nullOr path;
default = null;
};
maxUploadSize = mkOption {
description = ''
Maximum allowed size for uploaded files (bytes).
This affects Mediawiki itself, Nginx and PHP.
'';
type = nullOr int;
default = null;
};
localSettings = mkOption {
description = "Variables in LocalSettings.php";
type = submodule (import ./localSettings.nix (explicit cfg.extensions));
default = {};
};
extensions = mkOption {
description = "Mediawiki extensions";
default = {};
type = submodule
{ options = mapAttrs
(e: _:
mkOption {
description = "Enable the ${e} extension";
type = bool;
default = false;
}) pkgs.mediawikiExtensions;
};
};
users = mkOption {
description = "Mediawiki users (only bots or sysops)";
default = {};
type = attrsOf (submodule { options = {
role = mkOption { type = enum [ "bot" "sysop" ]; };
password-file = mkOption { type = path; };
}; });
};
};
config = mkIf cfg.enable {
nixsap.apps.php-fpm.mediawiki.pool =
recursiveUpdate defaultPool (cfg.fpmPool // { user = cfg.user ;});
nixsap.deployment.keyrings.${user} = keys;
users.users.${config.nixsap.apps.nginx.user}.extraGroups =
mkIf cfg.localSettings.wgEnableUploads [ user ];
nixsap.apps.nginx.http.servers.mediawiki = nginx;
systemd.services.mediawiki-db = {
description = "configure Mediawiki database";
after = [ "network.target" "local-fs.target" "keys.target" ];
wants = [ "keys.target" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
RemainAfterExit = true;
Type = "oneshot";
User = config.nixsap.apps.php-fpm.mediawiki.pool.user;
ExecStart = "${mediawiki-db}/bin/mediawiki-db";
};
};
systemd.services.mediawiki-upload = mkIf cfg.localSettings.wgEnableUploads {
description = "configure Mediawiki uploads";
after = [ "local-fs.target" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
RemainAfterExit = true;
Type = "oneshot";
ExecStart = "${mediawiki-upload}/bin/mediawiki-upload";
};
};
};
}
|