]> git.immae.eu Git - perso/Immae/Config/Nix.git/blobdiff - modules/private/websites/tools/cloud/farm.nix
Bump Nextcloud to latest version
[perso/Immae/Config/Nix.git] / modules / private / websites / tools / cloud / farm.nix
index 7be774c94e95a52bf459c5169787a013d6bbece1..de1cfaecb1056cdf5c62391c2dfa6e5a2bbf9889 100644 (file)
@@ -3,20 +3,14 @@ let
   cfg = config.myServices.tools.cloud.farm;
   apacheUser = config.services.httpd.Prod.user;
   apacheGroup = config.services.httpd.Prod.group;
-  nextcloud = (pkgs.webapps.nextcloud.override { varDir = null; }).withApps (a: [
-    a.apporder a.audioplayer a.bookmarks a.calendar a.carnet a.contacts
-    a.cookbook a.deck a.extract a.files_markdown a.files_readmemd
-    a.flowupload a.gpxedit a.gpxpod a.impersonate a.keeweb a.maps
-    a.metadata a.music a.notes a.ocsms a.passman a.polls a.spreed
-    a.tasks
-  ]);
   toVardir = name: "/var/lib/nextcloud_farm/${name}";
-  varDirs = map toVardir cfg.instances;
-  phpBaseDir = builtins.concatStringsSep ":" ([ nextcloud ] ++ varDirs ++ nextcloud.apps);
+  varDirs = lib.mapAttrsToList (name: v: toVardir name) cfg.instances;
+  toPhpBaseDir = name: [ cfg.rootDirs."${name}" (toVardir name) ] ++ cfg.rootDirs."${name}".apps;
+  phpBaseDir = builtins.concatStringsSep ":" (lib.unique (lib.flatten (lib.mapAttrsToList (name: v: toPhpBaseDir name) cfg.instances)));
   toVhost = name: ''
     SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
     SetEnv NEXTCLOUD_CONFIG_DIR "${toVardir name}"
-    <Directory ${nextcloud}>
+    <Directory ${cfg.rootDirs."${name}"}>
       AcceptPathInfo On
       DirectoryIndex index.php
       Options FollowSymlinks
@@ -33,29 +27,47 @@ let
 
     </Directory>
     '';
+  phpPackage = (pkgs.php74.withExtensions({ enabled, all }: enabled ++ [ all.redis all.apcu all.opcache all.imagick ])).override { extraConfig = ''
+    apc.enable_cli = 1
+    '';
+  };
 in
 {
   options.myServices.tools.cloud.farm = {
     instances = lib.mkOption {
       description = "Instances names for the nextcloud Farm";
-      default = [];
-      type = lib.types.listOf lib.types.str;
+      default = {};
+      type = lib.types.attrsOf (lib.types.submodule {
+        options = {
+          nextcloud = lib.mkOption {
+            description = "Nextcloud version to use";
+            default = pkgs.webapps.nextcloud_20;
+            type = lib.types.package;
+          };
+          apps = lib.mkOption {
+            description = "Applications to use";
+            default = a: [];
+            #type = functionTo (listOf packages)
+            type = lib.types.unspecified;
+          };
+        };
+      });
+    };
+    rootDirs = lib.mkOption {
+      description = "Instance root dirs";
+      readOnly = true;
+      type = lib.types.attrsOf lib.types.package;
+      default = lib.mapAttrs (name: v: (v.nextcloud.override { varDir = null; }).withApps v.apps) cfg.instances;
     };
     vhosts = lib.mkOption {
       description = "Instance vhosts configs";
       readOnly = true;
       type = lib.types.attrsOf lib.types.str;
-      default = lib.genAttrs cfg.instances toVhost;
-    };
-    package = lib.mkOption {
-      description = "Nextcloud derivation";
-      readOnly = true;
-      type = lib.types.package;
-      default = nextcloud;
+      default = lib.mapAttrs (name: v: toVhost name) cfg.instances;
     };
   };
 
-  config = lib.mkIf (builtins.length cfg.instances > 0) {
+  config = lib.mkIf (builtins.length (builtins.attrNames cfg.instances) > 0) {
     system.activationScripts.cloud_farm_vardirs = {
       deps = [ "httpd" ];
       text = ''
@@ -68,16 +80,23 @@ in
     services.phpfpm.pools.nextcloud_farm = {
       user = apacheUser;
       group = apacheGroup;
-      settings = {
+      settings = let
+        instanceNb = builtins.length (builtins.attrNames cfg.instances);
+      in {
         "listen.owner" = apacheUser;
         "listen.group" = apacheGroup;
-        "pm" = "ondemand";
-        "pm.max_children" = "60";
+        "pm" = "dynamic";
+        "pm.max_children" = builtins.toString (60 * instanceNb);
+        "pm.start_servers" = builtins.toString (3 * instanceNb);
+        "pm.min_spare_servers" = builtins.toString (3 * instanceNb);
+        "pm.max_spare_servers" = builtins.toString (5 * instanceNb);
         "pm.process_idle_timeout" = "60";
 
         "php_admin_value[output_buffering]" = "0";
         "php_admin_value[max_execution_time]" = "1800";
         "php_admin_value[zend_extension]" = "opcache";
+        "php_value[apcu.enable_cli]" = "1";
+        "php_value[apcu.enabled]" = "1";
         #already enabled by default?
         #"php_value[opcache.enable]" = "1";
         "php_value[opcache.enable_cli]" = "1";
@@ -91,19 +110,19 @@ in
         "php_admin_value[open_basedir]" = "/run/wrappers/bin/sendmail:${phpBaseDir}:/proc/meminfo:/dev/urandom:/proc/self/fd:/tmp";
         "php_admin_value[session.save_path]" = "/var/lib/nextcloud_farm/phpSessions";
       };
-      phpPackage = pkgs.php74.withExtensions({ enabled, all }: enabled ++ [ all.redis all.apcu all.opcache ]);
+      inherit phpPackage;
     };
     users.users.root.packages = let
       toOcc = name: pkgs.writeScriptBin "nextcloud-occ-${name}" ''
         #! ${pkgs.stdenv.shell}
-        cd ${nextcloud}
+        cd ${cfg.rootDirs."${name}"}
         NEXTCLOUD_CONFIG_DIR="${toVardir name}" \
           exec \
-          sudo -E -u wwwrun ${pkgs.php74}/bin/php \
-          -c ${pkgs.php74}/etc/php.ini \
+          sudo -E -u wwwrun ${phpPackage}/bin/php \
+          -c ${phpPackage}/etc/php.ini \
           occ $*
         '';
-      in map toOcc cfg.instances;
+      in lib.mapAttrsToList (name: v: toOcc name) cfg.instances;
     services.cron = {
       enable = true;
       systemCronJobs = let
@@ -112,12 +131,12 @@ in
           export LOCALE_ARCHIVE=/run/current-system/sw/lib/locale/locale-archive
           export PATH=/run/wrappers/bin:$PATH
           export NEXTCLOUD_CONFIG_DIR="${toVardir name}"
-          ${pkgs.php74}/bin/php -d memory_limit=512M -f ${nextcloud}/cron.php
+          ${phpPackage}/bin/php -c ${phpPackage}/etc/php.ini -d memory_limit=512M -f ${cfg.rootDirs."${name}"}/cron.php
           '';
         toLine = name: ''
           */15 * * * * wwwrun ${toScript name}/bin/nextcloud-cron
           '';
-        in map toLine cfg.instances;
+        in lib.mapAttrsToList (name: v: toLine name) cfg.instances;
     };
   };
 }