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