2020-10-12 00:22:58 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.services.imapnotify;
|
|
|
|
|
2023-01-10 09:35:00 +00:00
|
|
|
safeName = lib.replaceStrings [ "@" ":" "\\" "[" "]" ] [ "-" "-" "-" "" "" ];
|
2020-10-12 00:22:58 +00:00
|
|
|
|
|
|
|
imapnotifyAccounts =
|
|
|
|
filter (a: a.imapnotify.enable) (attrValues config.accounts.email.accounts);
|
|
|
|
|
|
|
|
genAccountUnit = account:
|
|
|
|
let name = safeName account.name;
|
|
|
|
in {
|
|
|
|
name = "imapnotify-${name}";
|
|
|
|
value = {
|
|
|
|
Unit = { Description = "imapnotify for ${name}"; };
|
|
|
|
|
|
|
|
Service = {
|
2021-07-02 22:36:30 +00:00
|
|
|
ExecStart = "${pkgs.goimapnotify}/bin/goimapnotify -conf ${
|
|
|
|
genAccountConfig account
|
|
|
|
}";
|
|
|
|
Restart = "always";
|
|
|
|
RestartSec = 30;
|
|
|
|
Type = "simple";
|
2020-10-12 00:22:58 +00:00
|
|
|
} // optionalAttrs account.notmuch.enable {
|
|
|
|
Environment =
|
2023-01-10 09:35:00 +00:00
|
|
|
"NOTMUCH_CONFIG=${config.xdg.configHome}/notmuch/default/config";
|
2020-10-12 00:22:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Install = { WantedBy = [ "default.target" ]; };
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
genAccountConfig = account:
|
2021-07-02 22:36:30 +00:00
|
|
|
pkgs.writeText "imapnotify-${safeName account.name}-config.json" (let
|
2020-10-12 00:22:58 +00: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-11-04 16:42:44 +00:00
|
|
|
in toJSON ({
|
2021-07-02 22:36:30 +00:00
|
|
|
inherit (account.imap) host;
|
|
|
|
inherit port;
|
|
|
|
tls = account.imap.tls.enable;
|
|
|
|
username = account.userName;
|
|
|
|
passwordCmd =
|
|
|
|
lib.concatMapStringsSep " " lib.escapeShellArg account.passwordCommand;
|
2021-11-04 16:42:44 +00:00
|
|
|
inherit (account.imapnotify) boxes;
|
|
|
|
} // optionalAttrs (account.imapnotify.onNotify != "") {
|
2021-07-02 22:36:30 +00:00
|
|
|
onNewMail = account.imapnotify.onNotify;
|
2021-11-04 16:42:44 +00:00
|
|
|
} // optionalAttrs (account.imapnotify.onNotifyPost != "") {
|
2021-07-02 22:36:30 +00:00
|
|
|
onNewMailPost = account.imapnotify.onNotifyPost;
|
2021-11-04 16:42:44 +00:00
|
|
|
} // account.imapnotify.extraConfig));
|
2020-10-12 00:22:58 +00:00
|
|
|
|
|
|
|
in {
|
|
|
|
meta.maintainers = [ maintainers.nickhu ];
|
|
|
|
|
|
|
|
options = {
|
|
|
|
services.imapnotify = { enable = mkEnableOption "imapnotify"; };
|
|
|
|
|
|
|
|
accounts.email.accounts = mkOption {
|
|
|
|
type = with types; attrsOf (submodule (import ./imapnotify-accounts.nix));
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
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 [
|
2021-11-04 16:42:44 +00:00
|
|
|
(lib.hm.assertions.assertPlatform "services.imapnotify" pkgs
|
|
|
|
lib.platforms.linux)
|
2020-10-12 00:22:58 +00:00
|
|
|
(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);
|
|
|
|
};
|
|
|
|
}
|