2014-04-14 15:26:48 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
with lib;
|
2012-07-23 15:18:19 +01:00
|
|
|
let
|
|
|
|
clamavUser = "clamav";
|
|
|
|
stateDir = "/var/lib/clamav";
|
2016-11-15 03:40:33 +00:00
|
|
|
runDir = "/run/clamav";
|
2012-07-23 15:18:19 +01:00
|
|
|
clamavGroup = clamavUser;
|
|
|
|
cfg = config.services.clamav;
|
2016-11-15 03:40:33 +00:00
|
|
|
pkg = pkgs.clamav;
|
|
|
|
|
2020-12-12 00:56:08 +00:00
|
|
|
toKeyValue = generators.toKeyValue {
|
2022-01-16 12:20:08 +00:00
|
|
|
mkKeyValue = generators.mkKeyValueDefault { } " ";
|
2020-12-12 00:56:08 +00:00
|
|
|
listsAsDuplicateKeys = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
clamdConfigFile = pkgs.writeText "clamd.conf" (toKeyValue cfg.daemon.settings);
|
|
|
|
freshclamConfigFile = pkgs.writeText "freshclam.conf" (toKeyValue cfg.updater.settings);
|
2012-07-23 15:18:19 +01:00
|
|
|
in
|
|
|
|
{
|
2019-12-10 01:51:19 +00:00
|
|
|
imports = [
|
2020-12-12 00:56:08 +00:00
|
|
|
(mkRemovedOptionModule [ "services" "clamav" "updater" "config" ] "Use services.clamav.updater.settings instead.")
|
|
|
|
(mkRemovedOptionModule [ "services" "clamav" "updater" "extraConfig" ] "Use services.clamav.updater.settings instead.")
|
|
|
|
(mkRemovedOptionModule [ "services" "clamav" "daemon" "extraConfig" ] "Use services.clamav.daemon.settings instead.")
|
2019-12-10 01:51:19 +00:00
|
|
|
];
|
|
|
|
|
2012-07-23 15:18:19 +01:00
|
|
|
options = {
|
|
|
|
services.clamav = {
|
2015-12-12 12:45:48 +00:00
|
|
|
daemon = {
|
2016-11-15 03:40:33 +00:00
|
|
|
enable = mkEnableOption "ClamAV clamd daemon";
|
2015-12-12 12:45:48 +00:00
|
|
|
|
2020-12-12 00:56:08 +00:00
|
|
|
settings = mkOption {
|
|
|
|
type = with types; attrsOf (oneOf [ bool int str (listOf str) ]);
|
2022-01-16 12:20:08 +00:00
|
|
|
default = { };
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
ClamAV configuration. Refer to <https://linux.die.net/man/5/clamd.conf>,
|
2020-12-12 00:56:08 +00:00
|
|
|
for details on supported values.
|
2015-12-12 12:06:14 +00:00
|
|
|
'';
|
|
|
|
};
|
2015-12-12 12:45:48 +00:00
|
|
|
};
|
|
|
|
updater = {
|
2016-11-15 03:40:33 +00:00
|
|
|
enable = mkEnableOption "ClamAV freshclam updater";
|
2012-07-23 15:18:19 +01:00
|
|
|
|
2015-12-12 12:06:14 +00:00
|
|
|
frequency = mkOption {
|
2016-11-15 03:40:33 +00:00
|
|
|
type = types.int;
|
2015-12-12 12:06:14 +00:00
|
|
|
default = 12;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2015-12-12 12:06:14 +00:00
|
|
|
Number of database checks per day.
|
|
|
|
'';
|
|
|
|
};
|
2012-07-23 15:18:19 +01:00
|
|
|
|
2016-11-15 03:40:33 +00:00
|
|
|
interval = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "hourly";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2016-11-15 03:40:33 +00:00
|
|
|
How often freshclam is invoked. See systemd.time(7) for more
|
|
|
|
information about the format.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-12-12 00:56:08 +00:00
|
|
|
settings = mkOption {
|
|
|
|
type = with types; attrsOf (oneOf [ bool int str (listOf str) ]);
|
2022-01-16 12:20:08 +00:00
|
|
|
default = { };
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
freshclam configuration. Refer to <https://linux.die.net/man/5/freshclam.conf>,
|
2020-12-12 00:56:08 +00:00
|
|
|
for details on supported values.
|
2015-12-12 12:06:14 +00:00
|
|
|
'';
|
|
|
|
};
|
2012-07-23 15:18:19 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-10-27 19:26:33 +01:00
|
|
|
config = mkIf (cfg.updater.enable || cfg.daemon.enable) {
|
2015-09-29 19:08:53 +01:00
|
|
|
environment.systemPackages = [ pkg ];
|
2017-10-27 19:26:33 +01:00
|
|
|
|
2019-09-14 18:51:29 +01:00
|
|
|
users.users.${clamavUser} = {
|
2015-12-12 12:45:48 +00:00
|
|
|
uid = config.ids.uids.clamav;
|
2017-01-15 18:58:29 +00:00
|
|
|
group = clamavGroup;
|
2015-12-12 12:45:48 +00:00
|
|
|
description = "ClamAV daemon user";
|
|
|
|
home = stateDir;
|
|
|
|
};
|
2012-07-23 15:18:19 +01:00
|
|
|
|
2019-09-14 18:51:29 +01:00
|
|
|
users.groups.${clamavGroup} =
|
|
|
|
{ gid = config.ids.gids.clamav; };
|
2012-07-23 15:18:19 +01:00
|
|
|
|
2020-12-12 00:56:08 +00:00
|
|
|
services.clamav.daemon.settings = {
|
|
|
|
DatabaseDirectory = stateDir;
|
|
|
|
LocalSocket = "${runDir}/clamd.ctl";
|
|
|
|
PidFile = "${runDir}/clamd.pid";
|
|
|
|
TemporaryDirectory = "/tmp";
|
|
|
|
User = "clamav";
|
|
|
|
Foreground = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
services.clamav.updater.settings = {
|
|
|
|
DatabaseDirectory = stateDir;
|
|
|
|
Foreground = true;
|
|
|
|
Checks = cfg.updater.frequency;
|
|
|
|
DatabaseMirror = [ "database.clamav.net" ];
|
|
|
|
};
|
|
|
|
|
2016-11-15 03:40:33 +00:00
|
|
|
environment.etc."clamav/freshclam.conf".source = freshclamConfigFile;
|
|
|
|
environment.etc."clamav/clamd.conf".source = clamdConfigFile;
|
2012-07-23 15:18:19 +01:00
|
|
|
|
2018-10-01 23:07:25 +01:00
|
|
|
systemd.services.clamav-daemon = mkIf cfg.daemon.enable {
|
2015-12-12 12:45:48 +00:00
|
|
|
description = "ClamAV daemon (clamd)";
|
2017-12-31 02:40:13 +00:00
|
|
|
after = optional cfg.updater.enable "clamav-freshclam.service";
|
2015-12-12 12:45:48 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2016-11-15 03:40:33 +00:00
|
|
|
restartTriggers = [ clamdConfigFile ];
|
|
|
|
|
2015-12-12 12:45:48 +00:00
|
|
|
preStart = ''
|
|
|
|
mkdir -m 0755 -p ${runDir}
|
|
|
|
chown ${clamavUser}:${clamavGroup} ${runDir}
|
|
|
|
'';
|
2016-11-15 03:40:33 +00:00
|
|
|
|
2015-12-12 12:45:48 +00:00
|
|
|
serviceConfig = {
|
2016-11-15 03:40:33 +00:00
|
|
|
ExecStart = "${pkg}/bin/clamd";
|
|
|
|
ExecReload = "${pkgs.coreutils}/bin/kill -USR2 $MAINPID";
|
|
|
|
PrivateTmp = "yes";
|
|
|
|
PrivateDevices = "yes";
|
|
|
|
PrivateNetwork = "yes";
|
2015-12-12 12:45:48 +00:00
|
|
|
};
|
2012-07-23 15:18:19 +01:00
|
|
|
};
|
|
|
|
|
2018-10-01 23:07:25 +01:00
|
|
|
systemd.timers.clamav-freshclam = mkIf cfg.updater.enable {
|
2016-11-15 03:40:33 +00:00
|
|
|
description = "Timer for ClamAV virus database updater (freshclam)";
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
timerConfig = {
|
|
|
|
OnCalendar = cfg.updater.interval;
|
|
|
|
Unit = "clamav-freshclam.service";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-10-01 23:07:25 +01:00
|
|
|
systemd.services.clamav-freshclam = mkIf cfg.updater.enable {
|
2016-11-15 03:40:33 +00:00
|
|
|
description = "ClamAV virus database updater (freshclam)";
|
|
|
|
restartTriggers = [ freshclamConfigFile ];
|
2022-01-16 12:20:08 +00:00
|
|
|
after = [ "network-online.target" ];
|
2015-12-12 12:45:48 +00:00
|
|
|
preStart = ''
|
|
|
|
mkdir -m 0755 -p ${stateDir}
|
|
|
|
chown ${clamavUser}:${clamavGroup} ${stateDir}
|
|
|
|
'';
|
2016-11-15 03:40:33 +00:00
|
|
|
|
2015-12-12 12:45:48 +00:00
|
|
|
serviceConfig = {
|
2016-11-15 03:40:33 +00:00
|
|
|
Type = "oneshot";
|
|
|
|
ExecStart = "${pkg}/bin/freshclam";
|
2018-10-01 22:34:06 +01:00
|
|
|
SuccessExitStatus = "1"; # if databases are up to date
|
2016-11-15 03:40:33 +00:00
|
|
|
PrivateTmp = "yes";
|
|
|
|
PrivateDevices = "yes";
|
2015-12-12 12:45:48 +00:00
|
|
|
};
|
|
|
|
};
|
2012-07-23 15:18:19 +01:00
|
|
|
};
|
2014-09-01 08:41:19 +01:00
|
|
|
}
|