fork/home-manager
git clone https://git.julia.blue/fork/home-manager
f5b03feb
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5let 6 7cfg = config . services . imapnotify ; 8 9safeName = lib . replaceStrings [ "@" ":" "\\" "[" "]" ] [ "-" "-" "-" "" "" ]; 10 11configName = account :"imapnotify- ${ safeName account . name } -config.json" ; 12 13imapnotifyAccounts = 14filter ( a :a . imapnotify . enable ) ( attrValues config . accounts . . accounts ); 15 16genAccountUnit = account : 17let name = safeName account . name ; 18in { 19name = "imapnotify- ${ name } " ; 20value = { 21Unit = { Description = "imapnotify for ${ name } " ; }; 22 23Service = { 24# Use the nix store path for config to ensure service restarts when it changes 25ExecStart = 26" ${ getExe cfg . package } -conf ' ${ genAccountConfig account } '" ; 27Restart = "always" ; 28RestartSec = 30 ; 29Type = "simple" ; 30} // optionalAttrs account . notmuch . enable { 31Environment = 32"NOTMUCH_CONFIG= ${ config . xdg . configHome } /notmuch/default/config" ; 33}; 34 35Install = { WantedBy = [ "default.target" ]; }; 36}; 37}; 38 39genAccountAgent = account : 40let name = safeName account . name ; 41in { 42name = "imapnotify- ${ name } " ; 43value = { 44enable = true ; 45config = { 46# Use the nix store path for config to ensure service restarts when it changes 47ProgramArguments = 48[ " ${ getExe cfg . package } " "-conf" " ${ genAccountConfig account } " ]; 49KeepAlive = true ; 50ThrottleInterval = 30 ; 51ExitTimeOut = 0 ; 52ProcessType = "Background" ; 53RunAtLoad = true ; 54} // optionalAttrs account . notmuch . enable { 55EnvironmentVariables . NOTMUCH_CONFIG = 56" ${ config . xdg . configHome } /notmuch/default/config" ; 57}; 58}; 59}; 60 61genAccountConfig = account : 62pkgs . writeText ( configName account ) ( let 63port = if account . imap . port != null then 64account . imap . port 65else if account . imap . tls . enable then 66993 67else 68143 ; 69 70toJSON = builtins . toJSON ; 71in toJSON ({ 72inherit ( account . imap ) host ; 73inherit port ; 74tls = account . imap . tls . enable ; 75username = account . userName ; 76passwordCmd = 77lib . concatMapStringsSep " " lib . escapeShellArg account . passwordCommand ; 78inherit ( account . imapnotify ) boxes ; 79} // optionalAttrs ( account . imapnotify . onNotify != "" ) { 80onNewMail = account . imapnotify . onNotify ; 81} // optionalAttrs ( account . imapnotify . onNotifyPost != "" ) { 82onNewMailPost = account . imapnotify . onNotifyPost ; 83} // account . imapnotify . extraConfig )); 84 85in { 86meta . maintainers = [ maintainers . nickhu ]; 87 88options = { 89services . imapnotify = { 90enable = mkEnableOption "imapnotify" ; 91 92package = mkOption { 93type = types . package ; 94default = pkgs . goimapnotify ; 95defaultText = literalExpression "pkgs.goimapnotify" ; 96example = literalExpression "pkgs.imapnotify" ; 97description = "The imapnotify package to use" ; 98}; 99}; 100 101accounts . . accounts = mkOption { 102type = with types ; 103attrsOf 104( submodule ( import ./imapnotify-accounts.nix { inherit pkgs lib ; })); 105}; 106}; 107 108config = mkIf cfg . enable { 109assertions = let 110checkAccounts = pred :msg : 111let badAccounts = filter pred imapnotifyAccounts ; 112in { 113assertion = badAccounts == [ ]; 114message = "imapnotify: Missing ${ msg } for accounts: " 115+ concatMapStringsSep ", " ( a :a . name ) badAccounts ; 116}; 117in [ 118( checkAccounts ( a :a . maildir == null ) "maildir configuration" ) 119( checkAccounts ( a :a . imap == null ) "IMAP configuration" ) 120( checkAccounts ( a :a . passwordCommand == null ) "password command" ) 121( checkAccounts ( a :a . userName == null ) "username" ) 122]; 123 124systemd . user . services = listToAttrs ( map genAccountUnit imapnotifyAccounts ); 125 126launchd . agents = listToAttrs ( map genAccountAgent imapnotifyAccounts ); 127 128xdg . configFile = listToAttrs ( map ( account :{ 129name = "imapnotify/ ${ configName account } " ; 130value . source = genAccountConfig account ; 131}) imapnotifyAccounts ); 132}; 133}