]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - virtual/modules/websites/default.nix
Add highlight for gitweb
[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
103 # And removed users / groups
54307da4
IB
104 ./apache/httpd_prod.nix
105 ./apache/httpd_inte.nix
10889174
IB
106 # Adapted from base phpfpm
107 ./phpfpm
42429ef0
IB
108 ];
109
110 options.services.myWebsites = {
f8bde3d6
IB
111 production = makeServiceOptions "production" myconfig.ips.production;
112 integration = makeServiceOptions "integration" myconfig.ips.integration;
950ca5ee 113 tools = makeServiceOptions "tools" myconfig.ips.main;
42429ef0
IB
114
115 apacheConfig = lib.mkOption {
116 type = lib.types.attrsOf (lib.types.submodule {
117 options = {
118 modules = lib.mkOption {
119 type = lib.types.listOf (lib.types.str);
120 default = [];
121 };
122 extraConfig = lib.mkOption {
123 type = lib.types.nullOr lib.types.lines;
124 default = null;
125 };
126 };
127 });
128 default = {};
129 description = "Extra global config";
130 };
131
132 };
133
134 config = {
54307da4
IB
135 networking = {
136 firewall = {
137 enable = true;
138 allowedTCPPorts = [ 80 443 ];
139 };
140 interfaces."eth0".ipv4.addresses = [
141 # 176.9.151.89 declared in nixops -> infra / tools
142 { address = myconfig.ips.production; prefixLength = 32; }
143 { address = myconfig.ips.integration; prefixLength = 32; }
144 ];
145 };
146
98584540
IB
147 nixpkgs.config.packageOverrides = oldpkgs: rec {
148 php = php72;
149 php72 = (oldpkgs.php72.override {
150 mysql.connector-c = pkgs.mariadb;
151 config.php.mysqlnd = false;
152 config.php.mysqli = false;
153 }).overrideAttrs(old: rec {
154 # Didn't manage to build with mysqli + mysql_config connector
155 configureFlags = old.configureFlags ++ [
912921a7 156 "--with-mysqli=shared,mysqlnd"
98584540
IB
157 ];
158 # preConfigure = (old.preConfigure or "") + ''
159 # export CPPFLAGS="$CPPFLAGS -I${pkgs.mariadb}/include/mysql/server";
160 # sed -i -e 's/#include "mysqli_priv.h"/#include "mysqli_priv.h"\n#include <mysql_version.h>/' \
161 # ext/mysqli/mysqli.c ext/mysqli/mysqli_prop.c
162 # '';
163 });
164 phpPackages = oldpkgs.php72Packages.override { inherit php; };
165 };
166
10889174
IB
167 services.myWebsites.tools.databases.enable = true;
168 services.myWebsites.tools.tools.enable = true;
169 services.myWebsites.tools.dav.enable = true;
170 services.myWebsites.tools.cloud.enable = true;
171 services.myWebsites.tools.git.enable = true;
35a397cd 172 services.myWebsites.tools.mastodon.enable = true;
10889174 173
42429ef0
IB
174 services.myWebsites.Chloe.production.enable = cfg.production.enable;
175 services.myWebsites.Ludivine.production.enable = cfg.production.enable;
176 services.myWebsites.Aten.production.enable = cfg.production.enable;
177 services.myWebsites.PiedsJaloux.production.enable = cfg.production.enable;
178 services.myWebsites.Connexionswing.production.enable = cfg.production.enable;
179
180 services.myWebsites.Chloe.integration.enable = cfg.integration.enable;
181 services.myWebsites.Ludivine.integration.enable = cfg.integration.enable;
182 services.myWebsites.Aten.integration.enable = cfg.integration.enable;
183 services.myWebsites.PiedsJaloux.integration.enable = cfg.integration.enable;
184 services.myWebsites.Connexionswing.integration.enable = cfg.integration.enable;
2f0f1c48 185 services.myWebsites.TellesFlorian.integration.enable = true;
42429ef0
IB
186
187 services.myWebsites.apacheConfig = {
188 gzip = {
189 modules = [ "deflate" "filter" ];
190 extraConfig = ''
191 AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
192 '';
193 };
194 macros = {
195 modules = [ "macro" ];
196 };
197 ldap = {
198 modules = [ "ldap" "authnz_ldap" ];
199 # FIXME: starttls
200 extraConfig = assert mylibs.checkEnv "NIXOPS_HTTP_LDAP_PASSWORD"; ''
201 <IfModule ldap_module>
202 LDAPSharedCacheSize 500000
203 LDAPCacheEntries 1024
204 LDAPCacheTTL 600
205 LDAPOpCacheEntries 1024
206 LDAPOpCacheTTL 600
207 </IfModule>
208
209 <Macro LDAPConnect>
210 <IfModule authnz_ldap_module>
211 AuthLDAPURL ldap://ldap.immae.eu:389/dc=immae,dc=eu STARTTLS
212 AuthLDAPBindDN cn=httpd,ou=services,dc=immae,dc=eu
213 AuthLDAPBindPassword "${builtins.getEnv "NIXOPS_HTTP_LDAP_PASSWORD"}"
214 AuthType Basic
215 AuthName "Authentification requise (Acces LDAP)"
216 AuthBasicProvider ldap
217 </IfModule>
218 </Macro>
219
220 <Macro Stats %{domain}>
221 Alias /awstats /var/lib/goaccess/%{domain}
222 <Directory /var/lib/goaccess/%{domain}>
223 DirectoryIndex index.html
224 AllowOverride None
225 Require all granted
226 </Directory>
227 <Location /awstats>
228 Use LDAPConnect
229 Require ldap-group cn=%{domain},ou=stats,cn=httpd,ou=services,dc=immae,dc=eu
230 </Location>
231 </Macro>
232 '';
233 };
234 http2 = {
235 modules = [ "http2" ];
236 extraConfig = ''
237 Protocols h2 http/1.1
238 '';
239 };
240 customLog = {
241 extraConfig = ''
242 LogFormat "%v:%p %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combinedVhost
243 '';
244 };
245 };
f8bde3d6 246
10889174
IB
247 system.activationScripts = {
248 httpd = ''
249 install -d -m 0755 /var/lib/acme/acme-challenge
250 install -d -m 0750 -o wwwrun -g wwwrun /var/lib/php/sessions
251 install -d -m 0750 -o wwwrun -g wwwrun /var/lib/php/sessions/adminer
252 install -d -m 0750 -o wwwrun -g wwwrun /var/lib/php/sessions/mantisbt
253 install -d -m 0750 -o wwwrun -g wwwrun /var/lib/php/sessions/davical
254 '';
255 };
256
257 services.myPhpfpm = {
258 phpPackage = pkgs.php;
259 phpOptions = ''
260 session.save_path = "/var/lib/php/sessions"
261 session.gc_maxlifetime = 60*60*24*15
262 session.cache_expire = 60*24*30
263 '';
264 extraConfig = ''
265 log_level = notice
266 '';
267 };
268
f8bde3d6
IB
269 # FIXME: logrotate
270 # FIXME: ipv6
271 services.httpdProd = makeService "production" config.services.myWebsites.production;
272 services.myWebsites.production.modules = pkgs.lib.lists.flatten (pkgs.lib.attrsets.mapAttrsToList (n: v: v.modules or []) cfg.apacheConfig);
273 services.myWebsites.production.extraConfig = (builtins.filter (x: x != null) (pkgs.lib.attrsets.mapAttrsToList (n: v: v.extraConfig or null) cfg.apacheConfig));
274
275 services.httpdInte = makeService "integration" config.services.myWebsites.integration;
276 services.myWebsites.integration.modules = pkgs.lib.lists.flatten (pkgs.lib.attrsets.mapAttrsToList (n: v: v.modules or []) cfg.apacheConfig);
277 services.myWebsites.integration.extraConfig = (builtins.filter (x: x != null) (pkgs.lib.attrsets.mapAttrsToList (n: v: v.extraConfig or null) cfg.apacheConfig));
950ca5ee
IB
278
279 services.httpd = makeService "tools" config.services.myWebsites.tools;
10889174 280 services.myWebsites.tools.modules = pkgs.lib.lists.flatten (pkgs.lib.attrsets.mapAttrsToList (n: v: v.modules or []) cfg.apacheConfig);
950ca5ee 281 services.myWebsites.tools.extraConfig = (builtins.filter (x: x != null) (pkgs.lib.attrsets.mapAttrsToList (n: v: v.extraConfig or null) cfg.apacheConfig));
42429ef0
IB
282 };
283}