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