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