summary refs log tree commit diff
path: root/modules/services/mail/dovecot.nix
blob: a41bd9eaad7f4a6401bad4efeb405d22a99d8cfe (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, pkgs, ... }:

with pkgs.lib;

let

  startingDependency = if config.services.gw6c.enable then "gw6c" else "network-interfaces";

  cfg = config.services.dovecot;

  dovecotConf = 
    ''
      base_dir = /var/run/dovecot/ 

      protocols = imap imaps pop3 pop3s
    ''
    + (if cfg.sslServerCert!="" then
    ''
      ssl_cert_file = ${cfg.sslServerCert}
      ssl_key_file = ${cfg.sslServerKey}
      ssl_ca_file = ${cfg.sslCACert}
    '' else ''
      ssl_disable = yes
      disable_plaintext_auth = no
    '')

    + ''
      login_user = ${cfg.user}
      login_chroot = no

      mail_location = maildir:/var/spool/mail/%u

      maildir_copy_with_hardlinks = yes

      auth default {
        mechanisms = plain login 
        userdb passwd {
        }
        passdb pam {
        }
        user = root 
      }
      auth_debug = yes
      auth_verbose = yes

      pop3_uidl_format = %08Xv%08Xu

      log_path = /var/log/dovecot.log
    '';
  
  confFile = pkgs.writeText "dovecot.conf" dovecotConf;

in

{

  ###### interface

  options = {
  
    services.dovecot = {
    
      enable = mkOption {
        default = false;
        description = "Whether to enable the Dovecot POP3/IMAP server.";
      };

      user = mkOption {
        default = "dovecot";
        description = "Dovecot user name.";
      };
      
      group = mkOption {
        default = "dovecot";
        description = "Dovecot group name.";
      };

      sslServerCert = mkOption {
        default = "";
        description = "Server certificate";
      };
      
      sslCACert = mkOption {
        default = "";
        description = "CA certificate used by the server certificate.";
      };
      
      sslServerKey = mkOption {
        default = "";
        description = "Server key.";
      };

    };
    
  };

  
  ###### implementation

  config = mkIf config.services.dovecot.enable {

    security.pam.services = [ { name = "dovecot"; } ];

    users.extraUsers = singleton
      { name = cfg.user;
        uid = config.ids.uids.dovecot;
        description = "Dovecot user";
        group = cfg.group;
      };

    users.extraGroups = singleton
      { name = cfg.group;
        gid = config.ids.gids.dovecot;
      };

    jobs.dovecot =
      { description = "Dovecot IMAP/POP3 server";

        startOn = "started ${startingDependency}";

        preStart =
          ''
            ${pkgs.coreutils}/bin/mkdir -p /var/run/dovecot /var/run/dovecot/login 
            ${pkgs.coreutils}/bin/chown -R ${cfg.user}.${cfg.group} /var/run/dovecot
          '';

        exec = "${pkgs.dovecot}/sbin/dovecot -F -c ${confFile}";
      };
      
  };
  
}