fork/home-manager
git clone https://git.julia.blue/fork/home-manager
408ba131
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5let 6cfg = config . programs . neomutt ; 7 8neomuttAccounts = 9filter ( a :a . neomutt . enable ) ( attrValues config . accounts . . accounts ); 10 11sidebarModule = types . submodule { 12options = { 13enable = mkEnableOption "sidebar support" ; 14 15width = mkOption { 16type = types . int ; 17default = 22 ; 18description = "Width of the sidebar" ; 19}; 20 21shortPath = mkOption { 22type = types . bool ; 23default = true ; 24description = '' 25By default sidebar shows the full path of the mailbox, but 26with this enabled only the relative name is shown. 27'' ; 28}; 29 30format = mkOption { 31type = types . str ; 32default = "%D%?F? [%F]?%* %?N?%N/?%S" ; 33description = '' 34Sidebar format. Check neomutt documentation for details. 35'' ; 36}; 37}; 38}; 39 40sortOptions = [ 41"date" 42"date-received" 43"from" 44"mailbox-order" 45"score" 46"size" 47"spam" 48"subject" 49"threads" 50"to" 51]; 52 53bindModule = types . submodule { 54options = { 55map = mkOption { 56type = let 57menus = [ 58"alias" 59"attach" 60"browser" 61"compose" 62"editor" 63"generic" 64"index" 65"mix" 66"pager" 67"pgp" 68"postpone" 69"query" 70"smime" 71]; 72in with types ; either ( enum menus ) ( listOf ( enum menus )); 73default = "index" ; 74description = "Select the menu to bind the command to." ; 75}; 76 77key = mkOption { 78type = types . str ; 79example = "<left>" ; 80description = "The key to bind." ; 81}; 82 83action = mkOption { 84type = types . str ; 85example = "<enter-command>toggle sidebar_visible<enter><refresh>" ; 86description = "Specify the action to take." ; 87}; 88}; 89}; 90 91mkNotmuchVirtualboxes = virtualMailboxes : 92" ${ concatStringsSep "\n" ( map ({ name , query , limit , type } : 93'' 94virtual-mailboxes " ${ name } " "notmuch://?query= ${ lib . escapeURL query }${ 95optionalString ( limit != null ) "&limit= ${ toString limit } " 96}${ optionalString ( type != null ) "&type= ${ type } " } "'' ) 97virtualMailboxes )} " ; 98 99setOption = n :v :if v == null then "unset ${ n } " else "set ${ n } = ${ v } " ; 100escape = replaceStrings [ "%" ] [ "%25" ]; 101 102accountFilename = account :config . xdg . configHome + "/neomutt/" + account . name ; 103 104genCommonFolderHooks = account : 105with account ; { 106from = "' ${ address } '" ; 107realname = "' ${ realName } '" ; 108spoolfile = "'+ ${ folders . inbox } '" ; 109record = if folders . sent == null then null else "'+ ${ folders . sent } '" ; 110postponed = "'+ ${ folders . drafts } '" ; 111trash = "'+ ${ folders . trash } '" ; 112}; 113 114mtaSection = account : 115with account ; 116let passCmd = concatStringsSep " " passwordCommand ; 117in if neomutt . sendMailCommand != null then { 118sendmail = "' ${ neomutt . sendMailCommand } '" ; 119} else 120let 121smtpProto = 122if smtp . tls . enable && ! smtp . tls . useStartTls then "smtps" else "smtp" ; 123smtpPort = if smtp . port != null then ": ${ toString smtp . port } " else "" ; 124smtpBaseUrl = 125" ${ smtpProto } :// ${ escape userName } @ ${ smtp . host }${ smtpPort } " ; 126in { 127smtp_url = "' ${ smtpBaseUrl } '" ; 128smtp_pass = ''"` ${ passCmd } `"'' ; 129}; 130 131genMaildirAccountConfig = account : 132with account ; 133let 134folderHook = mapAttrsToList setOption ( genCommonFolderHooks account 135// optionalAttrs cfg . changeFolderWhenSourcingAccount { 136folder = "' ${ account . maildir . absPath } '" ; 137}); 138in '' 139${ concatStringsSep "\n" folderHook } 140'' ; 141 142registerAccount = account : 143let 144mailboxes = if account . neomutt . mailboxName == null then 145"mailboxes" 146else 147''named-mailboxes " ${ account . neomutt . mailboxName } "'' ; 148extraMailboxes = concatMapStringsSep "\n" ( extra : 149if isString extra then 150''mailboxes " ${ account . maildir . absPath } / ${ extra } "'' 151else if extra . name == null then 152''mailboxes " ${ account . maildir . absPath } / ${ extra . mailbox } "'' 153else 154'' 155named-mailboxes " ${ extra . name } " " ${ account . maildir . absPath } / ${ extra . mailbox } "'' ) 156account . neomutt . extraMailboxes ; 157in with account ; '' 158# register account ${ name } 159${ mailboxes } " ${ maildir . absPath } / ${ folders . inbox } " 160${ extraMailboxes } 161folder-hook ${ maildir . absPath } / " \ 162source ${ accountFilename account } " 163'' ; 164 165mraSection = account : 166with account ; 167if account . maildir != null then 168genMaildirAccountConfig account 169else 170throw "Only maildir is supported at the moment" ; 171 172optionsStr = attrs :concatStringsSep "\n" ( mapAttrsToList setOption attrs ); 173 174sidebarSection = '' 175# Sidebar 176set sidebar_visible = yes 177set sidebar_short_path = ${ lib . hm . booleans . yesNo cfg . sidebar . shortPath } 178set sidebar_width = ${ toString cfg . sidebar . width } 179set sidebar_format = ' ${ cfg . sidebar . format } ' 180'' ; 181 182genBindMapper = bindType : 183concatMapStringsSep "\n" ( bind : 184'' 185${ bindType } ${ 186concatStringsSep "," ( toList bind . map ) 187} ${ bind . key } " ${ bind . action } "'' ); 188 189bindSection = ( genBindMapper "bind" ) cfg . binds ; 190 191macroSection = ( genBindMapper "macro" ) cfg . macros ; 192 193mailCheckSection = '' 194set mail_check_stats 195set mail_check_stats_interval = ${ toString cfg . checkStatsInterval } 196'' ; 197 198notmuchSection = account : 199let virtualMailboxes = account . notmuch . neomutt . virtualMailboxes ; 200in with account ; '' 201# notmuch section 202set nm_default_uri = "notmuch:// ${ config . accounts . . maildirBasePath } " 203${ optionalString 204( notmuch . neomutt . enable && builtins . length virtualMailboxes > 0 ) 205( mkNotmuchVirtualboxes virtualMailboxes )} 206'' ; 207 208accountStr = account : 209with account ; 210let 211signature = if account . signature . showSignature == "none" then 212"unset signature" 213else if account . signature . command != null then 214''set signature = " ${ account . signature . command } |"'' 215else 216"set signature = ${ 217pkgs . writeText "signature.txt" account . signature . text 218} " ; 219in '' 220# Generated by Home Manager. 221set ssl_force_tls = ${ 222lib . hm . booleans . yesNo ( smtp . tls . enable || smtp . tls . useStartTls ) 223} 224set certificate_file= ${ toString config . accounts . . certificatesFile } 225 226# GPG section 227set crypt_use_gpgme = yes 228set crypt_autosign = ${ lib . hm . booleans . yesNo ( gpg . signByDefault or false )} 229set crypt_opportunistic_encrypt = ${ 230lib . hm . booleans . yesNo ( gpg . encryptByDefault or false ) 231} 232set pgp_use_gpg_agent = yes 233set mbox_type = ${ if maildir != null then "Maildir" else "mbox" } 234set sort = " ${ cfg . sort } " 235 236# MTA section 237${ optionsStr ( mtaSection account )} 238 239${ optionalString ( cfg . checkStatsInterval != null ) mailCheckSection } 240 241${ optionalString cfg . sidebar . enable sidebarSection } 242 243# MRA section 244${ mraSection account } 245 246# Extra configuration 247${ account . neomutt . extraConfig } 248 249${ signature } 250'' 251+ optionalString ( account . notmuch . enable && account . notmuch . neomutt . enable ) 252( notmuchSection account ); 253 254in { 255options = { 256programs . neomutt = { 257enable = mkEnableOption "the NeoMutt mail client" ; 258 259package = mkOption { 260type = types . package ; 261default = pkgs . neomutt ; 262defaultText = literalExpression "pkgs.neomutt" ; 263description = "The neomutt package to use." ; 264}; 265 266sidebar = mkOption { 267type = sidebarModule ; 268default = { }; 269description = "Options related to the sidebar." ; 270}; 271 272binds = mkOption { 273type = types . listOf bindModule ; 274default = [ ]; 275description = "List of keybindings." ; 276}; 277 278macros = mkOption { 279type = types . listOf bindModule ; 280default = [ ]; 281description = "List of macros." ; 282}; 283 284sort = mkOption { 285# allow users to choose any option from sortOptions, or any option prefixed with "reverse-" 286type = types . enum 287( sortOptions ++ ( map ( option :"reverse-" + option ) sortOptions )); 288default = "threads" ; 289description = "Sorting method on messages." ; 290}; 291 292vimKeys = mkOption { 293type = types . bool ; 294default = false ; 295description = "Enable vim-like bindings." ; 296}; 297 298checkStatsInterval = mkOption { 299type = types . nullOr types . int ; 300default = null ; 301example = 60 ; 302description = "Enable and set the interval of automatic mail check." ; 303}; 304 305editor = mkOption { 306type = types . str ; 307default = "$EDITOR" ; 308description = "Select the editor used for writing mail." ; 309}; 310 311settings = mkOption { 312type = types . attrsOf types . str ; 313default = { }; 314description = "Extra configuration appended to the end." ; 315}; 316 317changeFolderWhenSourcingAccount = 318mkEnableOption "changing the folder when sourcing an account" // { 319default = true ; 320}; 321 322extraConfig = mkOption { 323type = types . lines ; 324default = "" ; 325description = "Extra configuration appended to the end." ; 326}; 327}; 328 329accounts . . accounts = mkOption { 330type = with types ; attrsOf ( submodule ( import ./neomutt-accounts.nix )); 331}; 332}; 333 334config = mkIf cfg . enable { 335home . packages = [ cfg . package ]; 336home . file = let 337rcFile = account :{ 338" ${ accountFilename account } " . text = accountStr account ; 339}; 340in foldl' ( a :b :a // b ) { } ( map rcFile neomuttAccounts ); 341 342xdg . configFile . "neomutt/neomuttrc" = mkIf ( neomuttAccounts != [ ]) { 343text = let 344# Find the primary account, if it has neomutt enabled; 345# otherwise use the first neomutt account as primary. 346primary = 347head ( filter ( a :a . primary ) neomuttAccounts ++ neomuttAccounts ); 348in '' 349# Generated by Home Manager. 350set header_cache = " ${ config . xdg . cacheHome } /neomutt/headers/" 351set message_cachedir = " ${ config . xdg . cacheHome } /neomutt/messages/" 352set editor = " ${ cfg . editor } " 353set implicit_autoview = yes 354 355alternative_order text/enriched text/plain text 356 357set delete = yes 358 359# Binds 360${ bindSection } 361 362# Macros 363${ macroSection } 364 365${ optionalString cfg . vimKeys 366"source ${ pkgs . neomutt } /share/doc/neomutt/vim-keys/vim-keys.rc" } 367 368# Register accounts 369${ concatMapStringsSep "\n" registerAccount neomuttAccounts } 370 371# Source primary account 372source ${ accountFilename primary } 373 374# Extra configuration 375${ optionsStr cfg . settings } 376 377${ cfg . extraConfig } 378'' ; 379}; 380 381assertions = [{ 382assertion = 383(( filter ( b :( length ( toList b . map )) == 0 ) ( cfg . binds ++ cfg . macros )) 384== [ ]); 385message = 386"The 'programs.neomutt.(binds|macros).map' list must contain at least one element." ; 387}]; 388 389warnings = 390let hasOldBinds = binds :( filter ( b :! ( isList b . map )) binds ) != [ ]; 391in mkIf ( hasOldBinds ( cfg . binds ++ cfg . macros )) [ 392"Specifying 'programs.neomutt.(binds|macros).map' as a string is deprecated, use a list of strings instead. See https://github.com/nix-community/home-manager/pull/1885." 393]; 394}; 395}