{ config, lib, pkgs, ... }:

{
  # Mautrix-signal settings
  services.signald.enable = true;
  systemd.services.matrix-as-signal = {
    requires = [ "signald.service" ];
    after = [ "signald.service" ];
    unitConfig = {
      JoinsNamespaceOf = "signald.service";
    };
    path = [
      pkgs.ffmpeg # voice messages need `ffmpeg`
    ];
  };

  services.matrix-appservices = {
    addRegistrationFiles = false;
    homeserverURL = "https://sealight.xyz/";
    homeserverDomain = "sealight.xyz";
    services = {
      whatsapp = {
        port = 29183;
        format = "mautrix-go";
        package = pkgs.mautrix-whatsapp;
      };

      discord = {
        port = 29188;
        format = "mautrix-go";
        package = pkgs.my-mautrix-discord;
      };

      signal = {
        port = 29184;
        format = "mautrix-python";
        package = pkgs.mautrix-signal;
        serviceConfig = {
          StateDirectory = [ "matrix-as-signal" "signald" ];
          SupplementaryGroups = [ "signald" ];
          TimeoutStopSec = 1; # work around the service ignoring SIGTERM, see https://gitlab.com/coffeetables/nix-matrix-appservices/-/issues/12
          User = lib.mkForce config.services.signald.user;
          Group = lib.mkForce config.services.signald.group;
        };
        settings.signal = {
          socket_path = config.services.signald.socketPath;
          outgoing_attachment_dir = "/var/lib/signald/tmp";
        };
      };
    };
  };
}
