summary refs log tree commit diff
path: root/nixos/modules/services/mail/exim.nix
blob: 7356db2b6a629e3b71fe1a3117f5872ac3b6c285 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
{ config, lib, pkgs, ... }:

let
  inherit (lib) literalExpression mkIf mkOption singleton types;
  inherit (pkgs) coreutils;
  cfg = config.services.exim;
in

{

  ###### interface

  options = {

    services.exim = {

      enable = mkOption {
        type = types.bool;
        default = false;
        description = "Whether to enable the Exim mail transfer agent.";
      };

      config = mkOption {
        type = types.lines;
        default = "";
        description = ''
          Verbatim Exim configuration.  This should not contain exim_user,
          exim_group, exim_path, or spool_directory.
        '';
      };

      user = mkOption {
        type = types.str;
        default = "exim";
        description = ''
          User to use when no root privileges are required.
          In particular, this applies when receiving messages and when doing
          remote deliveries.  (Local deliveries run as various non-root users,
          typically as the owner of a local mailbox.) Specifying this value
          as root is not supported.
        '';
      };

      group = mkOption {
        type = types.str;
        default = "exim";
        description = ''
          Group to use when no root privileges are required.
        '';
      };

      spoolDir = mkOption {
        type = types.path;
        default = "/var/spool/exim";
        description = ''
          Location of the spool directory of exim.
        '';
      };

      package = mkOption {
        type = types.package;
        default = pkgs.exim;
        defaultText = literalExpression "pkgs.exim";
        description = ''
          The Exim derivation to use.
          This can be used to enable features such as LDAP or PAM support.
        '';
      };

      queueRunnerInterval = mkOption {
        type = types.str;
        default = "5m";
        description = ''
          How often to spawn a new queue runner.
        '';
      };
    };

  };


  ###### implementation

  config = mkIf cfg.enable {

    environment = {
      etc."exim.conf".text = ''
        exim_user = ${cfg.user}
        exim_group = ${cfg.group}
        exim_path = /run/wrappers/bin/exim
        spool_directory = ${cfg.spoolDir}
        ${cfg.config}
      '';
      systemPackages = [ cfg.package ];
    };

    users.users.${cfg.user} = {
      description = "Exim mail transfer agent user";
      uid = config.ids.uids.exim;
      group = cfg.group;
    };

    users.groups.${cfg.group} = {
      gid = config.ids.gids.exim;
    };

    security.wrappers.exim =
      { setuid = true;
        owner = "root";
        group = "root";
        source = "${cfg.package}/bin/exim";
      };

    systemd.services.exim = {
      description = "Exim Mail Daemon";
      wantedBy = [ "multi-user.target" ];
      restartTriggers = [ config.environment.etc."exim.conf".source ];
      serviceConfig = {
        ExecStart   = "${cfg.package}/bin/exim -bdf -q${cfg.queueRunnerInterval}";
        ExecReload  = "${coreutils}/bin/kill -HUP $MAINPID";
      };
      preStart = ''
        if ! test -d ${cfg.spoolDir}; then
          ${coreutils}/bin/mkdir -p ${cfg.spoolDir}
          ${coreutils}/bin/chown ${cfg.user}:${cfg.group} ${cfg.spoolDir}
        fi
      '';
    };

  };

}