]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - nixops/modules/websites/aten/aten.nix
c35af6f098ee3dc1ea936ee397c7f8549486a1ee
[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 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
16 sha512sum /var/secrets/webapps/${environment}-aten > ${varDir}/currentKey
17 fi
18 '';
19 serviceDeps = [ "postgresql.service" ];
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"
31 php_admin_value[session.save_path] = "${varDir}/phpSessions"
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 };
45 keys = [{
46 dest = "webapps/${environment}-aten";
47 user = apache.user;
48 group = apache.group;
49 permissions = "0400";
50 text = ''
51 SetEnv APP_ENV "${environment}"
52 SetEnv APP_SECRET "${config.secret}"
53 SetEnv DATABASE_URL "${config.psql_url}"
54 '';
55 }];
56 apache = rec {
57 user = "wwwrun";
58 group = "wwwrun";
59 modules = [ "proxy_fcgi" ];
60 webappName = "aten_${environment}";
61 root = "/run/current-system/webapps/${webappName}";
62 vhostConf = ''
63 <FilesMatch "\.php$">
64 SetHandler "proxy:unix:${phpFpm.socket}|fcgi://localhost"
65 </FilesMatch>
66
67 Include /var/secrets/webapps/${environment}-aten
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 ''
82 Use Stats aten.pro
83
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
91 <Directory ${root}>
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}
104 install -m 0750 -o ${apache.user} -g ${apache.group} -d ${varDir}/phpSessions
105 '';
106 };
107 yarnModules = let
108 info = fetchedGitPrivate ./aten.json;
109 packagejson = runCommand "package.json" { buildInputs = [ jq ]; } ''
110 cat ${info.src}/package.json | jq -r '.version = "v1.0.0"|.name="aten"' > $out
111 '';
112 in
113 yarn2nixPackage.mkYarnModules rec {
114 name = "aten-yarn";
115 pname = name;
116 version = "v1.0.0";
117 packageJSON = packagejson;
118 yarnLock = "${info.src}/yarn.lock";
119 pkgConfig = {
120 all = {
121 buildInputs = [ yarn2nixPackage.src ];
122 };
123 node-sass = {
124 buildInputs = [ libsass python ];
125 postInstall = let
126 nodeHeaders = fetchurl {
127 url = "https://nodejs.org/download/release/v${nodejs.version}/node-v${nodejs.version}-headers.tar.gz";
128 sha256 = "16f20ya3ys6w5w6y6l4536f7jrgk4gz46bf71w1r1xxb26a54m32";
129 };
130 in
131 ''
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}"
145 '';
146 postInstall = ''
147 ln -sf ${yarnModules}/node_modules .
148 yarn run --offline encore production
149 rm -rf var/{log,cache}
150 ln -sf ${varDir}/{log,cache} var/
151 '';
152 buildInputs = [ yarnModules yarn2nixPackage.yarn ];
153 });
154 webRoot = "${webappDir}/public";
155 };
156 in
157 aten