2014-04-14 14:26:48 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
with lib;
|
2012-07-23 14:18:19 +00:00
|
|
|
let
|
|
|
|
clamavUser = "clamav";
|
|
|
|
stateDir = "/var/lib/clamav";
|
2016-11-15 03:40:33 +00:00
|
|
|
runDir = "/run/clamav";
|
2012-07-23 14:18:19 +00:00
|
|
|
clamavGroup = clamavUser;
|
|
|
|
cfg = config.services.clamav;
|
2016-11-15 03:40:33 +00:00
|
|
|
pkg = pkgs.clamav;
|
|
|
|
|
2015-12-12 12:45:48 +00:00
|
|
|
clamdConfigFile = pkgs.writeText "clamd.conf" ''
|
|
|
|
DatabaseDirectory ${stateDir}
|
|
|
|
LocalSocket ${runDir}/clamd.ctl
|
|
|
|
PidFile ${runDir}/clamd.pid
|
2016-11-15 03:40:33 +00:00
|
|
|
TemporaryDirectory /tmp
|
2015-12-12 12:45:48 +00:00
|
|
|
User clamav
|
2016-11-15 03:40:33 +00:00
|
|
|
Foreground yes
|
2015-12-12 12:45:48 +00:00
|
|
|
|
|
|
|
${cfg.daemon.extraConfig}
|
|
|
|
'';
|
2016-11-15 03:40:33 +00:00
|
|
|
|
|
|
|
freshclamConfigFile = pkgs.writeText "freshclam.conf" ''
|
|
|
|
DatabaseDirectory ${stateDir}
|
|
|
|
Foreground yes
|
|
|
|
Checks ${toString cfg.updater.frequency}
|
|
|
|
|
|
|
|
${cfg.updater.extraConfig}
|
|
|
|
|
|
|
|
DatabaseMirror database.clamav.net
|
|
|
|
'';
|
2012-07-23 14:18:19 +00:00
|
|
|
in
|
|
|
|
{
|
2019-12-10 01:51:19 +00:00
|
|
|
imports = [
|
|
|
|
(mkRenamedOptionModule [ "services" "clamav" "updater" "config" ] [ "services" "clamav" "updater" "extraConfig" ])
|
|
|
|
];
|
|
|
|
|
2012-07-23 14:18:19 +00: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
|
|
|
|
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
Extra configuration for clamd. Contents will be added verbatim to the
|
|
|
|
configuration file.
|
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 14:18:19 +00: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;
|
|
|
|
description = ''
|
|
|
|
Number of database checks per day.
|
|
|
|
'';
|
|
|
|
};
|
2012-07-23 14:18:19 +00:00
|
|
|
|
2016-11-15 03:40:33 +00:00
|
|
|
interval = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "hourly";
|
|
|
|
description = ''
|
|
|
|
How often freshclam is invoked. See systemd.time(7) for more
|
|
|
|
information about the format.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.lines;
|
2015-12-12 12:06:14 +00:00
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
Extra configuration for freshclam. Contents will be added verbatim to the
|
|
|
|
configuration file.
|
|
|
|
'';
|
|
|
|
};
|
2012-07-23 14:18:19 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-10-27 18:26:33 +00:00
|
|
|
config = mkIf (cfg.updater.enable || cfg.daemon.enable) {
|
2015-09-29 18:08:53 +00:00
|
|
|
environment.systemPackages = [ pkg ];
|
2017-10-27 18:26:33 +00:00
|
|
|
|
2019-09-14 17:51:29 +00: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 14:18:19 +00:00
|
|
|
|
2019-09-14 17:51:29 +00:00
|
|
|
users.groups.${clamavGroup} =
|
|
|
|
{ gid = config.ids.gids.clamav; };
|
2012-07-23 14:18:19 +00:00
|
|
|
|
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 14:18:19 +00:00
|
|
|
|
2018-10-01 22:07:25 +00: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";
|
|
|
|
requires = 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 14:18:19 +00:00
|
|
|
};
|
|
|
|
|
2018-10-01 22:07:25 +00: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 22:07:25 +00: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 ];
|
|
|
|
|
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 21:34:06 +00: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 14:18:19 +00:00
|
|
|
};
|
2014-09-01 07:41:19 +00:00
|
|
|
}
|