]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - virtual/modules/websites/default.nix
Fix ludivine's website
[perso/Immae/Config/Nix.git] / virtual / modules / websites / default.nix
CommitLineData
f8bde3d6 1{ lib, pkgs, config, mylibs, myconfig, ... }:
42429ef0 2let
950ca5ee
IB
3 mypkgs = pkgs.callPackage ../../packages.nix {
4 inherit (mylibs) checkEnv fetchedGit fetchedGithub;
5 };
42429ef0 6 cfg = config.services.myWebsites;
f8bde3d6
IB
7 makeService = name: cfg: let
8 toVhost = vhostConf: {
9 enableSSL = true;
10 sslServerCert = "/var/lib/acme/${vhostConf.certName}/cert.pem";
11 sslServerKey = "/var/lib/acme/${vhostConf.certName}/key.pem";
12 sslServerChain = "/var/lib/acme/${vhostConf.certName}/fullchain.pem";
13 logFormat = "combinedVhost";
14 listen = [
15 { ip = cfg.ip; port = 443; }
16 ];
17 hostName = builtins.head vhostConf.hosts;
18 serverAliases = builtins.tail vhostConf.hosts or [];
19 documentRoot = vhostConf.root;
20 extraConfig = builtins.concatStringsSep "\n" vhostConf.extraConfig;
21 };
950ca5ee
IB
22 redirectVhost = { # Should go last, catchall http -> https redirect
23 listen = [ { ip = cfg.ip; port = 80; } ];
24 hostName = "redirectSSL";
25 serverAliases = [ "*" ];
26 enableSSL = false;
27 logFormat = "combinedVhost";
28 documentRoot = "/var/lib/acme/acme-challenge";
29 extraConfig = ''
30 RewriteEngine on
31 RewriteCond "%{REQUEST_URI}" "!^/\.well-known"
32 RewriteRule ^(.+) https://%{HTTP_HOST}$1 [R=301]
33 # To redirect in specific "VirtualHost *:80", do
34 # RedirectMatch 301 ^/((?!\.well-known.*$).*)$ https://host/$1
35 # rather than rewrite
36 '';
37 };
38 fallbackVhost = toVhost { # Should go first, default choice
39 certName = "eldiron";
40 hosts = ["eldiron.immae.eu" ];
41 root = ../../www;
42 extraConfig = [ "DirectoryIndex index.htm" ];
43 };
f8bde3d6
IB
44 in rec {
45 enable = true;
46 listen = [
47 { ip = cfg.ip; port = 443; }
48 ];
49 stateDir = "/run/httpd_${name}";
50 logPerVirtualHost = true;
51 multiProcessingModule = "worker";
52 adminAddr = "httpd@immae.eu";
53 logFormat = "combinedVhost";
54 extraModules = pkgs.lib.lists.unique (pkgs.lib.lists.flatten cfg.modules);
55 extraConfig = builtins.concatStringsSep "\n" cfg.extraConfig;
950ca5ee
IB
56 virtualHosts = [ fallbackVhost ]
57 ++ (pkgs.lib.attrsets.mapAttrsToList (n: v: toVhost v) cfg.vhostConfs)
58 ++ [ redirectVhost ];
f8bde3d6
IB
59 };
60 makeServiceOptions = name: ip: {
61 enable = lib.mkEnableOption "enable websites in ${name}";
62 ip = lib.mkOption {
63 type = lib.types.string;
64 default = ip;
65 description = "${name} ip to listen to";
66 };
67 modules = lib.mkOption {
68 type = lib.types.listOf (lib.types.str);
69 default = [];
70 };
71 extraConfig = lib.mkOption {
72 type = lib.types.listOf (lib.types.lines);
73 default = [];
74 };
75 vhostConfs = lib.mkOption {
76 type = lib.types.attrsOf (lib.types.submodule {
77 options = {
78 certName = lib.mkOption { type = lib.types.string; };
79 hosts = lib.mkOption { type = lib.types.listOf lib.types.string; };
80 root = lib.mkOption { type = lib.types.nullOr lib.types.path; };
81 extraConfig = lib.mkOption { type = lib.types.listOf lib.types.lines; default = []; };
82 };
83 });
84 };
85 };
42429ef0
IB
86in
87{
88 imports = [
54307da4
IB
89 ./chloe
90 ./ludivine
91 ./aten
92 ./piedsjaloux
93 ./connexionswing
2f0f1c48 94 ./tellesflorian
10889174
IB
95 ./tools/db
96 ./tools/tools
97 ./tools/dav
98 ./tools/cloud
99 ./tools/git
35a397cd 100 ./tools/mastodon
f8bde3d6
IB
101 # built using:
102 # sed -e "s/services\.httpd/services\.httpdProd/g" .nix-defexpr/channels/nixpkgs/nixos/modules/services/web-servers/apache-httpd/default.nix
273e2c61 103 # Removed allGranted
f8bde3d6 104 # And removed users / groups
54307da4
IB
105 ./apache/httpd_prod.nix
106 ./apache/httpd_inte.nix
273e2c61
IB
107 # except for this one for users/groups
108 ./apache/httpd_tools.nix
10889174
IB
109 # Adapted from base phpfpm
110 ./phpfpm
42429ef0
IB
111 ];
112
113 options.services.myWebsites = {
f8bde3d6
IB
114 production = makeServiceOptions "production" myconfig.ips.production;
115 integration = makeServiceOptions "integration" myconfig.ips.integration;
950ca5ee 116 tools = makeServiceOptions "tools" myconfig.ips.main;
42429ef0
IB
117
118 apacheConfig = lib.mkOption {
119 type = lib.types.attrsOf (lib.types.submodule {
120 options = {
121 modules = lib.mkOption {
122 type = lib.types.listOf (lib.types.str);
123 default = [];
124 };
125 extraConfig = lib.mkOption {
126 type = lib.types.nullOr lib.types.lines;
127 default = null;
128 };
129 };
130 });
131 default = {};
132 description = "Extra global config";
133 };
134
135 };
136
137 config = {
54307da4
IB
138 networking = {
139 firewall = {
140 enable = true;
141 allowedTCPPorts = [ 80 443 ];
142 };
143 interfaces."eth0".ipv4.addresses = [
144 # 176.9.151.89 declared in nixops -> infra / tools
145 { address = myconfig.ips.production; prefixLength = 32; }
146 { address = myconfig.ips.integration; prefixLength = 32; }
147 ];
148 };
149
98584540
IB
150 nixpkgs.config.packageOverrides = oldpkgs: rec {
151 php = php72;
152 php72 = (oldpkgs.php72.override {
153 mysql.connector-c = pkgs.mariadb;
154 config.php.mysqlnd = false;
155 config.php.mysqli = false;
156 }).overrideAttrs(old: rec {
157 # Didn't manage to build with mysqli + mysql_config connector
158 configureFlags = old.configureFlags ++ [
912921a7 159 "--with-mysqli=shared,mysqlnd"
98584540
IB
160 ];
161 # preConfigure = (old.preConfigure or "") + ''
162 # export CPPFLAGS="$CPPFLAGS -I${pkgs.mariadb}/include/mysql/server";
163 # sed -i -e 's/#include "mysqli_priv.h"/#include "mysqli_priv.h"\n#include <mysql_version.h>/' \
164 # ext/mysqli/mysqli.c ext/mysqli/mysqli_prop.c
165 # '';
166 });
167 phpPackages = oldpkgs.php72Packages.override { inherit php; };
168 };
169
10889174
IB
170 services.myWebsites.tools.databases.enable = true;
171 services.myWebsites.tools.tools.enable = true;
172 services.myWebsites.tools.dav.enable = true;
173 services.myWebsites.tools.cloud.enable = true;
174 services.myWebsites.tools.git.enable = true;
35a397cd 175 services.myWebsites.tools.mastodon.enable = true;
10889174 176
42429ef0
IB
177 services.myWebsites.Chloe.production.enable = cfg.production.enable;
178 services.myWebsites.Ludivine.production.enable = cfg.production.enable;
179 services.myWebsites.Aten.production.enable = cfg.production.enable;
180 services.myWebsites.PiedsJaloux.production.enable = cfg.production.enable;
181 services.myWebsites.Connexionswing.production.enable = cfg.production.enable;
182
183 services.myWebsites.Chloe.integration.enable = cfg.integration.enable;
184 services.myWebsites.Ludivine.integration.enable = cfg.integration.enable;
185 services.myWebsites.Aten.integration.enable = cfg.integration.enable;
186 services.myWebsites.PiedsJaloux.integration.enable = cfg.integration.enable;
187 services.myWebsites.Connexionswing.integration.enable = cfg.integration.enable;
2f0f1c48 188 services.myWebsites.TellesFlorian.integration.enable = true;
42429ef0
IB
189
190 services.myWebsites.apacheConfig = {
191 gzip = {
192 modules = [ "deflate" "filter" ];
193 extraConfig = ''
194 AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
195 '';
196 };
197 macros = {
198 modules = [ "macro" ];
199 };
200 ldap = {
201 modules = [ "ldap" "authnz_ldap" ];
202 # FIXME: starttls
203 extraConfig = assert mylibs.checkEnv "NIXOPS_HTTP_LDAP_PASSWORD"; ''
204 <IfModule ldap_module>
205 LDAPSharedCacheSize 500000
206 LDAPCacheEntries 1024
207 LDAPCacheTTL 600
208 LDAPOpCacheEntries 1024
209 LDAPOpCacheTTL 600
210 </IfModule>
211
212 <Macro LDAPConnect>
213 <IfModule authnz_ldap_module>
214 AuthLDAPURL ldap://ldap.immae.eu:389/dc=immae,dc=eu STARTTLS
215 AuthLDAPBindDN cn=httpd,ou=services,dc=immae,dc=eu
216 AuthLDAPBindPassword "${builtins.getEnv "NIXOPS_HTTP_LDAP_PASSWORD"}"
217 AuthType Basic
218 AuthName "Authentification requise (Acces LDAP)"
219 AuthBasicProvider ldap
220 </IfModule>
221 </Macro>
222
223 <Macro Stats %{domain}>
224 Alias /awstats /var/lib/goaccess/%{domain}
225 <Directory /var/lib/goaccess/%{domain}>
226 DirectoryIndex index.html
227 AllowOverride None
228 Require all granted
229 </Directory>
230 <Location /awstats>
231 Use LDAPConnect
232 Require ldap-group cn=%{domain},ou=stats,cn=httpd,ou=services,dc=immae,dc=eu
233 </Location>
234 </Macro>
646bdf3e
IB
235
236 ErrorDocument 500 /maintenance_immae.html
237 ErrorDocument 501 /maintenance_immae.html
238 ErrorDocument 502 /maintenance_immae.html
239 ErrorDocument 503 /maintenance_immae.html
240 ErrorDocument 504 /maintenance_immae.html
241 Alias /maintenance_immae.html ${../../www}/maintenance_immae.html
242 ProxyPass /maintenance_immae.html !
243
244 AliasMatch "(.*)/googleb6d69446ff4ca3e5.html" ${../../www}/googleb6d69446ff4ca3e5.html
42429ef0
IB
245 '';
246 };
247 http2 = {
248 modules = [ "http2" ];
249 extraConfig = ''
250 Protocols h2 http/1.1
251 '';
252 };
253 customLog = {
254 extraConfig = ''
255 LogFormat "%v:%p %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combinedVhost
256 '';
257 };
258 };
f8bde3d6 259
10889174
IB
260 system.activationScripts = {
261 httpd = ''
262 install -d -m 0755 /var/lib/acme/acme-challenge
263 install -d -m 0750 -o wwwrun -g wwwrun /var/lib/php/sessions
264 install -d -m 0750 -o wwwrun -g wwwrun /var/lib/php/sessions/adminer
265 install -d -m 0750 -o wwwrun -g wwwrun /var/lib/php/sessions/mantisbt
266 install -d -m 0750 -o wwwrun -g wwwrun /var/lib/php/sessions/davical
267 '';
268 };
269
270 services.myPhpfpm = {
271 phpPackage = pkgs.php;
272 phpOptions = ''
273 session.save_path = "/var/lib/php/sessions"
274 session.gc_maxlifetime = 60*60*24*15
275 session.cache_expire = 60*24*30
276 '';
277 extraConfig = ''
278 log_level = notice
279 '';
280 };
281
f8bde3d6
IB
282 # FIXME: logrotate
283 # FIXME: ipv6
284 services.httpdProd = makeService "production" config.services.myWebsites.production;
285 services.myWebsites.production.modules = pkgs.lib.lists.flatten (pkgs.lib.attrsets.mapAttrsToList (n: v: v.modules or []) cfg.apacheConfig);
286 services.myWebsites.production.extraConfig = (builtins.filter (x: x != null) (pkgs.lib.attrsets.mapAttrsToList (n: v: v.extraConfig or null) cfg.apacheConfig));
287
288 services.httpdInte = makeService "integration" config.services.myWebsites.integration;
289 services.myWebsites.integration.modules = pkgs.lib.lists.flatten (pkgs.lib.attrsets.mapAttrsToList (n: v: v.modules or []) cfg.apacheConfig);
290 services.myWebsites.integration.extraConfig = (builtins.filter (x: x != null) (pkgs.lib.attrsets.mapAttrsToList (n: v: v.extraConfig or null) cfg.apacheConfig));
950ca5ee 291
273e2c61 292 services.httpdTools = makeService "tools" config.services.myWebsites.tools;
10889174 293 services.myWebsites.tools.modules = pkgs.lib.lists.flatten (pkgs.lib.attrsets.mapAttrsToList (n: v: v.modules or []) cfg.apacheConfig);
646bdf3e
IB
294 services.myWebsites.tools.extraConfig = (builtins.filter (x: x != null) (pkgs.lib.attrsets.mapAttrsToList (n: v: v.extraConfig or null) cfg.apacheConfig)) ++
295 [ ''
296 RedirectMatch ^/licen[cs]es?_et_tip(ping)?$ https://www.immae.eu/licences_et_tip.html
297 RedirectMatch ^/licen[cs]es?_and_tip(ping)?$ https://www.immae.eu/licenses_and_tipping.html
298 RedirectMatch ^/licen[cs]es?$ https://www.immae.eu/licenses_and_tipping.html
299 RedirectMatch ^/tip(ping)?$ https://www.immae.eu/licenses_and_tipping.html
300 RedirectMatch ^/(mentions|mentions_legales|legal)$ https://www.immae.eu/mentions.html
301 RedirectMatch ^/CGU$ https://www.immae.eu/CGU
302 ''
303 ]
304 ;
42429ef0
IB
305 };
306}