1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.offlineimap;
7in {
8
9 options.services.offlineimap = {
10 enable = mkEnableOption "Offlineimap, a software to dispose your mailbox(es) as a local Maildir(s).";
11
12 install = mkOption {
13 type = types.bool;
14 default = false;
15 description = ''
16 Whether to install a user service for Offlineimap. Once
17 the service is started, emails will be fetched automatically.
18
19 The service must be manually started for each user with
20 "systemctl --user start offlineimap" or globally through
21 <varname>services.offlineimap.enable</varname>.
22 '';
23 };
24
25 package = mkOption {
26 type = types.package;
27 default = pkgs.offlineimap;
28 defaultText = "pkgs.offlineimap";
29 description = "Offlineimap derivation to use.";
30 };
31
32 path = mkOption {
33 type = types.listOf types.path;
34 default = [];
35 example = literalExample "[ pkgs.pass pkgs.bash pkgs.notmuch ]";
36 description = "List of derivations to put in Offlineimap's path.";
37 };
38
39 onCalendar = mkOption {
40 type = types.str;
41 default = "*:0/3"; # every 3 minutes
42 description = "How often is offlineimap started. Default is '*:0/3' meaning every 3 minutes. See systemd.time(7) for more information about the format.";
43 };
44
45 timeoutStartSec = mkOption {
46 type = types.str;
47 default = "120sec"; # Kill if still alive after 2 minutes
48 description = "How long waiting for offlineimap before killing it. Default is '120sec' meaning every 2 minutes. See systemd.time(7) for more information about the format.";
49 };
50 };
51 config = mkIf (cfg.enable || cfg.install) {
52 systemd.user.services.offlineimap = {
53 description = "Offlineimap: a software to dispose your mailbox(es) as a local Maildir(s)";
54 serviceConfig = {
55 Type = "oneshot";
56 ExecStart = "${cfg.package}/bin/offlineimap -u syslog -o -1";
57 TimeoutStartSec = cfg.timeoutStartSec;
58 };
59 path = cfg.path;
60 };
61 environment.systemPackages = [ cfg.package ];
62 systemd.user.timers.offlineimap = {
63 description = "offlineimap timer";
64 timerConfig = {
65 Unit = "offlineimap.service";
66 OnCalendar = cfg.onCalendar;
67 # start immediately after computer is started:
68 Persistent = "true";
69 };
70 } // optionalAttrs cfg.enable { wantedBy = [ "default.target" ]; };
71 };
72}