1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-11-05 18:59:44 +01:00
home-manager/modules/services/imapnotify.nix
David Baynard 719de878f7
imapnotify: Add launchd agent (#3291)
* imapnotify: expose package (and exe) options

There are multiple packages that provide an imapnotify interface. Those
packages have differently named executables. This can now be customized.

This change also means test configurations can use stub packages.

* imapnotify: use/create config in configHome

Exposing the configuration file makes testing imapnotify configurations much
easier. It also allows for golden tests in home-manager.

* imapnotify: extend with launchd agent

Now that home-manager supports launchd agents, the imapnotify service
can be configured (and enabled) for darwin. The configuration matches
that of the linux/systemd version. In particular, by not setting a
`UserName`, this runs as the user whose configuration includes the
module.

Due to the launchd `Program` implementation (it must take an absolute
path) it is not possible to use that for the program and stub the path
in tests. Instead, this uses `ProgramArguments` for the program name.

The `ThrottleInterval` is equivalent to `RestartSec`. `KeepAlive` is
equivalent to `Restart`.

The `ExitTimeOut` default is 20 seconds, but goimapnotify should not
time out — this is achieved by setting the `ExitTimeout` to 0.

* imapnotify: add launchd plist test

This only tests the generated plist (which is new), not the original
systemd implementation, nor the json config file.

(Note the lack of a newline at the end of the plist file.)
2023-07-07 11:39:12 +02:00

134 lines
3.9 KiB
Nix

{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.imapnotify;
safeName = lib.replaceStrings [ "@" ":" "\\" "[" "]" ] [ "-" "-" "-" "" "" ];
configName = account: "imapnotify-${safeName account.name}-config.json";
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 = {
ExecStart =
"${getExe cfg.package} -conf '${config.xdg.configHome}/imapnotify/${
configName account
}'";
Restart = "always";
RestartSec = 30;
Type = "simple";
} // optionalAttrs account.notmuch.enable {
Environment =
"NOTMUCH_CONFIG=${config.xdg.configHome}/notmuch/default/config";
};
Install = { WantedBy = [ "default.target" ]; };
};
};
genAccountAgent = account:
let name = safeName account.name;
in {
name = "imapnotify-${name}";
value = {
enable = true;
config = {
ProgramArguments = [
"${getExe cfg.package}"
"-conf"
"${config.xdg.configHome}/imapnotify/${configName account}"
];
KeepAlive = true;
ThrottleInterval = 30;
ExitTimeOut = 0;
ProcessType = "Background";
RunAtLoad = true;
} // optionalAttrs account.notmuch.enable {
EnvironmentVariables.NOTMUCH_CONFIG =
"${config.xdg.configHome}/notmuch/default/config";
};
};
};
genAccountConfig = account:
pkgs.writeText (configName account) (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 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));
in {
meta.maintainers = [ maintainers.nickhu ];
options = {
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";
};
};
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 [
(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);
launchd.agents = listToAttrs (map genAccountAgent imapnotifyAccounts);
xdg.configFile = listToAttrs (map (account: {
name = "imapnotify/${configName account}";
value.source = genAccountConfig account;
}) imapnotifyAccounts);
};
}