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