]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - nixops/modules/websites/connexionswing/connexionswing.nix
8fbca2d8d6d703a8a1ad05be3966bf02b62a7dd4
[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 configRoot =
7 writeText "parameters.yml" ''
8 # This file is auto-generated during the composer install
9 parameters:
10 database_host: ${config.mysql.host}
11 database_port: ${config.mysql.port}
12 database_name: ${config.mysql.name}
13 database_user: ${config.mysql.user}
14 database_password: ${config.mysql.password}
15 database_server_version: ${pkgs.mariadb.mysqlVersion}
16 mailer_transport: sendmail
17 mailer_host: null
18 mailer_user: null
19 mailer_password: null
20 subscription_email: ${config.email}
21 allow_robots: true
22 secret: ${config.secret}
23 ${if environment == "prod" then ''
24 services:
25 swiftmailer.mailer.default.transport:
26 class: Swift_SendmailTransport
27 arguments: ['/run/wrappers/bin/sendmail -bs']
28 '' else ""}
29 '';
30 phpFpm = rec {
31 socket = "/var/run/phpfpm/connexionswing-${environment}.sock";
32 phpConfig = ''
33 extension=${phpPackages.imagick}/lib/php/extensions/imagick.so
34 '';
35 pool = ''
36 listen = ${socket}
37 user = ${apache.user}
38 group = ${apache.group}
39 listen.owner = ${apache.user}
40 listen.group = ${apache.group}
41 php_admin_value[upload_max_filesize] = 20M
42 php_admin_value[post_max_size] = 20M
43 ;php_admin_flag[log_errors] = on
44 php_admin_value[open_basedir] = "/run/wrappers/bin/sendmail:${configRoot}:${webappDir}:${varDir}:/tmp"
45 php_admin_value[session.save_path] = "${varDir}/phpSessions"
46 ${if environment == "dev" then ''
47 pm = ondemand
48 pm.max_children = 5
49 pm.process_idle_timeout = 60
50 env[SYMFONY_DEBUG_MODE] = "yes"
51 '' else ''
52 pm = dynamic
53 pm.max_children = 20
54 pm.start_servers = 2
55 pm.min_spare_servers = 1
56 pm.max_spare_servers = 3
57 ''}'';
58 };
59 apache = rec {
60 user = "wwwrun";
61 group = "wwwrun";
62 modules = [ "proxy_fcgi" ];
63 webappName = "connexionswing_${environment}";
64 root = "/run/current-system/webapps/${webappName}";
65 vhostConf = ''
66 <FilesMatch "\.php$">
67 SetHandler "proxy:unix:${phpFpm.socket}|fcgi://localhost"
68 </FilesMatch>
69
70 <Directory ${varDir}/medias>
71 Options FollowSymLinks
72 AllowOverride None
73 Require all granted
74 </Directory>
75
76 <Directory ${varDir}/uploads>
77 Options FollowSymLinks
78 AllowOverride None
79 Require all granted
80 </Directory>
81
82 ${if environment == "dev" then ''
83 <Location />
84 Use LDAPConnect
85 Require ldap-group cn=connexionswing.immae.eu,cn=httpd,ou=services,dc=immae,dc=eu
86 ErrorDocument 401 "<html><meta http-equiv=\"refresh\" content=\"0;url=https://connexionswing.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 Use Stats connexionswing.com
131
132 <Directory ${root}>
133 Options Indexes FollowSymLinks MultiViews Includes
134 AllowOverride All
135 Require all granted
136 </Directory>
137 ''}
138 '';
139 };
140 activationScript = {
141 deps = [ "wrappers" ];
142 text = ''
143 install -m 0755 -o ${apache.user} -g ${apache.group} -d ${varDir} \
144 ${varDir}/medias \
145 ${varDir}/uploads \
146 ${varDir}/var
147 install -m 0750 -o ${apache.user} -g ${apache.group} -d ${varDir}/phpSessions
148 if [ ! -f "${varDir}/currentWebappDir" -o \
149 "${webappDir}" != "$(cat ${varDir}/currentWebappDir 2>/dev/null)" ]; then
150 pushd ${webappDir} > /dev/null
151 $wrapperDir/sudo -u wwwrun ./bin/console --env=${environment} cache:clear --no-warmup
152 popd > /dev/null
153 echo -n "${webappDir}" > ${varDir}/currentWebappDir
154 fi
155 '';
156 };
157 webappDir = composerEnv.buildPackage (
158 import ./php-packages.nix { inherit composerEnv fetchurl fetchgit; } //
159 fetchedGitPrivate ./connexionswing.json //
160 rec {
161 noDev = (environment == "prod");
162 preInstall = ''
163 export SYMFONY_ENV="${environment}"
164 '';
165 postInstall = ''
166 cd $out
167 ${if environment == "prod" then "php ./bin/console assetic:dump --env=prod --no-debug" else ""}
168 rm app/config/parameters.yml
169 ln -sf ${configRoot} app/config/parameters.yml
170 rm -rf var/{logs,cache}
171 ln -sf ${varDir}/var/{logs,cache} var/
172 ln -sf ${varDir}/{medias,uploads} web/images/
173 '';
174 });
175 webRoot = "${webappDir}/web";
176 };
177 in
178 connexionswing