summaryrefslogtreecommitdiff
path: root/modules/matrix-appservices/default.nix
blob: f8eb98035d541e51ad07db3e5458fdd805aac934 (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
{ config, lib, pkgs, ... }:

with lib;
let
  cfg = config.services.matrix-appservices;
  asOpts = import ./as-options.nix {
    inherit lib pkgs;
    systemConfig = config;
  };
  mkService = name: opts:
    with opts;
    let
      settingsFormat = pkgs.formats.json { };
      dataDir = "/var/lib/matrix-as-${name}";
      registrationFile = "${dataDir}/${name}-registration.yaml";
      # Replace all references to $DIR to the dat directory
      settingsData = settingsFormat.generate "config.json" settings;
      settingsFile = "${dataDir}/config.json";
      serviceDeps = [ "network-online.target" ] ++ serviceDependencies;

      registrationContent = {
        id = name;
        url = "http://${host}:${toString port}";
        as_token = "$AS_TOKEN";
        hs_token = "$HS_TOKEN";
        sender_localpart = "$SENDER_LOCALPART";
        rate_limited = false;
      } // registrationData;
    in
    {
      description = "A matrix appservice for ${name}.";

      wantedBy = [ "multi-user.target" ];
      wants = serviceDeps;
      after = serviceDeps;
      # Appservices don't need synapse up, but synapse exists if registration files are missing
      before = mkIf (cfg.homeserver != null) [ "${cfg.homeserver}.service" ];

      path = [ pkgs.yq ];
      environment = {
        DIR = dataDir;
        SETTINGS_FILE = settingsFile;
        REGISTRATION_FILE = registrationFile;
      };

      preStart = ''
        if [ ! -f ${registrationFile} ]; then
          AS_TOKEN=$(cat /proc/sys/kernel/random/uuid) \
          HS_TOKEN=$(cat /proc/sys/kernel/random/uuid) \
          SENDER_LOCALPART=$(cat /proc/sys/kernel/random/uuid) \
          ${pkgs.envsubst}/bin/envsubst \
            -i ${settingsFormat.generate "config.json" registrationContent} \
            -o ${registrationFile}

          chmod 640 ${registrationFile}
        fi

        AS_TOKEN=$(cat ${registrationFile} | yq .as_token | tr -d '"') \
        HS_TOKEN=$(cat ${registrationFile} | yq .hs_token | tr -d '"') \
        ${pkgs.envsubst}/bin/envsubst -i ${settingsData} -o ${settingsFile}
        chmod 640 ${settingsFile}
      '';

      script = startupScript;

      serviceConfig = {
        Type = "simple";
        Restart = "always";

        ProtectSystem = "strict";
        PrivateTmp = true;
        ProtectHome = true;
        ProtectKernelTunables = true;
        ProtectKernelModules = true;
        ProtectControlGroups = true;

        User = "matrix-as-${name}";
        Group = "matrix-as-${name}";
        WorkingDirectory = dataDir;
        StateDirectory = baseNameOf dataDir;
        StateDirectoryMode = "0750";
        UMask = 0027;
      } // opts.serviceConfig;
    };

in
{
  options = {
    services.matrix-appservices = {
      services = mkOption {
        type = types.attrsOf asOpts;
        default = { };
        example = literalExpression ''
          whatsapp = {
            format = "mautrix-go";
            package = pkgs.mautrix-whatsapp;
          };
        '';
        description = ''
          Appservices to setup.
          Each appservice will be started as a systemd service with the prefix matrix-as.
          And its data will be stored in /var/lib/matrix-as-name.
        '';
      };

      homeserver = mkOption {
        type = types.enum [ "matrix-synapse" "dendrite" null ];
        default = "matrix-synapse";
        description = ''
          The homeserver software the appservices connect to. This will ensure appservices
          start after the homeserver and it will be used by the addRegistrationFiles option.
        '';
      };

      homeserverURL = mkOption {
        type = types.str;
        default = "https://${cfg.homeserverDomain}";
        description = ''
          URL of the homeserver the apservices connect to
        '';
      };

      homeserverDomain = mkOption {
        type = types.str;
        default = if config.networking.domain != null then config.networking.domain else "";
        defaultText = "\${config.networking.domain}";
        description = ''
          Domain of the homeserver the appservices connect to
        '';
      };

      addRegistrationFiles = mkOption {
        type = types.bool;
        default = false;
        description = ''
          Whether to add the application service registration files to the homeserver configuration.
          It is recommended to verify appservice files, located in /var/lib/matrix-as-*, before adding them
        '';
      };
    };
  };

  config = mkIf (cfg.services != { }) {

    assertions = mapAttrsToList
      (n: v: {
        assertion = v.format == "other" || v.package != null;
        message = "A package must be provided if a custom format is set";
      })
      cfg.services;

    users.users = mapAttrs'
      (n: v: nameValuePair "matrix-as-${n}" {
        group = "matrix-as-${n}";
        isSystemUser = true;
      })
      cfg.services;
    users.groups = mapAttrs' (n: v: nameValuePair "matrix-as-${n}" { }) cfg.services;

    # Create a service for each appservice
    systemd.services = (mapAttrs' (n: v: nameValuePair "matrix-as-${n}" (mkService n v)) cfg.services) // {
      # Add the matrix service to the groups of all appservices to give access to the registration file
      matrix-synapse.serviceConfig.SupplementaryGroups = mapAttrsToList (n: v: "matrix-as-${n}") cfg.services;
      dendrite.serviceConfig.SupplementaryGroups = mapAttrsToList (n: v: "matrix-as-${n}") cfg.services;
    };

    services =
      let
        registrationFiles = mapAttrsToList (n: _: "/var/lib/matrix-as-${n}/${n}-registration.yaml")
          (filterAttrs (_: v: v.registrationData != { }) cfg.services);
      in
      mkIf cfg.addRegistrationFiles {
        matrix-synapse.settings.app_service_config_files = mkIf (cfg.homeserver == "matrix-synapse") registrationFiles;
        dendrite.settings.app_service_api.config_files = mkIf (cfg.homeserver == "dendrite") registrationFiles;
      };
  };

  meta.maintainers = with maintainers; [ pacman99 Flakebi ];

}