<commit_before>{ config, lib, pkgs, ... }:

with lib;

let

  rspamdCfg = config.services.rspamd;
  cfg = config.services.rmilter;

  inetSockets = map (sock: let s = stringSplit ":" sock; in "inet:${last s}:${head s}") cfg.bindInetSockets;
  unixSockets = map (sock: "unix:${sock}") cfg.bindUnixSockets;

  allSockets = unixSockets ++ inetSockets;

  rmilterConf = ''
pidfile = /run/rmilter/rmilter.pid;
bind_socket = ${if cfg.socketActivation then "fd:3" else concatStringsSep ", " allSockets};
tempdir = /tmp;

  '' + (with cfg.rspamd; if enable then ''
spamd {
        servers = ${concatStringsSep ", " servers};
        connect_timeout = 1s;
        results_timeout = 20s;
        error_time = 10;
        dead_time = 300;
        maxerrors = 10;
        reject_message = "${rejectMessage}";
        ${optionalString (length whitelist != 0)  "whitelist = ${concatStringsSep ", " whitelist};"}

        # rspamd_metric - metric for using with rspamd
        # Default: "default"
        rspamd_metric = "default";
        ${extraConfig}
};
    '' else "") + cfg.extraConfig;

  rmilterConfigFile = pkgs.writeText "rmilter.conf" rmilterConf;

in

{

  ###### interface

  options = {

    services.rmilter = {

      enable = mkOption {
        default = cfg.rspamd.enable;
        description = "Whether to run the rmilter daemon.";
      };

      debug = mkOption {
        default = false;
        description = "Whether to run the rmilter daemon in debug mode.";
      };

      user = mkOption {
        type = types.string;
        default = "rmilter";
        description = ''
          User to use when no root privileges are required.
        '';
       };

      group = mkOption {
        type = types.string;
        default = "rmilter";
        description = ''
          Group to use when no root privileges are required.
        '';
       };

      bindUnixSockets =  mkOption {
        type = types.listOf types.str;
        default = ["/run/rmilter/rmilter.sock"];
        description = ''
          Unix domain sockets to listen for MTA requests.
        '';
        example = ''
            [ "/run/rmilter.sock"]
        '';
      };

      bindInetSockets = mkOption {
        type = types.listOf types.str;
        default = [];
        description = ''
          Inet addresses to listen (in format accepted by systemd.socket)
        '';
        example = ''
            ["127.0.0.1:11990"]
        '';
      };

      socketActivation = mkOption {
        type = types.bool;
        default = true;
        description = ''
          Enable systemd socket activation for rmilter.
          (disabling socket activation not recommended
          when unix socket used, and follow to wrong
          permissions on unix domain socket.)
        '';
      };

      rspamd = {
        enable = mkOption {
          default = rspamdCfg.enable;
          description = "Whether to use rspamd to filter mails";
        };

        servers = mkOption {
          type = types.listOf types.str;
          default = ["r:/run/rspamd/rspamd.sock"];
          description = ''
            Spamd socket definitions.
            Is server name is prefixed with r: it is rspamd server.
          '';
        };<commit_msg>rmilter service: Fix a couple of bugs<commit_after>{ config, lib, pkgs, ... }:

with lib;

let

  rspamdCfg = config.services.rspamd;
  cfg = config.services.rmilter;

  inetSockets = map (sock: let s = splitString ":" sock; in "inet:${last s}@${head s}") cfg.bindInetSockets;
  unixSockets = map (sock: "unix:${sock}") cfg.bindUnixSockets;

  allSockets = unixSockets ++ inetSockets;

  rmilterConf = ''
    pidfile = /run/rmilter/rmilter.pid;
    bind_socket = ${if cfg.socketActivation then "fd:3" else last inetSockets};
    tempdir = /tmp;
  '' + (with cfg.rspamd; if enable then ''
    spamd {
      servers = ${concatStringsSep ", " servers};
      connect_timeout = 1s;
      results_timeout = 20s;
      error_time = 10;
      dead_time = 300;
      maxerrors = 10;
      reject_message = "${rejectMessage}";
      ${optionalString (length whitelist != 0)  "whitelist = ${concatStringsSep ", " whitelist};"}

      # rspamd_metric - metric for using with rspamd
      # Default: "default"
      rspamd_metric = "default";
      ${extraConfig}
    };
    '' else "") + cfg.extraConfig;

  rmilterConfigFile = pkgs.writeText "rmilter.conf" rmilterConf;

in

{

  ###### interface

  options = {

    services.rmilter = {

      enable = mkOption {
        default = cfg.rspamd.enable;
        description = "Whether to run the rmilter daemon.";
      };

      debug = mkOption {
        default = false;
        description = "Whether to run the rmilter daemon in debug mode.";
      };

      user = mkOption {
        type = types.string;
        default = "rmilter";
        description = ''
          User to use when no root privileges are required.
        '';
       };

      group = mkOption {
        type = types.string;
        default = "rmilter";
        description = ''
          Group to use when no root privileges are required.
        '';
       };

      bindUnixSockets =  mkOption {
        type = types.listOf types.str;
        default = ["/run/rmilter/rmilter.sock"];
        description = ''
          Unix domain sockets to listen for MTA requests.
        '';
        example = ''
            [ "/run/rmilter.sock"]
        '';
      };

      bindInetSockets = mkOption {
        type = types.listOf types.str;
        default = [];
        description = ''
          Inet addresses to listen (in format accepted by systemd.socket)
        '';
        example = ''
            ["127.0.0.1:11990"]
        '';
      };

      socketActivation = mkOption {
        type = types.bool;
        default = true;
        description = ''
          Enable systemd socket activation for rmilter.

          Disabling socket activation is not recommended when a Unix
          domain socket is used and could lead to incorrect
          permissions.  Therefore, setting this to false will
          configure rmilter to use an inet socket only.
        '';
      };

      rspamd = {
        enable = mkOption {
          default = rspamdCfg.enable;
          description = "Whether to use rspamd to filter mails";
        };

        servers = mkOption {
          type = types.listOf types.str;
          default = ["r:/run/rspamd/rspamd.sock"];
          description = ''
            Spamd socket definitions.
            Is server name is prefixed with r: it is rspamd server.
          '';
        };
{'fraction_non_alphanumeric': 0.11158471609072919, 'fraction_numerical': 0.007002587912924341, 'mean_word_length': 1.9096545615589018, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4214862', 'n_tokens_mistral': 1982, 'n_tokens_neox': 1922, 'n_words': 525}