home-manager/modules/services/imapnotify.nix

91 lines
2.7 KiB
Nix
Raw Normal View History

2019-04-09 17:34:44 +00:00
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.imapnotify;
2020-02-01 23:39:17 +00:00
safeName = lib.replaceChars [ "@" ":" "\\" "[" "]" ] [ "-" "-" "-" "" "" ];
2019-04-09 17:34:44 +00:00
imapnotifyAccounts =
2020-02-01 23:39:17 +00:00
filter (a: a.imapnotify.enable) (attrValues config.accounts.email.accounts);
2019-04-09 17:34:44 +00:00
genAccountUnit = account:
2020-02-01 23:39:17 +00:00
let name = safeName account.name;
in {
name = "imapnotify-${name}";
value = {
Unit = { Description = "imapnotify for ${name}"; };
Service = {
ExecStart =
"${pkgs.imapnotify}/bin/imapnotify -c ${genAccountConfig account}";
} // optionalAttrs account.notmuch.enable {
Environment =
"NOTMUCH_CONFIG=${config.xdg.configHome}/notmuch/notmuchrc";
2019-04-09 17:34:44 +00:00
};
2020-02-01 23:39:17 +00:00
Install = { WantedBy = [ "default.target" ]; };
2019-04-09 17:34:44 +00:00
};
2020-02-01 23:39:17 +00:00
};
2019-04-09 17:34:44 +00:00
genAccountConfig = account:
2020-02-01 23:39:17 +00:00
pkgs.writeText "imapnotify-${safeName account.name}-config.js" (let
port = if account.imap.port != null then
account.imap.port
else if account.imap.tls.enable then
993
else
143;
toJSON = builtins.toJSON;
in ''
var child_process = require('child_process');
function getStdout(cmd) {
var stdout = child_process.execSync(cmd);
return stdout.toString().trim();
}
exports.host = ${toJSON account.imap.host}
exports.port = ${toJSON port};
exports.tls = ${toJSON account.imap.tls.enable};
exports.username = ${toJSON account.userName};
exports.password = getStdout("${toString account.passwordCommand}");
exports.onNotify = ${toJSON account.imapnotify.onNotify};
exports.onNotifyPost = ${toJSON account.imapnotify.onNotifyPost};
exports.boxes = ${toJSON account.imapnotify.boxes};
'');
in {
2019-04-09 17:34:44 +00:00
meta.maintainers = [ maintainers.nickhu ];
options = {
2020-02-01 23:39:17 +00:00
services.imapnotify = { enable = mkEnableOption "imapnotify"; };
2019-04-09 17:34:44 +00:00
accounts.email.accounts = mkOption {
2020-02-01 23:39:17 +00:00
type = with types; attrsOf (submodule (import ./imapnotify-accounts.nix));
2019-04-09 17:34:44 +00:00
};
};
config = mkIf cfg.enable {
2020-02-01 23:39:17 +00: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);
2019-04-09 17:34:44 +00:00
};
}