]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - nixops/modules/websites/ludivine/ludivinecassal.nix
b5450e61ea6526fdcb2cd07b08511f1c3f87b134
[perso/Immae/Config/Nix.git] / nixops / modules / websites / ludivine / ludivinecassal.nix
1 { pkgs, lib, writeText, fetchedGitPrivate, stdenv, composerEnv, fetchurl, fetchgit, ruby, sass, imagemagick }:
2 let
3 ludivinecassal = { config }: rec {
4 environment = config.environment;
5 varDir = "/var/lib/ludivinecassal_${environment}";
6 keys."${environment}-ludivinecassal" = {
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 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 ldap_host: ldap.immae.eu
26 ldap_port: 636
27 ldap_version: 3
28 ldap_ssl: true
29 ldap_tls: false
30 ldap_user_bind: 'uid={username},ou=users,dc=immae,dc=eu'
31 ldap_base_dn: 'dc=immae,dc=eu'
32 ldap_search_dn: '${config.ldap.dn}'
33 ldap_search_password: '${config.ldap.password}'
34 ldap_search_filter: '${config.ldap.search}'
35 leapt_im:
36 binary_path: ${imagemagick}/bin
37 assetic:
38 sass: ${sass}/bin/sass
39 ruby: ${ruby}/bin/ruby
40 '';
41 };
42 phpFpm = rec {
43 serviceDeps = [ "mysql.service" "${environment}-ludivinecassal-key.service" ];
44 socket = "/var/run/phpfpm/ludivinecassal-${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] = "/run/keys/webapps/${environment}-ludivinecassal:${webappDir}:${varDir}:/tmp"
55 php_admin_value[session.save_path] = "${varDir}/phpSessions"
56 ${if environment == "dev" then ''
57 pm = ondemand
58 pm.max_children = 5
59 pm.process_idle_timeout = 60
60 env[SYMFONY_DEBUG_MODE] = "yes"
61 '' else ''
62 pm = dynamic
63 pm.max_children = 20
64 pm.start_servers = 2
65 pm.min_spare_servers = 1
66 pm.max_spare_servers = 3
67 ''}'';
68 };
69 apache = rec {
70 user = "wwwrun";
71 group = "wwwrun";
72 modules = [ "proxy_fcgi" ];
73 webappName = "ludivine_${environment}";
74 root = "/run/current-system/webapps/${webappName}";
75 vhostConf = ''
76 <FilesMatch "\.php$">
77 SetHandler "proxy:unix:${phpFpm.socket}|fcgi://localhost"
78 </FilesMatch>
79
80 ${if environment == "dev" then ''
81 <Location />
82 Use LDAPConnect
83 Require ldap-group cn=ludivine.immae.eu,cn=httpd,ou=services,dc=immae,dc=eu
84 ErrorDocument 401 "<html><meta http-equiv=\"refresh\" content=\"0;url=https://ludivinecassal.com\"></html>"
85 </Location>
86
87 <Directory ${root}>
88 Options Indexes FollowSymLinks MultiViews Includes
89 AllowOverride None
90 Require all granted
91
92 DirectoryIndex app_dev.php
93
94 <IfModule mod_negotiation.c>
95 Options -MultiViews
96 </IfModule>
97
98 <IfModule mod_rewrite.c>
99 RewriteEngine On
100
101 RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
102 RewriteRule ^(.*) - [E=BASE:%1]
103
104 # Maintenance script
105 RewriteCond %{DOCUMENT_ROOT}/maintenance.php -f
106 RewriteCond %{SCRIPT_FILENAME} !maintenance.php
107 RewriteRule ^.*$ %{ENV:BASE}/maintenance.php [R=503,L]
108 ErrorDocument 503 /maintenance.php
109
110 # Sets the HTTP_AUTHORIZATION header removed by Apache
111 RewriteCond %{HTTP:Authorization} .
112 RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
113
114 RewriteCond %{ENV:REDIRECT_STATUS} ^$
115 RewriteRule ^app_dev\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
116
117 # If the requested filename exists, simply serve it.
118 # We only want to let Apache serve files and not directories.
119 RewriteCond %{REQUEST_FILENAME} -f
120 RewriteRule ^ - [L]
121
122 # Rewrite all other queries to the front controller.
123 RewriteRule ^ %{ENV:BASE}/app_dev.php [L]
124 </IfModule>
125
126 </Directory>
127 '' else ''
128 Use Stats ludivinecassal.com
129
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 install -m 0755 -o ${apache.user} -g ${apache.group} -d ${varDir}/tmp
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 fetchgit; } //
155 fetchedGitPrivate ./ludivinecassal.json //
156 rec {
157 noDev = (environment == "prod");
158 preInstall = ''
159 export SYMFONY_ENV="${environment}"
160 cp app/config/parameters.yml.dist app/config/parameters.yml
161 cat >> app/config/parameters.yml <<EOF
162 leapt_im:
163 binary_path: ${imagemagick}/bin
164 assetic:
165 sass: ${sass}/bin/sass
166 ruby: ${ruby}/bin/ruby
167 EOF
168 sed -i -e "/Incenteev..ParameterHandler..ScriptHandler::buildParameters/d" composer.json
169 '';
170 # /!\ miniatures and data need to be in the same physical dir due to a
171 # bug in leapt.im (searches for data/../miniatures)
172 postInstall = ''
173 rm -rf var/{logs,cache,data,miniatures,tmp}
174 ln -sf ${varDir}/{logs,cache,data,miniatures,tmp} var/
175 ln -sf /run/keys/webapps/${environment}-ludivinecassal app/config/parameters.yml
176 '';
177 buildInputs = [ sass ];
178 });
179 webRoot = "${webappDir}/web";
180 };
181 in
182 ludivinecassal