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;
|
|
|
|
|
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 15:18:19 +01:00
|
|
|
in
|
|
|
|
{
|
|
|
|
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 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;
|
|
|
|
description = ''
|
|
|
|
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";
|
|
|
|
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 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
|
|
|
|
2018-06-30 00:58:35 +01:00
|
|
|
users.users = singleton {
|
2015-12-12 12:45:48 +00:00
|
|
|
name = clamavUser;
|
|
|
|
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
|
|
|
|
2018-06-30 00:58:35 +01:00
|
|
|
users.groups = singleton {
|
2015-12-12 12:45:48 +00:00
|
|
|
name = clamavGroup;
|
|
|
|
gid = config.ids.gids.clamav;
|
|
|
|
};
|
2012-07-23 15:18:19 +01: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 15:18:19 +01:00
|
|
|
|
2017-10-27 19:26:33 +01:00
|
|
|
systemd.services.clamav-daemon = optionalAttrs 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 15:18:19 +01:00
|
|
|
};
|
|
|
|
|
2017-10-27 19:26:33 +01:00
|
|
|
systemd.timers.clamav-freshclam = optionalAttrs 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";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-10-27 19:26:33 +01:00
|
|
|
systemd.services.clamav-freshclam = optionalAttrs 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";
|
|
|
|
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
|
|
|
}
|