]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - modules/private/websites/tools/cloud/default.nix
50ed528a53dc1b8a35d324f729050a978d86e48f
[perso/Immae/Config/Nix.git] / modules / private / websites / tools / cloud / default.nix
1 { lib, pkgs, config, ... }:
2 let
3 nextcloud = pkgs.webapps.nextcloud.withApps (a: [
4 a.apporder a.audioplayer a.bookmarks a.calendar a.carnet a.contacts
5 a.cookbook a.deck a.extract a.files_markdown a.files_readmemd
6 a.flowupload a.gpxedit a.gpxpod a.keeweb a.maps a.metadata a.music
7 a.notes a.ocsms a.passman a.polls a.spreed a.tasks
8 ]);
9 env = config.myEnv.tools.nextcloud;
10 varDir = "/var/lib/nextcloud";
11 webappName = "tools_nextcloud";
12 apacheRoot = "/run/current-system/webapps/${webappName}";
13 cfg = config.myServices.websites.tools.cloud;
14 phpFpm = rec {
15 basedir = builtins.concatStringsSep ":" ([ nextcloud varDir ] ++ nextcloud.apps);
16 pool = {
17 "listen.owner" = "wwwrun";
18 "listen.group" = "wwwrun";
19 "pm" = "ondemand";
20 "pm.max_children" = "60";
21 "pm.process_idle_timeout" = "60";
22
23 "php_admin_value[output_buffering]" = "0";
24 "php_admin_value[max_execution_time]" = "1800";
25 "php_admin_value[zend_extension]" = "opcache";
26 #already enabled by default?
27 #"php_value[opcache.enable]" = "1";
28 "php_value[opcache.enable_cli]" = "1";
29 "php_value[opcache.interned_strings_buffer]" = "8";
30 "php_value[opcache.max_accelerated_files]" = "10000";
31 "php_value[opcache.memory_consumption]" = "128";
32 "php_value[opcache.save_comments]" = "1";
33 "php_value[opcache.revalidate_freq]" = "1";
34 "php_admin_value[memory_limit]" = "512M";
35
36 "php_admin_value[open_basedir]" = "/run/wrappers/bin/sendmail:${basedir}:/proc/meminfo:/dev/urandom:/proc/self/fd:/tmp";
37 "php_admin_value[session.save_path]" = "${varDir}/phpSessions";
38 };
39 };
40 in {
41 options.myServices.websites.tools.cloud = {
42 enable = lib.mkEnableOption "enable cloud website";
43 };
44
45 config = lib.mkIf cfg.enable {
46 # FIXME: find a way to backup the data
47 services.websites.env.tools.modules = [ "proxy_fcgi" ];
48
49 services.websites.env.tools.vhostConfs.cloud = {
50 certName = "eldiron";
51 addToCerts = true;
52 hosts = ["cloud.immae.eu" ];
53 root = apacheRoot;
54 extraConfig = [
55 ''
56 SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
57 <Directory ${apacheRoot}>
58 AcceptPathInfo On
59 DirectoryIndex index.php
60 Options FollowSymlinks
61 Require all granted
62 AllowOverride all
63
64 <IfModule mod_headers.c>
65 Header always set Strict-Transport-Security "max-age=15552000; includeSubDomains; preload"
66 </IfModule>
67 <FilesMatch "\.php$">
68 CGIPassAuth on
69 SetHandler "proxy:unix:${config.services.phpfpm.pools.nextcloud.socket}|fcgi://localhost"
70 </FilesMatch>
71
72 </Directory>
73 ''
74 ];
75 };
76
77 secrets.keys = [{
78 dest = "webapps/tools-nextcloud";
79 user = "wwwrun";
80 group = "wwwrun";
81 permissions = "0600";
82 # This file is not actually included, see activationScript below
83 text = ''
84 <?php
85 include('${nextcloud}/version.php');
86 $CONFIG = array (
87 // FIXME: change this value when nextcloud starts getting slow
88 'instanceid' => '${env.instance_id}1',
89 'datadirectory' => '/var/lib/nextcloud/',
90 'passwordsalt' => '${env.password_salt}',
91 'debug' => false,
92 'dbtype' => 'pgsql',
93 'version' => implode($OC_Version, '.'),
94 'dbname' => '${env.postgresql.database}',
95 'dbhost' => '${env.postgresql.socket}',
96 'dbtableprefix' => 'oc_',
97 'dbuser' => '${env.postgresql.user}',
98 'dbpassword' => '${env.postgresql.password}',
99 'installed' => true,
100 'maxZipInputSize' => 0,
101 'allowZipDownload' => true,
102 'forcessl' => true,
103 'theme' => ${"''"},
104 'maintenance' => false,
105 'trusted_domains' =>
106 array (
107 0 => 'cloud.immae.eu',
108 ),
109 'secret' => '${env.secret}',
110 'appstoreenabled' => false,
111 'appstore.experimental.enabled' => true,
112 'loglevel' => 2,
113 'trashbin_retention_obligation' => 'auto',
114 'htaccess.RewriteBase' => '/',
115 'mail_smtpmode' => 'sendmail',
116 'mail_smtphost' => '127.0.0.1',
117 'mail_smtpname' => ''',
118 'mail_smtppassword' => ''',
119 'mail_from_address' => 'nextcloud',
120 'mail_smtpauth' => false,
121 'mail_domain' => 'tools.immae.eu',
122 'memcache.local' => '\\OC\\Memcache\\APCu',
123 'memcache.locking' => '\\OC\\Memcache\\Redis',
124 'filelocking.enabled' => true,
125 'redis' =>
126 array (
127 'host' => '${env.redis.socket}',
128 'port' => 0,
129 'dbindex' => ${env.redis.db},
130 ),
131 'overwrite.cli.url' => 'https://cloud.immae.eu',
132 'ldapIgnoreNamingRules' => false,
133 'ldapProviderFactory' => '\\OCA\\User_LDAP\\LDAPProviderFactory',
134 'has_rebuilt_cache' => true,
135 );
136 '';
137 }];
138 users.users.root.packages = let
139 occ = pkgs.writeScriptBin "nextcloud-occ" ''
140 #! ${pkgs.stdenv.shell}
141 cd ${nextcloud}
142 NEXTCLOUD_CONFIG_DIR="${nextcloud}/config" \
143 exec \
144 sudo -u wwwrun ${pkgs.php74}/bin/php \
145 -c ${pkgs.php74}/etc/php.ini \
146 occ $*
147 '';
148 in [ occ ];
149
150 system.activationScripts.nextcloud = {
151 deps = [ "secrets" ];
152 text = let
153 confs = lib.attrsets.mapAttrs (n: v: pkgs.writeText "${n}.json" (builtins.toJSON v)) nextcloud.otherConfig;
154 in
155 ''
156 install -m 0755 -o wwwrun -g wwwrun -d ${varDir}
157 install -m 0750 -o wwwrun -g wwwrun -d ${varDir}/phpSessions
158 ${builtins.concatStringsSep "\n" (lib.attrsets.mapAttrsToList (n: v:
159 "install -D -m 0644 -o wwwrun -g wwwrun -T ${v} ${varDir}/config/${n}.json"
160 ) confs)}
161 #install -D -m 0600 -o wwwrun -g wwwrun -T /var/secrets/webapps/tools-nextcloud ${varDir}/config/config.php
162 '';
163 };
164 # FIXME: add a warning when config.php changes
165 system.extraSystemBuilderCmds = ''
166 mkdir -p $out/webapps
167 ln -s ${nextcloud} $out/webapps/${webappName}
168 '';
169
170 services.phpfpm.pools.nextcloud = {
171 user = "wwwrun";
172 group = "wwwrun";
173 settings = phpFpm.pool;
174 phpPackage = pkgs.php74.withExtensions({ enabled, all }: enabled ++ [ all.redis all.apcu all.opcache ]);
175 };
176
177 services.cron = {
178 enable = true;
179 systemCronJobs = let
180 script = pkgs.writeScriptBin "nextcloud-cron" ''
181 #! ${pkgs.stdenv.shell}
182 export LOCALE_ARCHIVE=/run/current-system/sw/lib/locale/locale-archive
183 export PATH=/run/wrappers/bin:$PATH
184 ${pkgs.php74}/bin/php -d memory_limit=512M -f ${nextcloud}/cron.php
185 '';
186 in [
187 ''
188 */15 * * * * wwwrun ${script}/bin/nextcloud-cron
189 ''
190 ];
191 };
192 };
193 }