From 98a4f345eb410b3bbd3345f4adc18eb053b4e7e9 Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Fri, 2 Apr 2021 14:01:38 +0200 Subject: [PATCH] fuuko/matrix/mautrix-whatsapp: Init --- machines/fuuko/services/matrix/default.nix | 1 + .../services/matrix/mautrix-whatsapp.nix | 92 +++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 machines/fuuko/services/matrix/mautrix-whatsapp.nix diff --git a/machines/fuuko/services/matrix/default.nix b/machines/fuuko/services/matrix/default.nix index f342253..7dc8c59 100644 --- a/machines/fuuko/services/matrix/default.nix +++ b/machines/fuuko/services/matrix/default.nix @@ -1,5 +1,6 @@ { imports = [ ./synapse.nix + ./mautrix-whatsapp.nix ]; } diff --git a/machines/fuuko/services/matrix/mautrix-whatsapp.nix b/machines/fuuko/services/matrix/mautrix-whatsapp.nix new file mode 100644 index 0000000..2df32f7 --- /dev/null +++ b/machines/fuuko/services/matrix/mautrix-whatsapp.nix @@ -0,0 +1,92 @@ +# somewhat adapted from https://github.com/NixOS/nixpkgs/pull/59211 +{ config, lib, pkgs, ... }: +let + synapseCfg = config.services.matrix-synapse; +in +let + config = rec { + homeserver = { + address = synapseCfg.public_baseurl; + domain = synapseCfg.server_name; + }; + appservice = rec { + hostname = "127.0.0.1"; + port = 29318; + address = "http://${hostname}:${toString port}"; + provisioning.shared_secret = "disable"; + database = { + type = "sqlite3"; + uri = "/var/lib/mautrix-whatsapp/mautrix-whatsapp.db"; + }; + id = "whatsapp"; + bot = { + username = "whatsappbot"; + displayname = "WhatsApp bridge bot"; + avatar = "mxc://maunium.net/NeXNQarUbrlYBiPCpprYsRqr"; + }; + }; + whatsapp = { + browser_name = "mx-wa"; + os_name = "Mautrix-WhatsApp bridge"; + }; + bridge = { + chat_list_wait = 30; + command_prefix = "!wa"; + connection_retry_delay = -1; + connection_timeout = 20; + delivery_receipts = true; + displayname_template = "{{if .Name}}{{.Name}}{{else if .Notify}}{{.Notify}}{{else}}{{.Jid}}{{end}} (WA)"; + fetch_message_on_timeout = true; + initial_chat_sync_count = 10; + initial_history_fill_count = 20; + invite_own_puppet_for_backfilling = true; + login_qr_regen_count = 2; + max_connection_attempts = 12; + portal_sync_wait = 600; + private_chat_portal_meta = true; + recovery_chat_sync_count = -1; + recovery_history_backfill = true; + relaybot.enabled = false; + report_connection_retry = true; + sync_max_chat_age = 259200; + sync_with_custom_puppets = true; + username_template = "whatsapp_{{.}}"; + call_notices = { + start = true; + end = true; + }; + permissions = { + # Only one user since using the name from the address book does not + # work with multiple users + "@simon:${homeserver.domain}" = 100; + }; + }; + logging.print_level = "info"; + }; + + generatedConfig = pkgs.runCommandNoCC "mautrix-whatsapp-config" + { + buildInputs = with pkgs; [ mautrix-whatsapp ]; + } + '' + mkdir $out + cat ${pkgs.writeText "mautrix-whatsapp.yaml" (lib.generators.toYAML { } config)} > $out/config.yaml + mautrix-whatsapp -c $out/config.yaml -g -r $out/registration.yaml + ''; +in +{ + systemd.services.mautrix-whatsapp = { + description = "Mautrix-WhatsApp Service - A WhatsApp bridge for Matrix"; + after = [ "network.target" "matrix-synapse.service" ]; + wantedBy = [ "multi-user.target" ]; + + serviceConfig = { + DynamicUser = true; + StateDirectory = "mautrix-whatsapp"; + ExecStart = "${pkgs.mautrix-whatsapp}/bin/mautrix-whatsapp -c ${generatedConfig}/config.yaml"; + Restart = "on-failure"; + }; + }; + + services.matrix-synapse.app_service_config_files = lib.singleton "${generatedConfig}/registration.yaml"; +}