2021-06-27 00:29:42 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
2023-05-04 12:28:08 +02:00
|
|
|
let
|
|
|
|
# aliases
|
|
|
|
inherit (config.programs) himalaya;
|
2021-06-27 00:29:42 +02:00
|
|
|
tomlFormat = pkgs.formats.toml { };
|
|
|
|
|
2023-05-04 12:28:08 +02:00
|
|
|
# attrs util that removes entries containing a null value
|
|
|
|
compactAttrs = lib.filterAttrs (_: val: !isNull val);
|
|
|
|
|
2024-02-05 23:03:40 +01:00
|
|
|
# needed for notmuch config, because the DB is here, and not in each
|
|
|
|
# account's dir
|
2023-07-03 20:30:11 +02:00
|
|
|
maildirBasePath = config.accounts.email.maildirBasePath;
|
|
|
|
|
2024-02-05 23:03:40 +01:00
|
|
|
# make encryption config based on the given home-manager email
|
|
|
|
# account TLS config
|
|
|
|
mkEncryptionConfig = tls:
|
|
|
|
if tls.useStartTls then
|
|
|
|
"start-tls"
|
|
|
|
else if tls.enable then
|
|
|
|
"tls"
|
|
|
|
else
|
|
|
|
"none";
|
|
|
|
|
|
|
|
# make a himalaya account config based on the given home-manager
|
|
|
|
# email account config
|
2023-05-04 12:28:08 +02:00
|
|
|
mkAccountConfig = _: account:
|
|
|
|
let
|
2023-07-03 20:30:11 +02:00
|
|
|
notmuchEnabled = account.notmuch.enable;
|
|
|
|
imapEnabled = !isNull account.imap && !notmuchEnabled;
|
|
|
|
maildirEnabled = !isNull account.maildir && !imapEnabled
|
|
|
|
&& !notmuchEnabled;
|
2023-06-21 10:59:29 +02:00
|
|
|
|
2023-05-04 12:28:08 +02:00
|
|
|
globalConfig = {
|
2021-06-27 00:29:42 +02:00
|
|
|
email = account.address;
|
2022-10-16 10:25:31 +02:00
|
|
|
display-name = account.realName;
|
2021-06-27 00:29:42 +02:00
|
|
|
default = account.primary;
|
2024-02-05 23:03:40 +01:00
|
|
|
folder.alias = {
|
2022-05-30 21:04:13 +02:00
|
|
|
inbox = account.folders.inbox;
|
|
|
|
sent = account.folders.sent;
|
2023-05-04 12:28:08 +02:00
|
|
|
drafts = account.folders.drafts;
|
|
|
|
trash = account.folders.trash;
|
2022-05-30 21:04:13 +02:00
|
|
|
};
|
2023-05-04 12:28:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
signatureConfig =
|
|
|
|
lib.optionalAttrs (account.signature.showSignature == "append") {
|
|
|
|
# TODO: signature cannot be attached yet
|
2023-08-02 04:11:08 +02:00
|
|
|
# https://todo.sr.ht/~soywod/pimalaya/27
|
2023-05-04 12:28:08 +02:00
|
|
|
signature = account.signature.text;
|
|
|
|
signature-delim = account.signature.delimiter;
|
|
|
|
};
|
|
|
|
|
2023-06-21 10:59:29 +02:00
|
|
|
imapConfig = lib.optionalAttrs imapEnabled (compactAttrs {
|
2023-05-04 12:28:08 +02:00
|
|
|
backend = "imap";
|
2024-02-05 23:03:40 +01:00
|
|
|
imap.host = account.imap.host;
|
|
|
|
imap.port = account.imap.port;
|
|
|
|
imap.encryption = mkEncryptionConfig account.imap.tls;
|
|
|
|
imap.login = account.userName;
|
|
|
|
imap.passwd.cmd = builtins.concatStringsSep " " account.passwordCommand;
|
2023-05-04 12:28:08 +02:00
|
|
|
});
|
|
|
|
|
2023-06-21 10:59:29 +02:00
|
|
|
maildirConfig = lib.optionalAttrs maildirEnabled (compactAttrs {
|
|
|
|
backend = "maildir";
|
2024-02-05 23:03:40 +01:00
|
|
|
maildir.root-dir = account.maildir.absPath;
|
2023-06-21 10:59:29 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
notmuchConfig = lib.optionalAttrs notmuchEnabled (compactAttrs {
|
|
|
|
backend = "notmuch";
|
2024-02-05 23:03:40 +01:00
|
|
|
notmuch.database-path = maildirBasePath;
|
2023-06-21 10:59:29 +02:00
|
|
|
});
|
2023-05-04 12:28:08 +02:00
|
|
|
|
|
|
|
smtpConfig = lib.optionalAttrs (!isNull account.smtp) (compactAttrs {
|
2024-02-05 23:03:40 +01:00
|
|
|
message.send.backend = "smtp";
|
|
|
|
smtp.host = account.smtp.host;
|
|
|
|
smtp.port = account.smtp.port;
|
|
|
|
smtp.encryption = mkEncryptionConfig account.smtp.tls;
|
|
|
|
smtp.login = account.userName;
|
|
|
|
smtp.passwd.cmd = builtins.concatStringsSep " " account.passwordCommand;
|
2023-05-04 12:28:08 +02:00
|
|
|
});
|
2021-06-27 00:29:42 +02:00
|
|
|
|
2023-05-04 12:28:08 +02:00
|
|
|
sendmailConfig =
|
2023-06-21 10:59:29 +02:00
|
|
|
lib.optionalAttrs (isNull account.smtp && !isNull account.msmtp) {
|
|
|
|
sender = "sendmail";
|
2024-02-05 23:03:40 +01:00
|
|
|
sendmail.cmd = "${pkgs.msmtp}/bin/msmtp";
|
2023-06-21 10:59:29 +02:00
|
|
|
};
|
2021-06-27 00:29:42 +02:00
|
|
|
|
2024-02-05 23:03:40 +01:00
|
|
|
config = lib.attrsets.mergeAttrsList [
|
|
|
|
globalConfig
|
|
|
|
signatureConfig
|
|
|
|
imapConfig
|
|
|
|
maildirConfig
|
|
|
|
notmuchConfig
|
|
|
|
smtpConfig
|
|
|
|
sendmailConfig
|
|
|
|
];
|
2023-05-04 12:28:08 +02:00
|
|
|
|
|
|
|
in lib.recursiveUpdate config account.himalaya.settings;
|
|
|
|
|
|
|
|
in {
|
|
|
|
meta.maintainers = with lib.hm.maintainers; [ soywod toastal ];
|
2021-06-27 00:29:42 +02:00
|
|
|
|
2023-05-04 12:28:08 +02:00
|
|
|
options = {
|
|
|
|
programs.himalaya = {
|
2024-02-05 23:03:40 +01:00
|
|
|
enable = lib.mkEnableOption "the email client Himalaya CLI";
|
2023-07-02 01:45:18 +02:00
|
|
|
package = lib.mkPackageOption pkgs "himalaya" { };
|
2023-05-04 12:28:08 +02:00
|
|
|
settings = lib.mkOption {
|
|
|
|
type = lib.types.submodule { freeformType = tomlFormat.type; };
|
2021-06-27 00:29:42 +02:00
|
|
|
default = { };
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2024-02-05 23:03:40 +01:00
|
|
|
Himalaya CLI global configuration.
|
|
|
|
See <https://pimalaya.org/himalaya/cli/latest/configuration/index.html#global-configuration> for supported values.
|
2021-06-27 00:29:42 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-02-05 23:03:40 +01:00
|
|
|
services.himalaya-watch = {
|
|
|
|
enable = lib.mkEnableOption
|
|
|
|
"the email client Himalaya CLI envelopes watcher service";
|
2023-05-04 12:28:08 +02:00
|
|
|
|
2024-02-05 23:03:40 +01:00
|
|
|
environment = lib.mkOption {
|
|
|
|
type = with lib.types; attrsOf str;
|
|
|
|
default = { };
|
|
|
|
example = lib.literalExpression ''
|
|
|
|
{
|
|
|
|
"PASSWORD_STORE_DIR" = "~/.password-store";
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
description = ''
|
|
|
|
Extra environment variables to be exported in the service.
|
|
|
|
'';
|
2023-05-04 12:28:08 +02:00
|
|
|
};
|
|
|
|
|
2024-02-05 23:03:40 +01:00
|
|
|
settings.account = lib.mkOption {
|
|
|
|
type = with lib.types; nullOr str;
|
|
|
|
default = null;
|
|
|
|
example = "personal";
|
|
|
|
description = ''
|
|
|
|
Name of the account the watcher should be started for.
|
|
|
|
If no account is given, the default one is used.
|
|
|
|
'';
|
2023-05-04 12:28:08 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
accounts.email.accounts = lib.mkOption {
|
|
|
|
type = lib.types.attrsOf (lib.types.submodule {
|
|
|
|
options.himalaya = {
|
2024-02-05 23:03:40 +01:00
|
|
|
enable = lib.mkEnableOption
|
|
|
|
"the email client Himalaya CLI for this email account";
|
2023-05-04 12:28:08 +02:00
|
|
|
|
|
|
|
settings = lib.mkOption {
|
|
|
|
type = lib.types.submodule { freeformType = tomlFormat.type; };
|
|
|
|
default = { };
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2024-02-05 23:03:40 +01:00
|
|
|
Himalaya CLI configuration for this email account.
|
|
|
|
See <https://pimalaya.org/himalaya/cli/latest/configuration/index.html#account-configuration> for supported values.
|
2023-05-04 12:28:08 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
2021-06-27 00:29:42 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-05-04 12:28:08 +02:00
|
|
|
config = lib.mkIf himalaya.enable {
|
|
|
|
home.packages = [ himalaya.package ];
|
|
|
|
|
|
|
|
xdg.configFile."himalaya/config.toml".source = let
|
|
|
|
enabledAccounts = lib.filterAttrs (_: account: account.himalaya.enable)
|
|
|
|
config.accounts.email.accounts;
|
|
|
|
accountsConfig = lib.mapAttrs mkAccountConfig enabledAccounts;
|
|
|
|
globalConfig = compactAttrs himalaya.settings;
|
2024-03-08 13:58:55 +01:00
|
|
|
allConfig = globalConfig // { accounts = accountsConfig; };
|
2023-05-04 12:28:08 +02:00
|
|
|
in tomlFormat.generate "himalaya-config.toml" allConfig;
|
2024-02-05 23:03:40 +01:00
|
|
|
systemd.user.services = let
|
|
|
|
inherit (config.services.himalaya-watch) enable environment settings;
|
|
|
|
optionalArg = key:
|
|
|
|
if (key ? settings && !isNull settings."${key}") then
|
|
|
|
[ "--${key} ${settings."${key}"}" ]
|
|
|
|
else
|
|
|
|
[ ];
|
|
|
|
in {
|
|
|
|
himalaya-watch = lib.mkIf enable {
|
|
|
|
Unit = {
|
|
|
|
Description = "Email client Himalaya CLI envelopes watcher service";
|
|
|
|
After = [ "network.target" ];
|
|
|
|
};
|
|
|
|
Install = { WantedBy = [ "default.target" ]; };
|
|
|
|
Service = {
|
|
|
|
ExecStart = lib.concatStringsSep " "
|
|
|
|
([ "${himalaya.package}/bin/himalaya" "envelopes" "watch" ]
|
|
|
|
++ optionalArg "account");
|
|
|
|
ExecSearchPath = "/bin";
|
|
|
|
Environment =
|
|
|
|
lib.mapAttrsToList (key: val: "${key}=${val}") environment;
|
|
|
|
Restart = "always";
|
|
|
|
RestartSec = 10;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2021-06-27 00:29:42 +02:00
|
|
|
};
|
|
|
|
}
|