1{ config, lib, pkgs, ... }:
2
3let
4 cfg = config.services.matrix-synapse.sliding-sync;
5in
6{
7 options.services.matrix-synapse.sliding-sync = {
8 enable = lib.mkEnableOption (lib.mdDoc "sliding sync");
9
10 package = lib.mkPackageOptionMD pkgs "matrix-sliding-sync" { };
11
12 settings = lib.mkOption {
13 type = lib.types.submodule {
14 freeformType = with lib.types; attrsOf str;
15 options = {
16 SYNCV3_SERVER = lib.mkOption {
17 type = lib.types.str;
18 description = lib.mdDoc ''
19 The destination homeserver to talk to not including `/_matrix/` e.g `https://matrix.example.org`.
20 '';
21 };
22
23 SYNCV3_DB = lib.mkOption {
24 type = lib.types.str;
25 default = "postgresql:///matrix-sliding-sync?host=/run/postgresql";
26 description = lib.mdDoc ''
27 The postgres connection string.
28 Refer to <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING>.
29 '';
30 };
31
32 SYNCV3_BINDADDR = lib.mkOption {
33 type = lib.types.str;
34 default = "127.0.0.1:8009";
35 example = "[::]:8008";
36 description = lib.mdDoc "The interface and port to listen on.";
37 };
38
39 SYNCV3_LOG_LEVEL = lib.mkOption {
40 type = lib.types.enum [ "trace" "debug" "info" "warn" "error" "fatal" ];
41 default = "info";
42 description = lib.mdDoc "The level of verbosity for messages logged.";
43 };
44 };
45 };
46 default = { };
47 description = lib.mdDoc ''
48 Freeform environment variables passed to the sliding sync proxy.
49 Refer to <https://github.com/matrix-org/sliding-sync#setup> for all supported values.
50 '';
51 };
52
53 createDatabase = lib.mkOption {
54 type = lib.types.bool;
55 default = true;
56 description = lib.mdDoc ''
57 Whether to enable and configure `services.postgres` to ensure that the database user `matrix-sliding-sync`
58 and the database `matrix-sliding-sync` exist.
59 '';
60 };
61
62 environmentFile = lib.mkOption {
63 type = lib.types.str;
64 description = lib.mdDoc ''
65 Environment file as defined in {manpage}`systemd.exec(5)`.
66
67 This must contain the {env}`SYNCV3_SECRET` variable which should
68 be generated with {command}`openssl rand -hex 32`.
69 '';
70 };
71 };
72
73 config = lib.mkIf cfg.enable {
74 services.postgresql = lib.optionalAttrs cfg.createDatabase {
75 enable = true;
76 ensureDatabases = [ "matrix-sliding-sync" ];
77 ensureUsers = [ {
78 name = "matrix-sliding-sync";
79 ensureDBOwnership = true;
80 } ];
81 };
82
83 systemd.services.matrix-sliding-sync = rec {
84 after =
85 lib.optional cfg.createDatabase "postgresql.service"
86 ++ lib.optional config.services.matrix-synapse.enable config.services.matrix-synapse.serviceUnit;
87 wants = after;
88 wantedBy = [ "multi-user.target" ];
89 environment = cfg.settings;
90 serviceConfig = {
91 DynamicUser = true;
92 EnvironmentFile = cfg.environmentFile;
93 ExecStart = lib.getExe cfg.package;
94 StateDirectory = "matrix-sliding-sync";
95 WorkingDirectory = "%S/matrix-sliding-sync";
96 Restart = "on-failure";
97 RestartSec = "1s";
98 };
99 };
100 };
101}