]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - nixops/modules/websites/aten/aten.nix
Add Florian's website
[perso/Immae/Config/Nix.git] / nixops / modules / websites / aten / aten.nix
1 { lib, writeText, fetchedGitPrivate, stdenv, composerEnv, fetchurl, fetchgit, binutils, python, nodejs, libsass, yarn2nix }:
2 let
3 aten = { config }: rec {
4 environment = config.environment;
5 varDir = "/var/lib/aten_${environment}";
6 phpFpm = rec {
7 socket = "/var/run/phpfpm/aten-${environment}.sock";
8 pool = ''
9 listen = ${socket}
10 user = ${apache.user}
11 group = ${apache.group}
12 listen.owner = ${apache.user}
13 listen.group = ${apache.group}
14 php_admin_value[upload_max_filesize] = 20M
15 php_admin_value[post_max_size] = 20M
16 ;php_admin_flag[log_errors] = on
17 php_admin_value[open_basedir] = "${webappDir}:${varDir}:/tmp"
18 php_admin_value[session.save_path] = "${varDir}/phpSessions"
19 ${if environment == "dev" then ''
20 pm = ondemand
21 pm.max_children = 5
22 pm.process_idle_timeout = 60
23 env[SYMFONY_DEBUG_MODE] = "yes"
24 '' else ''
25 pm = dynamic
26 pm.max_children = 20
27 pm.start_servers = 2
28 pm.min_spare_servers = 1
29 pm.max_spare_servers = 3
30 ''}'';
31 };
32 apache = {
33 user = "wwwrun";
34 group = "wwwrun";
35 modules = [ "proxy_fcgi" ];
36 vhostConf = ''
37 <FilesMatch "\.php$">
38 SetHandler "proxy:unix:${phpFpm.socket}|fcgi://localhost"
39 </FilesMatch>
40
41 SetEnv APP_ENV "${environment}"
42 SetEnv APP_SECRET "${config.secret}"
43 SetEnv DATABASE_URL "${config.psql_url}"
44
45 ${if environment == "dev" then ''
46 <Location />
47 Use LDAPConnect
48 Require ldap-group cn=dev.aten.pro,cn=httpd,ou=services,dc=immae,dc=eu
49 ErrorDocument 401 "<html><meta http-equiv=\"refresh\" content=\"0;url=https://aten.pro\"></html>"
50 </Location>
51
52 <Location /backend>
53 Use LDAPConnect
54 Require ldap-group cn=dev.aten.pro,cn=httpd,ou=services,dc=immae,dc=eu
55 ErrorDocument 401 "<html><meta http-equiv=\"refresh\" content=\"0;url=https://aten.pro\"></html>"
56 </Location>
57 '' else ''
58 Use Stats aten.pro
59
60 <Location /backend>
61 Use LDAPConnect
62 Require ldap-group cn=aten.pro,cn=httpd,ou=services,dc=immae,dc=eu
63 ErrorDocument 401 "<html><meta http-equiv=\"refresh\" content=\"0;url=https://aten.pro\"></html>"
64 </Location>
65 ''}
66
67 <Directory ${webRoot}>
68 Options Indexes FollowSymLinks MultiViews Includes
69 AllowOverride All
70 Require all granted
71 DirectoryIndex index.php
72 FallbackResource /index.php
73 </Directory>
74 '';
75 };
76 activationScript = {
77 deps = [ "wrappers" ];
78 text = ''
79 install -m 0755 -o ${apache.user} -g ${apache.group} -d ${varDir}
80 install -m 0750 -o ${apache.user} -g ${apache.group} -d ${varDir}/phpSessions
81 if [ ! -f "${varDir}/currentWebappDir" -o \
82 "${webappDir}" != "$(cat ${varDir}/currentWebappDir 2>/dev/null)" ]; then
83 pushd ${webappDir} > /dev/null
84 $wrapperDir/sudo -u wwwrun APP_ENV=${environment} ./bin/console --env=${environment} cache:clear --no-warmup
85 popd > /dev/null
86 echo -n "${webappDir}" > ${varDir}/currentWebappDir
87 fi
88 '';
89 };
90 yarnModules = let
91 info = fetchedGitPrivate ./aten.json;
92 in
93 yarn2nix.mkYarnModules {
94 name = "aten-yarn-modules";
95 packageJSON = "${info.src}/package.json";
96 yarnLock = "${info.src}/yarn.lock";
97 pkgConfig = {
98 node-sass = {
99 buildInputs = [ binutils libsass python ];
100 postInstall = let
101 nodeHeaders = fetchurl {
102 url = "https://nodejs.org/download/release/v${nodejs.version}/node-v${nodejs.version}-headers.tar.gz";
103 sha256 = "12zzsf8my43b8qnlacp871ih5vqafl2vlpqp51xp6h3gckn2frwy";
104 };
105 in
106 ''
107 export AR=${binutils.bintools}/bin/ar
108 node scripts/build.js --tarball=${nodeHeaders}
109 '';
110 };
111 };
112 };
113 webappDir = composerEnv.buildPackage (
114 import ./php-packages.nix { inherit composerEnv fetchurl fetchgit; } //
115 fetchedGitPrivate ./aten.json //
116 rec {
117 noDev = (environment == "prod");
118 preInstall = ''
119 export SYMFONY_ENV="${environment}"
120 export APP_ENV="${environment}"
121 export DATABASE_URL="${config.psql_url}"
122 export APP_SECRET="${config.secret}"
123 '';
124 postInstall = ''
125 cd $out
126 ln -sf ${yarnModules}/node_modules .
127 yarn run --offline encore production
128 rm -rf var/{log,cache}
129 ln -sf ../../../../../../../${varDir}/{log,cache} var/
130 '';
131 buildInputs = [ yarn2nix.yarn ];
132 });
133 webRoot = "${webappDir}/public";
134 };
135 in
136 aten