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