nixos-config/modules/matrix/mautrix-discord.nix

155 lines
5.2 KiB
Nix
Raw Normal View History

2022-07-22 10:54:03 +00:00
{
nix-packages,
system,
config,
pkgs,
lib,
...
}:
with lib; let
dataDir = "/var/lib/mautrix-discord";
2023-04-18 18:30:26 +00:00
registrationFile = config.sops.secrets."services/mautrix/discord.yaml".path;
2022-07-22 10:54:03 +00:00
cfg = config.services.mautrix-discord;
settingsFormat = pkgs.formats.yaml {};
2023-04-18 16:14:54 +00:00
settingsFileUnsubstituted = settingsFormat.generate "mautrix-discord-config-unsubstituted.yaml" cfg.settings;
2022-07-22 10:54:03 +00:00
settingsFile = "${dataDir}/config.yaml";
2023-06-13 05:46:23 +00:00
inherit (pkgs) mautrix-discord;
2022-07-22 10:54:03 +00:00
in {
options = {
services.mautrix-discord = {
enable = mkEnableOption "Mautrix-Whatsapp, a Matrix-Whatsapp hybrid puppeting/relaybot bridge";
settings = mkOption rec {
apply = recursiveUpdate default;
inherit (settingsFormat) type;
default = {
appservice = {
2023-04-18 18:28:10 +00:00
address = "http://mautrix-discord.int.chir.rs:29320";
2022-07-22 10:54:03 +00:00
hostname = "0.0.0.0";
2022-07-22 16:30:08 +00:00
port = 29320;
2022-07-22 10:54:03 +00:00
database = {
type = "sqlite";
uri = "sqlite:///${dataDir}/mautrix-telegram.db";
};
as_token = "$AS_TOKEN";
hs_token = "$HS_TOKEN";
};
logging = {
file_name_format = null;
};
};
};
environmentFile = mkOption {
type = types.nullOr types.path;
default = null;
description = ''
File containing environment variables to be passed to the mautrix-telegram service,
in which secret tokens can be specified securely by defining values for
<literal>MAUTRIX_TELEGRAM_APPSERVICE_AS_TOKEN</literal>,
<literal>MAUTRIX_TELEGRAM_APPSERVICE_HS_TOKEN</literal>,
<literal>MAUTRIX_TELEGRAM_TELEGRAM_API_ID</literal>,
<literal>MAUTRIX_TELEGRAM_TELEGRAM_API_HASH</literal> and optionally
<literal>MAUTRIX_TELEGRAM_TELEGRAM_BOT_TOKEN</literal>.
'';
};
};
};
config = mkIf cfg.enable {
systemd.services.mautrix-discord-genregistration = {
2023-04-18 16:14:54 +00:00
description = "Mautrix-Discord Registration";
2022-07-22 10:54:03 +00:00
script = ''
# Not all secrets can be passed as environment variable (yet)
# https://github.com/tulir/mautrix-telegram/issues/584
[ -f ${settingsFile} ] && rm -f ${settingsFile}
export AS_TOKEN=$(${pkgs.yq}/bin/yq -r '.as_token' ${registrationFile})
export HS_TOKEN=$(${pkgs.yq}/bin/yq -r '.hs_token' ${registrationFile})
umask 0177
${pkgs.envsubst}/bin/envsubst \
-o ${settingsFile} \
-i ${settingsFileUnsubstituted}
'';
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ReadWritePaths = baseNameOf dataDir;
NoNewPrivileges = true;
MemoryDenyWriteExecute = true;
PrivateDevices = true;
PrivateTmp = true;
ProtectHome = true;
ProtectSystem = "strict";
ProtectControlGroups = true;
RestrictSUIDSGID = true;
RestrictRealtime = true;
LockPersonality = true;
ProtectKernelLogs = true;
ProtectKernelTunables = true;
ProtectHostname = true;
ProtectKernelModules = true;
ProtectClock = true;
SystemCallArchitectures = "native";
SystemCallErrorNumber = "EPERM";
SystemCallFilter = "@system-service";
WorkingDirectory = dataDir;
StateDirectory = baseNameOf dataDir;
UMask = 0117;
User = "mautrix-discord";
2023-04-18 16:14:54 +00:00
Group = "mautrix-discord";
2022-07-22 10:54:03 +00:00
EnvironmentFile = cfg.environmentFile;
};
restartTriggers = [settingsFileUnsubstituted cfg.environmentFile];
};
systemd.services.mautrix-discord = {
2023-04-18 16:14:54 +00:00
description = "Mautrix-Discord";
2023-06-08 06:25:24 +00:00
path = with pkgs; [ffmpeg lottieconverter];
2022-07-22 10:54:03 +00:00
wantedBy = ["multi-user.target"];
2023-04-18 17:01:15 +00:00
wants = ["mautrix-discord-genregistration.service"];
after = ["mautrix-discord-genregistration.service"];
2022-07-22 10:54:03 +00:00
serviceConfig = {
Type = "simple";
Restart = "always";
ReadWritePaths = baseNameOf dataDir;
NoNewPrivileges = true;
MemoryDenyWriteExecute = true;
PrivateDevices = true;
PrivateTmp = true;
ProtectHome = true;
ProtectSystem = "strict";
ProtectControlGroups = true;
RestrictSUIDSGID = true;
RestrictRealtime = true;
LockPersonality = true;
ProtectKernelLogs = true;
ProtectKernelTunables = true;
ProtectHostname = true;
ProtectKernelModules = true;
ProtectClock = true;
SystemCallArchitectures = "native";
SystemCallErrorNumber = "EPERM";
SystemCallFilter = "@system-service";
WorkingDirectory = dataDir;
StateDirectory = baseNameOf dataDir;
UMask = 0117;
User = "mautrix-discord";
2023-04-18 16:14:54 +00:00
Group = "mautrix-discord";
2022-07-22 10:54:03 +00:00
EnvironmentFile = cfg.environmentFile;
ExecStart = ''
${mautrix-discord}/bin/mautrix-discord \
--config='${settingsFile}'
'';
};
restartTriggers = [cfg.environmentFile];
};
users.users.mautrix-discord = {
description = "Mautrix Whatsapp bridge";
home = "${dataDir}";
useDefaultShell = true;
2023-04-18 16:14:54 +00:00
group = "mautrix-discord";
2022-07-22 10:54:03 +00:00
isSystemUser = true;
};
2023-04-18 16:14:54 +00:00
users.groups.mautrix-discord = {};
sops.secrets."services/mautrix/discord.yaml".owner = "mautrix-discord";
2022-07-22 10:54:03 +00:00
};
}