commit 87df0fd2b4798a1a7c78d60abd64be2fe995112a
parent 9398810b0e70d1284239782b4d3535c8faf0e52e
Author: Katja (ctucx) <git@ctu.cx>
Date: Thu, 20 Mar 2025 15:16:03 +0100
parent 9398810b0e70d1284239782b4d3535c8faf0e52e
Author: Katja (ctucx) <git@ctu.cx>
Date: Thu, 20 Mar 2025 15:16:03 +0100
modules/nixos: add `mautrix-telegram`
2 files changed, 124 insertions(+), 0 deletions(-)
A
|
122
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
diff --git a/modules/nixos/default.nix b/modules/nixos/default.nix @@ -8,6 +8,7 @@ "services/web-apps/gotosocial.nix" "services/matrix/mautrix-whatsapp.nix" "services/matrix/mautrix-signal.nix" + "services/matrix/mautrix-telegram.nix" "services/matrix/conduwuit.nix" ]; @@ -22,6 +23,7 @@ ./conduwuit.nix ./mautrix-whatsapp.nix ./mautrix-signal.nix + ./mautrix-telegram.nix ]; }
diff --git a/modules/nixos/mautrix-telegram.nix b/modules/nixos/mautrix-telegram.nix @@ -0,0 +1,122 @@ +{ lib, config, pkgs, ... }: + +let + cfg = config.services.mautrix-telegram; + dataDir = "/var/lib/mautrix-telegram"; + registrationFile = "${dataDir}/telegram-registration.yaml"; + settingsFile = "${dataDir}/config.yaml"; + settingsFileUnsubstituted = settingsFormat.generate "mautrix-telegram-config-unsubstituted.json" cfg.settings; + settingsFormat = pkgs.formats.json { }; + +in { + + options.services.mautrix-telegram = { + enable = lib.mkEnableOption "mautrix-telegram, a Matrix-Telegram puppeting bridge"; + + settings = lib.mkOption { + type = settingsFormat.type; + default = {}; + }; + + environmentFile = lib.mkOption { + type = lib.types.nullOr lib.types.path; + default = null; + }; + + serviceDependencies = lib.mkOption { + type = lib.types.listOf lib.types.str; + default = []; + }; + }; + + config = lib.mkIf cfg.enable { + users.groups.mautrix-telegram = { }; + users.users.mautrix-telegram = { + isSystemUser = true; + group = "mautrix-telegram"; + home = dataDir; + description = "Mautrix-Telegram bridge user"; + }; + + systemd.services.mautrix-telegram = { + description = "mautrix-telegram, a Matrix-Telegram puppeting bridge."; + restartTriggers = [ settingsFileUnsubstituted ]; + + wantedBy = [ "multi-user.target" ]; + wants = [ "network-online.target" ] ++ cfg.serviceDependencies; + after = [ "network-online.target" ] ++ cfg.serviceDependencies; + path = [ pkgs.ffmpeg-headless ]; + + preStart = '' + # substitute the settings file by environment variables + # in this case read from EnvironmentFile + test -f '${settingsFile}' && rm -f '${settingsFile}' + old_umask=$(umask) + umask 0177 + ${pkgs.envsubst}/bin/envsubst \ + -o '${settingsFile}' \ + -i '${settingsFileUnsubstituted}' + umask $old_umask + + # generate the appservice's registration file if absent + if [ ! -f '${registrationFile}' ]; then + ${lib.getExe pkgs.mautrix-telegramgo} \ + --generate-registration \ + --config='${settingsFile}' \ + --registration='${registrationFile}' + fi + chmod 640 ${registrationFile} + + umask 0177 + # 1. Overwrite registration tokens in config + # 2. If environment variable MAUTRIX_SIGNAL_BRIDGE_LOGIN_SHARED_SECRET + # is set, set it as the login shared secret value for the configured + # homeserver domain. + ${pkgs.yq}/bin/yq -s '.[0].network.api_id = (.[0].network.api_id | tonumber) + | .[0].appservice.as_token = .[1].as_token + | .[0].appservice.hs_token = .[1].hs_token + | .[0] + | if env.MAUTRIX_TELEGRAM_BRIDGE_LOGIN_SHARED_SECRET then .double_puppet.secrets.[.homeserver.domain] = env.MAUTRIX_TELEGRAM_BRIDGE_LOGIN_SHARED_SECRET else . end' \ + '${settingsFile}' '${registrationFile}' > '${settingsFile}.tmp' + mv '${settingsFile}.tmp' '${settingsFile}' + umask $old_umask + ''; + + serviceConfig = { + User = "mautrix-telegram"; + Group = "mautrix-telegram"; + EnvironmentFile = cfg.environmentFile; + StateDirectory = baseNameOf dataDir; + WorkingDirectory = dataDir; + ExecStart = '' + ${lib.getExe pkgs.mautrix-telegramgo} \ + --config='${settingsFile}' \ + --registration='${registrationFile}' + ''; + LockPersonality = true; + NoNewPrivileges = true; + PrivateDevices = true; + PrivateTmp = true; + PrivateUsers = true; + ProtectClock = true; + ProtectControlGroups = true; + ProtectHome = true; + ProtectHostname = true; + ProtectKernelLogs = true; + ProtectKernelModules = true; + ProtectKernelTunables = true; + ProtectSystem = "strict"; + Restart = "on-failure"; + RestartSec = "30s"; + RestrictRealtime = true; + RestrictSUIDSGID = true; + SystemCallArchitectures = "native"; + SystemCallErrorNumber = "EPERM"; + SystemCallFilter = [ "@system-service" ]; + Type = "simple"; + UMask = 27; + }; + }; + }; + +}