fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Damien Cassouofflineimap: cleanup unused bindings455cc8cf

master
5.4 KiB181 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.programs.offlineimap;
8
9  accounts = filter (a: a.offlineimap.enable)
10    (attrValues config.accounts.email.accounts);
11
12  toIni = generators.toINI {
13    mkKeyValue = key: value:
14      let
15        value' =
16          (if isBool value then lib.hm.booleans.yesNo else toString) value;
17      in "${key} = ${value'}";
18  };
19
20  accountStr = account:
21    with account;
22    let
23      postSyncHook = optionalAttrs (offlineimap.postSyncHookCommand != "") {
24        postsynchook = pkgs.writeShellScriptBin "postsynchook"
25          offlineimap.postSyncHookCommand + "/bin/postsynchook";
26      };
27
28      localType =
29        if account.flavor == "gmail.com" then "GmailMaildir" else "Maildir";
30
31      remoteType = if account.flavor == "gmail.com" then "Gmail" else "IMAP";
32
33      remoteHost =
34        optionalAttrs (imap.host != null) { remotehost = imap.host; };
35
36      remotePort =
37        optionalAttrs ((imap.port or null) != null) { remoteport = imap.port; };
38
39      ssl = if imap.tls.enable then {
40        ssl = true;
41        sslcacertfile = toString imap.tls.certificatesFile;
42        starttls = imap.tls.useStartTls;
43      } else {
44        ssl = false;
45      };
46
47      remotePassEval =
48        let arglist = concatMapStringsSep "," (x: "'${x}'") passwordCommand;
49        in optionalAttrs (passwordCommand != null) {
50          remotepasseval = ''get_pass("${name}", [${arglist}]).strip(b"\n")'';
51        };
52    in toIni {
53      "Account ${name}" = {
54        localrepository = "${name}-local";
55        remoterepository = "${name}-remote";
56      } // postSyncHook // offlineimap.extraConfig.account;
57
58      "Repository ${name}-local" = {
59        type = localType;
60        localfolders = maildir.absPath;
61      } // offlineimap.extraConfig.local;
62
63      "Repository ${name}-remote" = {
64        type = remoteType;
65        remoteuser = userName;
66      } // remoteHost // remotePort // remotePassEval // ssl
67        // offlineimap.extraConfig.remote;
68    };
69
70  extraConfigType = with types; attrsOf (either (either str int) bool);
71
72in {
73  options = {
74    programs.offlineimap = {
75      enable = mkEnableOption "OfflineIMAP";
76
77      package = mkPackageOption pkgs "offlineimap" {
78        example = ''
79          pkgs.offlineimap.overridePythonAttrs ( old: {
80            propagatedBuildInputs = old.propagatedBuildInputs
81              ++ (with pkgs.python3Packages; [
82                requests_oauthlib xdg gpgme]);
83          })'';
84        extraDescription = "Can be used to specify extensions.";
85      };
86
87      pythonFile = mkOption {
88        type = types.lines;
89        default = ''
90          import subprocess
91
92          def get_pass(service, cmd):
93              return subprocess.check_output(cmd, )
94        '';
95        description = ''
96          Python code that can then be used in other parts of the
97          configuration.
98        '';
99      };
100
101      extraConfig.general = mkOption {
102        type = extraConfigType;
103        default = { };
104        example = {
105          maxage = 30;
106          ui = "blinkenlights";
107        };
108        description = ''
109          Extra configuration options added to the
110          {option}`general` section.
111        '';
112      };
113
114      extraConfig.default = mkOption {
115        type = extraConfigType;
116        default = { };
117        example = { gmailtrashfolder = "[Gmail]/Papierkorb"; };
118        description = ''
119          Extra configuration options added to the
120          {option}`DEFAULT` section.
121        '';
122      };
123
124      extraConfig.mbnames = mkOption {
125        type = extraConfigType;
126        default = { };
127        example = literalExpression ''
128          {
129            filename = "~/.config/mutt/mailboxes";
130            header = "'mailboxes '";
131            peritem = "'+%(accountname)s/%(foldername)s'";
132            sep = "' '";
133            footer = "'\\n'";
134          }
135        '';
136        description = ''
137          Extra configuration options added to the
138          `mbnames` section.
139        '';
140      };
141    };
142
143    accounts.email.accounts = mkOption {
144      type = with types;
145        attrsOf (submodule (import ./offlineimap-accounts.nix));
146    };
147  };
148
149  config = mkIf cfg.enable {
150    home.packages = [ cfg.package ];
151
152    xdg.configFile."offlineimap/get_settings.py".text = cfg.pythonFile;
153    xdg.configFile."offlineimap/get_settings.pyc".source = "${
154        pkgs.runCommandLocal "get_settings-compile" {
155          nativeBuildInputs = [ cfg.package ];
156          pythonFile = cfg.pythonFile;
157          passAsFile = [ "pythonFile" ];
158        } ''
159          mkdir -p $out/bin
160          cp $pythonFilePath $out/bin/get_settings.py
161          python -m py_compile $out/bin/get_settings.py
162        ''
163      }/bin/get_settings.pyc";
164
165    xdg.configFile."offlineimap/config".text = ''
166      # Generated by Home Manager.
167      # See https://github.com/OfflineIMAP/offlineimap/blob/master/offlineimap.conf
168      # for an exhaustive list of options.
169    '' + toIni ({
170      general = {
171        accounts = concatMapStringsSep "," (a: a.name) accounts;
172        pythonfile = "${config.xdg.configHome}/offlineimap/get_settings.py";
173        metadata = "${config.xdg.dataHome}/offlineimap";
174      } // cfg.extraConfig.general;
175    } // optionalAttrs (cfg.extraConfig.mbnames != { }) {
176      mbnames = { enabled = true; } // cfg.extraConfig.mbnames;
177    } // optionalAttrs (cfg.extraConfig.default != { }) {
178      DEFAULT = cfg.extraConfig.default;
179    }) + "\n" + concatStringsSep "\n" (map accountStr accounts);
180  };
181}