summaryrefslogtreecommitdiff
path: root/modules/matrix-sliding-sync.nix
blob: 3785515b37a84047221b5fd28ab3246010e0fcac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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 <https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING>.
            '';
          };

          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 <https://github.com/matrix-org/sliding-sync#setup> 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";
      };
    };
  };
}