]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - virtual/modules/websites/default.nix
Add SSL for pam ldap connection
[perso/Immae/Config/Nix.git] / virtual / modules / websites / default.nix
CommitLineData
f8bde3d6 1{ lib, pkgs, config, mylibs, myconfig, ... }:
42429ef0
IB
2let
3 cfg = config.services.myWebsites;
f8bde3d6
IB
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 };
950ca5ee
IB
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 };
f8bde3d6
IB
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;
950ca5ee
IB
53 virtualHosts = [ fallbackVhost ]
54 ++ (pkgs.lib.attrsets.mapAttrsToList (n: v: toVhost v) cfg.vhostConfs)
55 ++ [ redirectVhost ];
f8bde3d6
IB
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 };
f8d3b61a
IB
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));
42429ef0
IB
85in
86{
87 imports = [
54307da4
IB
88 ./chloe
89 ./ludivine
90 ./aten
91 ./piedsjaloux
92 ./connexionswing
2f0f1c48 93 ./tellesflorian
10889174
IB
94 ./tools/db
95 ./tools/tools
96 ./tools/dav
97 ./tools/cloud
98 ./tools/git
35a397cd 99 ./tools/mastodon
56eba416 100 ./tools/mediagoblin
a7f7fdae 101 ./tools/diaspora
f8bde3d6
IB
102 # built using:
103 # sed -e "s/services\.httpd/services\.httpdProd/g" .nix-defexpr/channels/nixpkgs/nixos/modules/services/web-servers/apache-httpd/default.nix
273e2c61 104 # Removed allGranted
f8bde3d6 105 # And removed users / groups
54307da4
IB
106 ./apache/httpd_prod.nix
107 ./apache/httpd_inte.nix
273e2c61
IB
108 # except for this one for users/groups
109 ./apache/httpd_tools.nix
10889174
IB
110 # Adapted from base phpfpm
111 ./phpfpm
42429ef0
IB
112 ];
113
114 options.services.myWebsites = {
f8bde3d6
IB
115 production = makeServiceOptions "production" myconfig.ips.production;
116 integration = makeServiceOptions "integration" myconfig.ips.integration;
950ca5ee 117 tools = makeServiceOptions "tools" myconfig.ips.main;
42429ef0
IB
118
119 apacheConfig = lib.mkOption {
120 type = lib.types.attrsOf (lib.types.submodule {
121 options = {
122 modules = lib.mkOption {
123 type = lib.types.listOf (lib.types.str);
124 default = [];
125 };
126 extraConfig = lib.mkOption {
127 type = lib.types.nullOr lib.types.lines;
128 default = null;
129 };
130 };
131 });
132 default = {};
133 description = "Extra global config";
134 };
135
136 };
137
138 config = {
54307da4
IB
139 networking = {
140 firewall = {
141 enable = true;
142 allowedTCPPorts = [ 80 443 ];
143 };
144 interfaces."eth0".ipv4.addresses = [
145 # 176.9.151.89 declared in nixops -> infra / tools
146 { address = myconfig.ips.production; prefixLength = 32; }
147 { address = myconfig.ips.integration; prefixLength = 32; }
148 ];
149 };
150
98584540
IB
151 nixpkgs.config.packageOverrides = oldpkgs: rec {
152 php = php72;
153 php72 = (oldpkgs.php72.override {
154 mysql.connector-c = pkgs.mariadb;
155 config.php.mysqlnd = false;
156 config.php.mysqli = false;
157 }).overrideAttrs(old: rec {
158 # Didn't manage to build with mysqli + mysql_config connector
159 configureFlags = old.configureFlags ++ [
912921a7 160 "--with-mysqli=shared,mysqlnd"
98584540
IB
161 ];
162 # preConfigure = (old.preConfigure or "") + ''
163 # export CPPFLAGS="$CPPFLAGS -I${pkgs.mariadb}/include/mysql/server";
164 # sed -i -e 's/#include "mysqli_priv.h"/#include "mysqli_priv.h"\n#include <mysql_version.h>/' \
165 # ext/mysqli/mysqli.c ext/mysqli/mysqli_prop.c
166 # '';
167 });
168 phpPackages = oldpkgs.php72Packages.override { inherit php; };
d78128ec
IB
169 composerEnv = import ./commons/composer-env.nix {
170 inherit (pkgs) stdenv writeTextFile fetchurl php unzip;
171 };
98584540
IB
172 };
173
10889174
IB
174 services.myWebsites.tools.databases.enable = true;
175 services.myWebsites.tools.tools.enable = true;
176 services.myWebsites.tools.dav.enable = true;
177 services.myWebsites.tools.cloud.enable = true;
178 services.myWebsites.tools.git.enable = true;
35a397cd 179 services.myWebsites.tools.mastodon.enable = true;
56eba416 180 services.myWebsites.tools.mediagoblin.enable = true;
a7f7fdae 181 services.myWebsites.tools.diaspora.enable = true;
10889174 182
42429ef0
IB
183 services.myWebsites.Chloe.production.enable = cfg.production.enable;
184 services.myWebsites.Ludivine.production.enable = cfg.production.enable;
185 services.myWebsites.Aten.production.enable = cfg.production.enable;
186 services.myWebsites.PiedsJaloux.production.enable = cfg.production.enable;
187 services.myWebsites.Connexionswing.production.enable = cfg.production.enable;
188
189 services.myWebsites.Chloe.integration.enable = cfg.integration.enable;
190 services.myWebsites.Ludivine.integration.enable = cfg.integration.enable;
191 services.myWebsites.Aten.integration.enable = cfg.integration.enable;
192 services.myWebsites.PiedsJaloux.integration.enable = cfg.integration.enable;
193 services.myWebsites.Connexionswing.integration.enable = cfg.integration.enable;
2f0f1c48 194 services.myWebsites.TellesFlorian.integration.enable = true;
42429ef0
IB
195
196 services.myWebsites.apacheConfig = {
197 gzip = {
198 modules = [ "deflate" "filter" ];
199 extraConfig = ''
200 AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
201 '';
202 };
203 macros = {
204 modules = [ "macro" ];
205 };
206 ldap = {
207 modules = [ "ldap" "authnz_ldap" ];
9d90e7e2 208 extraConfig = ''
42429ef0
IB
209 <IfModule ldap_module>
210 LDAPSharedCacheSize 500000
211 LDAPCacheEntries 1024
212 LDAPCacheTTL 600
213 LDAPOpCacheEntries 1024
214 LDAPOpCacheTTL 600
215 </IfModule>
216
217 <Macro LDAPConnect>
218 <IfModule authnz_ldap_module>
219 AuthLDAPURL ldap://ldap.immae.eu:389/dc=immae,dc=eu STARTTLS
220 AuthLDAPBindDN cn=httpd,ou=services,dc=immae,dc=eu
9d90e7e2 221 AuthLDAPBindPassword "${myconfig.env.httpd.ldap.password}"
42429ef0
IB
222 AuthType Basic
223 AuthName "Authentification requise (Acces LDAP)"
224 AuthBasicProvider ldap
225 </IfModule>
226 </Macro>
227
228 <Macro Stats %{domain}>
229 Alias /awstats /var/lib/goaccess/%{domain}
230 <Directory /var/lib/goaccess/%{domain}>
231 DirectoryIndex index.html
232 AllowOverride None
233 Require all granted
234 </Directory>
235 <Location /awstats>
236 Use LDAPConnect
237 Require ldap-group cn=%{domain},ou=stats,cn=httpd,ou=services,dc=immae,dc=eu
238 </Location>
239 </Macro>
646bdf3e
IB
240
241 ErrorDocument 500 /maintenance_immae.html
242 ErrorDocument 501 /maintenance_immae.html
243 ErrorDocument 502 /maintenance_immae.html
244 ErrorDocument 503 /maintenance_immae.html
245 ErrorDocument 504 /maintenance_immae.html
246 Alias /maintenance_immae.html ${../../www}/maintenance_immae.html
247 ProxyPass /maintenance_immae.html !
248
249 AliasMatch "(.*)/googleb6d69446ff4ca3e5.html" ${../../www}/googleb6d69446ff4ca3e5.html
42429ef0
IB
250 '';
251 };
252 http2 = {
253 modules = [ "http2" ];
254 extraConfig = ''
255 Protocols h2 http/1.1
256 '';
257 };
258 customLog = {
259 extraConfig = ''
260 LogFormat "%v:%p %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combinedVhost
261 '';
262 };
263 };
f8bde3d6 264
10889174
IB
265 system.activationScripts = {
266 httpd = ''
267 install -d -m 0755 /var/lib/acme/acme-challenge
268 install -d -m 0750 -o wwwrun -g wwwrun /var/lib/php/sessions
269 install -d -m 0750 -o wwwrun -g wwwrun /var/lib/php/sessions/adminer
270 install -d -m 0750 -o wwwrun -g wwwrun /var/lib/php/sessions/mantisbt
271 install -d -m 0750 -o wwwrun -g wwwrun /var/lib/php/sessions/davical
272 '';
273 };
274
275 services.myPhpfpm = {
276 phpPackage = pkgs.php;
277 phpOptions = ''
278 session.save_path = "/var/lib/php/sessions"
279 session.gc_maxlifetime = 60*60*24*15
280 session.cache_expire = 60*24*30
281 '';
282 extraConfig = ''
283 log_level = notice
284 '';
285 };
286
f8bde3d6 287 services.httpdProd = makeService "production" config.services.myWebsites.production;
f8d3b61a
IB
288 services.myWebsites.production.modules = makeModules cfg;
289 services.myWebsites.production.extraConfig = makeExtraConfig cfg;
f8bde3d6
IB
290
291 services.httpdInte = makeService "integration" config.services.myWebsites.integration;
f8d3b61a
IB
292 services.myWebsites.integration.modules = makeModules cfg;
293 services.myWebsites.integration.extraConfig = makeExtraConfig cfg;
950ca5ee 294
273e2c61 295 services.httpdTools = makeService "tools" config.services.myWebsites.tools;
f8d3b61a
IB
296 services.myWebsites.tools.modules = makeModules cfg;
297 services.myWebsites.tools.extraConfig = makeExtraConfig cfg ++
646bdf3e
IB
298 [ ''
299 RedirectMatch ^/licen[cs]es?_et_tip(ping)?$ https://www.immae.eu/licences_et_tip.html
300 RedirectMatch ^/licen[cs]es?_and_tip(ping)?$ https://www.immae.eu/licenses_and_tipping.html
301 RedirectMatch ^/licen[cs]es?$ https://www.immae.eu/licenses_and_tipping.html
302 RedirectMatch ^/tip(ping)?$ https://www.immae.eu/licenses_and_tipping.html
303 RedirectMatch ^/(mentions|mentions_legales|legal)$ https://www.immae.eu/mentions.html
304 RedirectMatch ^/CGU$ https://www.immae.eu/CGU
305 ''
306 ]
307 ;
42429ef0
IB
308 };
309}