]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - nixops/modules/websites/default.nix
Add release and temp websites
[perso/Immae/Config/Nix.git] / nixops / modules / websites / default.nix
1 { lib, pkgs, config, mylibs, myconfig, ... }:
2 let
3 cfg = config.services.myWebsites;
4 makeService = name: cfg: let
5 toVhost = vhostConf: {
6 enableSSL = true;
7 sslServerCert = "/var/lib/acme/${vhostConf.certName}/cert.pem";
8 sslServerKey = "/var/lib/acme/${vhostConf.certName}/key.pem";
9 sslServerChain = "/var/lib/acme/${vhostConf.certName}/fullchain.pem";
10 logFormat = "combinedVhost";
11 listen = [
12 { ip = cfg.ip; port = 443; }
13 ];
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 };
19 redirectVhost = { # Should go last, catchall http -> https redirect
20 listen = [ { ip = cfg.ip; port = 80; } ];
21 hostName = "redirectSSL";
22 serverAliases = [ "*" ];
23 enableSSL = false;
24 logFormat = "combinedVhost";
25 documentRoot = "/var/lib/acme/acme-challenge";
26 extraConfig = ''
27 RewriteEngine on
28 RewriteCond "%{REQUEST_URI}" "!^/\.well-known"
29 RewriteRule ^(.+) https://%{HTTP_HOST}$1 [R=301]
30 # To redirect in specific "VirtualHost *:80", do
31 # RedirectMatch 301 ^/((?!\.well-known.*$).*)$ https://host/$1
32 # rather than rewrite
33 '';
34 };
35 fallbackVhost = toVhost { # Should go first, default choice
36 certName = "eldiron";
37 hosts = ["eldiron.immae.eu" ];
38 root = ../../www;
39 extraConfig = [ "DirectoryIndex index.htm" ];
40 };
41 in rec {
42 enable = true;
43 listen = [
44 { ip = cfg.ip; port = 443; }
45 ];
46 stateDir = "/run/httpd_${name}";
47 logPerVirtualHost = true;
48 multiProcessingModule = "worker";
49 adminAddr = "httpd@immae.eu";
50 logFormat = "combinedVhost";
51 extraModules = pkgs.lib.lists.unique (pkgs.lib.lists.flatten cfg.modules);
52 extraConfig = builtins.concatStringsSep "\n" cfg.extraConfig;
53 virtualHosts = [ fallbackVhost ]
54 ++ (pkgs.lib.attrsets.mapAttrsToList (n: v: toVhost v) cfg.vhostConfs)
55 ++ [ redirectVhost ];
56 };
57 makeServiceOptions = name: ip: {
58 enable = lib.mkEnableOption "enable websites in ${name}";
59 ip = lib.mkOption {
60 type = lib.types.string;
61 default = ip;
62 description = "${name} ip to listen to";
63 };
64 modules = lib.mkOption {
65 type = lib.types.listOf (lib.types.str);
66 default = [];
67 };
68 extraConfig = lib.mkOption {
69 type = lib.types.listOf (lib.types.lines);
70 default = [];
71 };
72 vhostConfs = lib.mkOption {
73 type = lib.types.attrsOf (lib.types.submodule {
74 options = {
75 certName = lib.mkOption { type = lib.types.string; };
76 hosts = lib.mkOption { type = lib.types.listOf lib.types.string; };
77 root = lib.mkOption { type = lib.types.nullOr lib.types.path; };
78 extraConfig = lib.mkOption { type = lib.types.listOf lib.types.lines; default = []; };
79 };
80 });
81 };
82 };
83 makeModules = cfg: pkgs.lib.lists.flatten (pkgs.lib.attrsets.mapAttrsToList (n: v: v.modules or []) cfg.apacheConfig);
84 makeExtraConfig = cfg: (builtins.filter (x: x != null) (pkgs.lib.attrsets.mapAttrsToList (n: v: v.extraConfig or null) cfg.apacheConfig));
85 in
86 {
87 imports = [
88 ./chloe
89 ./ludivine
90 ./aten
91 ./piedsjaloux
92 ./connexionswing
93 ./tellesflorian
94 ./emilia
95 ./capitaines
96 ./ftp/jerome.nix
97 ./ftp/nassime.nix
98 ./ftp/florian.nix
99 ./ftp/denisejerome.nix
100 ./ftp/immae.nix
101 ./ftp/release.nix
102 ./ftp/temp.nix
103 ./tools/db
104 ./tools/tools
105 ./tools/dav
106 ./tools/cloud
107 ./tools/git
108 ./tools/mastodon
109 ./tools/mediagoblin
110 ./tools/diaspora
111 ./tools/ether
112 # built using:
113 # sed -e "s/services\.httpd/services\.httpdProd/g" .nix-defexpr/channels/nixpkgs/nixos/modules/services/web-servers/apache-httpd/default.nix
114 # Removed allGranted
115 # And removed users / groups
116 ./apache/httpd_prod.nix
117 ./apache/httpd_inte.nix
118 # except for this one for users/groups
119 ./apache/httpd_tools.nix
120 # Adapted from base phpfpm
121 ./phpfpm
122 ];
123
124 options.services.myWebsites = {
125 production = makeServiceOptions "production" myconfig.ips.production;
126 integration = makeServiceOptions "integration" myconfig.ips.integration;
127 tools = makeServiceOptions "tools" myconfig.ips.main;
128
129 apacheConfig = lib.mkOption {
130 type = lib.types.attrsOf (lib.types.submodule {
131 options = {
132 modules = lib.mkOption {
133 type = lib.types.listOf (lib.types.str);
134 default = [];
135 };
136 extraConfig = lib.mkOption {
137 type = lib.types.nullOr lib.types.lines;
138 default = null;
139 };
140 };
141 });
142 default = {};
143 description = "Extra global config";
144 };
145
146 };
147
148 config = {
149 networking = {
150 firewall = {
151 enable = true;
152 allowedTCPPorts = [ 80 443 ];
153 };
154 interfaces."eth0".ipv4.addresses = [
155 # 176.9.151.89 declared in nixops -> infra / tools
156 { address = myconfig.ips.production; prefixLength = 32; }
157 { address = myconfig.ips.integration; prefixLength = 32; }
158 ];
159 };
160
161 nixpkgs.config.packageOverrides = oldpkgs: rec {
162 php = php72;
163 php72 = (oldpkgs.php72.override {
164 mysql.connector-c = pkgs.mariadb;
165 config.php.mysqlnd = false;
166 config.php.mysqli = false;
167 }).overrideAttrs(old: rec {
168 # Didn't manage to build with mysqli + mysql_config connector
169 configureFlags = old.configureFlags ++ [
170 "--with-mysqli=shared,mysqlnd"
171 ];
172 # preConfigure = (old.preConfigure or "") + ''
173 # export CPPFLAGS="$CPPFLAGS -I${pkgs.mariadb}/include/mysql/server";
174 # sed -i -e 's/#include "mysqli_priv.h"/#include "mysqli_priv.h"\n#include <mysql_version.h>/' \
175 # ext/mysqli/mysqli.c ext/mysqli/mysqli_prop.c
176 # '';
177 });
178 phpPackages = oldpkgs.php72Packages.override { inherit php; };
179 composerEnv = import ./commons/composer-env.nix {
180 inherit (pkgs) stdenv writeTextFile fetchurl php unzip;
181 };
182 };
183
184 services.myWebsites.tools.databases.enable = true;
185 services.myWebsites.tools.tools.enable = true;
186 services.myWebsites.tools.dav.enable = true;
187 services.myWebsites.tools.cloud.enable = true;
188 services.myWebsites.tools.git.enable = true;
189 services.myWebsites.tools.mastodon.enable = true;
190 services.myWebsites.tools.mediagoblin.enable = true;
191 services.myWebsites.tools.diaspora.enable = true;
192 services.myWebsites.tools.etherpad-lite.enable = true;
193
194 services.myWebsites.Chloe.production.enable = cfg.production.enable;
195 services.myWebsites.Ludivine.production.enable = cfg.production.enable;
196 services.myWebsites.Aten.production.enable = cfg.production.enable;
197 services.myWebsites.PiedsJaloux.production.enable = cfg.production.enable;
198 services.myWebsites.Connexionswing.production.enable = cfg.production.enable;
199 services.myWebsites.Jerome.production.enable = cfg.production.enable;
200 services.myWebsites.Nassime.production.enable = cfg.production.enable;
201 services.myWebsites.Florian.production.enable = cfg.production.enable;
202 services.myWebsites.DeniseJerome.production.enable = cfg.production.enable;
203 services.myWebsites.Emilia.production.enable = cfg.production.enable;
204 services.myWebsites.Capitaines.production.enable = cfg.production.enable;
205 services.myWebsites.Immae.production.enable = cfg.production.enable;
206 services.myWebsites.Release.production.enable = cfg.production.enable;
207 services.myWebsites.Temp.production.enable = cfg.production.enable;
208
209 services.myWebsites.Chloe.integration.enable = cfg.integration.enable;
210 services.myWebsites.Ludivine.integration.enable = cfg.integration.enable;
211 services.myWebsites.Aten.integration.enable = cfg.integration.enable;
212 services.myWebsites.PiedsJaloux.integration.enable = cfg.integration.enable;
213 services.myWebsites.Connexionswing.integration.enable = cfg.integration.enable;
214 services.myWebsites.TellesFlorian.integration.enable = true;
215 services.myWebsites.Florian.integration.enable = true;
216
217 services.myWebsites.apacheConfig = {
218 gzip = {
219 modules = [ "deflate" "filter" ];
220 extraConfig = ''
221 AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
222 '';
223 };
224 macros = {
225 modules = [ "macro" ];
226 };
227 stats = {
228 extraConfig = ''
229 <Macro Stats %{domain}>
230 Alias /awstats /var/lib/goaccess/%{domain}
231 <Directory /var/lib/goaccess/%{domain}>
232 DirectoryIndex index.html
233 AllowOverride None
234 Require all granted
235 </Directory>
236 <Location /awstats>
237 Use LDAPConnect
238 Require ldap-group cn=%{domain},ou=stats,cn=httpd,ou=services,dc=immae,dc=eu
239 </Location>
240 </Macro>
241 '';
242 };
243 ldap = {
244 modules = [ "ldap" "authnz_ldap" ];
245 extraConfig = ''
246 <IfModule ldap_module>
247 LDAPSharedCacheSize 500000
248 LDAPCacheEntries 1024
249 LDAPCacheTTL 600
250 LDAPOpCacheEntries 1024
251 LDAPOpCacheTTL 600
252 </IfModule>
253
254 <Macro LDAPConnect>
255 <IfModule authnz_ldap_module>
256 AuthLDAPURL ldap://ldap.immae.eu:389/dc=immae,dc=eu STARTTLS
257 AuthLDAPBindDN cn=httpd,ou=services,dc=immae,dc=eu
258 AuthLDAPBindPassword "${myconfig.env.httpd.ldap.password}"
259 AuthType Basic
260 AuthName "Authentification requise (Acces LDAP)"
261 AuthBasicProvider ldap
262 </IfModule>
263 </Macro>
264 '';
265 };
266 global = {
267 extraConfig = ''
268 ErrorDocument 500 /maintenance_immae.html
269 ErrorDocument 501 /maintenance_immae.html
270 ErrorDocument 502 /maintenance_immae.html
271 ErrorDocument 503 /maintenance_immae.html
272 ErrorDocument 504 /maintenance_immae.html
273 Alias /maintenance_immae.html ${../../www}/maintenance_immae.html
274 ProxyPass /maintenance_immae.html !
275
276 AliasMatch "(.*)/googleb6d69446ff4ca3e5.html" ${../../www}/googleb6d69446ff4ca3e5.html
277 '';
278 };
279 apaxy = {
280 extraConfig = ''
281 <Macro Apaxy %{folder} %{ignored}>
282 Alias /theme ${./apache/theme}
283 <Directory ${./apache/theme}>
284 Options -Indexes
285 AllowOverride None
286 Require all granted
287 </Directory>
288
289 # mod_autoindex
290 <Directory %{folder}>
291 Options Indexes
292 AllowOverride None
293 Require all granted
294
295 # Inspired from Apaxy by @adamwhitcroft
296
297 IndexOptions +Charset=UTF-8 +FancyIndexing +IgnoreCase +FoldersFirst +XHTML +HTMLTable +SuppressRules +SuppressDescription +NameWidth=* +IconsAreLinks +ShowForbidden
298
299 IndexHeadInsert "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />"
300
301 IndexIgnoreReset ON
302 IndexIgnore /theme .htaccess %{ignored}
303
304 AddIcon /theme/icons/blank.png ^^BLANKICON^^
305 AddIcon /theme/icons/folder.png ^^DIRECTORY^^
306 AddIcon /theme/icons/folder-home.png ..
307
308 AddIconByType (TXT,/theme/icons/text.png) text/*
309 AddIconByType (IMG,/theme/icons/image.png) image/*
310 AddIconByType (SND,/theme/icons/audio.png) audio/*
311 AddIconByType (VID,/theme/icons/video.png) video/*
312
313 AddIcon /theme/icons/archive.png .7z .bz2 .cab .gz .tar
314 AddIcon /theme/icons/audio.png .aac .aif .aifc .aiff .ape .au .flac .iff .m4a .mid .mp3 .mpa .ra .wav .wma .f4a .f4b .oga .ogg .xm .it .s3m .mod
315 AddIcon /theme/icons/bin.png .bin .hex
316 AddIcon /theme/icons/bmp.png .bmp
317 AddIcon /theme/icons/c.png .c
318 AddIcon /theme/icons/calc.png .xlsx .xlsm .xltx .xltm .xlam .xlr .xls .csv
319 AddIcon /theme/icons/cd.png .iso
320 AddIcon /theme/icons/cpp.png .cpp
321 AddIcon /theme/icons/css.png .css .sass .scss
322 AddIcon /theme/icons/deb.png .deb
323 AddIcon /theme/icons/doc.png .doc .docx .docm .dot .dotx .dotm .log .msg .odt .pages .rtf .tex .wpd .wps
324 AddIcon /theme/icons/draw.png .svg .svgz
325 AddIcon /theme/icons/eps.png .ai .eps
326 AddIcon /theme/icons/exe.png .exe
327 AddIcon /theme/icons/gif.png .gif
328 AddIcon /theme/icons/h.png .h
329 AddIcon /theme/icons/html.png .html .xhtml .shtml .htm .URL .url
330 AddIcon /theme/icons/ico.png .ico
331 AddIcon /theme/icons/java.png .jar
332 AddIcon /theme/icons/jpg.png .jpg .jpeg .jpe
333 AddIcon /theme/icons/js.png .js .json
334 AddIcon /theme/icons/markdown.png .md
335 AddIcon /theme/icons/package.png .pkg .dmg
336 AddIcon /theme/icons/pdf.png .pdf
337 AddIcon /theme/icons/php.png .php .phtml
338 AddIcon /theme/icons/playlist.png .m3u .m3u8 .pls .pls8
339 AddIcon /theme/icons/png.png .png
340 AddIcon /theme/icons/ps.png .ps
341 AddIcon /theme/icons/psd.png .psd
342 AddIcon /theme/icons/py.png .py
343 AddIcon /theme/icons/rar.png .rar
344 AddIcon /theme/icons/rb.png .rb
345 AddIcon /theme/icons/rpm.png .rpm
346 AddIcon /theme/icons/rss.png .rss
347 AddIcon /theme/icons/script.png .bat .cmd .sh
348 AddIcon /theme/icons/sql.png .sql
349 AddIcon /theme/icons/tiff.png .tiff .tif
350 AddIcon /theme/icons/text.png .txt .nfo
351 AddIcon /theme/icons/video.png .asf .asx .avi .flv .mkv .mov .mp4 .mpg .rm .srt .swf .vob .wmv .m4v .f4v .f4p .ogv
352 AddIcon /theme/icons/xml.png .xml
353 AddIcon /theme/icons/zip.png .zip
354 DefaultIcon /theme/icons/default.png
355
356 HeaderName /theme/header.html
357 ReadmeName /theme/footer.html
358 IndexStyleSheet /theme/style.css
359 </Directory>
360 </Macro>
361 '';
362 };
363 http2 = {
364 modules = [ "http2" ];
365 extraConfig = ''
366 Protocols h2 http/1.1
367 '';
368 };
369 customLog = {
370 extraConfig = ''
371 LogFormat "%v:%p %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combinedVhost
372 '';
373 };
374 };
375
376 system.activationScripts = {
377 httpd = ''
378 install -d -m 0755 /var/lib/acme/acme-challenge
379 install -d -m 0750 -o wwwrun -g wwwrun /var/lib/php/sessions
380 install -d -m 0750 -o wwwrun -g wwwrun /var/lib/php/sessions/adminer
381 install -d -m 0750 -o wwwrun -g wwwrun /var/lib/php/sessions/mantisbt
382 install -d -m 0750 -o wwwrun -g wwwrun /var/lib/php/sessions/davical
383 '';
384 };
385
386 services.myPhpfpm = {
387 phpPackage = pkgs.php;
388 phpOptions = ''
389 session.save_path = "/var/lib/php/sessions"
390 session.gc_maxlifetime = 60*60*24*15
391 session.cache_expire = 60*24*30
392 '';
393 extraConfig = ''
394 log_level = notice
395 '';
396 };
397
398 services.httpdProd = makeService "production" config.services.myWebsites.production;
399 services.myWebsites.production.modules = makeModules cfg;
400 services.myWebsites.production.extraConfig = makeExtraConfig cfg;
401
402 services.httpdInte = makeService "integration" config.services.myWebsites.integration;
403 services.myWebsites.integration.modules = makeModules cfg;
404 services.myWebsites.integration.extraConfig = makeExtraConfig cfg;
405
406 services.httpdTools = makeService "tools" config.services.myWebsites.tools;
407 services.myWebsites.tools.modules = makeModules cfg;
408 services.myWebsites.tools.extraConfig = makeExtraConfig cfg ++
409 [ ''
410 RedirectMatch ^/licen[cs]es?_et_tip(ping)?$ https://www.immae.eu/licences_et_tip.html
411 RedirectMatch ^/licen[cs]es?_and_tip(ping)?$ https://www.immae.eu/licenses_and_tipping.html
412 RedirectMatch ^/licen[cs]es?$ https://www.immae.eu/licenses_and_tipping.html
413 RedirectMatch ^/tip(ping)?$ https://www.immae.eu/licenses_and_tipping.html
414 RedirectMatch ^/(mentions|mentions_legales|legal)$ https://www.immae.eu/mentions.html
415 RedirectMatch ^/CGU$ https://www.immae.eu/CGU
416 ''
417 ]
418 ;
419 };
420 }