]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - nixops/modules/websites/tellesflorian/tellesflorian.nix
142ba9833d440216d58046cfceaf1e21e7e6c872
[perso/Immae/Config/Nix.git] / nixops / modules / websites / tellesflorian / tellesflorian.nix
1 { lib, writeText, fetchedGitPrivate, fetchurl, stdenv, composerEnv }:
2 let
3 tellesflorian = { config }: rec {
4 environment = config.environment;
5 varDir = "/var/lib/tellesflorian_${environment}";
6 keys."${environment}-tellesflorian" = {
7 destDir = "/run/keys/webapps";
8 user = apache.user;
9 group = apache.group;
10 permissions = "0700";
11 text = ''
12 # This file is auto-generated during the composer install
13 parameters:
14 database_host: ${config.mysql.host}
15 database_port: ${config.mysql.port}
16 database_name: ${config.mysql.name}
17 database_user: ${config.mysql.user}
18 database_password: ${config.mysql.password}
19 mailer_transport: smtp
20 mailer_host: 127.0.0.1
21 mailer_user: null
22 mailer_password: null
23 secret: ${config.secret}
24 '';
25 };
26 phpFpm = rec {
27 serviceDeps = [
28 "mysql.service"
29 "${environment}-tellesflorian-passwords-key.service"
30 "${environment}-tellesflorian-key.service"
31 ];
32 socket = "/var/run/phpfpm/floriantelles-${environment}.sock";
33 pool = ''
34 listen = ${socket}
35 user = ${apache.user}
36 group = ${apache.group}
37 listen.owner = ${apache.user}
38 listen.group = ${apache.group}
39 php_admin_value[upload_max_filesize] = 20M
40 php_admin_value[post_max_size] = 20M
41 ;php_admin_flag[log_errors] = on
42 php_admin_value[open_basedir] = "/run/keys/webapps/${environment}-tellesflorian:${webappDir}:${varDir}:/tmp"
43 php_admin_value[session.save_path] = "${varDir}/phpSessions"
44 ${if environment == "dev" then ''
45 pm = ondemand
46 pm.max_children = 5
47 pm.process_idle_timeout = 60
48 env[SYMFONY_DEBUG_MODE] = "yes"
49 '' else ''
50 pm = dynamic
51 pm.max_children = 20
52 pm.start_servers = 2
53 pm.min_spare_servers = 1
54 pm.max_spare_servers = 3
55 ''}'';
56 };
57 keys."${environment}-tellesflorian-passwords" = {
58 destDir = "/run/keys/webapps";
59 user = apache.user;
60 group = apache.group;
61 permissions = "0700";
62 text = ''
63 invite:${config.invite_passwords}
64 '';
65 };
66 apache = rec {
67 user = "wwwrun";
68 group = "wwwrun";
69 modules = [ "proxy_fcgi" ];
70 webappName = "florian_${environment}";
71 root = "/run/current-system/webapps/${webappName}";
72 vhostConf = ''
73 <FilesMatch "\.php$">
74 SetHandler "proxy:unix:${phpFpm.socket}|fcgi://localhost"
75 </FilesMatch>
76
77 ${if environment == "dev" then ''
78 <Location />
79 AuthBasicProvider file ldap
80 Use LDAPConnect
81 Require ldap-group cn=app.tellesflorian.com,cn=httpd,ou=services,dc=immae,dc=eu
82
83 AuthUserFile "/run/keys/webapps/${environment}-tellesflorian-passwords"
84 Require user "invite"
85
86 ErrorDocument 401 "<html><meta http-equiv=\"refresh\" content=\"0;url=https://tellesflorian.com\"></html>"
87 </Location>
88
89 <Directory ${root}>
90 Options Indexes FollowSymLinks MultiViews Includes
91 AllowOverride None
92 Require all granted
93
94 DirectoryIndex app_dev.php
95
96 <IfModule mod_negotiation.c>
97 Options -MultiViews
98 </IfModule>
99
100 <IfModule mod_rewrite.c>
101 RewriteEngine On
102
103 RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
104 RewriteRule ^(.*) - [E=BASE:%1]
105
106 # Maintenance script
107 RewriteCond %{DOCUMENT_ROOT}/maintenance.php -f
108 RewriteCond %{SCRIPT_FILENAME} !maintenance.php
109 RewriteRule ^.*$ %{ENV:BASE}/maintenance.php [R=503,L]
110 ErrorDocument 503 /maintenance.php
111
112 # Sets the HTTP_AUTHORIZATION header removed by Apache
113 RewriteCond %{HTTP:Authorization} .
114 RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
115
116 RewriteCond %{ENV:REDIRECT_STATUS} ^$
117 RewriteRule ^app_dev\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
118
119 # If the requested filename exists, simply serve it.
120 # We only want to let Apache serve files and not directories.
121 RewriteCond %{REQUEST_FILENAME} -f
122 RewriteRule ^ - [L]
123
124 # Rewrite all other queries to the front controller.
125 RewriteRule ^ %{ENV:BASE}/app_dev.php [L]
126 </IfModule>
127
128 </Directory>
129 '' else ''
130 <Directory ${root}>
131 Options Indexes FollowSymLinks MultiViews Includes
132 AllowOverride All
133 Require all granted
134 </Directory>
135 ''}
136 '';
137 };
138 activationScript = {
139 deps = [ "wrappers" ];
140 text = ''
141 install -m 0755 -o ${apache.user} -g ${apache.group} -d ${varDir} \
142 ${varDir}/var
143 install -m 0750 -o ${apache.user} -g ${apache.group} -d ${varDir}/phpSessions
144 if [ ! -f "${varDir}/currentWebappDir" -o \
145 "${webappDir}" != "$(cat ${varDir}/currentWebappDir 2>/dev/null)" ]; then
146 pushd ${webappDir} > /dev/null
147 $wrapperDir/sudo -u wwwrun ./bin/console --env=${environment} cache:clear --no-warmup
148 popd > /dev/null
149 echo -n "${webappDir}" > ${varDir}/currentWebappDir
150 fi
151 '';
152 };
153 webappDir = composerEnv.buildPackage (
154 import ./php-packages.nix { inherit composerEnv fetchurl; } //
155 fetchedGitPrivate ./tellesflorian.json //
156 rec {
157 noDev = (environment == "prod");
158 preInstall = ''
159 export SYMFONY_ENV="${environment}"
160 '';
161 postInstall = ''
162 cd $out
163 rm app/config/parameters.yml
164 ln -sf /run/keys/webapps/${environment}-tellesflorian app/config/parameters.yml
165 rm -rf var/{logs,cache}
166 ln -sf ${varDir}/var/{logs,cache,sessions} var/
167 '';
168 });
169 webRoot = "${webappDir}/web";
170 };
171 in
172 tellesflorian