]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - modules/private/websites/tools/cloud/default.nix
Migrate php sessions to redis
[perso/Immae/Config/Nix.git] / modules / private / websites / tools / cloud / default.nix
1 { lib, pkgs, config, ... }:
2 let
3 phpPackage = (pkgs.php74.withExtensions({ enabled, all }: enabled ++ [ all.redis all.apcu all.opcache all.imagick ])).override { extraConfig = ''
4 apc.enable_cli = 1
5 '';
6 };
7 nextcloud = pkgs.webapps.nextcloud_22.withApps (a: [
8 a.apporder a.audioplayer a.bookmarks a.calendar a.carnet a.contacts
9 a.cookbook a.deck a.extract a.files_markdown a.files_readmemd a.files_mindmap
10 a.flowupload a.gpxedit a.gpxpod a.keeweb a.maps a.metadata a.music
11 a.notes a.passman a.polls a.spreed a.tasks
12 ]);
13 env = config.myEnv.tools.nextcloud;
14 varDir = "/var/lib/nextcloud";
15 cfg = config.myServices.websites.tools.cloud;
16 phpFpm = rec {
17 basedir = builtins.concatStringsSep ":" ([ nextcloud varDir ] ++ nextcloud.apps);
18 pool = {
19 "listen.owner" = "wwwrun";
20 "listen.group" = "wwwrun";
21 "pm" = "dynamic";
22 "pm.max_children" = "60";
23 "pm.start_servers" = "2";
24 "pm.min_spare_servers" = "2";
25 "pm.max_spare_servers" = "3";
26 "pm.process_idle_timeout" = "60";
27
28 "php_admin_value[output_buffering]" = "0";
29 "php_admin_value[max_execution_time]" = "1800";
30 "php_admin_value[zend_extension]" = "opcache";
31 #already enabled by default?
32 #"php_value[opcache.enable]" = "1";
33 "php_value[opcache.enable_cli]" = "1";
34 "php_value[opcache.interned_strings_buffer]" = "8";
35 "php_value[opcache.max_accelerated_files]" = "10000";
36 "php_value[opcache.memory_consumption]" = "128";
37 "php_value[opcache.save_comments]" = "1";
38 "php_value[opcache.revalidate_freq]" = "1";
39 "php_admin_value[memory_limit]" = "512M";
40
41 "php_admin_value[open_basedir]" = "/run/wrappers/bin/sendmail:${basedir}:/proc/meminfo:/dev/urandom:/proc/self/fd:/tmp";
42 "php_admin_value[session.save_handler]" = "redis";
43 "php_admin_value[session.save_path]" = "'unix:///run/redis-php-sessions/redis.sock?persistent=1&prefix=Tools:Nextcloud:'";
44 };
45 };
46 in {
47 options.myServices.websites.tools.cloud = {
48 enable = lib.mkEnableOption "enable cloud website";
49 };
50
51 config = lib.mkIf cfg.enable {
52 myServices.chatonsProperties.hostings.nextcloud = {
53 file.datetime = "2022-08-21T19:50:00";
54 hosting = {
55 name = "Nextcloud";
56 description = "The self-hosted productivity platform that keeps you in control";
57 website = "https://cloud.immae.eu/";
58 logo = "https://cloud.immae.eu/core/img/favicon.ico";
59 type = "INSTANCE";
60 status.level = "OK";
61 status.description = "OK";
62 registration.load = "OPEN";
63 install.type = "PACKAGE";
64 };
65 };
66 myServices.chatonsProperties.services.nextcloud = {
67 file.datetime = "2022-08-21T19:50:00";
68 service = {
69 name = "Nextcloud";
70 description = "The self-hosted productivity platform that keeps you in control";
71 website = "https://cloud.immae.eu/";
72 logo = "https://cloud.immae.eu/core/img/favicon.ico";
73 status.level = "OK";
74 status.description = "OK";
75 registration."" = ["MEMBER" "CLIENT"];
76 registration.load = "OPEN";
77 install.type = "PACKAGE";
78 guide.user = "https://www.immae.eu/docs/nextcloud.html";
79 };
80 software = {
81 name = "Nextcloud";
82 website = "https://nextcloud.com/";
83 license.url = "https://github.com/nextcloud/server/blob/master/COPYING";
84 license.name = "GNU Affero General Public License v3.0";
85 version = nextcloud.version;
86 source.url = "https://github.com/nextcloud/server";
87 modules = map (a: a.appName) nextcloud.apps;
88 };
89 };
90 services.websites.env.tools.modules = [ "proxy_fcgi" ];
91
92 services.websites.env.tools.vhostConfs.cloud = {
93 certName = "eldiron";
94 addToCerts = true;
95 hosts = ["cloud.immae.eu" ];
96 root = nextcloud;
97 extraConfig = [
98 ''
99 SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
100 <Directory ${nextcloud}>
101 AcceptPathInfo On
102 DirectoryIndex index.php
103 Options FollowSymlinks
104 Require all granted
105 AllowOverride all
106
107 <IfModule mod_headers.c>
108 Header always set Strict-Transport-Security "max-age=15552000; includeSubDomains; preload"
109 </IfModule>
110 <FilesMatch "\.php$">
111 CGIPassAuth on
112 SetHandler "proxy:unix:${config.services.phpfpm.pools.nextcloud.socket}|fcgi://localhost"
113 </FilesMatch>
114
115 </Directory>
116 ''
117 ];
118 };
119
120 secrets.keys."webapps/tools-nextcloud" = {
121 user = "wwwrun";
122 group = "wwwrun";
123 permissions = "0600";
124 # This file is not actually included, see activationScript below
125 text = ''
126 <?php
127 include('${nextcloud}/version.php');
128 $CONFIG = array (
129 // FIXME: change this value when nextcloud starts getting slow
130 'instanceid' => '${env.instance_id}',
131 'datadirectory' => '/var/lib/nextcloud/',
132 'passwordsalt' => '${env.password_salt}',
133 'debug' => false,
134 'dbtype' => 'pgsql',
135 'version' => implode($OC_Version, '.'),
136 'dbname' => '${env.postgresql.database}',
137 'dbhost' => '${env.postgresql.socket}',
138 'dbtableprefix' => 'oc_',
139 'dbuser' => '${env.postgresql.user}',
140 'dbpassword' => '${env.postgresql.password}',
141 'installed' => true,
142 'maxZipInputSize' => 0,
143 'allowZipDownload' => true,
144 'forcessl' => true,
145 'theme' => ${"''"},
146 'remember_login_cookie_lifetime' => 60*60*24*30,
147 'session_keepalive' => true,
148 'session_lifefime' => 60*60*24*30,
149 'maintenance' => false,
150 'trusted_domains' =>
151 array (
152 0 => 'cloud.immae.eu',
153 ),
154 'secret' => '${env.secret}',
155 'appstoreenabled' => false,
156 'appstore.experimental.enabled' => true,
157 'loglevel' => 2,
158 'trashbin_retention_obligation' => 'auto',
159 'htaccess.RewriteBase' => '/',
160 'mail_smtpmode' => 'sendmail',
161 'mail_smtphost' => '127.0.0.1',
162 'mail_smtpname' => ''',
163 'mail_smtppassword' => ''',
164 'mail_from_address' => 'nextcloud',
165 'mail_smtpauth' => false,
166 'mail_domain' => 'tools.immae.eu',
167 'memcache.local' => '\\OC\\Memcache\\APCu',
168 'memcache.locking' => '\\OC\\Memcache\\Redis',
169 'filelocking.enabled' => true,
170 'redis' =>
171 array (
172 'host' => '${env.redis.socket}',
173 'port' => 0,
174 'dbindex' => ${env.redis.db},
175 ),
176 'overwrite.cli.url' => 'https://cloud.immae.eu',
177 'ldapIgnoreNamingRules' => false,
178 'ldapProviderFactory' => '\\OCA\\User_LDAP\\LDAPProviderFactory',
179 'has_rebuilt_cache' => true,
180 );
181 '';
182 };
183 users.users.root.packages = let
184 occ = pkgs.writeScriptBin "nextcloud-occ" ''
185 #! ${pkgs.stdenv.shell}
186 cd ${nextcloud}
187 NEXTCLOUD_CONFIG_DIR="${nextcloud}/config" \
188 exec \
189 sudo -E -u wwwrun ${phpPackage}/bin/php \
190 -c ${phpPackage}/etc/php.ini \
191 occ $*
192 '';
193 in [ occ ];
194
195 system.activationScripts.nextcloud = {
196 deps = [ "secrets" ];
197 text = let
198 confs = lib.attrsets.mapAttrs (n: v: pkgs.writeText "${n}.json" (builtins.toJSON v)) nextcloud.otherConfig;
199 in
200 ''
201 install -m 0755 -o wwwrun -g wwwrun -d ${varDir}
202 ${builtins.concatStringsSep "\n" (lib.attrsets.mapAttrsToList (n: v:
203 "install -D -m 0644 -o wwwrun -g wwwrun -T ${v} ${varDir}/config/${n}.json"
204 ) confs)}
205 #install -D -m 0600 -o wwwrun -g wwwrun -T ${config.secrets.fullPaths."webapps/tools-nextcloud"} ${varDir}/config/config.php
206 '';
207 };
208
209 services.phpfpm.pools.nextcloud = {
210 user = "wwwrun";
211 group = "wwwrun";
212 settings = phpFpm.pool;
213 inherit phpPackage;
214 };
215
216 services.cron = {
217 enable = true;
218 systemCronJobs = let
219 script = pkgs.writeScriptBin "nextcloud-cron" ''
220 #! ${pkgs.stdenv.shell}
221 export LOCALE_ARCHIVE=/run/current-system/sw/lib/locale/locale-archive
222 export PATH=/run/wrappers/bin:$PATH
223 ${phpPackage}/bin/php -d memory_limit=2048M -f ${nextcloud}/cron.php
224 '';
225 in [
226 ''
227 */15 * * * * wwwrun ${script}/bin/nextcloud-cron
228 ''
229 ];
230 };
231 };
232 }