aboutsummaryrefslogtreecommitdiff
path: root/home-manager/modules/services/lieer.nix
blob: 571e2af75c84644437190acdbfd62d0d4dc0dcd6 (plain)
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
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.lieer;

  syncAccounts = filter (a: a.lieer.enable && a.lieer.sync.enable)
    (attrValues config.accounts.email.accounts);

  escapeUnitName = name:
    let
      good = upperChars ++ lowerChars ++ stringToCharacters "0123456789-_";
      subst = c: if any (x: x == c) good then c else "-";
    in stringAsChars subst name;

  serviceUnit = account: {
    name = escapeUnitName "lieer-${account.name}";
    value = {
      Unit = {
        Description = "lieer Gmail synchronization for ${account.name}";
        ConditionPathExists = "${account.maildir.absPath}/.gmailieer.json";
      };

      Service = {
        Type = "oneshot";
        ExecStart = "${pkgs.gmailieer}/bin/gmi sync";
        WorkingDirectory = account.maildir.absPath;
      };
    };
  };

  timerUnit = account: {
    name = escapeUnitName "lieer-${account.name}";
    value = {
      Unit = {
        Description = "lieer Gmail synchronization for ${account.name}";
      };

      Timer = {
        OnCalendar = account.lieer.sync.frequency;
        RandomizedDelaySec = 30;
      };

      Install = { WantedBy = [ "timers.target" ]; };
    };
  };

in {
  meta.maintainers = [ maintainers.tadfisher ];

  options = {
    services.lieer.enable =
      mkEnableOption "lieer Gmail synchronization service";

    accounts.email.accounts = mkOption {
      type = with types; attrsOf (submodule (import ./lieer-accounts.nix));
    };
  };

  config = mkIf cfg.enable {
    programs.lieer.enable = true;
    systemd.user.services = listToAttrs (map serviceUnit syncAccounts);
    systemd.user.timers = listToAttrs (map timerUnit syncAccounts);
  };
}