about summary refs log tree commit diff
path: root/nixos/modules/services/matrix
diff options
context:
space:
mode:
authorMartin Weinelt <hexa@darmstadt.ccc.de>2023-03-15 13:22:45 +0100
committerMartin Weinelt <hexa@darmstadt.ccc.de>2023-07-18 17:53:27 +0200
commit1076c3ada61204581af579474791fc67451a7b39 (patch)
treef0cabc995487d527d7e5786783b93808f7e3578f /nixos/modules/services/matrix
parent3453128510040c6ad343b98cd44eab5397c63c2e (diff)
downloadnixlib-1076c3ada61204581af579474791fc67451a7b39.tar
nixlib-1076c3ada61204581af579474791fc67451a7b39.tar.gz
nixlib-1076c3ada61204581af579474791fc67451a7b39.tar.bz2
nixlib-1076c3ada61204581af579474791fc67451a7b39.tar.lz
nixlib-1076c3ada61204581af579474791fc67451a7b39.tar.xz
nixlib-1076c3ada61204581af579474791fc67451a7b39.tar.zst
nixlib-1076c3ada61204581af579474791fc67451a7b39.zip
nixos/matrix-synapse: Allow passing extras, discover extras from config
With this change we allow the user to configure extras, exposed as
optional-dependencies on the matrix-synapse package.

The vertical integration between package, user configuration and
deployment is a huge boon which then allows us to dynamically adapt the
python environment the service runs in, by inspecting the configuration
and autodiscovering certain used extras from config paths.
Diffstat (limited to 'nixos/modules/services/matrix')
-rw-r--r--nixos/modules/services/matrix/synapse.nix74
1 files changed, 62 insertions, 12 deletions
diff --git a/nixos/modules/services/matrix/synapse.nix b/nixos/modules/services/matrix/synapse.nix
index 3dca3ff94f21..5185e2ed8d39 100644
--- a/nixos/modules/services/matrix/synapse.nix
+++ b/nixos/modules/services/matrix/synapse.nix
@@ -9,11 +9,6 @@ let
   # remove null values from the final configuration
   finalSettings = lib.filterAttrsRecursive (_: v: v != null) cfg.settings;
   configFile = format.generate "homeserver.yaml" finalSettings;
-  logConfigFile = format.generate "log_config.yaml" cfg.logConfig;
-
-  pluginsEnv = cfg.package.python.buildEnv.override {
-    extraLibs = cfg.plugins;
-  };
 
   usePostgresql = cfg.settings.database.name == "psycopg2";
   hasLocalPostgresDB = let args = cfg.settings.database.args; in
@@ -50,6 +45,30 @@ let
             "${bindAddress}"
         }:${builtins.toString listener.port}/"
     '';
+
+  defaultExtras = [
+    "systemd"
+    "postgres"
+    "url-preview"
+    "user-search"
+  ];
+
+  wantedExtras = cfg.extras
+    ++ lib.optional (cfg.settings ? oidc_providers) "oidc"
+    ++ lib.optional (cfg.settings ? jwt_config) "jwt"
+    ++ lib.optional (cfg.settings ? saml2_config) "saml2"
+    ++ lib.optional (cfg.settings ? opentracing) "opentracing"
+    ++ lib.optional (cfg.settings ? redis) "redis"
+    ++ lib.optional (cfg.settings ? sentry) "sentry"
+    ++ lib.optional (cfg.settings ? user_directory) "user-search"
+    ++ lib.optional (cfg.settings.url_preview_enabled) "url-preview"
+    ++ lib.optional (cfg.settings.database.name == "psycopg2") "postgres";
+
+  wrapped = pkgs.matrix-synapse.override {
+    matrix-synapse-unwrapped = cfg.package.unwrapped;
+    extras = wantedExtras;
+    inherit (cfg) plugins;
+  };
 in {
 
   imports = [
@@ -153,8 +172,38 @@ in {
         type = types.package;
         default = pkgs.matrix-synapse;
         defaultText = literalExpression "pkgs.matrix-synapse";
+        readOnly = true;
+        description = lib.mdDoc ''
+          Wrapper package that gets configured through the module.
+
+          If you want to override the unwrapped package use an overlay.
+        '';
+      };
+
+      extras = mkOption {
+        type = types.listOf (types.enum (lib.attrNames cfg.package.unwrapped.optional-dependencies));
+        default = defaultExtras;
+        example = literalExpression ''
+          [
+            "cache-memory" # Provide statistics about caching memory consumption
+            "jwt"          # JSON Web Token authentication
+            "opentracing"  # End-to-end tracing support using Jaeger
+            "oidc"         # OpenID Connect authentication
+            "postgres"     # PostgreSQL database backend
+            "redis"        # Redis support for the replication stream between worker processes
+            "saml2"        # SAML2 authentication
+            "sentry"       # Error tracking and performance metrics
+            "systemd"      # Provide the JournalHandler used in the default log_config
+            "url-preview"  # Support for oEmbed URL previews
+            "user-search"  # Support internationalized domain names in user-search
+          ]
+        '';
         description = lib.mdDoc ''
-          Overridable attribute of the matrix synapse server package to use.
+          Explicitly install extras provided by matrix-synapse. Most
+          will reconfigure some additional configuration.
+
+          Extras will automatically be enabled, when the relevant
+          configuration sections are present.
         '';
       };
 
@@ -193,7 +242,7 @@ in {
         default = {};
         description = mdDoc ''
           The primary synapse configuration. See the
-          [sample configuration](https://github.com/matrix-org/synapse/blob/v${cfg.package.version}/docs/sample_config.yaml)
+          [sample configuration](https://github.com/matrix-org/synapse/blob/v${cfg.package.unwrapped.version}/docs/sample_config.yaml)
           for possible values.
 
           Secrets should be passed in by using the `extraConfigFiles` option.
@@ -707,6 +756,9 @@ in {
 
     services.matrix-synapse.configFile = configFile;
 
+    # default them, so they are additive
+    services.matrix-synapse.settings.extras = defaultExtras;
+
     users.users.matrix-synapse = {
       group = "matrix-synapse";
       home = cfg.dataDir;
@@ -724,14 +776,12 @@ in {
       after = [ "network.target" ] ++ optional hasLocalPostgresDB "postgresql.service";
       wantedBy = [ "multi-user.target" ];
       preStart = ''
-        ${cfg.package}/bin/synapse_homeserver \
+        ${wrapped}/bin/synapse_homeserver \
           --config-path ${configFile} \
           --keys-directory ${cfg.dataDir} \
           --generate-keys
       '';
-      environment = {
-        PYTHONPATH = makeSearchPathOutput "lib" cfg.package.python.sitePackages [ pluginsEnv ];
-      } // optionalAttrs (cfg.withJemalloc) {
+      environment = optionalAttrs (cfg.withJemalloc) {
         LD_PRELOAD = "${pkgs.jemalloc}/lib/libjemalloc.so";
       };
       serviceConfig = {
@@ -744,7 +794,7 @@ in {
           chmod 0600 ${cfg.settings.signing_key_path}
         '')) ];
         ExecStart = ''
-          ${cfg.package}/bin/synapse_homeserver \
+          ${wrapped}/bin/synapse_homeserver \
             ${ concatMapStringsSep "\n  " (x: "--config-path ${x} \\") ([ configFile ] ++ cfg.extraConfigFiles) }
             --keys-directory ${cfg.dataDir}
         '';