]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - virtual/modules/websites/aten/aten.nix
1520439e349eef427dd248c00203e7ab64ffe6f0
[perso/Immae/Config/Nix.git] / virtual / modules / websites / aten / aten.nix
1 { lib, writeText, fetchedGitPrivate, stdenv, php, git, cacert, phpPackages, yarn }:
2 let
3 aten = { config }: rec {
4 environment = config.environment;
5 varDir = "/var/lib/aten_${environment}";
6 phpFpm = rec {
7 socket = "/var/run/phpfpm/aten-${environment}.sock";
8 pool = ''
9 listen = ${socket}
10 user = ${apache.user}
11 group = ${apache.group}
12 listen.owner = ${apache.user}
13 listen.group = ${apache.group}
14 php_admin_value[upload_max_filesize] = 20M
15 php_admin_value[post_max_size] = 20M
16 ;php_admin_flag[log_errors] = on
17 php_admin_value[open_basedir] = "${webappDir}:${varDir}:/tmp"
18 php_admin_value[session.save_path] = "${varDir}/phpSessions"
19 ${if environment == "dev" then ''
20 pm = ondemand
21 pm.max_children = 5
22 pm.process_idle_timeout = 60
23 env[SYMFONY_DEBUG_MODE] = "yes"
24 '' else ''
25 pm = dynamic
26 pm.max_children = 20
27 pm.start_servers = 2
28 pm.min_spare_servers = 1
29 pm.max_spare_servers = 3
30 ''}'';
31 };
32 apache = {
33 user = "wwwrun";
34 group = "wwwrun";
35 modules = [ "proxy_fcgi" ];
36 vhostConf = ''
37 <FilesMatch "\.php$">
38 SetHandler "proxy:unix:${phpFpm.socket}|fcgi://localhost"
39 </FilesMatch>
40
41 SetEnv APP_ENV "${environment}"
42 SetEnv APP_SECRET "${config.secret}"
43 SetEnv DATABASE_URL "${config.psql_url}"
44
45 ${if environment == "dev" then ''
46 <Location />
47 Use LDAPConnect
48 Require ldap-group cn=dev.aten.pro,cn=httpd,ou=services,dc=immae,dc=eu
49 ErrorDocument 401 "<html><meta http-equiv=\"refresh\" content=\"0;url=https://aten.pro\"></html>"
50 </Location>
51
52 <Location /backend>
53 Use LDAPConnect
54 Require ldap-group cn=dev.aten.pro,cn=httpd,ou=services,dc=immae,dc=eu
55 ErrorDocument 401 "<html><meta http-equiv=\"refresh\" content=\"0;url=https://aten.pro\"></html>"
56 </Location>
57 '' else ''
58 Use Stats aten.pro
59
60 <Location /backend>
61 Use LDAPConnect
62 Require ldap-group cn=aten.pro,cn=httpd,ou=services,dc=immae,dc=eu
63 ErrorDocument 401 "<html><meta http-equiv=\"refresh\" content=\"0;url=https://aten.pro\"></html>"
64 </Location>
65 ''}
66
67 <Directory ${webRoot}>
68 Options Indexes FollowSymLinks MultiViews Includes
69 AllowOverride All
70 Require all granted
71 DirectoryIndex index.php
72 FallbackResource /index.php
73 </Directory>
74 '';
75 };
76 activationScript = {
77 deps = [ "wrappers" ];
78 text = ''
79 install -m 0755 -o ${apache.user} -g ${apache.group} -d ${varDir}
80 install -m 0750 -o ${apache.user} -g ${apache.group} -d ${varDir}/phpSessions
81 if [ ! -f "${varDir}/currentWebappDir" -o \
82 "${webappDir}" != "$(cat ${varDir}/currentWebappDir 2>/dev/null)" ]; then
83 pushd ${webappDir} > /dev/null
84 $wrapperDir/sudo -u wwwrun APP_ENV=${environment} ./bin/console --env=${environment} cache:clear --no-warmup
85 popd > /dev/null
86 echo -n "${webappDir}" > ${varDir}/currentWebappDir
87 fi
88 '';
89 };
90 webappDir = stdenv.mkDerivation (fetchedGitPrivate ./aten.json // rec {
91 buildPhase = ''
92 export GIT_SSL_CAINFO=${cacert}/etc/ssl/certs/ca-bundle.crt
93 export SSL_CERT_FILE=${cacert}/etc/ssl/certs/ca-bundle.crt
94 export APP_ENV="${environment}"
95 export DATABASE_URL="${config.psql_url}"
96 export APP_SECRET="${config.secret}"
97
98 ${if environment == "dev" then ''
99 composer install
100 '' else ''
101 SYMFONY_ENV=prod composer install --no-dev
102 ''}
103 yarn install
104 yarn run encore production
105 rm -rf var
106 ln -sf ../../../../../${varDir} var
107 '';
108 installPhase = ''
109 cp -a . $out
110 '';
111 buildInputs = [
112 php git cacert phpPackages.composer yarn
113 ];
114 });
115 webRoot = "${webappDir}/public";
116 };
117 in
118 aten