2019-04-09 19:34:44 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.services.imapnotify;
|
|
|
|
|
2022-12-16 05:45:46 +01:00
|
|
|
safeName = lib.replaceStrings [ "@" ":" "\\" "[" "]" ] [ "-" "-" "-" "" "" ];
|
2019-04-09 19:34:44 +02:00
|
|
|
|
2023-07-07 11:39:12 +02:00
|
|
|
configName = account: "imapnotify-${safeName account.name}-config.json";
|
|
|
|
|
2019-04-09 19:34:44 +02:00
|
|
|
imapnotifyAccounts =
|
2020-02-02 00:39:17 +01:00
|
|
|
filter (a: a.imapnotify.enable) (attrValues config.accounts.email.accounts);
|
2019-04-09 19:34:44 +02:00
|
|
|
|
|
|
|
genAccountUnit = account:
|
2020-02-02 00:39:17 +01:00
|
|
|
let name = safeName account.name;
|
|
|
|
in {
|
|
|
|
name = "imapnotify-${name}";
|
|
|
|
value = {
|
|
|
|
Unit = { Description = "imapnotify for ${name}"; };
|
|
|
|
|
|
|
|
Service = {
|
2023-07-08 00:08:00 +02:00
|
|
|
# Use the nix store path for config to ensure service restarts when it changes
|
2023-07-07 11:39:12 +02:00
|
|
|
ExecStart =
|
2023-07-08 00:08:00 +02:00
|
|
|
"${getExe cfg.package} -conf '${genAccountConfig account}'";
|
2020-12-27 16:10:55 +01:00
|
|
|
Restart = "always";
|
|
|
|
RestartSec = 30;
|
|
|
|
Type = "simple";
|
2020-02-02 00:39:17 +01:00
|
|
|
} // optionalAttrs account.notmuch.enable {
|
|
|
|
Environment =
|
2022-02-05 17:45:12 +01:00
|
|
|
"NOTMUCH_CONFIG=${config.xdg.configHome}/notmuch/default/config";
|
2019-04-09 19:34:44 +02:00
|
|
|
};
|
2020-02-02 00:39:17 +01:00
|
|
|
|
|
|
|
Install = { WantedBy = [ "default.target" ]; };
|
2019-04-09 19:34:44 +02:00
|
|
|
};
|
2020-02-02 00:39:17 +01:00
|
|
|
};
|
2019-04-09 19:34:44 +02:00
|
|
|
|
2023-07-07 11:39:12 +02:00
|
|
|
genAccountAgent = account:
|
|
|
|
let name = safeName account.name;
|
|
|
|
in {
|
|
|
|
name = "imapnotify-${name}";
|
|
|
|
value = {
|
|
|
|
enable = true;
|
|
|
|
config = {
|
2023-07-08 00:08:00 +02:00
|
|
|
# Use the nix store path for config to ensure service restarts when it changes
|
|
|
|
ProgramArguments =
|
|
|
|
[ "${getExe cfg.package}" "-conf" "${genAccountConfig account}" ];
|
2023-07-07 11:39:12 +02:00
|
|
|
KeepAlive = true;
|
|
|
|
ThrottleInterval = 30;
|
|
|
|
ExitTimeOut = 0;
|
|
|
|
ProcessType = "Background";
|
|
|
|
RunAtLoad = true;
|
|
|
|
} // optionalAttrs account.notmuch.enable {
|
|
|
|
EnvironmentVariables.NOTMUCH_CONFIG =
|
|
|
|
"${config.xdg.configHome}/notmuch/default/config";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-04-09 19:34:44 +02:00
|
|
|
genAccountConfig = account:
|
2023-07-07 11:39:12 +02:00
|
|
|
pkgs.writeText (configName account) (let
|
2020-02-02 00:39:17 +01:00
|
|
|
port = if account.imap.port != null then
|
|
|
|
account.imap.port
|
|
|
|
else if account.imap.tls.enable then
|
|
|
|
993
|
|
|
|
else
|
|
|
|
143;
|
|
|
|
|
|
|
|
toJSON = builtins.toJSON;
|
2021-10-18 08:42:13 +02:00
|
|
|
in toJSON ({
|
2020-12-27 16:10:55 +01:00
|
|
|
inherit (account.imap) host;
|
|
|
|
inherit port;
|
|
|
|
tls = account.imap.tls.enable;
|
2024-02-10 23:01:01 +01:00
|
|
|
tlsOptions.starttls = account.imap.tls.useStartTls;
|
2020-12-27 16:10:55 +01:00
|
|
|
username = account.userName;
|
|
|
|
passwordCmd =
|
|
|
|
lib.concatMapStringsSep " " lib.escapeShellArg account.passwordCommand;
|
2021-10-18 08:42:13 +02:00
|
|
|
inherit (account.imapnotify) boxes;
|
|
|
|
} // optionalAttrs (account.imapnotify.onNotify != "") {
|
2020-12-27 16:10:55 +01:00
|
|
|
onNewMail = account.imapnotify.onNotify;
|
2021-10-18 08:42:13 +02:00
|
|
|
} // optionalAttrs (account.imapnotify.onNotifyPost != "") {
|
2020-12-27 16:10:55 +01:00
|
|
|
onNewMailPost = account.imapnotify.onNotifyPost;
|
2021-10-18 09:34:51 +02:00
|
|
|
} // account.imapnotify.extraConfig));
|
2020-02-02 00:39:17 +01:00
|
|
|
|
|
|
|
in {
|
2019-04-09 19:34:44 +02:00
|
|
|
meta.maintainers = [ maintainers.nickhu ];
|
|
|
|
|
|
|
|
options = {
|
2023-07-07 11:39:12 +02:00
|
|
|
services.imapnotify = {
|
|
|
|
enable = mkEnableOption "imapnotify";
|
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.goimapnotify;
|
|
|
|
defaultText = literalExpression "pkgs.goimapnotify";
|
|
|
|
example = literalExpression "pkgs.imapnotify";
|
|
|
|
description = "The imapnotify package to use";
|
|
|
|
};
|
|
|
|
};
|
2019-04-09 19:34:44 +02:00
|
|
|
|
|
|
|
accounts.email.accounts = mkOption {
|
2023-07-15 20:53:43 +02:00
|
|
|
type = with types;
|
|
|
|
attrsOf
|
|
|
|
(submodule (import ./imapnotify-accounts.nix { inherit pkgs lib; }));
|
2019-04-09 19:34:44 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2020-02-02 00:39:17 +01:00
|
|
|
assertions = let
|
|
|
|
checkAccounts = pred: msg:
|
|
|
|
let badAccounts = filter pred imapnotifyAccounts;
|
|
|
|
in {
|
|
|
|
assertion = badAccounts == [ ];
|
|
|
|
message = "imapnotify: Missing ${msg} for accounts: "
|
|
|
|
+ concatMapStringsSep ", " (a: a.name) badAccounts;
|
|
|
|
};
|
|
|
|
in [
|
|
|
|
(checkAccounts (a: a.maildir == null) "maildir configuration")
|
|
|
|
(checkAccounts (a: a.imap == null) "IMAP configuration")
|
|
|
|
(checkAccounts (a: a.passwordCommand == null) "password command")
|
|
|
|
(checkAccounts (a: a.userName == null) "username")
|
|
|
|
];
|
|
|
|
|
|
|
|
systemd.user.services = listToAttrs (map genAccountUnit imapnotifyAccounts);
|
2023-07-07 11:39:12 +02:00
|
|
|
|
|
|
|
launchd.agents = listToAttrs (map genAccountAgent imapnotifyAccounts);
|
|
|
|
|
|
|
|
xdg.configFile = listToAttrs (map (account: {
|
|
|
|
name = "imapnotify/${configName account}";
|
|
|
|
value.source = genAccountConfig account;
|
|
|
|
}) imapnotifyAccounts);
|
2019-04-09 19:34:44 +02:00
|
|
|
};
|
|
|
|
}
|