]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - nixops/modules/websites/default.nix
Make httpd service builder
[perso/Immae/Config/Nix.git] / nixops / modules / websites / default.nix
CommitLineData
8a964143 1{ lib, pkgs, config, myconfig, ... }:
42429ef0
IB
2let
3 cfg = config.services.myWebsites;
7da3ceec
IB
4 www_root = "/run/current-system/webapps/_www";
5 theme_root = "/run/current-system/webapps/_theme";
f8bde3d6
IB
6 makeService = name: cfg: let
7 toVhost = vhostConf: {
8 enableSSL = true;
9 sslServerCert = "/var/lib/acme/${vhostConf.certName}/cert.pem";
10 sslServerKey = "/var/lib/acme/${vhostConf.certName}/key.pem";
dfb0e6df 11 sslServerChain = "/var/lib/acme/${vhostConf.certName}/chain.pem";
f8bde3d6 12 logFormat = "combinedVhost";
d68bb46b 13 listen = map (ip: { inherit ip; port = 443; }) cfg.ips;
f8bde3d6
IB
14 hostName = builtins.head vhostConf.hosts;
15 serverAliases = builtins.tail vhostConf.hosts or [];
16 documentRoot = vhostConf.root;
17 extraConfig = builtins.concatStringsSep "\n" vhostConf.extraConfig;
18 };
e2f5cc37 19 nosslVhost = {
d68bb46b 20 listen = map (ip: { inherit ip; port = 80; }) cfg.ips;
e2f5cc37
IB
21 hostName = "nossl.immae.eu";
22 enableSSL = false;
23 logFormat = "combinedVhost";
7da3ceec 24 documentRoot = www_root;
e2f5cc37 25 extraConfig = ''
7da3ceec 26 <Directory ${www_root}>
e2f5cc37
IB
27 DirectoryIndex nossl.html
28 AllowOverride None
29 Require all granted
30
31 RewriteEngine on
32 RewriteRule ^/(.+) / [L]
33 </Directory>
34 '';
35 };
950ca5ee 36 redirectVhost = { # Should go last, catchall http -> https redirect
d68bb46b 37 listen = map (ip: { inherit ip; port = 80; }) cfg.ips;
950ca5ee
IB
38 hostName = "redirectSSL";
39 serverAliases = [ "*" ];
40 enableSSL = false;
41 logFormat = "combinedVhost";
42 documentRoot = "/var/lib/acme/acme-challenge";
43 extraConfig = ''
44 RewriteEngine on
45 RewriteCond "%{REQUEST_URI}" "!^/\.well-known"
46 RewriteRule ^(.+) https://%{HTTP_HOST}$1 [R=301]
47 # To redirect in specific "VirtualHost *:80", do
48 # RedirectMatch 301 ^/((?!\.well-known.*$).*)$ https://host/$1
49 # rather than rewrite
50 '';
51 };
52 fallbackVhost = toVhost { # Should go first, default choice
53 certName = "eldiron";
54 hosts = ["eldiron.immae.eu" ];
7da3ceec 55 root = www_root;
950ca5ee
IB
56 extraConfig = [ "DirectoryIndex index.htm" ];
57 };
f8bde3d6
IB
58 in rec {
59 enable = true;
d68bb46b 60 listen = map (ip: { inherit ip; port = 443; }) cfg.ips;
f8bde3d6
IB
61 stateDir = "/run/httpd_${name}";
62 logPerVirtualHost = true;
63 multiProcessingModule = "worker";
64 adminAddr = "httpd@immae.eu";
65 logFormat = "combinedVhost";
66 extraModules = pkgs.lib.lists.unique (pkgs.lib.lists.flatten cfg.modules);
67 extraConfig = builtins.concatStringsSep "\n" cfg.extraConfig;
950ca5ee 68 virtualHosts = [ fallbackVhost ]
e2f5cc37 69 ++ lib.optionals (name == "tools") [ nosslVhost ]
950ca5ee
IB
70 ++ (pkgs.lib.attrsets.mapAttrsToList (n: v: toVhost v) cfg.vhostConfs)
71 ++ [ redirectVhost ];
f8bde3d6 72 };
d68bb46b 73 makeServiceOptions = name: {
f8bde3d6 74 enable = lib.mkEnableOption "enable websites in ${name}";
d68bb46b
IB
75 ips = lib.mkOption {
76 type = lib.types.listOf lib.types.string;
77 default = let
78 ips = myconfig.env.servers.eldiron.ips.${name};
79 in
80 [ips.ip4] ++ (ips.ip6 or []);
81 description = "${name} ips to listen to";
f8bde3d6
IB
82 };
83 modules = lib.mkOption {
84 type = lib.types.listOf (lib.types.str);
85 default = [];
86 };
87 extraConfig = lib.mkOption {
88 type = lib.types.listOf (lib.types.lines);
89 default = [];
90 };
91 vhostConfs = lib.mkOption {
92 type = lib.types.attrsOf (lib.types.submodule {
93 options = {
94 certName = lib.mkOption { type = lib.types.string; };
95 hosts = lib.mkOption { type = lib.types.listOf lib.types.string; };
96 root = lib.mkOption { type = lib.types.nullOr lib.types.path; };
97 extraConfig = lib.mkOption { type = lib.types.listOf lib.types.lines; default = []; };
98 };
99 });
100 };
101 };
f8d3b61a
IB
102 makeModules = cfg: pkgs.lib.lists.flatten (pkgs.lib.attrsets.mapAttrsToList (n: v: v.modules or []) cfg.apacheConfig);
103 makeExtraConfig = cfg: (builtins.filter (x: x != null) (pkgs.lib.attrsets.mapAttrsToList (n: v: v.extraConfig or null) cfg.apacheConfig));
42429ef0
IB
104in
105{
106 imports = [
54307da4
IB
107 ./chloe
108 ./ludivine
109 ./aten
110 ./piedsjaloux
111 ./connexionswing
2f0f1c48 112 ./tellesflorian
9a35b8f9 113 ./emilia
10bd8c08 114 ./capitaines
d578d270 115 ./ftp/jerome.nix
53b8fad9 116 ./ftp/nassime.nix
79f239be 117 ./ftp/florian.nix
dc9fb826 118 ./ftp/denisejerome.nix
c336bac4 119 ./ftp/leila.nix
97953ca4 120 ./ftp/papa.nix
f759f52e 121 ./ftp/immae.nix
ce493c5d
IB
122 ./ftp/release.nix
123 ./ftp/temp.nix
79d2de8b 124 ./tools/db.nix
10889174
IB
125 ./tools/tools
126 ./tools/dav
79d2de8b 127 ./tools/cloud.nix
10889174 128 ./tools/git
79d2de8b
IB
129 ./tools/mastodon.nix
130 ./tools/mediagoblin.nix
131 ./tools/diaspora.nix
bf3b7671 132 ./tools/ether.nix
f3a8fab5 133 ./tools/peertube.nix
10889174
IB
134 # Adapted from base phpfpm
135 ./phpfpm
42429ef0
IB
136 ];
137
138 options.services.myWebsites = {
d68bb46b
IB
139 production = makeServiceOptions "production";
140 integration = makeServiceOptions "integration";
141 tools = makeServiceOptions "main";
42429ef0
IB
142
143 apacheConfig = lib.mkOption {
144 type = lib.types.attrsOf (lib.types.submodule {
145 options = {
146 modules = lib.mkOption {
147 type = lib.types.listOf (lib.types.str);
148 default = [];
149 };
150 extraConfig = lib.mkOption {
151 type = lib.types.nullOr lib.types.lines;
152 default = null;
153 };
154 };
155 });
156 default = {};
157 description = "Extra global config";
158 };
159
160 };
161
162 config = {
98163486 163 users.users.wwwrun.extraGroups = [ "keys" ];
d68bb46b 164 networking.firewall.allowedTCPPorts = [ 80 443 ];
54307da4 165
2368a4b7 166 nixpkgs.overlays = [ (self: super: rec {
e2ca51b2 167 #openssl = self.openssl_1_1;
98584540 168 php = php72;
2368a4b7
IB
169 php72 = (super.php72.override {
170 mysql.connector-c = self.mariadb;
98584540
IB
171 config.php.mysqlnd = false;
172 config.php.mysqli = false;
173 }).overrideAttrs(old: rec {
174 # Didn't manage to build with mysqli + mysql_config connector
175 configureFlags = old.configureFlags ++ [
912921a7 176 "--with-mysqli=shared,mysqlnd"
98584540
IB
177 ];
178 # preConfigure = (old.preConfigure or "") + ''
179 # export CPPFLAGS="$CPPFLAGS -I${pkgs.mariadb}/include/mysql/server";
180 # sed -i -e 's/#include "mysqli_priv.h"/#include "mysqli_priv.h"\n#include <mysql_version.h>/' \
181 # ext/mysqli/mysqli.c ext/mysqli/mysqli_prop.c
182 # '';
183 });
2368a4b7 184 phpPackages = super.php72Packages.override { inherit php; };
2368a4b7 185 }) ];
98584540 186
10889174
IB
187 services.myWebsites.tools.databases.enable = true;
188 services.myWebsites.tools.tools.enable = true;
189 services.myWebsites.tools.dav.enable = true;
190 services.myWebsites.tools.cloud.enable = true;
191 services.myWebsites.tools.git.enable = true;
35a397cd 192 services.myWebsites.tools.mastodon.enable = true;
56eba416 193 services.myWebsites.tools.mediagoblin.enable = true;
a7f7fdae 194 services.myWebsites.tools.diaspora.enable = true;
17146204 195 services.myWebsites.tools.etherpad-lite.enable = true;
0eaac6ba 196 services.myWebsites.tools.peertube.enable = true;
10889174 197
42429ef0
IB
198 services.myWebsites.Chloe.production.enable = cfg.production.enable;
199 services.myWebsites.Ludivine.production.enable = cfg.production.enable;
200 services.myWebsites.Aten.production.enable = cfg.production.enable;
201 services.myWebsites.PiedsJaloux.production.enable = cfg.production.enable;
202 services.myWebsites.Connexionswing.production.enable = cfg.production.enable;
d578d270 203 services.myWebsites.Jerome.production.enable = cfg.production.enable;
53b8fad9 204 services.myWebsites.Nassime.production.enable = cfg.production.enable;
79f239be 205 services.myWebsites.Florian.production.enable = cfg.production.enable;
c336bac4 206 services.myWebsites.Leila.production.enable = cfg.production.enable;
97953ca4 207 services.myWebsites.Papa.production.enable = cfg.production.enable;
dc9fb826 208 services.myWebsites.DeniseJerome.production.enable = cfg.production.enable;
9a35b8f9 209 services.myWebsites.Emilia.production.enable = cfg.production.enable;
10bd8c08 210 services.myWebsites.Capitaines.production.enable = cfg.production.enable;
f759f52e 211 services.myWebsites.Immae.production.enable = cfg.production.enable;
ce493c5d
IB
212 services.myWebsites.Release.production.enable = cfg.production.enable;
213 services.myWebsites.Temp.production.enable = cfg.production.enable;
42429ef0
IB
214
215 services.myWebsites.Chloe.integration.enable = cfg.integration.enable;
216 services.myWebsites.Ludivine.integration.enable = cfg.integration.enable;
217 services.myWebsites.Aten.integration.enable = cfg.integration.enable;
218 services.myWebsites.PiedsJaloux.integration.enable = cfg.integration.enable;
219 services.myWebsites.Connexionswing.integration.enable = cfg.integration.enable;
2f0f1c48 220 services.myWebsites.TellesFlorian.integration.enable = true;
79f239be 221 services.myWebsites.Florian.integration.enable = true;
42429ef0 222
1a718805 223 secrets.keys = [{
8db8e666 224 dest = "apache-ldap";
415bcd27
IB
225 user = "wwwrun";
226 group = "wwwrun";
85f5ed68 227 permissions = "0400";
415bcd27
IB
228 text = ''
229 <Macro LDAPConnect>
230 <IfModule authnz_ldap_module>
231 AuthLDAPURL ldap://ldap.immae.eu:389/dc=immae,dc=eu STARTTLS
232 AuthLDAPBindDN cn=httpd,ou=services,dc=immae,dc=eu
233 AuthLDAPBindPassword "${myconfig.env.httpd.ldap.password}"
234 AuthType Basic
235 AuthName "Authentification requise (Acces LDAP)"
236 AuthBasicProvider ldap
237 </IfModule>
238 </Macro>
239 '';
8db8e666 240 }];
415bcd27 241
42429ef0
IB
242 services.myWebsites.apacheConfig = {
243 gzip = {
244 modules = [ "deflate" "filter" ];
245 extraConfig = ''
246 AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
247 '';
248 };
249 macros = {
250 modules = [ "macro" ];
251 };
ce493c5d
IB
252 stats = {
253 extraConfig = ''
254 <Macro Stats %{domain}>
9eae2b47
IB
255 Alias /webstats ${config.services.webstats.dataDir}/%{domain}
256 <Directory ${config.services.webstats.dataDir}/%{domain}>
ce493c5d
IB
257 DirectoryIndex index.html
258 AllowOverride None
259 Require all granted
260 </Directory>
9eae2b47 261 <Location /webstats>
ce493c5d
IB
262 Use LDAPConnect
263 Require ldap-group cn=%{domain},ou=stats,cn=httpd,ou=services,dc=immae,dc=eu
264 </Location>
265 </Macro>
266 '';
267 };
42429ef0
IB
268 ldap = {
269 modules = [ "ldap" "authnz_ldap" ];
9d90e7e2 270 extraConfig = ''
42429ef0
IB
271 <IfModule ldap_module>
272 LDAPSharedCacheSize 500000
273 LDAPCacheEntries 1024
274 LDAPCacheTTL 600
275 LDAPOpCacheEntries 1024
276 LDAPOpCacheTTL 600
277 </IfModule>
278
8db8e666 279 Include /var/secrets/apache-ldap
ce493c5d
IB
280 '';
281 };
282 global = {
8f904d0d 283 extraConfig = (pkgs.webapps.apache-default.override { inherit www_root;}).apacheConfig;
42429ef0 284 };
ce493c5d 285 apaxy = {
d7d031b6 286 extraConfig = (pkgs.webapps.apache-theme.override { inherit theme_root; }).apacheConfig;
ce493c5d 287 };
42429ef0
IB
288 http2 = {
289 modules = [ "http2" ];
290 extraConfig = ''
291 Protocols h2 http/1.1
292 '';
293 };
294 customLog = {
295 extraConfig = ''
296 LogFormat "%v:%p %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combinedVhost
297 '';
298 };
299 };
f8bde3d6 300
10889174
IB
301 system.activationScripts = {
302 httpd = ''
303 install -d -m 0755 /var/lib/acme/acme-challenge
304 install -d -m 0750 -o wwwrun -g wwwrun /var/lib/php/sessions
305 install -d -m 0750 -o wwwrun -g wwwrun /var/lib/php/sessions/adminer
b7d2d4e3 306 install -d -m 0750 -o wwwrun -g wwwrun /var/lib/php/tmp/adminer
10889174
IB
307 install -d -m 0750 -o wwwrun -g wwwrun /var/lib/php/sessions/mantisbt
308 install -d -m 0750 -o wwwrun -g wwwrun /var/lib/php/sessions/davical
b7d2d4e3 309 install -d -m 0750 -o wwwrun -g wwwrun /var/lib/php/sessions/phpldapadmin
10889174
IB
310 '';
311 };
312
7da3ceec
IB
313 system.extraSystemBuilderCmds = let
314 adminer = pkgs.callPackage ./commons/adminer.nix {};
315 in ''
316 mkdir -p $out/webapps
8f904d0d 317 ln -s ${pkgs.webapps.apache-default.www} $out/webapps/_www
d7d031b6 318 ln -s ${pkgs.webapps.apache-theme.theme} $out/webapps/_theme
7da3ceec
IB
319 ln -s ${adminer.webRoot} $out/webapps/${adminer.apache.webappName}
320 '';
321
10889174
IB
322 services.myPhpfpm = {
323 phpPackage = pkgs.php;
324 phpOptions = ''
325 session.save_path = "/var/lib/php/sessions"
95b20e17 326 post_max_size = 20M
98163486
IB
327 ; 15 days (seconds)
328 session.gc_maxlifetime = 1296000
329 ; 30 days (minutes)
330 session.cache_expire = 43200
10889174
IB
331 '';
332 extraConfig = ''
333 log_level = notice
334 '';
335 };
336
f8bde3d6 337 services.httpdProd = makeService "production" config.services.myWebsites.production;
f8d3b61a
IB
338 services.myWebsites.production.modules = makeModules cfg;
339 services.myWebsites.production.extraConfig = makeExtraConfig cfg;
f8bde3d6
IB
340
341 services.httpdInte = makeService "integration" config.services.myWebsites.integration;
f8d3b61a
IB
342 services.myWebsites.integration.modules = makeModules cfg;
343 services.myWebsites.integration.extraConfig = makeExtraConfig cfg;
950ca5ee 344
273e2c61 345 services.httpdTools = makeService "tools" config.services.myWebsites.tools;
f8d3b61a
IB
346 services.myWebsites.tools.modules = makeModules cfg;
347 services.myWebsites.tools.extraConfig = makeExtraConfig cfg ++
646bdf3e
IB
348 [ ''
349 RedirectMatch ^/licen[cs]es?_et_tip(ping)?$ https://www.immae.eu/licences_et_tip.html
350 RedirectMatch ^/licen[cs]es?_and_tip(ping)?$ https://www.immae.eu/licenses_and_tipping.html
351 RedirectMatch ^/licen[cs]es?$ https://www.immae.eu/licenses_and_tipping.html
352 RedirectMatch ^/tip(ping)?$ https://www.immae.eu/licenses_and_tipping.html
353 RedirectMatch ^/(mentions|mentions_legales|legal)$ https://www.immae.eu/mentions.html
354 RedirectMatch ^/CGU$ https://www.immae.eu/CGU
355 ''
356 ]
357 ;
42429ef0
IB
358 };
359}