2018-08-06 12:04:56 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.programs.offlineimap;
|
|
|
|
|
|
|
|
accounts = filter (a: a.offlineimap.enable)
|
|
|
|
(attrValues config.accounts.email.accounts);
|
|
|
|
|
|
|
|
toIni = generators.toINI {
|
|
|
|
mkKeyValue = key: value:
|
|
|
|
let
|
2022-04-08 06:36:13 +02:00
|
|
|
value' =
|
|
|
|
(if isBool value then lib.hm.booleans.yesNo else toString) value;
|
2020-02-02 00:39:17 +01:00
|
|
|
in "${key} = ${value'}";
|
2018-08-06 12:04:56 +02:00
|
|
|
};
|
|
|
|
|
2020-02-02 00:39:17 +01:00
|
|
|
accountStr = account:
|
|
|
|
with account;
|
2018-08-06 12:04:56 +02:00
|
|
|
let
|
|
|
|
postSyncHook = optionalAttrs (offlineimap.postSyncHookCommand != "") {
|
2020-02-02 00:39:17 +01:00
|
|
|
postsynchook = pkgs.writeShellScriptBin "postsynchook"
|
|
|
|
offlineimap.postSyncHookCommand + "/bin/postsynchook";
|
2018-08-06 12:04:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
localType =
|
2020-02-02 00:39:17 +01:00
|
|
|
if account.flavor == "gmail.com" then "GmailMaildir" else "Maildir";
|
2018-08-06 12:04:56 +02:00
|
|
|
|
2020-02-02 00:39:17 +01:00
|
|
|
remoteType = if account.flavor == "gmail.com" then "Gmail" else "IMAP";
|
2018-08-06 12:04:56 +02:00
|
|
|
|
2020-02-02 00:39:17 +01:00
|
|
|
remoteHost =
|
|
|
|
optionalAttrs (imap.host != null) { remotehost = imap.host; };
|
2018-08-06 12:04:56 +02:00
|
|
|
|
2020-02-02 00:39:17 +01:00
|
|
|
remotePort =
|
|
|
|
optionalAttrs ((imap.port or null) != null) { remoteport = imap.port; };
|
2018-08-06 12:04:56 +02:00
|
|
|
|
2020-02-02 00:39:17 +01:00
|
|
|
ssl = if imap.tls.enable then {
|
|
|
|
ssl = true;
|
2021-08-02 20:23:50 +02:00
|
|
|
sslcacertfile = toString imap.tls.certificatesFile;
|
2020-02-02 00:39:17 +01:00
|
|
|
starttls = imap.tls.useStartTls;
|
|
|
|
} else {
|
|
|
|
ssl = false;
|
2024-03-13 13:47:31 +01:00
|
|
|
starttls = false;
|
2020-02-02 00:39:17 +01:00
|
|
|
};
|
2018-08-06 12:04:56 +02:00
|
|
|
|
|
|
|
remotePassEval =
|
2020-02-02 00:39:17 +01:00
|
|
|
let arglist = concatMapStringsSep "," (x: "'${x}'") passwordCommand;
|
|
|
|
in optionalAttrs (passwordCommand != null) {
|
2021-11-16 18:31:01 +01:00
|
|
|
remotepasseval = ''get_pass("${name}", [${arglist}]).strip(b"\n")'';
|
2020-02-02 00:39:17 +01:00
|
|
|
};
|
|
|
|
in toIni {
|
|
|
|
"Account ${name}" = {
|
|
|
|
localrepository = "${name}-local";
|
|
|
|
remoterepository = "${name}-remote";
|
|
|
|
} // postSyncHook // offlineimap.extraConfig.account;
|
|
|
|
|
|
|
|
"Repository ${name}-local" = {
|
|
|
|
type = localType;
|
|
|
|
localfolders = maildir.absPath;
|
|
|
|
} // offlineimap.extraConfig.local;
|
|
|
|
|
|
|
|
"Repository ${name}-remote" = {
|
|
|
|
type = remoteType;
|
|
|
|
remoteuser = userName;
|
|
|
|
} // remoteHost // remotePort // remotePassEval // ssl
|
2018-08-06 12:04:56 +02:00
|
|
|
// offlineimap.extraConfig.remote;
|
2020-02-02 00:39:17 +01:00
|
|
|
};
|
2018-08-06 12:04:56 +02:00
|
|
|
|
|
|
|
extraConfigType = with types; attrsOf (either (either str int) bool);
|
|
|
|
|
2020-02-02 00:39:17 +01:00
|
|
|
in {
|
2018-08-06 12:04:56 +02:00
|
|
|
options = {
|
|
|
|
programs.offlineimap = {
|
2023-07-02 01:45:18 +02:00
|
|
|
enable = mkEnableOption "OfflineIMAP";
|
2018-08-06 12:04:56 +02:00
|
|
|
|
2023-07-02 01:45:18 +02:00
|
|
|
package = mkPackageOption pkgs "offlineimap" {
|
2023-06-13 10:41:25 +02:00
|
|
|
example = ''
|
|
|
|
pkgs.offlineimap.overridePythonAttrs ( old: {
|
|
|
|
propagatedBuildInputs = old.propagatedBuildInputs
|
|
|
|
++ (with pkgs.python3Packages; [
|
|
|
|
requests_oauthlib xdg gpgme]);
|
|
|
|
})'';
|
|
|
|
extraDescription = "Can be used to specify extensions.";
|
|
|
|
};
|
|
|
|
|
2018-08-06 12:04:56 +02:00
|
|
|
pythonFile = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = ''
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
def get_pass(service, cmd):
|
|
|
|
return subprocess.check_output(cmd, )
|
|
|
|
'';
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2018-08-06 12:04:56 +02:00
|
|
|
Python code that can then be used in other parts of the
|
|
|
|
configuration.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
extraConfig.general = mkOption {
|
|
|
|
type = extraConfigType;
|
2020-02-02 00:39:17 +01:00
|
|
|
default = { };
|
2018-08-06 12:04:56 +02:00
|
|
|
example = {
|
|
|
|
maxage = 30;
|
|
|
|
ui = "blinkenlights";
|
|
|
|
};
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2018-08-06 12:04:56 +02:00
|
|
|
Extra configuration options added to the
|
2023-07-01 01:30:13 +02:00
|
|
|
{option}`general` section.
|
2018-08-06 12:04:56 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
extraConfig.default = mkOption {
|
|
|
|
type = extraConfigType;
|
2020-02-02 00:39:17 +01:00
|
|
|
default = { };
|
|
|
|
example = { gmailtrashfolder = "[Gmail]/Papierkorb"; };
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2018-08-06 12:04:56 +02:00
|
|
|
Extra configuration options added to the
|
2023-07-01 01:30:13 +02:00
|
|
|
{option}`DEFAULT` section.
|
2018-08-06 12:04:56 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
extraConfig.mbnames = mkOption {
|
|
|
|
type = extraConfigType;
|
2020-02-02 00:39:17 +01:00
|
|
|
default = { };
|
2021-10-09 11:14:08 +02:00
|
|
|
example = literalExpression ''
|
2018-08-06 12:04:56 +02:00
|
|
|
{
|
|
|
|
filename = "~/.config/mutt/mailboxes";
|
|
|
|
header = "'mailboxes '";
|
|
|
|
peritem = "'+%(accountname)s/%(foldername)s'";
|
|
|
|
sep = "' '";
|
|
|
|
footer = "'\\n'";
|
|
|
|
}
|
|
|
|
'';
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2018-08-06 12:04:56 +02:00
|
|
|
Extra configuration options added to the
|
2023-07-01 01:30:13 +02:00
|
|
|
`mbnames` section.
|
2018-08-06 12:04:56 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2020-06-16 00:45:20 +02:00
|
|
|
|
|
|
|
accounts.email.accounts = mkOption {
|
|
|
|
type = with types;
|
|
|
|
attrsOf (submodule (import ./offlineimap-accounts.nix));
|
|
|
|
};
|
2018-08-06 12:04:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2023-06-13 10:41:25 +02:00
|
|
|
home.packages = [ cfg.package ];
|
2018-08-06 12:04:56 +02:00
|
|
|
|
|
|
|
xdg.configFile."offlineimap/get_settings.py".text = cfg.pythonFile;
|
2021-02-28 12:02:53 +01:00
|
|
|
xdg.configFile."offlineimap/get_settings.pyc".source = "${
|
|
|
|
pkgs.runCommandLocal "get_settings-compile" {
|
2023-06-13 10:41:25 +02:00
|
|
|
nativeBuildInputs = [ cfg.package ];
|
2021-02-28 12:02:53 +01:00
|
|
|
pythonFile = cfg.pythonFile;
|
|
|
|
passAsFile = [ "pythonFile" ];
|
|
|
|
} ''
|
|
|
|
mkdir -p $out/bin
|
|
|
|
cp $pythonFilePath $out/bin/get_settings.py
|
2022-05-10 09:30:40 +02:00
|
|
|
python -m py_compile $out/bin/get_settings.py
|
2021-02-28 12:02:53 +01:00
|
|
|
''
|
|
|
|
}/bin/get_settings.pyc";
|
2018-08-06 12:04:56 +02:00
|
|
|
|
2020-02-02 00:39:17 +01:00
|
|
|
xdg.configFile."offlineimap/config".text = ''
|
|
|
|
# Generated by Home Manager.
|
|
|
|
# See https://github.com/OfflineIMAP/offlineimap/blob/master/offlineimap.conf
|
|
|
|
# for an exhaustive list of options.
|
|
|
|
'' + toIni ({
|
|
|
|
general = {
|
|
|
|
accounts = concatMapStringsSep "," (a: a.name) accounts;
|
|
|
|
pythonfile = "${config.xdg.configHome}/offlineimap/get_settings.py";
|
|
|
|
metadata = "${config.xdg.dataHome}/offlineimap";
|
|
|
|
} // cfg.extraConfig.general;
|
|
|
|
} // optionalAttrs (cfg.extraConfig.mbnames != { }) {
|
|
|
|
mbnames = { enabled = true; } // cfg.extraConfig.mbnames;
|
|
|
|
} // optionalAttrs (cfg.extraConfig.default != { }) {
|
|
|
|
DEFAULT = cfg.extraConfig.default;
|
|
|
|
}) + "\n" + concatStringsSep "\n" (map accountStr accounts);
|
2018-08-06 12:04:56 +02:00
|
|
|
};
|
|
|
|
}
|