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