From 06855f1cd823555f1b79f0bf8be1f96f91556739 Mon Sep 17 00:00:00 2001 From: Alexander Linder <34923333+SeineEloquenz@users.noreply.github.com> Date: Thu, 22 Aug 2024 14:14:51 +0200 Subject: [PATCH] nixos/mautrix-whatsapp: automatic appservice registration option, pull login_shared_secret_map setting from env file (#311681) Co-authored-by: Sandro --- .../services/matrix/mautrix-whatsapp.nix | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/nixos/modules/services/matrix/mautrix-whatsapp.nix b/nixos/modules/services/matrix/mautrix-whatsapp.nix index d124edc216dd..89bf7f9e9fac 100644 --- a/nixos/modules/services/matrix/mautrix-whatsapp.nix +++ b/nixos/modules/services/matrix/mautrix-whatsapp.nix @@ -108,6 +108,16 @@ in { List of Systemd services to require and wait for when starting the application service. ''; }; + + registerToSynapse = lib.mkOption { + type = lib.types.bool; + default = config.services.matrix-synapse.enable; + defaultText = lib.literalExpression "config.services.matrix-synapse.enable"; + description = '' + Whether to add the bridge's app service registration file to + `services.matrix-synapse.settings.app_service_config_files`. + ''; + }; }; config = lib.mkIf cfg.enable { @@ -121,6 +131,13 @@ in { users.groups.mautrix-whatsapp = {}; + services.matrix-synapse = lib.mkIf cfg.registerToSynapse { + settings.app_service_config_files = [ registrationFile ]; + }; + systemd.services.matrix-synapse = lib.mkIf cfg.registerToSynapse { + serviceConfig.SupplementaryGroups = [ "mautrix-whatsapp" ]; + }; + services.mautrix-whatsapp.settings = lib.mkMerge (map mkDefaults [ defaultConfig # Note: this is defined here to avoid the docs depending on `config` @@ -155,10 +172,15 @@ in { chmod 640 ${registrationFile} umask 0177 + # 1. Overwrite registration tokens in config + # 2. If environment variable MAUTRIX_WHATSAPP_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].appservice.as_token = .[1].as_token | .[0].appservice.hs_token = .[1].hs_token - | .[0]' '${settingsFile}' '${registrationFile}' \ - > '${settingsFile}.tmp' + | .[0] + | if env.MAUTRIX_WHATSAPP_BRIDGE_LOGIN_SHARED_SECRET then .bridge.login_shared_secret_map.[.homeserver.domain] = env.MAUTRIX_WHATSAPP_BRIDGE_LOGIN_SHARED_SECRET else . end' \ + '${settingsFile}' '${registrationFile}' > '${settingsFile}.tmp' mv '${settingsFile}.tmp' '${settingsFile}' umask $old_umask '';