# This file contains patches for Nixos 25.05 to be compatible with new stalwart mail.
# Also some minor patches to avoid having warnings on startup
{
  lib,
  config,
  pkgs,
  ...
}:
let
  configFormat = pkgs.formats.toml { };
  configFile = configFormat.generate "stalwart-mail.toml" config.services.stalwart-mail.settings;
in
{
  services.stalwart-mail.settings = {
    config.local-keys = [
      "spam-filter.resource"
    ]
    ++ [
      # I think these maybe should be added to nixpkgs?
      "resolver.*"
      "webadmin.*"
    ];
    spam-filter.resource = "file://${config.services.stalwart-mail.package.spam-filter}/spam-filter.toml";
  };
  systemd.services.stalwart-mail = lib.mkIf config.services.stalwart-mail.enable {
    serviceConfig = {
      User = "stalwart-mail";
      Group = "stalwart-mail";
      ExecStart = lib.mkForce [
        ""
        "${lib.getExe config.services.stalwart-mail.package} --config=${configFile}"
      ];
      ReadOnlyPaths = [ "${config.services.stalwart-mail.package.spam-filter}/spam-filter.toml" ];
    };
  };
}
