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