1 { config, lib, pkgs, ... }:
3 cfg = config.services.mjolnir;
6 inherit (cfg) dataPath managementRoom protectedRooms;
8 accessToken = "@ACCESS_TOKEN@"; # will be replaced in "generateConfig"
10 if cfg.pantalaimon.enable then
11 "http://${cfg.pantalaimon.options.listenAddress}:${toString cfg.pantalaimon.options.listenPort}"
15 rawHomeserverUrl = cfg.homeserverUrl;
18 inherit (cfg.pantalaimon) username;
20 use = cfg.pantalaimon.enable;
21 password = "@PANTALAIMON_PASSWORD@"; # will be replaced in "generateConfig"
25 moduleConfigFile = pkgs.writeText "module-config.yaml" (
26 lib.generators.toYAML { } (lib.filterAttrs (_: v: v != null)
27 (lib.fold lib.recursiveUpdate { } [ yamlConfig cfg.settings ])));
29 # these config files will be merged one after the other to build the final config
31 "${pkgs.mjolnir}/libexec/mjolnir/deps/mjolnir/config/default.yaml"
35 # this will generate the default.yaml file with all configFiles as inputs and
36 # replace all secret strings using replace-secret
37 generateConfig = pkgs.writeShellScript "mjolnir-generate-config" (
39 yqEvalStr = lib.concatImapStringsSep " * " (pos: _: "select(fileIndex == ${toString (pos - 1)})") configFiles;
40 yqEvalArgs = lib.concatStringsSep " " configFiles;
47 # mjolnir will try to load a config from "./config/default.yaml" in the working directory
48 # -> let's place the generated config there
49 mkdir -p ${cfg.dataPath}/config
51 # merge all config files into one, overriding settings of the previous one with the next config
52 # e.g. "eval-all 'select(fileIndex == 0) * select(fileIndex == 1)' filea.yaml fileb.yaml" will merge filea.yaml with fileb.yaml
53 ${pkgs.yq-go}/bin/yq eval-all -P '${yqEvalStr}' ${yqEvalArgs} > ${cfg.dataPath}/config/default.yaml
55 ${lib.optionalString (cfg.accessTokenFile != null) ''
56 ${pkgs.replace-secret}/bin/replace-secret '@ACCESS_TOKEN@' '${cfg.accessTokenFile}' ${cfg.dataPath}/config/default.yaml
58 ${lib.optionalString (cfg.pantalaimon.passwordFile != null) ''
59 ${pkgs.replace-secret}/bin/replace-secret '@PANTALAIMON_PASSWORD@' '${cfg.pantalaimon.passwordFile}' ${cfg.dataPath}/config/default.yaml
65 options.services.mjolnir = {
66 enable = lib.mkEnableOption "Mjolnir, a moderation tool for Matrix";
68 homeserverUrl = lib.mkOption {
70 default = "https://matrix.org";
72 Where the homeserver is located (client-server URL).
74 If `pantalaimon.enable` is `true`, this option will become the homeserver to which `pantalaimon` connects.
75 The listen address of `pantalaimon` will then become the `homeserverUrl` of `mjolnir`.
79 accessTokenFile = lib.mkOption {
80 type = with lib.types; nullOr path;
83 File containing the matrix access token for the `mjolnir` user.
87 pantalaimon = lib.mkOption {
89 `pantalaimon` options (enables E2E Encryption support).
91 This will create a `pantalaimon` instance with the name "mjolnir".
94 type = lib.types.submodule {
96 enable = lib.mkEnableOption ''
97 ignoring the accessToken. If true, accessToken is ignored and the username/password below will be
98 used instead. The access token of the bot will be stored in the dataPath
101 username = lib.mkOption {
102 type = lib.types.str;
103 description = "The username to login with.";
106 passwordFile = lib.mkOption {
107 type = with lib.types; nullOr path;
110 File containing the matrix password for the `mjolnir` user.
114 options = lib.mkOption {
115 type = lib.types.submodule (import ./pantalaimon-options.nix);
118 passthrough additional options to the `pantalaimon` service.
125 dataPath = lib.mkOption {
126 type = lib.types.path;
127 default = "/var/lib/mjolnir";
129 The directory the bot should store various bits of information in.
133 managementRoom = lib.mkOption {
134 type = lib.types.str;
135 default = "#moderators:example.org";
137 The room ID where people can use the bot. The bot has no access controls, so
138 anyone in this room can use the bot - secure your room!
139 This should be a room alias or room ID - not a matrix.to URL.
140 Note: `mjolnir` is fairly verbose - expect a lot of messages from it.
144 protectedRooms = lib.mkOption {
145 type = lib.types.listOf lib.types.str;
147 example = lib.literalExpression ''
149 "https://matrix.to/#/#yourroom:example.org"
150 "https://matrix.to/#/#anotherroom:example.org"
154 A list of rooms to protect (matrix.to URLs).
158 settings = lib.mkOption {
160 type = (pkgs.formats.yaml { }).type;
161 example = lib.literalExpression ''
163 autojoinOnlyIfManager = true;
164 automaticallyRedactForReasons = [ "spam" "advertising" ];
168 Additional settings (see [mjolnir default config](https://github.com/matrix-org/mjolnir/blob/main/config/default.yaml) for available settings). These settings will override settings made by the module config.
173 config = lib.mkIf config.services.mjolnir.enable {
176 assertion = !(cfg.pantalaimon.enable && cfg.pantalaimon.passwordFile == null);
177 message = "Specify pantalaimon.passwordFile";
180 assertion = !(cfg.pantalaimon.enable && cfg.accessTokenFile != null);
181 message = "Do not specify accessTokenFile when using pantalaimon";
184 assertion = !(!cfg.pantalaimon.enable && cfg.accessTokenFile == null);
185 message = "Specify accessTokenFile when not using pantalaimon";
189 # This defaults to true in the application,
190 # which breaks older configs using pantalaimon or access tokens
191 services.mjolnir.settings.encryption.use = lib.mkDefault false;
193 services.pantalaimon-headless.instances."mjolnir" = lib.mkIf cfg.pantalaimon.enable
195 homeserver = cfg.homeserverUrl;
196 } // cfg.pantalaimon.options;
198 systemd.services.mjolnir = {
199 description = "mjolnir - a moderation tool for Matrix";
200 wants = [ "network-online.target" ] ++ lib.optionals (cfg.pantalaimon.enable) [ "pantalaimon-mjolnir.service" ];
201 after = [ "network-online.target" ] ++ lib.optionals (cfg.pantalaimon.enable) [ "pantalaimon-mjolnir.service" ];
202 wantedBy = [ "multi-user.target" ];
205 ExecStart = ''${pkgs.mjolnir}/bin/mjolnir --mjolnir-config ./config/default.yaml'';
206 ExecStartPre = [ generateConfig ];
207 WorkingDirectory = cfg.dataPath;
208 StateDirectory = "mjolnir";
209 StateDirectoryMode = "0700";
210 ProtectSystem = "strict";
213 NoNewPrivileges = true;
214 PrivateDevices = true;
216 Restart = "on-failure";
218 /* TODO: wait for #102397 to be resolved. Then load secrets from $CREDENTIALS_DIRECTORY+"/NAME"
220 LoadCredential = [] ++
221 lib.optionals (cfg.accessTokenFile != null) [
222 "access_token:${cfg.accessTokenFile}"
224 lib.optionals (cfg.pantalaimon.passwordFile != null) [
225 "pantalaimon_password:${cfg.pantalaimon.passwordFile}"
236 groups.mjolnir = { };
242 maintainers = with lib.maintainers; [ jojosch ];