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
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2015-12-12 12:45:48 +00:00
|
|
|
config = mkIf cfg.updater.enable or cfg.daemon.enable {
|
2015-09-29 19:08:53 +01:00
|
|
|
environment.systemPackages = [ pkg ];
|
2015-12-12 12:45:48 +00:00
|
|
|
users.extraUsers = singleton {
|
|
|
|
name = clamavUser;
|
|
|
|
uid = config.ids.uids.clamav;
|
|
|
|
description = "ClamAV daemon user";
|
|
|
|
home = stateDir;
|
|
|
|
};
|
2012-07-23 15:18:19 +01:00
|
|
|
|
2015-12-12 12:45:48 +00:00
|
|
|
users.extraGroups = singleton {
|
|
|
|
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
|
|
|
|
2016-11-15 03:40:33 +00:00
|
|
|
systemd.services.clamav-daemon = mkIf cfg.daemon.enable {
|
2015-12-12 12:45:48 +00:00
|
|
|
description = "ClamAV daemon (clamd)";
|
2016-11-15 03:40:33 +00:00
|
|
|
after = mkIf cfg.updater.enable [ "clamav-freshclam.service" ];
|
|
|
|
requires = mkIf 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
|
|
|
};
|
|
|
|
|
2016-11-15 03:40:33 +00:00
|
|
|
systemd.timers.clamav-freshclam = mkIf cfg.updater.enable {
|
|
|
|
description = "Timer for ClamAV virus database updater (freshclam)";
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
timerConfig = {
|
|
|
|
OnCalendar = cfg.updater.interval;
|
|
|
|
Unit = "clamav-freshclam.service";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.clamav-freshclam = mkIf cfg.updater.enable {
|
|
|
|
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
|
|
|
}
|