From 565225847ebf54aa57722271dd83d99954a55076 Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Sun, 24 Dec 2023 07:22:21 +0530 Subject: modules/matrix-sliding-sync: init dendrite --- common.nix | 6 +- hosts/kay/modules/matrix-sliding-sync.nix | 14 ++++ hosts/kay/modules/matrix_sliding_sync.nix | 14 ---- hosts/kay/modules/www.nix | 4 +- modules/matrix-sliding-sync.nix | 109 ++++++++++++++++++++++++++++++ 5 files changed, 130 insertions(+), 17 deletions(-) create mode 100644 hosts/kay/modules/matrix-sliding-sync.nix delete mode 100644 hosts/kay/modules/matrix_sliding_sync.nix create mode 100644 modules/matrix-sliding-sync.nix diff --git a/common.nix b/common.nix index 4e52b04..9245d3e 100644 --- a/common.nix +++ b/common.nix @@ -8,11 +8,15 @@ let host = config.networking.hostName; in { - disabledModules = [ "services/networking/pppd.nix" ]; + disabledModules = [ + "services/networking/pppd.nix" + "services/matrix/matrix-sliding-sync.nix" + ]; imports = [ ./modules/userdata.nix ./modules/dev.nix ./modules/pppd.nix + ./modules/matrix-sliding-sync.nix ]; # boot diff --git a/hosts/kay/modules/matrix-sliding-sync.nix b/hosts/kay/modules/matrix-sliding-sync.nix new file mode 100644 index 0000000..526734b --- /dev/null +++ b/hosts/kay/modules/matrix-sliding-sync.nix @@ -0,0 +1,14 @@ +{ config, ... }: + +let + domain = config.userdata.domain; +in +{ + sops.secrets."matrix-${domain}/sliding_sync" = {}; + + services.matrix-sliding-sync = { + enable = true; + environmentFile = config.sops.secrets."matrix-${domain}/sliding_sync".path; + settings.SYNCV3_SERVER = "https://${domain}"; + }; +} diff --git a/hosts/kay/modules/matrix_sliding_sync.nix b/hosts/kay/modules/matrix_sliding_sync.nix deleted file mode 100644 index f18ef10..0000000 --- a/hosts/kay/modules/matrix_sliding_sync.nix +++ /dev/null @@ -1,14 +0,0 @@ -{ config, ... }: - -let - domain = config.userdata.domain; -in -{ - sops.secrets."matrix-${domain}/sliding_sync" = {}; - - services.matrix-synapse.sliding-sync = { - enable = true; - environmentFile = config.sops.secrets."matrix-${domain}/sliding_sync".path; - settings.SYNCV3_SERVER = "https://${domain}"; - }; -} diff --git a/hosts/kay/modules/www.nix b/hosts/kay/modules/www.nix index 4ffa141..aec5d5a 100644 --- a/hosts/kay/modules/www.nix +++ b/hosts/kay/modules/www.nix @@ -9,7 +9,7 @@ in { imports = [ ./dendrite.nix - ./matrix_sliding_sync.nix + ./matrix-sliding-sync.nix ./cgit.nix ]; @@ -53,7 +53,7 @@ in }}' ''; locations."/_matrix/client/unstable/org.matrix.msc3575/sync" = let - addr = "${config.services.matrix-synapse.sliding-sync.settings.SYNCV3_BINDADDR}"; + addr = "${config.services.matrix-sliding-sync.settings.SYNCV3_BINDADDR}"; in { proxyPass = "http://${addr}"; }; diff --git a/modules/matrix-sliding-sync.nix b/modules/matrix-sliding-sync.nix new file mode 100644 index 0000000..3785515 --- /dev/null +++ b/modules/matrix-sliding-sync.nix @@ -0,0 +1,109 @@ +{ config, lib, pkgs, ... }: + +let + cfg = config.services.matrix-sliding-sync; +in +{ + imports = [ + (lib.mkRenamedOptionModule + [ "services" "matrix-synapse" "sliding-sync" ] + [ "services" "matrix-sliding-sync" ] + ) + ]; + + options.services.matrix-sliding-sync = { + enable = lib.mkEnableOption (lib.mdDoc "sliding sync"); + + package = lib.mkPackageOption pkgs "matrix-sliding-sync" { }; + + settings = lib.mkOption { + type = lib.types.submodule { + freeformType = with lib.types; attrsOf str; + options = { + SYNCV3_SERVER = lib.mkOption { + type = lib.types.str; + description = lib.mdDoc '' + The destination homeserver to talk to not including `/_matrix/` e.g `https://matrix.example.org`. + ''; + }; + + SYNCV3_DB = lib.mkOption { + type = lib.types.str; + default = "postgresql:///matrix-sliding-sync?host=/run/postgresql"; + description = lib.mdDoc '' + The postgres connection string. + Refer to . + ''; + }; + + SYNCV3_BINDADDR = lib.mkOption { + type = lib.types.str; + default = "127.0.0.1:8009"; + example = "[::]:8008"; + description = lib.mdDoc "The interface and port to listen on."; + }; + + SYNCV3_LOG_LEVEL = lib.mkOption { + type = lib.types.enum [ "trace" "debug" "info" "warn" "error" "fatal" ]; + default = "info"; + description = lib.mdDoc "The level of verbosity for messages logged."; + }; + }; + }; + default = { }; + description = lib.mdDoc '' + Freeform environment variables passed to the sliding sync proxy. + Refer to for all supported values. + ''; + }; + + createDatabase = lib.mkOption { + type = lib.types.bool; + default = true; + description = lib.mdDoc '' + Whether to enable and configure `services.postgres` to ensure that the database user `matrix-sliding-sync` + and the database `matrix-sliding-sync` exist. + ''; + }; + + environmentFile = lib.mkOption { + type = lib.types.str; + description = lib.mdDoc '' + Environment file as defined in {manpage}`systemd.exec(5)`. + + This must contain the {env}`SYNCV3_SECRET` variable which should + be generated with {command}`openssl rand -hex 32`. + ''; + }; + }; + + config = lib.mkIf cfg.enable { + services.postgresql = lib.optionalAttrs cfg.createDatabase { + enable = true; + ensureDatabases = [ "matrix-sliding-sync" ]; + ensureUsers = [ { + name = "matrix-sliding-sync"; + ensureDBOwnership = true; + } ]; + }; + + systemd.services.matrix-sliding-sync = rec { + after = + lib.optional cfg.createDatabase "postgresql.service" + ++ lib.optional config.services.dendrite.enable "dendrite.service" + ++ lib.optional config.services.matrix-synapse.enable config.services.matrix-synapse.serviceUnit; + wants = after; + wantedBy = [ "multi-user.target" ]; + environment = cfg.settings; + serviceConfig = { + DynamicUser = true; + EnvironmentFile = cfg.environmentFile; + ExecStart = lib.getExe cfg.package; + StateDirectory = "matrix-sliding-sync"; + WorkingDirectory = "%S/matrix-sliding-sync"; + Restart = "on-failure"; + RestartSec = "1s"; + }; + }; + }; +} -- cgit v1.2.3