fork/home-manager
git clone https://git.julia.blue/fork/home-manager
a146ab6a
master
1{ config , lib , pkgs , ...} : 2 3let 4# aliases 5inherit ( config . programs ) himalaya ; 6tomlFormat = pkgs . formats . toml { }; 7 8# attrs util that removes entries containing a null value 9compactAttrs = lib . filterAttrs ( _ :val :! isNull val ); 10 11# Needed for notmuch config, because the DB is here, and not in each account's dir 12maildirBasePath = config . accounts . . maildirBasePath ; 13 14# make a himalaya config from a home-manager email account config 15mkAccountConfig = _ :account : 16let 17# Use notmuch if it's enabled, otherwise fallback to IMAP then maildir 18# Maildir is always set, so there's no easy way to detect if it's being used 19notmuchEnabled = account . notmuch . enable ; 20imapEnabled = ! isNull account . imap && ! notmuchEnabled ; 21maildirEnabled = ! isNull account . maildir && ! imapEnabled 22&& ! notmuchEnabled ; 23 24globalConfig = { 25= account . address ; 26display-name = account . realName ; 27default = account . primary ; 28folder-aliases = { 29inbox = account . folders . inbox ; 30sent = account . folders . sent ; 31drafts = account . folders . drafts ; 32trash = account . folders . trash ; 33}; 34}; 35 36signatureConfig = 37lib . optionalAttrs ( account . signature . showSignature == "append" ) { 38# TODO: signature cannot be attached yet 39# https://todo.sr.ht/~soywod/pimalaya/27 40signature = account . signature . text ; 41signature-delim = account . signature . delimiter ; 42}; 43 44imapConfig = lib . optionalAttrs imapEnabled ( compactAttrs { 45backend = "imap" ; 46imap-host = account . imap . host ; 47imap-port = account . imap . port ; 48imap-ssl = account . imap . tls . enable ; 49imap-starttls = account . imap . tls . useStartTls ; 50imap-login = account . userName ; 51imap-auth = "passwd" ; 52imap-passwd . cmd = builtins . concatStringsSep " " account . passwordCommand ; 53}); 54 55maildirConfig = lib . optionalAttrs maildirEnabled ( compactAttrs { 56backend = "maildir" ; 57maildir-root-dir = account . maildir . absPath ; 58}); 59 60notmuchConfig = lib . optionalAttrs notmuchEnabled ( compactAttrs { 61backend = "notmuch" ; 62notmuch-db-path = maildirBasePath ; 63}); 64 65smtpConfig = lib . optionalAttrs ( ! isNull account . smtp ) ( compactAttrs { 66sender = "smtp" ; 67smtp-host = account . smtp . host ; 68smtp-port = account . smtp . port ; 69smtp-ssl = account . smtp . tls . enable ; 70smtp-starttls = account . smtp . tls . useStartTls ; 71smtp-login = account . userName ; 72smtp-auth = "passwd" ; 73smtp-passwd . cmd = builtins . concatStringsSep " " account . passwordCommand ; 74}); 75 76sendmailConfig = 77lib . optionalAttrs ( isNull account . smtp && ! isNull account . msmtp ) { 78sender = "sendmail" ; 79sendmail-cmd = " ${ pkgs . msmtp } /bin/msmtp" ; 80}; 81 82config = globalConfig // signatureConfig // imapConfig // maildirConfig 83// notmuchConfig // smtpConfig // sendmailConfig ; 84 85in lib . recursiveUpdate config account . himalaya . settings ; 86 87# make a systemd service config from a name and a description 88mkServiceConfig = name :desc : 89let 90inherit ( config . services . "himalaya- ${ name } " ) enable environment settings ; 91optionalArg = key : 92if ( key ?settings && ! isNull settings . " ${ key } " ) then 93[ "-- ${ key } ${ settings . " ${ key } " } " ] 94else 95[ ]; 96in { 97"himalaya- ${ name } " = lib . mkIf enable { 98Unit = { 99Description = desc ; 100After = [ "network.target" ]; 101}; 102Install = { WantedBy = [ "default.target" ]; }; 103Service = { 104ExecStart = lib . concatStringsSep " " 105([ " ${ himalaya . package } /bin/himalaya" ] ++ optionalArg "account" 106++ [ name ] ++ optionalArg "keepalive" ); 107ExecSearchPath = "/bin" ; 108Environment = 109lib . mapAttrsToList ( key :val :" ${ key } = ${ val } " ) environment ; 110Restart = "always" ; 111RestartSec = 10 ; 112}; 113}; 114}; 115 116in { 117meta . maintainers = with lib . hm . maintainers ; [ soywod toastal ]; 118 119options = { 120programs . himalaya = { 121enable = lib . mkEnableOption "the Himalaya email client" ; 122package = lib . mkPackageOption pkgs "himalaya" { }; 123settings = lib . mkOption { 124type = lib . types . submodule { freeformType = tomlFormat . type ; }; 125default = { }; 126description = '' 127Himalaya global configuration. 128See <https://pimalaya.org/himalaya/cli/configuration/global.html> for supported values. 129'' ; 130}; 131}; 132 133services = { 134himalaya-notify = { 135enable = lib . mkEnableOption "the Himalaya new emails notifier service" ; 136 137environment = lib . mkOption { 138type = with lib . types ; attrsOf str ; 139default = { }; 140example = lib . literalExpression '' 141{ 142"PASSWORD_STORE_DIR" = "~/.password-store"; 143} 144'' ; 145description = '' 146Extra environment variables to be exported in the service. 147'' ; 148}; 149 150settings = { 151account = lib . mkOption { 152type = with lib . types ; nullOr str ; 153default = null ; 154example = "gmail" ; 155description = '' 156Name of the account the notifier should be started for. If 157no account is given, the default one is used. 158'' ; 159}; 160 161keepalive = lib . mkOption { 162type = with lib . types ; nullOr int ; 163default = null ; 164example = "500" ; 165description = '' 166Notifier lifetime of the IDLE session (in seconds). 167'' ; 168}; 169}; 170}; 171 172himalaya-watch = { 173enable = 174lib . mkEnableOption "the Himalaya folder changes watcher service" ; 175 176environment = lib . mkOption { 177type = with lib . types ; attrsOf str ; 178default = { }; 179example = lib . literalExpression '' 180{ 181"PASSWORD_STORE_DIR" = "~/.password-store"; 182} 183'' ; 184description = '' 185Extra environment variables to be exported in the service. 186'' ; 187}; 188 189settings = { 190account = lib . mkOption { 191type = with lib . types ; nullOr str ; 192default = null ; 193example = "gmail" ; 194description = '' 195Name of the account the watcher should be started for. If 196no account is given, the default one is used. 197'' ; 198}; 199 200keepalive = lib . mkOption { 201type = with lib . types ; nullOr int ; 202default = null ; 203example = "500" ; 204description = '' 205Watcher lifetime of the IDLE session (in seconds). 206'' ; 207}; 208}; 209}; 210}; 211 212accounts . . accounts = lib . mkOption { 213type = lib . types . attrsOf ( lib . types . submodule { 214options . himalaya = { 215enable = lib . mkEnableOption "Himalaya for this email account" ; 216 217# TODO: remove me for the next release 218backend = lib . mkOption { 219type = with lib . types ; nullOr str ; 220default = null ; 221description = '' 222Specifying {option}`accounts.email.accounts.*.himalaya.backend` is deprecated, 223set {option}`accounts.email.accounts.*.himalaya.settings.backend` instead. 224'' ; 225}; 226 227# TODO: remove me for the next release 228sender = lib . mkOption { 229type = with lib . types ; nullOr str ; 230description = '' 231Specifying {option}`accounts.email.accounts.*.himalaya.sender` is deprecated, 232set {option}'accounts.email.accounts.*.himalaya.settings.sender' instead. 233'' ; 234}; 235 236settings = lib . mkOption { 237type = lib . types . submodule { freeformType = tomlFormat . type ; }; 238default = { }; 239description = '' 240Himalaya configuration for this email account. 241See <https://pimalaya.org/himalaya/cli/configuration/account.html> for supported values. 242'' ; 243}; 244}; 245}); 246}; 247}; 248 249config = lib . mkIf himalaya . enable { 250home . packages = [ himalaya . package ]; 251 252xdg . configFile . "himalaya/config.toml" . source = let 253enabledAccounts = lib . filterAttrs ( _ :account :account . himalaya . enable ) 254config . accounts . . accounts ; 255accountsConfig = lib . mapAttrs mkAccountConfig enabledAccounts ; 256globalConfig = compactAttrs himalaya . settings ; 257allConfig = globalConfig // accountsConfig ; 258in tomlFormat . generate "himalaya-config.toml" allConfig ; 259 260systemd . user . services = { } 261// mkServiceConfig "notify" "Himalaya new emails notifier service" 262// mkServiceConfig "watch" "Himalaya folder changes watcher service" ; 263 264# TODO: remove me for the next release 265warnings = ( lib . optional ( "backend" ?himalaya && ! isNull himalaya . backend ) 266"Specifying 'accounts.email.accounts.*.himalaya.backend' is deprecated, set 'accounts.email.accounts.*.himalaya.settings.backend' instead" ) 267++ ( lib . optional ( "sender" ?himalaya && ! isNull himalaya . sender ) 268"Specifying 'accounts.email.accounts.*.himalaya.sender' is deprecated, set 'accounts.email.accounts.*.himalaya.settings.sender' instead." ); 269}; 270}