]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - virtual/eldiron.nix
modularize apache configuration
[perso/Immae/Config/Nix.git] / virtual / eldiron.nix
1 {
2 network = {
3 description = "Immae's network";
4 enableRollback = true;
5 };
6
7 eldiron = { config, pkgs, ... }:
8 let mypkgs = import ./packages.nix;
9 mylibs = import ../libs.nix;
10 in
11 {
12 networking = {
13 firewall = {
14 enable = true;
15 allowedTCPPorts = [ 22 80 443 3306 5432 ];
16 };
17 };
18
19 deployment = {
20 targetEnv = "hetzner";
21 hetzner = {
22 #robotUser = "defined in HETZNER_ROBOT_USER";
23 #robotPass = "defined in HETZNER_ROBOT_PASS";
24 mainIPv4 = "176.9.151.89";
25 partitions = ''
26 clearpart --all --initlabel --drives=sda,sdb
27
28 part swap1 --recommended --label=swap1 --fstype=swap --ondisk=sda
29 part swap2 --recommended --label=swap2 --fstype=swap --ondisk=sdb
30
31 part raid.1 --grow --ondisk=sda
32 part raid.2 --grow --ondisk=sdb
33
34 raid / --level=1 --device=md0 --fstype=ext4 --label=root raid.1 raid.2
35 '';
36 };
37 };
38
39 environment.systemPackages = [
40 pkgs.telnet
41 pkgs.vim
42 ];
43
44 security.acme.certs = {
45 "eldiron" = {
46 webroot = "/var/lib/acme/acme-challenge";
47 email = "ismael@bouya.org";
48 domain = "eldiron.immae.eu";
49 plugins = [ "cert.pem" "chain.pem" "fullchain.pem" "full.pem" "key.pem" "account_key.json" ];
50 postRun = ''
51 systemctl reload httpd.service
52 '';
53 extraDomains = {
54 "db-1.immae.eu" = null;
55 "tools.immae.eu" = null;
56 "connexionswing.immae.eu" = null;
57 "sandetludo.immae.eu" = null;
58 };
59 };
60 # "connexionswing" = {
61 # webroot = "/var/lib/acme/acme-challenge";
62 # email = "ismael@bouya.org";
63 # domain = "connexionswing.com";
64 # plugins = [ "cert.pem" "chain.pem" "fullchain.pem" "full.pem" "key.pem" "account_key.json" ];
65 # postRun = ''
66 # systemctl reload httpd.service
67 # '';
68 # extraDomains = {
69 # "www.connexionswing.com" = null;
70 # "sandetludo.com" = null;
71 # "www.sandetludo.com" = null;
72 # };
73 # };
74 };
75
76 services.ympd = mypkgs.ympd.config // { enable = true; };
77
78 services.phpfpm = {
79 extraConfig = ''
80 log_level = notice
81 '';
82 poolConfigs = {
83 adminer = mypkgs.adminer.phpFpm.pool;
84 connexionswing_dev = mypkgs.connexionswing_dev.phpFpm.pool;
85 connexionswing_prod = mypkgs.connexionswing_prod.phpFpm.pool;
86 www = ''
87 listen = /var/run/phpfpm/www.sock
88 user = wwwrun
89 group = wwwrun
90 listen.owner = wwwrun
91 listen.group = wwwrun
92 pm = ondemand
93 pm.max_children = 5
94 pm.process_idle_timeout = 60
95 ;php_admin_flag[log_errors] = on
96 php_admin_value[open_basedir] = "/var/www"
97 '';
98 };
99 };
100
101 system.activationScripts = {
102 connexionswing_dev = mypkgs.connexionswing_dev.activationScript;
103 connexionswing_prod = mypkgs.connexionswing_prod.activationScript;
104 httpd = ''
105 install -d -m 0755 /var/lib/acme/acme-challenge
106 install -d -m 0755 /var/www
107 '';
108 };
109
110 services.httpd = let
111 withSSL = domain: {
112 enableSSL = true;
113 sslServerCert = "/var/lib/acme/${domain}/cert.pem";
114 sslServerKey = "/var/lib/acme/${domain}/key.pem";
115 sslServerChain = "/var/lib/acme/${domain}/fullchain.pem";
116 };
117 apacheConfig = {
118 gzip = {
119 modules = [ "deflate" "filter" ];
120 extraConfig = ''
121 AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
122 '';
123 };
124 ldap = {
125 modules = [ "ldap" "authnz_ldap" ];
126 extraConfig = assert mylibs.checkEnv "NIXOPS_HTTP_LDAP_PASSWORD"; ''
127 <IfModule ldap_module>
128 LDAPSharedCacheSize 500000
129 LDAPCacheEntries 1024
130 LDAPCacheTTL 600
131 LDAPOpCacheEntries 1024
132 LDAPOpCacheTTL 600
133 </IfModule>
134
135 <Macro LDAPConnect>
136 <IfModule authnz_ldap_module>
137 AuthLDAPURL ldap://ldap.immae.eu:389/dc=immae,dc=eu
138 AuthLDAPBindDN cn=httpd,ou=services,dc=immae,dc=eu
139 AuthLDAPBindPassword "${builtins.getEnv "NIXOPS_HTTP_LDAP_PASSWORD"}"
140 AuthType Basic
141 AuthName "Authentification requise (Acces LDAP)"
142 AuthBasicProvider ldap
143 </IfModule>
144 </Macro>
145 '';
146 };
147 };
148 in rec {
149 enable = true;
150 logPerVirtualHost = true;
151 multiProcessingModule = "worker";
152 adminAddr = "httpd@immae.eu";
153 # FIXME: http2
154 # FIXME: voir les autres modules:
155 # authz_core_module
156 # reqtimeout_module
157 # http2_module
158 # version_module
159 # proxy_connect_module
160 # proxy_ftp_module
161 # proxy_scgi_module
162 # proxy_ajp_module
163 # proxy_balancer_module
164 # proxy_express_module
165 # lbmethod_byrequests_module
166 # lbmethod_bytraffic_module
167 # lbmethod_bybusyness_module
168 # lbmethod_heartbeat_module
169
170 extraModules = pkgs.lib.lists.unique (
171 mypkgs.adminer.apache.modules ++
172 mypkgs.connexionswing_dev.apache.modules ++
173 mypkgs.connexionswing_prod.apache.modules ++
174 pkgs.lib.lists.flatten (pkgs.lib.attrsets.mapAttrsToList (n: v: v.modules) apacheConfig) ++
175 [ "macro" ]);
176 extraConfig = builtins.concatStringsSep "\n"
177 (pkgs.lib.attrsets.mapAttrsToList (n: v: v.extraConfig) apacheConfig);
178 virtualHosts = [
179 (withSSL "eldiron" // {
180 listen = [ { ip = "*"; port = 443; } ];
181 hostName = "eldiron.immae.eu";
182 # FIXME: directory needs to exist
183 documentRoot = "/var/www";
184 })
185 (withSSL "eldiron" // {
186 listen = [ { ip = "*"; port = 443; } ];
187 hostName = "db-1.immae.eu";
188 documentRoot = null;
189 extraConfig = builtins.concatStringsSep "\n" [
190 mypkgs.adminer.apache.vhostConf
191 ];
192 })
193 (withSSL "eldiron" // {
194 listen = [ { ip = "*"; port = 443; } ];
195 hostName = "tools.immae.eu";
196 documentRoot = null;
197 extraConfig = builtins.concatStringsSep "\n" [
198 mypkgs.adminer.apache.vhostConf
199 mypkgs.ympd.apache.vhostConf
200 ];
201 })
202 (withSSL "eldiron" // {
203 listen = [ { ip = "*"; port = 443; } ];
204 hostName = "connexionswing.immae.eu";
205 serverAliases = [ "sandetludo.immae.eu" ];
206 documentRoot = mypkgs.connexionswing_dev.webRoot;
207 extraConfig = builtins.concatStringsSep "\n" [
208 mypkgs.connexionswing_dev.apache.vhostConf
209 ];
210 })
211 { # Should go last, default fallback
212 listen = [ { ip = "*"; port = 80; } ];
213 hostName = "redirectSSL";
214 serverAliases = [ "*" ];
215 enableSSL = false;
216 documentRoot = "/var/lib/acme/acme-challenge";
217 extraConfig = ''
218 RewriteEngine on
219 RewriteCond "%{REQUEST_URI}" "!^/\.well-known"
220 RewriteRule ^(.+) https://%{HTTP_HOST}$1 [R=301]
221 # To redirect in specific "VirtualHost *:80", do
222 # RedirectMatch 301 ^/((?!\.well-known.*$).*)$ https://host/$1
223 # rather than rewrite
224 '';
225 }
226 ];
227 };
228
229 security.pam.services = let
230 pam_ldap = pkgs.pam_ldap;
231 pam_ldap_mysql = assert mylibs.checkEnv "NIXOPS_MYSQL_PAM_PASSWORD";
232 pkgs.writeText "mysql.conf" ''
233 host ldap.immae.eu
234 base dc=immae,dc=eu
235 binddn cn=mysql,cn=pam,ou=services,dc=immae,dc=eu
236 bindpw ${builtins.getEnv "NIXOPS_MYSQL_PAM_PASSWORD"}
237 pam_filter memberOf=cn=users,cn=mysql,cn=pam,ou=services,dc=immae,dc=eu
238 '';
239 in [
240 {
241 name = "mysql";
242 text = ''
243 # https://mariadb.com/kb/en/mariadb/pam-authentication-plugin/
244 auth required ${pam_ldap}/lib/security/pam_ldap.so config=${pam_ldap_mysql}
245 account required ${pam_ldap}/lib/security/pam_ldap.so config=${pam_ldap_mysql}
246 '';
247 }
248 ];
249
250 # FIXME: initial sync
251 # FIXME: backup
252 # FIXME: restart after pam
253 # FIXME: pam access doesn’t work (because of php module)
254 # FIXME: ssl
255 services.mysql = rec {
256 enable = true;
257 package = pkgs.mariadb.overrideAttrs(old: rec {
258 cmakeFlags = old.cmakeFlags ++ [ "-DWITH_AUTHENTICATION_PAM=ON" ];
259 buildInputs = old.buildInputs ++ [ pkgs.pam ];
260 });
261 };
262
263 # FIXME: initial sync
264 # FIXME: backup
265 # FIXME: ssl
266 services.postgresql = rec {
267 enable = true;
268 package = pkgs.postgresql100.overrideAttrs(old: rec {
269 passthru = old.passthru // { psqlSchema = "11.0"; };
270 name = "postgresql-11.1";
271 src = pkgs.fetchurl {
272 url = "mirror://postgresql/source/v11.1/${name}.tar.bz2";
273 sha256 = "026v0sicsh7avzi45waf8shcbhivyxmi7qgn9fd1x0vl520mx0ch";
274 };
275 });
276 enableTCPIP = true;
277 extraConfig = ''
278 max_connections = 100
279 wal_level = logical
280 shared_buffers = 128MB
281 max_wal_size = 1GB
282 min_wal_size = 80MB
283 log_timezone = 'Europe/Paris'
284 datestyle = 'iso, mdy'
285 timezone = 'Europe/Paris'
286 lc_messages = 'en_US.UTF-8'
287 lc_monetary = 'en_US.UTF-8'
288 lc_numeric = 'en_US.UTF-8'
289 lc_time = 'en_US.UTF-8'
290 default_text_search_config = 'pg_catalog.english'
291 # ssl = on
292 # ssl_cert_file = '/var/lib/acme/eldiron/fullchain.pem'
293 # ssl_key_file = '/var/lib/acme/eldiron/key.pem'
294 '';
295 authentication = ''
296 local all postgres ident
297 local all all md5
298 host all all 178.33.252.96/32 md5
299 host all all 188.165.209.148/32 md5
300 #host all all all pam
301 '';
302 };
303 };
304 }