about summary refs log tree commit diff
path: root/nixos/modules/services
diff options
context:
space:
mode:
authorSophie Tauchert <sophie@999eagle.moe>2023-07-06 22:42:36 +0200
committerSophie Tauchert <sophie@999eagle.moe>2023-09-18 08:24:35 +0200
commit72a26e2b5465b967c462e08b8c64151356683c17 (patch)
treee8295f66fa32e82a83ff07c4a7eaba605ea8c4d6 /nixos/modules/services
parentb32918012814fcca1c5cd6c815b5565d71e96610 (diff)
downloadnixlib-72a26e2b5465b967c462e08b8c64151356683c17.tar
nixlib-72a26e2b5465b967c462e08b8c64151356683c17.tar.gz
nixlib-72a26e2b5465b967c462e08b8c64151356683c17.tar.bz2
nixlib-72a26e2b5465b967c462e08b8c64151356683c17.tar.lz
nixlib-72a26e2b5465b967c462e08b8c64151356683c17.tar.xz
nixlib-72a26e2b5465b967c462e08b8c64151356683c17.tar.zst
nixlib-72a26e2b5465b967c462e08b8c64151356683c17.zip
nixos/synapse: add options to configure workers
Diffstat (limited to 'nixos/modules/services')
-rw-r--r--nixos/modules/services/matrix/synapse.nix99
1 files changed, 91 insertions, 8 deletions
diff --git a/nixos/modules/services/matrix/synapse.nix b/nixos/modules/services/matrix/synapse.nix
index 60ad1fa42bc7..b7591f984730 100644
--- a/nixos/modules/services/matrix/synapse.nix
+++ b/nixos/modules/services/matrix/synapse.nix
@@ -10,13 +10,10 @@ let
   finalSettings = lib.filterAttrsRecursive (_: v: v != null) cfg.settings;
   configFile = format.generate "homeserver.yaml" finalSettings;
 
-  pluginsEnv = cfg.package.python.buildEnv.override {
-    extraLibs = cfg.plugins;
-  };
-
   usePostgresql = cfg.settings.database.name == "psycopg2";
   hasLocalPostgresDB = let args = cfg.settings.database.args; in
     usePostgresql && (!(args ? host) || (elem args.host [ "localhost" "127.0.0.1" "::1" ]));
+  hasWorkers = cfg.workers.enable && (cfg.workers.config != { });
 
   registerNewMatrixUser =
     let
@@ -758,6 +755,45 @@ in {
         };
       };
 
+      workers = lib.mkOption {
+        default = { };
+        description = lib.mdDoc ''
+          Options for configuring workers. See `services.matrix-synapse.workers.enable`
+          for a more detailed description.
+        '';
+        type = types.submodule {
+          options = {
+            enable = lib.mkOption {
+              type = types.bool;
+              default = false;
+              description = lib.mdDoc ''
+                Whether to enable matrix synapse workers
+              '';
+            };
+            config = lib.mkOption {
+              type = types.attrsOf (types.submodule {
+                freeformType = format.type;
+                options = {
+                  worker_listeners = lib.mkOption {
+                    default = [ ];
+                    type = types.listOf listenerType;
+                    description = lib.mdDoc ''
+                      List of ports that this worker should listen on, their purpose and their configuration.
+                    '';
+                  };
+                };
+              });
+              default = { };
+              description = lib.mdDoc ''
+                List of workers to configure. See the
+                [worker documention](https://matrix-org.github.io/synapse/latest/workers.html#worker-configuration)
+                for possible values.
+              '';
+            };
+          };
+        };
+      };
+
       extraConfigFiles = mkOption {
         type = types.listOf types.path;
         default = [ ];
@@ -800,6 +836,13 @@ in {
           For further information about this update, please read the release-notes of 20.03 carefully.
         '';
       }
+      {
+        assertion = hasWorkers -> cfg.settings.redis.enabled;
+        message = ''
+          Workers for matrix-synapse require configuring a redis instance. This can be done
+          automatically by setting `services.matrix-synapse.configureRedisLocally = true`.
+        '';
+      }
     ];
 
     services.matrix-synapse.settings.redis = lib.mkIf cfg.configureRedisLocally {
@@ -825,11 +868,26 @@ in {
       gid = config.ids.gids.matrix-synapse;
     };
 
+    systemd.targets.matrix-synapse = lib.mkIf hasWorkers {
+      description = "Synapse Matrix parent target";
+      after = [ "network.target" ] ++ optional hasLocalPostgresDB "postgresql.service";
+      wantedBy = [ "multi-user.target" ];
+    };
+
     systemd.services =
       let
+        targetConfig =
+          if hasWorkers
+          then {
+            partOf = [ "matrix-synapse.target" ];
+            wantedBy = [ "matrix-synapse.target" ];
+            unitConfig.ReloadPropagatedFrom = "matrix-synapse.target";
+          }
+          else {
+            after = [ "network.target" ] ++ optional hasLocalPostgresDB "postgresql.service";
+            wantedBy = [ "multi-user.target" ];
+          };
         baseServiceConfig = {
-          after = [ "network.target" ] ++ optional hasLocalPostgresDB "postgresql.service";
-          wantedBy = [ "multi-user.target" ];
           environment = optionalAttrs (cfg.withJemalloc) {
             LD_PRELOAD = "${pkgs.jemalloc}/lib/libjemalloc.so";
           };
@@ -869,7 +927,31 @@ in {
             SystemCallArchitectures = "native";
             SystemCallFilter = [ "@system-service" "~@resources" "~@privileged" ];
           };
-        };
+        }
+        // targetConfig;
+        genWorkerService = name: workerCfg:
+          let
+            finalWorkerCfg = workerCfg // { worker_name = name; };
+            workerConfigFile = format.generate "worker-${name}.yaml" finalWorkerCfg;
+          in
+          {
+            name = "matrix-synapse-worker-${name}";
+            value = lib.mkMerge [
+              baseServiceConfig
+              {
+                description = "Synapse Matrix worker ${name}";
+                # make sure the main process starts first for potential database migrations
+                after = [ "matrix-synapse.service" ];
+                serviceConfig = {
+                  ExecStart = ''
+                    ${cfg.package}/bin/synapse_worker \
+                      ${ concatMapStringsSep "\n  " (x: "--config-path ${x} \\") ([ configFile workerConfigFile ] ++ cfg.extraConfigFiles) }
+                      --keys-directory ${cfg.dataDir}
+                  '';
+                };
+              }
+            ];
+          };
       in
       {
         matrix-synapse = lib.mkMerge [
@@ -897,7 +979,8 @@ in {
             };
           }
         ];
-      };
+      }
+      // (lib.mapAttrs' genWorkerService cfg.workers.config);
 
     services.redis.servers.matrix-synapse = lib.mkIf cfg.configureRedisLocally {
       enable = true;