1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-12-19 08:19:48 +01:00
home-manager/modules/services/imapnotify.nix

93 lines
2.8 KiB
Nix
Raw Normal View History

2019-04-09 19:34:44 +02:00
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.imapnotify;
2020-02-02 00:39:17 +01:00
safeName = lib.replaceChars [ "@" ":" "\\" "[" "]" ] [ "-" "-" "-" "" "" ];
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 = {
ExecStart = "${pkgs.goimapnotify}/bin/goimapnotify -conf ${
genAccountConfig account
}";
Restart = "always";
RestartSec = 30;
Type = "simple";
2020-02-02 00:39:17 +01:00
} // optionalAttrs account.notmuch.enable {
Environment =
"NOTMUCH_CONFIG=${config.xdg.configHome}/notmuch/notmuchrc";
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
genAccountConfig = account:
pkgs.writeText "imapnotify-${safeName account.name}-config.json" (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;
in toJSON ({
inherit (account.imap) host;
inherit port;
tls = account.imap.tls.enable;
username = account.userName;
passwordCmd =
lib.concatMapStringsSep " " lib.escapeShellArg account.passwordCommand;
inherit (account.imapnotify) boxes;
} // optionalAttrs (account.imapnotify.onNotify != "") {
onNewMail = account.imapnotify.onNotify;
} // optionalAttrs (account.imapnotify.onNotifyPost != "") {
onNewMailPost = account.imapnotify.onNotifyPost;
} // 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 = {
2020-02-02 00:39:17 +01:00
services.imapnotify = { enable = mkEnableOption "imapnotify"; };
2019-04-09 19:34:44 +02:00
accounts.email.accounts = mkOption {
2020-02-02 00:39:17 +01:00
type = with types; attrsOf (submodule (import ./imapnotify-accounts.nix));
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 [
(lib.hm.assertions.assertPlatform "services.imapnotify" pkgs
lib.platforms.linux)
2020-02-02 00:39:17 +01: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);
2019-04-09 19:34:44 +02:00
};
}