]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - virtual/eldiron.nix
68eddf607446c5630f46ac13b9659271893c6377
[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 with import ../libs.nix;
9 let
10 mypkgs = pkgs.callPackage ./packages.nix {
11 inherit checkEnv fetchedGitPrivate fetchedGithub;
12 };
13 in
14 {
15 nixpkgs.config.packageOverrides = oldpkgs: rec {
16 gitolite = oldpkgs.gitolite.overrideAttrs(old: rec {
17 name = "gitolite-${version}";
18 version = "3.6.10";
19 src = pkgs.fetchFromGitHub {
20 owner = "sitaramc";
21 repo = "gitolite";
22 rev = "v${version}";
23 sha256 = "0p2697mn6rwm03ndlv7q137zczai82n41aplq1g006ii7f12xy8h";
24 };
25 });
26 gitweb = oldpkgs.gitweb.overrideAttrs(old: {
27 installPhase = old.installPhase + ''
28 cp -r ${./packages/gitweb} $out/gitweb-theme;
29 '';
30 });
31 postgresql = postgresql111;
32 postgresql111 = oldpkgs.postgresql100.overrideAttrs(old: rec {
33 passthru = old.passthru // { psqlSchema = "11.0"; };
34 name = "postgresql-11.1";
35 src = pkgs.fetchurl {
36 url = "mirror://postgresql/source/v11.1/${name}.tar.bz2";
37 sha256 = "026v0sicsh7avzi45waf8shcbhivyxmi7qgn9fd1x0vl520mx0ch";
38 };
39 });
40 mariadb = mariadbPAM;
41 mariadbPAM = oldpkgs.mariadb.overrideAttrs(old: rec {
42 cmakeFlags = old.cmakeFlags ++ [ "-DWITH_AUTHENTICATION_PAM=ON" ];
43 buildInputs = old.buildInputs ++ [ pkgs.pam ];
44 });
45 };
46
47 networking = {
48 firewall = {
49 enable = true;
50 allowedTCPPorts = [ 22 80 443 3306 5432 9418 ];
51 };
52 };
53
54 deployment = {
55 targetEnv = "hetzner";
56 hetzner = {
57 #robotUser = "defined in HETZNER_ROBOT_USER";
58 #robotPass = "defined in HETZNER_ROBOT_PASS";
59 mainIPv4 = "176.9.151.89";
60 partitions = ''
61 clearpart --all --initlabel --drives=sda,sdb
62
63 part swap1 --recommended --label=swap1 --fstype=swap --ondisk=sda
64 part swap2 --recommended --label=swap2 --fstype=swap --ondisk=sdb
65
66 part raid.1 --grow --ondisk=sda
67 part raid.2 --grow --ondisk=sdb
68
69 raid / --level=1 --device=md0 --fstype=ext4 --label=root raid.1 raid.2
70 '';
71 };
72 };
73
74 environment.systemPackages = let
75 # FIXME: move it to nextcloud
76 occ = pkgs.writeScriptBin "nextcloud-occ" ''
77 #! ${pkgs.stdenv.shell}
78 cd ${mypkgs.nextcloud.webRoot}
79 NEXTCLOUD_CONFIG_DIR="${mypkgs.nextcloud.webRoot}/config" \
80 exec \
81 ${config.services.phpfpm.phpPackage}/bin/php \
82 -c ${config.services.phpfpm.phpPackage}/etc/php.ini \
83 occ $*
84 '';
85 in [
86 pkgs.telnet
87 pkgs.vim
88 occ
89 ];
90
91 security.acme.certs = {
92 # /!\ To create a new certificate, add first the domain to an
93 # existing certificate, deploy, and then use it in httpd.
94 "eldiron" = {
95 webroot = "/var/lib/acme/acme-challenge";
96 email = "ismael@bouya.org";
97 domain = "eldiron.immae.eu";
98 plugins = [ "cert.pem" "chain.pem" "fullchain.pem" "full.pem" "key.pem" "account_key.json" ];
99 postRun = ''
100 systemctl reload httpd.service
101 '';
102 extraDomains = {
103 "db-1.immae.eu" = null;
104 "git.immae.eu" = null;
105 "tools.immae.eu" = null;
106 "connexionswing.immae.eu" = null;
107 "sandetludo.immae.eu" = null;
108 "cloud.immae.eu" = null;
109 "ludivine.immae.eu" = null;
110 "dev.aten.pro" = null;
111 };
112 };
113 "ludivinecassal" = {
114 webroot = "/var/lib/acme/acme-challenge";
115 email = "ismael@bouya.org";
116 domain = "ludivinecassal.com";
117 plugins = [ "cert.pem" "chain.pem" "fullchain.pem" "full.pem" "key.pem" "account_key.json" ];
118 postRun = ''
119 systemctl reload httpd.service
120 '';
121 extraDomains = {
122 "www.ludivinecassal.com" = null;
123 };
124 };
125 "aten" = {
126 webroot = "/var/lib/acme/acme-challenge";
127 email = "ismael@bouya.org";
128 domain = "aten.pro";
129 plugins = [ "cert.pem" "chain.pem" "fullchain.pem" "full.pem" "key.pem" "account_key.json" ];
130 postRun = ''
131 systemctl reload httpd.service
132 '';
133 extraDomains = {
134 "www.aten.pro" = null;
135 };
136 };
137 # "connexionswing" = {
138 # webroot = "/var/lib/acme/acme-challenge";
139 # email = "ismael@bouya.org";
140 # domain = "connexionswing.com";
141 # plugins = [ "cert.pem" "chain.pem" "fullchain.pem" "full.pem" "key.pem" "account_key.json" ];
142 # postRun = ''
143 # systemctl reload httpd.service
144 # '';
145 # extraDomains = {
146 # "www.connexionswing.com" = null;
147 # "sandetludo.com" = null;
148 # "www.sandetludo.com" = null;
149 # };
150 # };
151 };
152
153 services.openssh.extraConfig = ''
154 AuthorizedKeysCommand /etc/ssh/ldap_authorized_keys
155 AuthorizedKeysCommandUser nobody
156 '';
157
158 users.users.wwwrun.extraGroups = [ "gitolite" ];
159
160 users.users.gitolite.packages = let
161 python-packages = python-packages: with python-packages; [
162 simplejson
163 urllib3
164 ];
165 in
166 [
167 (pkgs.python3.withPackages python-packages)
168 ];
169 # FIXME: after initial install, need to
170 # (1) copy rc file (adjust gitolite_ldap_groups.sh)
171 # (2) (mark old readonly and) sync repos except gitolite-admin
172 # rsync -av --exclude=gitolite-admin.git old:/var/lib/gitolite/repositories /var/lib/gitolite/
173 # chown -R gitolite:gitolite /var/lib/gitolite
174 # (3) push force the gitolite-admin to new location (from external point)
175 # Don't use an existing key, it will take precedence over
176 # gitolite-admin
177 # (4) su -u gitolite gitolite setup
178 services.gitolite = {
179 enable = true;
180 # FIXME: key from ./ssh
181 adminPubkey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDXqRbiHw7QoHADNIEuo4nUT9fSOIEBMdJZH0bkQAxXyJFyCM1IMz0pxsHV0wu9tdkkr36bPEUj2aV5bkYLBN6nxcV2Y49X8bjOSCPfx3n6Own1h+NeZVBj4ZByrFmqCbTxUJIZ2bZKcWOFncML39VmWdsVhNjg0X4NBBehqXRIKr2gt3E/ESAxTYJFm0BnU0baciw9cN0bsRGqvFgf5h2P48CIAfwhVcGmPQnnAwabnosYQzRWxR0OygH5Kd8mePh6FheIRIigfXsDO8f/jdxwut8buvNIf3m5EBr3tUbTsvM+eV3M5vKGt7sk8T64DVtepTSdOOWtp+47ktsnHOMh immae@immae.eu";
182 };
183
184 services.ympd = mypkgs.ympd.config // { enable = true; };
185
186 services.phpfpm = {
187 # /!\ phppackage is used in nextcloud configuation
188 phpOptions = ''
189 ; For nextcloud
190 extension=${pkgs.phpPackages.redis}/lib/php/extensions/redis.so
191 ; For nextcloud
192 extension=${pkgs.phpPackages.apcu}/lib/php/extensions/apcu.so
193 ; For nextcloud
194 zend_extension=${pkgs.php}/lib/php/extensions/opcache.so
195 '';
196 extraConfig = ''
197 log_level = notice
198 '';
199 poolConfigs = {
200 adminer = mypkgs.adminer.phpFpm.pool;
201 connexionswing_dev = mypkgs.connexionswing_dev.phpFpm.pool;
202 connexionswing_prod = mypkgs.connexionswing_prod.phpFpm.pool;
203 ludivinecassal_dev = mypkgs.ludivinecassal_dev.phpFpm.pool;
204 ludivinecassal_prod = mypkgs.ludivinecassal_prod.phpFpm.pool;
205 aten_dev = mypkgs.aten_dev.phpFpm.pool;
206 aten_prod = mypkgs.aten_prod.phpFpm.pool;
207 nextcloud = mypkgs.nextcloud.phpFpm.pool;
208 mantisbt = mypkgs.mantisbt.phpFpm.pool;
209 };
210 };
211
212 system.activationScripts = {
213 connexionswing_dev = mypkgs.connexionswing_dev.activationScript;
214 connexionswing_prod = mypkgs.connexionswing_prod.activationScript;
215 ludivinecassal_dev = mypkgs.ludivinecassal_dev.activationScript;
216 ludivinecassal_prod = mypkgs.ludivinecassal_prod.activationScript;
217 aten_dev = mypkgs.aten_dev.activationScript;
218 aten_prod = mypkgs.aten_prod.activationScript;
219 nextcloud = mypkgs.nextcloud.activationScript;
220 httpd = ''
221 install -d -m 0755 /var/lib/acme/acme-challenge
222 '';
223 redis = ''
224 mkdir -p /run/redis
225 chown redis /run/redis
226 '';
227 gitolite =
228 assert checkEnv "NIXOPS_GITOLITE_LDAP_PASSWORD";
229 let
230 gitolite_ldap_groups = wrap {
231 name = "gitolite_ldap_groups.sh";
232 file = ./packages/gitolite_ldap_groups.sh;
233 vars = {
234 LDAP_PASS = builtins.getEnv "NIXOPS_GITOLITE_LDAP_PASSWORD";
235 };
236 paths = [ pkgs.openldap pkgs.stdenv.shellPackage pkgs.gnugrep pkgs.coreutils ];
237 };
238 in {
239 deps = [ "users" ];
240 text = ''
241 if [ -d /var/lib/gitolite ]; then
242 ln -sf ${gitolite_ldap_groups} /var/lib/gitolite/gitolite_ldap_groups.sh
243 chmod g+rx /var/lib/gitolite
244 fi
245 if [ -f /var/lib/gitolite/projects.list ]; then
246 chmod g+r /var/lib/gitolite/projects.list
247 fi
248 '';
249 };
250 };
251
252 environment.etc."ssh/ldap_authorized_keys" = let
253 ldap_authorized_keys =
254 assert checkEnv "NIXOPS_SSHD_LDAP_PASSWORD";
255 wrap {
256 name = "ldap_authorized_keys";
257 file = ./ldap_authorized_keys.sh;
258 vars = {
259 LDAP_PASS = builtins.getEnv "NIXOPS_SSHD_LDAP_PASSWORD";
260 GITOLITE_SHELL = "${pkgs.gitolite}/bin/gitolite-shell";
261 ECHO = "${pkgs.coreutils}/bin/echo";
262 };
263 paths = [ pkgs.openldap pkgs.stdenv.shellPackage pkgs.gnugrep pkgs.gnused pkgs.coreutils ];
264 };
265 in {
266 enable = true;
267 mode = "0755";
268 user = "root";
269 source = ldap_authorized_keys;
270 };
271
272 services.gitDaemon = {
273 enable = true;
274 user = "gitolite";
275 group = "gitolite";
276 basePath = "${mypkgs.git.web.varDir}/repositories";
277 };
278
279 services.httpd = let
280 withConf = domain: {
281 enableSSL = true;
282 sslServerCert = "/var/lib/acme/${domain}/cert.pem";
283 sslServerKey = "/var/lib/acme/${domain}/key.pem";
284 sslServerChain = "/var/lib/acme/${domain}/fullchain.pem";
285 logFormat = "combinedVhost";
286 listen = [ { ip = "*"; port = 443; } ];
287 };
288 apacheConfig = {
289 gzip = {
290 modules = [ "deflate" "filter" ];
291 extraConfig = ''
292 AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
293 '';
294 };
295 ldap = {
296 modules = [ "ldap" "authnz_ldap" ];
297 extraConfig = assert checkEnv "NIXOPS_HTTP_LDAP_PASSWORD"; ''
298 <IfModule ldap_module>
299 LDAPSharedCacheSize 500000
300 LDAPCacheEntries 1024
301 LDAPCacheTTL 600
302 LDAPOpCacheEntries 1024
303 LDAPOpCacheTTL 600
304 </IfModule>
305
306 <Macro LDAPConnect>
307 <IfModule authnz_ldap_module>
308 AuthLDAPURL ldap://ldap.immae.eu:389/dc=immae,dc=eu
309 AuthLDAPBindDN cn=httpd,ou=services,dc=immae,dc=eu
310 AuthLDAPBindPassword "${builtins.getEnv "NIXOPS_HTTP_LDAP_PASSWORD"}"
311 AuthType Basic
312 AuthName "Authentification requise (Acces LDAP)"
313 AuthBasicProvider ldap
314 </IfModule>
315 </Macro>
316 '';
317 };
318 http2 = {
319 modules = [ "http2" ];
320 extraConfig = ''
321 Protocols h2 http/1.1
322 '';
323 };
324 customLog = {
325 modules = [];
326 extraConfig = ''
327 LogFormat "%v %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %p" combinedVhost
328 '';
329 };
330 };
331 in rec {
332 enable = true;
333 logPerVirtualHost = true;
334 multiProcessingModule = "worker";
335 adminAddr = "httpd@immae.eu";
336 logFormat = "combinedVhost";
337 extraModules = pkgs.lib.lists.unique (
338 mypkgs.adminer.apache.modules ++
339 mypkgs.nextcloud.apache.modules ++
340 mypkgs.connexionswing_dev.apache.modules ++
341 mypkgs.connexionswing_prod.apache.modules ++
342 mypkgs.ludivinecassal_dev.apache.modules ++
343 mypkgs.ludivinecassal_prod.apache.modules ++
344 mypkgs.aten_dev.apache.modules ++
345 mypkgs.aten_prod.apache.modules ++
346 mypkgs.ympd.apache.modules ++
347 mypkgs.git.web.apache.modules ++
348 mypkgs.mantisbt.apache.modules ++
349 pkgs.lib.lists.flatten (pkgs.lib.attrsets.mapAttrsToList (n: v: v.modules) apacheConfig) ++
350 [ "macro" ]);
351 extraConfig = builtins.concatStringsSep "\n"
352 (pkgs.lib.attrsets.mapAttrsToList (n: v: v.extraConfig) apacheConfig);
353 virtualHosts = [
354 (withConf "eldiron" // {
355 hostName = "eldiron.immae.eu";
356 documentRoot = ./www;
357 extraConfig = ''
358 DirectoryIndex index.htm
359 '';
360 })
361 (withConf "eldiron" // {
362 hostName = "db-1.immae.eu";
363 documentRoot = null;
364 extraConfig = builtins.concatStringsSep "\n" [
365 mypkgs.adminer.apache.vhostConf
366 ];
367 })
368 (withConf "eldiron" // {
369 hostName = "tools.immae.eu";
370 documentRoot = null;
371 extraConfig = builtins.concatStringsSep "\n" [
372 mypkgs.adminer.apache.vhostConf
373 mypkgs.ympd.apache.vhostConf
374 ];
375 })
376 (withConf "eldiron" // {
377 hostName = "connexionswing.immae.eu";
378 serverAliases = [ "sandetludo.immae.eu" ];
379 documentRoot = mypkgs.connexionswing_dev.webRoot;
380 extraConfig = builtins.concatStringsSep "\n" [
381 mypkgs.connexionswing_dev.apache.vhostConf
382 ];
383 })
384 (withConf "eldiron" // {
385 hostName = "ludivine.immae.eu";
386 documentRoot = mypkgs.ludivinecassal_dev.webRoot;
387 extraConfig = builtins.concatStringsSep "\n" [
388 mypkgs.ludivinecassal_dev.apache.vhostConf
389 ];
390 })
391 (withConf "ludivinecassal" // {
392 hostName = "ludivinecassal.com";
393 serverAliases = [ "www.ludivinecassal.com" ];
394 documentRoot = mypkgs.ludivinecassal_prod.webRoot;
395 extraConfig = builtins.concatStringsSep "\n" [
396 mypkgs.ludivinecassal_prod.apache.vhostConf
397 ];
398 })
399 (withConf "eldiron" // {
400 hostName = "dev.aten.pro";
401 documentRoot = mypkgs.aten_dev.webRoot;
402 extraConfig = builtins.concatStringsSep "\n" [
403 mypkgs.aten_dev.apache.vhostConf
404 ];
405 })
406 (withConf "aten" // {
407 hostName = "aten.pro";
408 serverAliases = [ "www.aten.pro" ];
409 documentRoot = mypkgs.aten_prod.webRoot;
410 extraConfig = builtins.concatStringsSep "\n" [
411 mypkgs.aten_prod.apache.vhostConf
412 ];
413 })
414 (withConf "eldiron" // {
415 hostName = "cloud.immae.eu";
416 documentRoot = mypkgs.nextcloud.webRoot;
417 extraConfig = builtins.concatStringsSep "\n" [
418 mypkgs.nextcloud.apache.vhostConf
419 ];
420 })
421 (withConf "eldiron" // {
422 hostName = "git.immae.eu";
423 documentRoot = mypkgs.git.web.webRoot;
424 extraConfig = builtins.concatStringsSep "\n" [
425 mypkgs.git.web.apache.vhostConf
426 mypkgs.mantisbt.apache.vhostConf
427 ] + ''
428 RewriteEngine on
429 RewriteCond %{REQUEST_URI} ^/releases
430 RewriteRule /releases(.*) https://release.immae.eu$1 [P,L]
431 '';
432 })
433 { # Should go last, default fallback
434 listen = [ { ip = "*"; port = 80; } ];
435 hostName = "redirectSSL";
436 serverAliases = [ "*" ];
437 enableSSL = false;
438 logFormat = "combinedVhost";
439 documentRoot = "/var/lib/acme/acme-challenge";
440 extraConfig = ''
441 RewriteEngine on
442 RewriteCond "%{REQUEST_URI}" "!^/\.well-known"
443 RewriteRule ^(.+) https://%{HTTP_HOST}$1 [R=301]
444 # To redirect in specific "VirtualHost *:80", do
445 # RedirectMatch 301 ^/((?!\.well-known.*$).*)$ https://host/$1
446 # rather than rewrite
447 '';
448 }
449 ];
450 };
451
452 security.pam.services = let
453 pam_ldap = pkgs.pam_ldap;
454 pam_ldap_mysql = assert checkEnv "NIXOPS_MYSQL_PAM_PASSWORD";
455 pkgs.writeText "mysql.conf" ''
456 host ldap.immae.eu
457 base dc=immae,dc=eu
458 binddn cn=mysql,cn=pam,ou=services,dc=immae,dc=eu
459 bindpw ${builtins.getEnv "NIXOPS_MYSQL_PAM_PASSWORD"}
460 pam_filter memberOf=cn=users,cn=mysql,cn=pam,ou=services,dc=immae,dc=eu
461 '';
462 in [
463 {
464 name = "mysql";
465 text = ''
466 # https://mariadb.com/kb/en/mariadb/pam-authentication-plugin/
467 auth required ${pam_ldap}/lib/security/pam_ldap.so config=${pam_ldap_mysql}
468 account required ${pam_ldap}/lib/security/pam_ldap.so config=${pam_ldap_mysql}
469 '';
470 }
471 ];
472
473 # FIXME: backup
474 services.redis = rec {
475 enable = true;
476 bind = "127.0.0.1";
477 unixSocket = "/run/redis/redis.sock";
478 extraConfig = ''
479 unixsocketperm 777
480 maxclients 1024
481 '';
482 };
483
484 # FIXME: initial sync
485 # FIXME: backup
486 # FIXME: restart after pam
487 # FIXME: pam access doesn’t work (because of php module)
488 # FIXME: ssl
489 services.mysql = rec {
490 enable = true;
491 package = pkgs.mariadb;
492 };
493
494 # FIXME: initial sync
495 # FIXME: backup
496 # FIXME: ssl
497 services.postgresql = rec {
498 enable = true;
499 package = pkgs.postgresql;
500 enableTCPIP = true;
501 extraConfig = ''
502 max_connections = 100
503 wal_level = logical
504 shared_buffers = 128MB
505 max_wal_size = 1GB
506 min_wal_size = 80MB
507 log_timezone = 'Europe/Paris'
508 datestyle = 'iso, mdy'
509 timezone = 'Europe/Paris'
510 lc_messages = 'en_US.UTF-8'
511 lc_monetary = 'en_US.UTF-8'
512 lc_numeric = 'en_US.UTF-8'
513 lc_time = 'en_US.UTF-8'
514 default_text_search_config = 'pg_catalog.english'
515 # ssl = on
516 # ssl_cert_file = '/var/lib/acme/eldiron/fullchain.pem'
517 # ssl_key_file = '/var/lib/acme/eldiron/key.pem'
518 '';
519 authentication = ''
520 local all postgres ident
521 local all all md5
522 host all all samehost md5
523 host all all 178.33.252.96/32 md5
524 host all all 188.165.209.148/32 md5
525 #host all all all pam
526 '';
527 };
528 };
529 }