2018-02-03 19:04:31 +01:00
|
|
|
{ config, options, pkgs, lib, ... }:
|
2016-01-12 10:06:46 +01:00
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.services.rspamd;
|
2018-02-03 19:04:31 +01:00
|
|
|
opts = options.services.rspamd;
|
2016-01-12 10:06:46 +01:00
|
|
|
|
2018-02-03 19:04:31 +01:00
|
|
|
bindSocketOpts = {options, config, ... }: {
|
|
|
|
options = {
|
|
|
|
socket = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
example = "localhost:11333";
|
|
|
|
description = ''
|
|
|
|
Socket for this worker to listen on in a format acceptable by rspamd.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
mode = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "0644";
|
|
|
|
description = "Mode to set on unix socket";
|
|
|
|
};
|
|
|
|
owner = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "${cfg.user}";
|
|
|
|
description = "Owner to set on unix socket";
|
|
|
|
};
|
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "${cfg.group}";
|
|
|
|
description = "Group to set on unix socket";
|
|
|
|
};
|
|
|
|
rawEntry = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
internal = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
config.rawEntry = let
|
|
|
|
maybeOption = option:
|
|
|
|
optionalString options.${option}.isDefined " ${option}=${config.${option}}";
|
|
|
|
in
|
|
|
|
if (!(hasPrefix "/" config.socket)) then "${config.socket}"
|
|
|
|
else "${config.socket}${maybeOption "mode"}${maybeOption "owner"}${maybeOption "group"}";
|
|
|
|
};
|
2016-03-25 15:12:59 +01:00
|
|
|
|
2018-02-03 19:04:31 +01:00
|
|
|
workerOpts = { name, ... }: {
|
|
|
|
options = {
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.nullOr types.bool;
|
|
|
|
default = null;
|
|
|
|
description = "Whether to run the rspamd worker.";
|
|
|
|
};
|
|
|
|
name = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = name;
|
|
|
|
description = "Name of the worker";
|
|
|
|
};
|
|
|
|
type = mkOption {
|
|
|
|
type = types.nullOr (types.enum [
|
2018-11-06 00:32:14 +01:00
|
|
|
"normal" "controller" "fuzzy_storage" "rspamd_proxy" "lua"
|
2018-02-03 19:04:31 +01:00
|
|
|
]);
|
|
|
|
description = "The type of this worker";
|
|
|
|
};
|
|
|
|
bindSockets = mkOption {
|
|
|
|
type = types.listOf (types.either types.str (types.submodule bindSocketOpts));
|
|
|
|
default = [];
|
|
|
|
description = ''
|
|
|
|
List of sockets to listen, in format acceptable by rspamd
|
|
|
|
'';
|
|
|
|
example = [{
|
|
|
|
socket = "/run/rspamd.sock";
|
|
|
|
mode = "0666";
|
|
|
|
owner = "rspamd";
|
|
|
|
} "*:11333"];
|
|
|
|
apply = value: map (each: if (isString each)
|
|
|
|
then if (isUnixSocket each)
|
|
|
|
then {socket = each; owner = cfg.user; group = cfg.group; mode = "0644"; rawEntry = "${each}";}
|
|
|
|
else {socket = each; rawEntry = "${each}";}
|
|
|
|
else each) value;
|
|
|
|
};
|
|
|
|
count = mkOption {
|
|
|
|
type = types.nullOr types.int;
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
Number of worker instances to run
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
includes = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
description = ''
|
|
|
|
List of files to include in configuration
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
description = "Additional entries to put verbatim into worker section of rspamd config file.";
|
|
|
|
};
|
|
|
|
};
|
2018-11-06 00:32:14 +01:00
|
|
|
config = mkIf (name == "normal" || name == "controller" || name == "fuzzy" || name == "rspamd_proxy") {
|
2018-02-03 19:04:31 +01:00
|
|
|
type = mkDefault name;
|
2018-11-06 00:32:14 +01:00
|
|
|
includes = mkDefault [ "$CONFDIR/worker-${if name == "rspamd_proxy" then "proxy" else name}.inc" ];
|
|
|
|
bindSockets =
|
|
|
|
let
|
|
|
|
unixSocket = name: {
|
|
|
|
mode = "0660";
|
|
|
|
socket = "/run/rspamd/${name}.sock";
|
|
|
|
owner = cfg.user;
|
|
|
|
group = cfg.group;
|
|
|
|
};
|
|
|
|
in mkDefault (if name == "normal" then [(unixSocket "rspamd")]
|
|
|
|
else if name == "controller" then [ "localhost:11334" ]
|
|
|
|
else if name == "rspamd_proxy" then [ (unixSocket "proxy") ]
|
|
|
|
else [] );
|
2018-02-03 19:04:31 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
isUnixSocket = socket: hasPrefix "/" (if (isString socket) then socket else socket.socket);
|
2018-09-28 04:00:24 +02:00
|
|
|
|
|
|
|
mkBindSockets = enabled: socks: concatStringsSep "\n "
|
|
|
|
(flatten (map (each: "bind_socket = \"${each.rawEntry}\";") socks));
|
2018-02-03 19:04:31 +01:00
|
|
|
|
|
|
|
rspamdConfFile = pkgs.writeText "rspamd.conf"
|
2016-03-25 15:12:59 +01:00
|
|
|
''
|
|
|
|
.include "$CONFDIR/common.conf"
|
|
|
|
|
|
|
|
options {
|
|
|
|
pidfile = "$RUNDIR/rspamd.pid";
|
|
|
|
.include "$CONFDIR/options.inc"
|
2018-11-01 23:55:44 +01:00
|
|
|
.include(try=true; priority=1,duplicate=merge) "$LOCAL_CONFDIR/local.d/options.inc"
|
|
|
|
.include(try=true; priority=10) "$LOCAL_CONFDIR/override.d/options.inc"
|
2016-03-25 15:12:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
logging {
|
2016-07-28 06:03:38 +02:00
|
|
|
type = "syslog";
|
2016-03-25 15:12:59 +01:00
|
|
|
.include "$CONFDIR/logging.inc"
|
2018-11-01 23:55:44 +01:00
|
|
|
.include(try=true; priority=1,duplicate=merge) "$LOCAL_CONFDIR/local.d/logging.inc"
|
|
|
|
.include(try=true; priority=10) "$LOCAL_CONFDIR/override.d/logging.inc"
|
2016-03-25 15:12:59 +01:00
|
|
|
}
|
|
|
|
|
2018-02-03 19:04:31 +01:00
|
|
|
${concatStringsSep "\n" (mapAttrsToList (name: value: ''
|
2018-11-06 00:26:55 +01:00
|
|
|
worker "${value.type}" {
|
2018-02-03 19:04:31 +01:00
|
|
|
type = "${value.type}";
|
|
|
|
${optionalString (value.enable != null)
|
|
|
|
"enabled = ${if value.enable != false then "yes" else "no"};"}
|
|
|
|
${mkBindSockets value.enable value.bindSockets}
|
|
|
|
${optionalString (value.count != null) "count = ${toString value.count};"}
|
|
|
|
${concatStringsSep "\n " (map (each: ".include \"${each}\"") value.includes)}
|
|
|
|
${value.extraConfig}
|
|
|
|
}
|
|
|
|
'') cfg.workers)}
|
2017-12-31 16:11:15 +01:00
|
|
|
|
|
|
|
${cfg.extraConfig}
|
2016-03-25 15:12:59 +01:00
|
|
|
'';
|
|
|
|
|
2018-11-01 23:55:44 +01:00
|
|
|
rspamdDir = pkgs.linkFarm "etc-rspamd-dir" (
|
|
|
|
(mapAttrsToList (name: file: { name = "local.d/${name}"; path = file.source; }) cfg.locals) ++
|
|
|
|
(mapAttrsToList (name: file: { name = "override.d/${name}"; path = file.source; }) cfg.overrides) ++
|
|
|
|
(optional (cfg.localLuaRules != null) { name = "rspamd.local.lua"; path = cfg.localLuaRules; }) ++
|
|
|
|
[ { name = "rspamd.conf"; path = rspamdConfFile; } ]
|
|
|
|
);
|
|
|
|
|
|
|
|
configFileModule = prefix: { name, config, ... }: {
|
|
|
|
options = {
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Whether this file ${prefix} should be generated. This
|
|
|
|
option allows specific ${prefix} files to be disabled.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
text = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = types.nullOr types.lines;
|
|
|
|
description = "Text of the file.";
|
|
|
|
};
|
|
|
|
|
|
|
|
source = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
description = "Path of the source file.";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
config = {
|
|
|
|
source = mkIf (config.text != null) (
|
|
|
|
let name' = "rspamd-${prefix}-" + baseNameOf name;
|
|
|
|
in mkDefault (pkgs.writeText name' config.text));
|
|
|
|
};
|
|
|
|
};
|
2016-01-12 10:06:46 +01:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
services.rspamd = {
|
|
|
|
|
2018-10-05 08:14:45 +02:00
|
|
|
enable = mkEnableOption "rspamd, the Rapid spam filtering system";
|
2016-01-12 10:06:46 +01:00
|
|
|
|
|
|
|
debug = mkOption {
|
2018-02-03 19:04:31 +01:00
|
|
|
type = types.bool;
|
2016-01-12 10:06:46 +01:00
|
|
|
default = false;
|
|
|
|
description = "Whether to run the rspamd daemon in debug mode.";
|
|
|
|
};
|
|
|
|
|
2018-11-01 23:55:44 +01:00
|
|
|
locals = mkOption {
|
|
|
|
type = with types; loaOf (submodule (configFileModule "locals"));
|
|
|
|
default = {};
|
|
|
|
description = ''
|
|
|
|
Local configuration files, written into <filename>/etc/rspamd/local.d/{name}</filename>.
|
|
|
|
'';
|
|
|
|
example = literalExample ''
|
|
|
|
{ "redis.conf".source = "/nix/store/.../etc/dir/redis.conf";
|
|
|
|
"arc.conf".text = "allow_envfrom_empty = true;";
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
overrides = mkOption {
|
|
|
|
type = with types; loaOf (submodule (configFileModule "overrides"));
|
|
|
|
default = {};
|
|
|
|
description = ''
|
|
|
|
Overridden configuration files, written into <filename>/etc/rspamd/override.d/{name}</filename>.
|
|
|
|
'';
|
|
|
|
example = literalExample ''
|
|
|
|
{ "redis.conf".source = "/nix/store/.../etc/dir/redis.conf";
|
|
|
|
"arc.conf".text = "allow_envfrom_empty = true;";
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
localLuaRules = mkOption {
|
|
|
|
default = null;
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
description = ''
|
|
|
|
Path of file to link to <filename>/etc/rspamd/rspamd.local.lua</filename> for local
|
|
|
|
rules written in Lua
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-02-03 19:04:31 +01:00
|
|
|
workers = mkOption {
|
|
|
|
type = with types; attrsOf (submodule workerOpts);
|
2016-03-25 15:12:59 +01:00
|
|
|
description = ''
|
2018-02-03 19:04:31 +01:00
|
|
|
Attribute set of workers to start.
|
|
|
|
'';
|
|
|
|
default = {
|
|
|
|
normal = {};
|
|
|
|
controller = {};
|
|
|
|
};
|
|
|
|
example = literalExample ''
|
|
|
|
{
|
|
|
|
normal = {
|
|
|
|
includes = [ "$CONFDIR/worker-normal.inc" ];
|
|
|
|
bindSockets = [{
|
|
|
|
socket = "/run/rspamd/rspamd.sock";
|
|
|
|
mode = "0660";
|
|
|
|
owner = "${cfg.user}";
|
|
|
|
group = "${cfg.group}";
|
|
|
|
}];
|
|
|
|
};
|
|
|
|
controller = {
|
|
|
|
includes = [ "$CONFDIR/worker-controller.inc" ];
|
|
|
|
bindSockets = [ "[::1]:11334" ];
|
|
|
|
};
|
|
|
|
}
|
2016-03-25 15:12:59 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2017-12-31 16:11:15 +01:00
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
Extra configuration to add at the end of the rspamd configuration
|
|
|
|
file.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-01-12 10:06:46 +01:00
|
|
|
user = mkOption {
|
|
|
|
type = types.string;
|
|
|
|
default = "rspamd";
|
|
|
|
description = ''
|
|
|
|
User to use when no root privileges are required.
|
|
|
|
'';
|
2018-11-06 00:32:14 +01:00
|
|
|
};
|
2016-01-12 10:06:46 +01:00
|
|
|
|
|
|
|
group = mkOption {
|
|
|
|
type = types.string;
|
|
|
|
default = "rspamd";
|
|
|
|
description = ''
|
|
|
|
Group to use when no root privileges are required.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
|
|
|
# Allow users to run 'rspamc' and 'rspamadm'.
|
|
|
|
environment.systemPackages = [ pkgs.rspamd ];
|
|
|
|
|
2018-06-30 01:58:35 +02:00
|
|
|
users.users = singleton {
|
2016-01-12 10:06:46 +01:00
|
|
|
name = cfg.user;
|
|
|
|
description = "rspamd daemon";
|
|
|
|
uid = config.ids.uids.rspamd;
|
|
|
|
group = cfg.group;
|
|
|
|
};
|
|
|
|
|
2018-06-30 01:58:35 +02:00
|
|
|
users.groups = singleton {
|
2016-01-12 10:06:46 +01:00
|
|
|
name = cfg.group;
|
2016-03-25 15:12:59 +01:00
|
|
|
gid = config.ids.gids.rspamd;
|
2016-01-12 10:06:46 +01:00
|
|
|
};
|
|
|
|
|
2018-11-01 23:55:44 +01:00
|
|
|
environment.etc."rspamd".source = rspamdDir;
|
2018-02-03 19:04:31 +01:00
|
|
|
|
2016-01-12 10:06:46 +01:00
|
|
|
systemd.services.rspamd = {
|
|
|
|
description = "Rspamd Service";
|
|
|
|
|
2018-09-28 04:00:24 +02:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
2018-11-01 23:55:44 +01:00
|
|
|
restartTriggers = [ rspamdDir ];
|
2016-01-12 10:06:46 +01:00
|
|
|
|
|
|
|
serviceConfig = {
|
2018-11-01 23:55:44 +01:00
|
|
|
ExecStart = "${pkgs.rspamd}/bin/rspamd ${optionalString cfg.debug "-d"} --user=${cfg.user} --group=${cfg.group} --pid=/run/rspamd.pid -c /etc/rspamd/rspamd.conf -f";
|
2016-01-12 10:06:46 +01:00
|
|
|
Restart = "always";
|
2016-07-28 06:03:38 +02:00
|
|
|
RuntimeDirectory = "rspamd";
|
|
|
|
PrivateTmp = true;
|
2016-01-12 10:06:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
preStart = ''
|
2016-07-28 06:03:38 +02:00
|
|
|
${pkgs.coreutils}/bin/mkdir -p /var/lib/rspamd
|
2016-01-12 10:06:46 +01:00
|
|
|
${pkgs.coreutils}/bin/chown ${cfg.user}:${cfg.group} /var/lib/rspamd
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2018-02-03 19:04:31 +01:00
|
|
|
imports = [
|
2018-09-28 04:00:24 +02:00
|
|
|
(mkRemovedOptionModule [ "services" "rspamd" "socketActivation" ]
|
|
|
|
"Socket activation never worked correctly and could at this time not be fixed and so was removed")
|
2018-02-03 19:04:31 +01:00
|
|
|
(mkRenamedOptionModule [ "services" "rspamd" "bindSocket" ] [ "services" "rspamd" "workers" "normal" "bindSockets" ])
|
|
|
|
(mkRenamedOptionModule [ "services" "rspamd" "bindUISocket" ] [ "services" "rspamd" "workers" "controller" "bindSockets" ])
|
|
|
|
];
|
2016-01-12 10:06:46 +01:00
|
|
|
}
|