mirror of
https://github.com/nix-community/home-manager.git
synced 2025-11-08 11:36:05 +01:00
Allow a user to disable an email account by setting `accounts.email.accounts.<name>.enable = false`. This is useful if someone wants to configure email accounts globally but only use them in certain circumstances. Everywhere email account configuration is used, check if the account is enabled before checking any attributes of the account.
71 lines
2.1 KiB
Nix
71 lines
2.1 KiB
Nix
{
|
|
config,
|
|
lib,
|
|
pkgs,
|
|
...
|
|
}:
|
|
let
|
|
accounts = lib.filter (a: a.enable && a.getmail.enable) (
|
|
lib.attrValues config.accounts.email.accounts
|
|
);
|
|
|
|
renderAccountConfig =
|
|
account:
|
|
let
|
|
inherit (account) getmail imap passwordCommand;
|
|
passCmd = lib.concatMapStringsSep ", " (x: "'${x}'") passwordCommand;
|
|
renderedMailboxes = lib.concatMapStrings (x: "'${x}', ") getmail.mailboxes;
|
|
retrieverType = if imap.tls.enable then "SimpleIMAPSSLRetriever" else "SimpleIMAPRetriever";
|
|
destination =
|
|
if getmail.destinationCommand != null then
|
|
{
|
|
destinationType = "MDA_external";
|
|
destinationPath = getmail.destinationCommand;
|
|
}
|
|
else
|
|
{
|
|
destinationType = "Maildir";
|
|
destinationPath = "${account.maildir.absPath}/";
|
|
};
|
|
renderGetmailBoolean = v: if v then "true" else "false";
|
|
in
|
|
''
|
|
# Generated by Home-Manager.
|
|
[retriever]
|
|
type = ${retrieverType}
|
|
server = ${imap.host}
|
|
${lib.optionalString (imap.port != null) "port = ${toString imap.port}"}
|
|
username = ${account.userName}
|
|
password_command = (${passCmd})
|
|
mailboxes = ( ${renderedMailboxes} )
|
|
|
|
[destination]
|
|
type = ${destination.destinationType}
|
|
path = ${destination.destinationPath}
|
|
|
|
[options]
|
|
delete = ${renderGetmailBoolean getmail.delete}
|
|
read_all = ${renderGetmailBoolean getmail.readAll}
|
|
'';
|
|
getmailEnabled = lib.length (lib.filter (a: a.getmail.enable) accounts) > 0;
|
|
# Watch out! This is used by the getmail.service too!
|
|
renderConfigFilepath = a: ".getmail/getmail${if a.primary then "rc" else a.name}";
|
|
|
|
in
|
|
{
|
|
options = {
|
|
accounts.email.accounts = lib.mkOption {
|
|
type = with lib.types; attrsOf (submodule (import ./accounts.nix));
|
|
};
|
|
};
|
|
|
|
config = lib.mkIf getmailEnabled {
|
|
assertions = [
|
|
(lib.hm.assertions.assertPlatform "programs.getmail" pkgs lib.platforms.linux)
|
|
];
|
|
|
|
home.file = lib.foldl' (a: b: a // b) { } (
|
|
map (a: { "${renderConfigFilepath a}".text = renderAccountConfig a; }) accounts
|
|
);
|
|
};
|
|
}
|