Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 49 additions & 7 deletions modules/services/imapnotify.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ let

safeName = lib.replaceStrings [ "@" ":" "\\" "[" "]" ] [ "-" "-" "-" "" "" ];

configName = account: "imapnotify-${safeName account.name}-config.json";

imapnotifyAccounts =
filter (a: a.imapnotify.enable) (attrValues config.accounts.email.accounts);

Expand All @@ -19,9 +21,10 @@ let
Unit = { Description = "imapnotify for ${name}"; };

Service = {
ExecStart = "${pkgs.goimapnotify}/bin/goimapnotify -conf ${
genAccountConfig account
}";
ExecStart =
"${getExe cfg.package} -conf '${config.xdg.configHome}/imapnotify/${
Comment thread
dbaynard marked this conversation as resolved.
configName account
}'";
Restart = "always";
RestartSec = 30;
Type = "simple";
Expand All @@ -34,8 +37,32 @@ let
};
};

genAccountAgent = account:
let name = safeName account.name;
Comment thread
dbaynard marked this conversation as resolved.
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 "imapnotify-${safeName account.name}-config.json" (let
pkgs.writeText (configName account) (let
port = if account.imap.port != null then
account.imap.port
else if account.imap.tls.enable then
Expand All @@ -62,7 +89,17 @@ in {
meta.maintainers = [ maintainers.nickhu ];

options = {
services.imapnotify = { enable = mkEnableOption "imapnotify"; };
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));
Expand All @@ -79,14 +116,19 @@ in {
+ concatMapStringsSep ", " (a: a.name) badAccounts;
};
in [
(lib.hm.assertions.assertPlatform "services.imapnotify" pkgs
lib.platforms.linux)
(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);
};
}
1 change: 1 addition & 0 deletions tests/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ import nmt {
] ++ lib.optionals isDarwin [
./modules/launchd
./modules/targets-darwin
./modules/programs/goimapnotify
] ++ lib.optionals isLinux [
./modules/config/i18n
./modules/i18n/input-method
Expand Down
1 change: 1 addition & 0 deletions tests/modules/programs/goimapnotify/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ goimapnotify-launchd = ./launchd.nix; }
Comment thread
dbaynard marked this conversation as resolved.
41 changes: 41 additions & 0 deletions tests/modules/programs/goimapnotify/launchd.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{ config, lib, pkgs, ... }:

with lib;

{
imports = [ ../../accounts/email-test-accounts.nix ];

config = {
accounts.email.accounts = {
"hm@example.com" = {
notmuch.enable = true;
imap.port = 993;

imapnotify = {
enable = true;
boxes = [ "Inbox" ];
onNotify = ''
${pkgs.notmuch}/bin/notmuch new
'';
};
};
};

services.imapnotify = {
enable = true;
package = (config.lib.test.mkStubPackage {
name = "goimapnotify";
outPath = "@goimapnotify@";
});
};

nmt.script = let
serviceFileName =
"org.nix-community.home.imapnotify-hm-example.com.plist";
in ''
serviceFile=LaunchAgents/${serviceFileName}
assertFileExists $serviceFile
assertFileContent $serviceFile ${./launchd.plist}
'';
};
}
29 changes: 29 additions & 0 deletions tests/modules/programs/goimapnotify/launchd.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>EnvironmentVariables</key>
<dict>
<key>NOTMUCH_CONFIG</key>
<string>/home/hm-user/.config/notmuch/default/config</string>
</dict>
<key>ExitTimeOut</key>
<integer>0</integer>
<key>KeepAlive</key>
<true/>
<key>Label</key>
<string>org.nix-community.home.imapnotify-hm-example.com</string>
<key>ProcessType</key>
<string>Background</string>
<key>ProgramArguments</key>
<array>
<string>@goimapnotify@/bin/goimapnotify</string>
<string>-conf</string>
<string>/home/hm-user/.config/imapnotify/imapnotify-hm-example.com-config.json</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>ThrottleInterval</key>
<integer>30</integer>
</dict>
</plist>