2021-05-30 15:33:03 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
let
|
|
|
|
cfg = config.services.mjolnir;
|
|
|
|
|
|
|
|
yamlConfig = {
|
|
|
|
inherit (cfg) dataPath managementRoom protectedRooms;
|
|
|
|
|
|
|
|
accessToken = "@ACCESS_TOKEN@"; # will be replaced in "generateConfig"
|
|
|
|
homeserverUrl =
|
|
|
|
if cfg.pantalaimon.enable then
|
|
|
|
"http://${cfg.pantalaimon.options.listenAddress}:${toString cfg.pantalaimon.options.listenPort}"
|
|
|
|
else
|
|
|
|
cfg.homeserverUrl;
|
|
|
|
|
2021-11-16 18:21:33 +00:00
|
|
|
rawHomeserverUrl = cfg.homeserverUrl;
|
|
|
|
|
2021-05-30 15:33:03 +00:00
|
|
|
pantalaimon = {
|
|
|
|
inherit (cfg.pantalaimon) username;
|
|
|
|
|
|
|
|
use = cfg.pantalaimon.enable;
|
|
|
|
password = "@PANTALAIMON_PASSWORD@"; # will be replaced in "generateConfig"
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
moduleConfigFile = pkgs.writeText "module-config.yaml" (
|
2024-08-24 20:05:37 +00:00
|
|
|
lib.generators.toYAML { } (lib.filterAttrs (_: v: v != null)
|
|
|
|
(lib.fold lib.recursiveUpdate { } [ yamlConfig cfg.settings ])));
|
2021-05-30 15:33:03 +00:00
|
|
|
|
|
|
|
# these config files will be merged one after the other to build the final config
|
|
|
|
configFiles = [
|
2023-05-15 09:49:54 +00:00
|
|
|
"${pkgs.mjolnir}/libexec/mjolnir/deps/mjolnir/config/default.yaml"
|
2021-05-30 15:33:03 +00:00
|
|
|
moduleConfigFile
|
|
|
|
];
|
|
|
|
|
|
|
|
# this will generate the default.yaml file with all configFiles as inputs and
|
|
|
|
# replace all secret strings using replace-secret
|
|
|
|
generateConfig = pkgs.writeShellScript "mjolnir-generate-config" (
|
|
|
|
let
|
2024-08-24 20:05:37 +00:00
|
|
|
yqEvalStr = lib.concatImapStringsSep " * " (pos: _: "select(fileIndex == ${toString (pos - 1)})") configFiles;
|
|
|
|
yqEvalArgs = lib.concatStringsSep " " configFiles;
|
2021-05-30 15:33:03 +00:00
|
|
|
in
|
|
|
|
''
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
umask 077
|
|
|
|
|
|
|
|
# mjolnir will try to load a config from "./config/default.yaml" in the working directory
|
|
|
|
# -> let's place the generated config there
|
|
|
|
mkdir -p ${cfg.dataPath}/config
|
|
|
|
|
|
|
|
# merge all config files into one, overriding settings of the previous one with the next config
|
|
|
|
# e.g. "eval-all 'select(fileIndex == 0) * select(fileIndex == 1)' filea.yaml fileb.yaml" will merge filea.yaml with fileb.yaml
|
|
|
|
${pkgs.yq-go}/bin/yq eval-all -P '${yqEvalStr}' ${yqEvalArgs} > ${cfg.dataPath}/config/default.yaml
|
|
|
|
|
2024-08-24 20:05:37 +00:00
|
|
|
${lib.optionalString (cfg.accessTokenFile != null) ''
|
2021-05-30 15:33:03 +00:00
|
|
|
${pkgs.replace-secret}/bin/replace-secret '@ACCESS_TOKEN@' '${cfg.accessTokenFile}' ${cfg.dataPath}/config/default.yaml
|
|
|
|
''}
|
2024-08-24 20:05:37 +00:00
|
|
|
${lib.optionalString (cfg.pantalaimon.passwordFile != null) ''
|
2021-05-30 15:33:03 +00:00
|
|
|
${pkgs.replace-secret}/bin/replace-secret '@PANTALAIMON_PASSWORD@' '${cfg.pantalaimon.passwordFile}' ${cfg.dataPath}/config/default.yaml
|
|
|
|
''}
|
|
|
|
''
|
|
|
|
);
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options.services.mjolnir = {
|
2024-08-24 20:05:37 +00:00
|
|
|
enable = lib.mkEnableOption "Mjolnir, a moderation tool for Matrix";
|
2021-05-30 15:33:03 +00:00
|
|
|
|
2024-08-24 20:05:37 +00:00
|
|
|
homeserverUrl = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2021-05-30 15:33:03 +00:00
|
|
|
default = "https://matrix.org";
|
|
|
|
description = ''
|
|
|
|
Where the homeserver is located (client-server URL).
|
|
|
|
|
|
|
|
If `pantalaimon.enable` is `true`, this option will become the homeserver to which `pantalaimon` connects.
|
|
|
|
The listen address of `pantalaimon` will then become the `homeserverUrl` of `mjolnir`.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-08-24 20:05:37 +00:00
|
|
|
accessTokenFile = lib.mkOption {
|
|
|
|
type = with lib.types; nullOr path;
|
2021-05-30 15:33:03 +00:00
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
File containing the matrix access token for the `mjolnir` user.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-08-24 20:05:37 +00:00
|
|
|
pantalaimon = lib.mkOption {
|
2021-05-30 15:33:03 +00:00
|
|
|
description = ''
|
|
|
|
`pantalaimon` options (enables E2E Encryption support).
|
|
|
|
|
|
|
|
This will create a `pantalaimon` instance with the name "mjolnir".
|
|
|
|
'';
|
|
|
|
default = { };
|
2024-08-24 20:05:37 +00:00
|
|
|
type = lib.types.submodule {
|
2021-05-30 15:33:03 +00:00
|
|
|
options = {
|
2024-08-24 20:05:37 +00:00
|
|
|
enable = lib.mkEnableOption ''
|
2023-10-18 20:59:26 +00:00
|
|
|
ignoring the accessToken. If true, accessToken is ignored and the username/password below will be
|
|
|
|
used instead. The access token of the bot will be stored in the dataPath
|
2021-05-30 15:33:03 +00:00
|
|
|
'';
|
|
|
|
|
2024-08-24 20:05:37 +00:00
|
|
|
username = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2021-05-30 15:33:03 +00:00
|
|
|
description = "The username to login with.";
|
|
|
|
};
|
|
|
|
|
2024-08-24 20:05:37 +00:00
|
|
|
passwordFile = lib.mkOption {
|
|
|
|
type = with lib.types; nullOr path;
|
2021-05-30 15:33:03 +00:00
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
File containing the matrix password for the `mjolnir` user.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-08-24 20:05:37 +00:00
|
|
|
options = lib.mkOption {
|
|
|
|
type = lib.types.submodule (import ./pantalaimon-options.nix);
|
2021-05-30 15:33:03 +00:00
|
|
|
default = { };
|
|
|
|
description = ''
|
|
|
|
passthrough additional options to the `pantalaimon` service.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-08-24 20:05:37 +00:00
|
|
|
dataPath = lib.mkOption {
|
|
|
|
type = lib.types.path;
|
2021-05-30 15:33:03 +00:00
|
|
|
default = "/var/lib/mjolnir";
|
|
|
|
description = ''
|
|
|
|
The directory the bot should store various bits of information in.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-08-24 20:05:37 +00:00
|
|
|
managementRoom = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2021-05-30 15:33:03 +00:00
|
|
|
default = "#moderators:example.org";
|
|
|
|
description = ''
|
|
|
|
The room ID where people can use the bot. The bot has no access controls, so
|
|
|
|
anyone in this room can use the bot - secure your room!
|
|
|
|
This should be a room alias or room ID - not a matrix.to URL.
|
|
|
|
Note: `mjolnir` is fairly verbose - expect a lot of messages from it.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-08-24 20:05:37 +00:00
|
|
|
protectedRooms = lib.mkOption {
|
|
|
|
type = lib.types.listOf lib.types.str;
|
2021-05-30 15:33:03 +00:00
|
|
|
default = [ ];
|
2024-08-24 20:05:37 +00:00
|
|
|
example = lib.literalExpression ''
|
2021-05-30 15:33:03 +00:00
|
|
|
[
|
|
|
|
"https://matrix.to/#/#yourroom:example.org"
|
|
|
|
"https://matrix.to/#/#anotherroom:example.org"
|
|
|
|
]
|
|
|
|
'';
|
|
|
|
description = ''
|
|
|
|
A list of rooms to protect (matrix.to URLs).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-08-24 20:05:37 +00:00
|
|
|
settings = lib.mkOption {
|
2021-05-30 15:33:03 +00:00
|
|
|
default = { };
|
|
|
|
type = (pkgs.formats.yaml { }).type;
|
2024-08-24 20:05:37 +00:00
|
|
|
example = lib.literalExpression ''
|
2021-05-30 15:33:03 +00:00
|
|
|
{
|
|
|
|
autojoinOnlyIfManager = true;
|
|
|
|
automaticallyRedactForReasons = [ "spam" "advertising" ];
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
description = ''
|
|
|
|
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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-08-24 20:05:37 +00:00
|
|
|
config = lib.mkIf config.services.mjolnir.enable {
|
2021-05-30 15:33:03 +00:00
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = !(cfg.pantalaimon.enable && cfg.pantalaimon.passwordFile == null);
|
|
|
|
message = "Specify pantalaimon.passwordFile";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
assertion = !(cfg.pantalaimon.enable && cfg.accessTokenFile != null);
|
|
|
|
message = "Do not specify accessTokenFile when using pantalaimon";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
assertion = !(!cfg.pantalaimon.enable && cfg.accessTokenFile == null);
|
|
|
|
message = "Specify accessTokenFile when not using pantalaimon";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2024-10-14 02:45:31 +00:00
|
|
|
# This defaults to true in the application,
|
|
|
|
# which breaks older configs using pantalaimon or access tokens
|
|
|
|
services.mjolnir.settings.encryption.use = lib.mkDefault false;
|
|
|
|
|
2024-08-24 20:05:37 +00:00
|
|
|
services.pantalaimon-headless.instances."mjolnir" = lib.mkIf cfg.pantalaimon.enable
|
2021-05-30 15:33:03 +00:00
|
|
|
{
|
|
|
|
homeserver = cfg.homeserverUrl;
|
|
|
|
} // cfg.pantalaimon.options;
|
|
|
|
|
|
|
|
systemd.services.mjolnir = {
|
|
|
|
description = "mjolnir - a moderation tool for Matrix";
|
2024-08-24 20:05:37 +00:00
|
|
|
wants = [ "network-online.target" ] ++ lib.optionals (cfg.pantalaimon.enable) [ "pantalaimon-mjolnir.service" ];
|
|
|
|
after = [ "network-online.target" ] ++ lib.optionals (cfg.pantalaimon.enable) [ "pantalaimon-mjolnir.service" ];
|
2021-05-30 15:33:03 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
|
|
|
|
serviceConfig = {
|
2023-05-15 09:49:54 +00:00
|
|
|
ExecStart = ''${pkgs.mjolnir}/bin/mjolnir --mjolnir-config ./config/default.yaml'';
|
2021-05-30 15:33:03 +00:00
|
|
|
ExecStartPre = [ generateConfig ];
|
|
|
|
WorkingDirectory = cfg.dataPath;
|
|
|
|
StateDirectory = "mjolnir";
|
|
|
|
StateDirectoryMode = "0700";
|
|
|
|
ProtectSystem = "strict";
|
|
|
|
ProtectHome = true;
|
|
|
|
PrivateTmp = true;
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
PrivateDevices = true;
|
|
|
|
User = "mjolnir";
|
|
|
|
Restart = "on-failure";
|
|
|
|
|
|
|
|
/* TODO: wait for #102397 to be resolved. Then load secrets from $CREDENTIALS_DIRECTORY+"/NAME"
|
|
|
|
DynamicUser = true;
|
|
|
|
LoadCredential = [] ++
|
2024-08-24 20:05:37 +00:00
|
|
|
lib.optionals (cfg.accessTokenFile != null) [
|
2021-05-30 15:33:03 +00:00
|
|
|
"access_token:${cfg.accessTokenFile}"
|
|
|
|
] ++
|
2024-08-24 20:05:37 +00:00
|
|
|
lib.optionals (cfg.pantalaimon.passwordFile != null) [
|
2021-05-30 15:33:03 +00:00
|
|
|
"pantalaimon_password:${cfg.pantalaimon.passwordFile}"
|
|
|
|
];
|
|
|
|
*/
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
users = {
|
|
|
|
users.mjolnir = {
|
|
|
|
group = "mjolnir";
|
|
|
|
isSystemUser = true;
|
|
|
|
};
|
|
|
|
groups.mjolnir = { };
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
meta = {
|
2023-01-24 23:33:40 +00:00
|
|
|
doc = ./mjolnir.md;
|
2024-08-24 20:05:37 +00:00
|
|
|
maintainers = with lib.maintainers; [ jojosch ];
|
2021-05-30 15:33:03 +00:00
|
|
|
};
|
|
|
|
}
|