summary refs log tree commit diff
path: root/nixos/modules/services/security/clamav.nix
blob: 548aee29b266878cd69d1c9d57ba81dc913113ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
{ config, lib, pkgs, ... }:
with lib;
let
  clamavUser = "clamav";
  stateDir = "/var/lib/clamav";
  runDir = "/var/run/clamav";
  logDir = "/var/log/clamav";
  clamavGroup = clamavUser;
  cfg = config.services.clamav;
  clamdConfigFile = pkgs.writeText "clamd.conf" ''
    DatabaseDirectory ${stateDir}
    LocalSocket ${runDir}/clamd.ctl
    LogFile ${logDir}/clamav.log
    PidFile ${runDir}/clamd.pid
    User clamav

    ${cfg.daemon.extraConfig}
  '';
in
{
  options = {
    services.clamav = {
      daemon = {
        enable = mkEnableOption "clamd daemon";

        extraConfig = mkOption {
          type = types.lines;
          default = "";
          description = ''
            Extra configuration for clamd. Contents will be added verbatim to the
            configuration file.
          '';
        };
      };
      updater = {
        enable = mkEnableOption "freshclam updater";

        frequency = mkOption {
          default = 12;
          description = ''
            Number of database checks per day.
          '';
        };

        config = mkOption {
          default = "";
          description = ''
            Extra configuration for freshclam. Contents will be added verbatim to the
            configuration file.
          '';
        };
      };
    };
  };

  config = mkIf cfg.updater.enable or cfg.daemon.enable {
    environment.systemPackages = [ pkgs.clamav ];
    users.extraUsers = singleton {
      name = clamavUser;
      uid = config.ids.uids.clamav;
      description = "ClamAV daemon user";
      home = stateDir;
    };

    users.extraGroups = singleton {
      name = clamavGroup;
      gid = config.ids.gids.clamav;
    };

    services.clamav.updater.config = mkIf cfg.updater.enable ''
      DatabaseDirectory ${stateDir}
      Foreground yes
      Checks ${toString cfg.updater.frequency}
      DatabaseMirror database.clamav.net
    '';

    systemd.services.clamd = mkIf cfg.daemon.enable {
      description = "ClamAV daemon (clamd)";
      path = [ pkgs.clamav ];
      after = [ "network.target" "freshclam.service" ];
      requires = [ "freshclam.service" ];
      wantedBy = [ "multi-user.target" ];
      preStart = ''
        mkdir -m 0755 -p ${logDir}
        mkdir -m 0755 -p ${runDir}
        chown ${clamavUser}:${clamavGroup} ${logDir}
        chown ${clamavUser}:${clamavGroup} ${runDir}
      '';
      serviceConfig = {
        ExecStart = "${pkgs.clamav}/bin/clamd --config-file=${clamdConfigFile}";
        Type = "forking";
        ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
        Restart = "on-failure";
        RestartSec = "10s";
        StartLimitInterval = "1min";
      };
    };

    systemd.services.freshclam = mkIf cfg.updater.enable {
      description = "ClamAV updater (freshclam)";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      path = [ pkgs.clamav ];
      preStart = ''
        mkdir -m 0755 -p ${stateDir}
        chown ${clamavUser}:${clamavGroup} ${stateDir}
      '';
      serviceConfig = {
        ExecStart = "${pkgs.clamav}/bin/freshclam --daemon --config-file=${pkgs.writeText "freshclam.conf" cfg.updater.config}";
        ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
        Restart = "on-failure";
        RestartSec = "10s";
        StartLimitInterval = "1min";
      };
    };
  };
}