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