]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - nixops/modules/websites/aten/aten.nix
d9286c3fa7bd402ccfadd2f6e0d3d9100cfaf5c1
[perso/Immae/Config/Nix.git] / nixops / modules / websites / aten / aten.nix
1 { lib, writeText, fetchedGitPrivate, stdenv, runCommand, composerEnv, fetchurl, fetchgit, jq, python, nodejs, libsass, yarn2nixPackage }:
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 = rec {
33 user = "wwwrun";
34 group = "wwwrun";
35 modules = [ "proxy_fcgi" ];
36 webappName = "aten_${environment}";
37 root = "/run/current-system/webapps/${webappName}";
38 vhostConf = ''
39 <FilesMatch "\.php$">
40 SetHandler "proxy:unix:${phpFpm.socket}|fcgi://localhost"
41 </FilesMatch>
42
43 SetEnv APP_ENV "${environment}"
44 SetEnv APP_SECRET "${config.secret}"
45 SetEnv DATABASE_URL "${config.psql_url}"
46
47 ${if environment == "dev" then ''
48 <Location />
49 Use LDAPConnect
50 Require ldap-group cn=dev.aten.pro,cn=httpd,ou=services,dc=immae,dc=eu
51 ErrorDocument 401 "<html><meta http-equiv=\"refresh\" content=\"0;url=https://aten.pro\"></html>"
52 </Location>
53
54 <Location /backend>
55 Use LDAPConnect
56 Require ldap-group cn=dev.aten.pro,cn=httpd,ou=services,dc=immae,dc=eu
57 ErrorDocument 401 "<html><meta http-equiv=\"refresh\" content=\"0;url=https://aten.pro\"></html>"
58 </Location>
59 '' else ''
60 Use Stats aten.pro
61
62 <Location /backend>
63 Use LDAPConnect
64 Require ldap-group cn=aten.pro,cn=httpd,ou=services,dc=immae,dc=eu
65 ErrorDocument 401 "<html><meta http-equiv=\"refresh\" content=\"0;url=https://aten.pro\"></html>"
66 </Location>
67 ''}
68
69 <Directory ${root}>
70 Options Indexes FollowSymLinks MultiViews Includes
71 AllowOverride All
72 Require all granted
73 DirectoryIndex index.php
74 FallbackResource /index.php
75 </Directory>
76 '';
77 };
78 activationScript = {
79 deps = [ "wrappers" ];
80 text = ''
81 install -m 0755 -o ${apache.user} -g ${apache.group} -d ${varDir}
82 install -m 0750 -o ${apache.user} -g ${apache.group} -d ${varDir}/phpSessions
83 if [ ! -f "${varDir}/currentWebappDir" -o \
84 "${webappDir}" != "$(cat ${varDir}/currentWebappDir 2>/dev/null)" ]; then
85 pushd ${webappDir} > /dev/null
86 $wrapperDir/sudo -u wwwrun APP_ENV=${environment} ./bin/console --env=${environment} cache:clear --no-warmup
87 popd > /dev/null
88 echo -n "${webappDir}" > ${varDir}/currentWebappDir
89 fi
90 '';
91 };
92 yarnModules = let
93 info = fetchedGitPrivate ./aten.json;
94 packagejson = runCommand "package.json" { buildInputs = [ jq ]; } ''
95 cat ${info.src}/package.json | jq -r '.version = "v1.0.0"|.name="aten"' > $out
96 '';
97 in
98 yarn2nixPackage.mkYarnModules rec {
99 name = "aten-yarn";
100 pname = name;
101 version = "v1.0.0";
102 packageJSON = packagejson;
103 yarnLock = "${info.src}/yarn.lock";
104 pkgConfig = {
105 node-sass = {
106 buildInputs = [ libsass python ];
107 postInstall = let
108 nodeHeaders = fetchurl {
109 url = "https://nodejs.org/download/release/v${nodejs.version}/node-v${nodejs.version}-headers.tar.gz";
110 sha256 = "16f20ya3ys6w5w6y6l4536f7jrgk4gz46bf71w1r1xxb26a54m32";
111 };
112 in
113 ''
114 node scripts/build.js --tarball=${nodeHeaders}
115 '';
116 };
117 };
118 };
119 webappDir = composerEnv.buildPackage (
120 import ./php-packages.nix { inherit composerEnv fetchurl fetchgit; } //
121 fetchedGitPrivate ./aten.json //
122 rec {
123 noDev = (environment == "prod");
124 preInstall = ''
125 export SYMFONY_ENV="${environment}"
126 export APP_ENV="${environment}"
127 export DATABASE_URL="${config.psql_url}"
128 export APP_SECRET="${config.secret}"
129 '';
130 postInstall = ''
131 ln -sf ${yarnModules}/node_modules .
132 yarn run --offline encore production
133 rm -rf var/{log,cache}
134 ln -sf ${varDir}/{log,cache} var/
135 '';
136 buildInputs = [ yarnModules yarn2nixPackage.yarn ];
137 });
138 webRoot = "${webappDir}/public";
139 };
140 in
141 aten