fork/home-manager
git clone https://git.julia.blue/fork/home-manager
1369d2ce
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4let 5cfg = config . programs . aerc ; 6 7primitive = with types ; 8(( type :either type ( listOf type )) ( nullOr ( oneOf [ str int bool float ]))) 9// { 10description = 11"values (null, bool, int, string, or float) or a list of values, that will be joined with a comma" ; 12}; 13 14confSection = types . attrsOf primitive ; 15 16confSections = types . attrsOf confSection ; 17 18sectionsOrLines = types . either types . lines confSections ; 19 20accounts = import ./aerc-accounts.nix { 21inherit config pkgs lib confSection confSections ; 22}; 23 24aerc-accounts = 25attrsets . filterAttrs ( _ :v :v . aerc . enable ) config . accounts . . accounts ; 26 27configDir = if ( pkgs . stdenv . isDarwin && ! config . xdg . enable ) then 28"Library/Preferences/aerc" 29else 30" ${ config . xdg . configHome } /aerc" ; 31 32in { 33meta . maintainers = with lib . hm . maintainers ; [ lukasngl ]; 34 35options . accounts . . accounts = accounts . type ; 36 37options . programs . aerc = { 38 39enable = mkEnableOption "aerc" ; 40 41package = mkPackageOption pkgs "aerc" { }; 42 43extraAccounts = mkOption { 44type = sectionsOrLines ; 45default = { }; 46example = literalExpression 47''{ Work = { source = "maildir://~/Maildir/work"; }; }'' ; 48description = '' 49Extra lines added to {file}`$HOME/.config/aerc/accounts.conf`. 50 51See {manpage}`aerc-config(5)`. 52'' ; 53}; 54 55extraBinds = mkOption { 56type = sectionsOrLines ; 57default = { }; 58example = literalExpression ''{ messages = { q = ":quit<Enter>"; }; }'' ; 59description = '' 60Extra lines added to {file}`$HOME/.config/aerc/binds.conf`. 61Global keybindings can be set in the `global` section. 62 63See {manpage}`aerc-config(5)`. 64'' ; 65}; 66 67extraConfig = mkOption { 68type = sectionsOrLines ; 69default = { }; 70example = literalExpression ''{ ui = { sort = "-r date"; }; }'' ; 71description = '' 72Extra lines added to {file}`$HOME/.config/aerc/aerc.conf`. 73 74See {manpage}`aerc-config(5)`. 75'' ; 76}; 77 78stylesets = mkOption { 79type = with types ; attrsOf ( either confSection lines ); 80default = { }; 81example = literalExpression '' 82{ default = { ui = { "tab.selected.reverse" = toggle; }; }; }; 83'' ; 84description = '' 85Stylesets added to {file}`$HOME/.config/aerc/stylesets/`. 86 87See {manpage}`aerc-stylesets(7)`. 88'' ; 89}; 90 91templates = mkOption { 92type = with types ; attrsOf lines ; 93default = { }; 94example = literalExpression '' 95{ new_message = "Hello!"; }; 96'' ; 97description = '' 98Templates added to {file}`$HOME/.config/aerc/templates/`. 99 100See {manpage}`aerc-templates(7)`. 101'' ; 102}; 103}; 104 105config = let 106joinCfg = cfgs :concatStringsSep "\n" ( filter ( v :v != "" ) cfgs ); 107 108toINI = conf :# quirk: global section is prepended w/o section heading 109let 110global = conf . global or { }; 111local = removeAttrs conf [ "global" ]; 112mkValueString = v : 113if isList v then # join with comma 114concatStringsSep "," ( map ( generators . mkValueStringDefault { }) v ) 115else 116generators . mkValueStringDefault { } v ; 117mkKeyValue = 118generators . mkKeyValueDefault { inherit mkValueString ; } " = " ; 119in joinCfg [ 120( generators . toKeyValue { inherit mkKeyValue ; } global ) 121( generators . toINI { inherit mkKeyValue ; } local ) 122]; 123 124mkINI = conf :if isString conf then conf else toINI conf ; 125 126mkStyleset = attrsets . mapAttrs' ( k :v : 127let value = if isString v then v else toINI { global = v ; }; 128in { 129name = " ${ configDir } /stylesets/ ${ k } " ; 130value . text = joinCfg [ header value ]; 131}); 132 133mkTemplates = attrsets . mapAttrs' ( k :v :{ 134name = " ${ configDir } /templates/ ${ k } " ; 135value . text = v ; 136}); 137 138primaryAccount = attrsets . filterAttrs ( _ :v :v . primary ) aerc-accounts ; 139otherAccounts = attrsets . filterAttrs ( _ :v :! v . primary ) aerc-accounts ; 140 141primaryAccountAccounts = mapAttrs accounts . mkAccount primaryAccount ; 142 143accountsExtraAccounts = mapAttrs accounts . mkAccount otherAccounts ; 144 145accountsExtraConfig = mapAttrs accounts . mkAccountConfig aerc-accounts ; 146 147accountsExtraBinds = mapAttrs accounts . mkAccountBinds aerc-accounts ; 148 149joinContextual = contextual :joinCfg ( map mkINI ( attrValues contextual )); 150 151isRecursivelyEmpty = x : 152if isAttrs x then 153all ( x :x == { } || isRecursivelyEmpty x ) ( attrValues x ) 154else 155false ; 156 157genAccountsConf = (( cfg . extraAccounts != "" && cfg . extraAccounts != { }) 158|| ! ( isRecursivelyEmpty accountsExtraAccounts ) 159|| ! ( isRecursivelyEmpty primaryAccountAccounts )); 160 161genAercConf = (( cfg . extraConfig != "" && cfg . extraConfig != { }) 162|| ! ( isRecursivelyEmpty accountsExtraConfig )); 163 164genBindsConf = (( cfg . extraBinds != "" && cfg . extraBinds != { }) 165|| ! ( isRecursivelyEmpty accountsExtraBinds )); 166 167header = '' 168# Generated by Home Manager. 169'' ; 170 171in mkIf cfg . enable { 172warnings = if genAccountsConf 173&& ( cfg . extraConfig . general . unsafe-accounts-conf or false ) == false then [ '' 174aerc: `programs.aerc.enable` is set, but `...extraConfig.general.unsafe-accounts-conf` is set to false or unset. 175This will prevent aerc from starting; see `unsafe-accounts-conf` in the man page aerc-config(5): 176> By default, the file permissions of accounts.conf must be restrictive and only allow reading by the file owner (0600). 177> Set this option to true to ignore this permission check. Use this with care as it may expose your credentials. 178These permissions are not possible with home-manager, since the generated file is in the nix-store (permissions 0444). 179Therefore, please set `programs.aerc.extraConfig.general.unsafe-accounts-conf = true`. 180This option is safe; if `passwordCommand` is properly set, no credentials will be written to the nix store. 181'' ] else 182[ ]; 183 184assertions = [{ 185assertion = let 186extraConfigSections = ( unique ( flatten 187( mapAttrsToList ( _ :v :attrNames v . aerc . extraConfig ) aerc-accounts ))); 188in extraConfigSections == [ ] || extraConfigSections == [ "ui" ]; 189message = '' 190Only the ui section of $XDG_CONFIG_HOME/aerc.conf supports contextual (per-account) configuration. 191Please configure it with accounts.email.accounts._.aerc.extraConfig.ui and move any other 192configuration to programs.aerc.extraConfig. 193'' ; 194}]; 195 196home . packages = [ cfg . package ]; 197 198home . file = { 199" ${ configDir } /accounts.conf" = mkIf genAccountsConf { 200text = joinCfg [ 201header 202( mkINI cfg . extraAccounts ) 203( mkINI primaryAccountAccounts ) 204( mkINI accountsExtraAccounts ) 205]; 206}; 207 208" ${ configDir } /aerc.conf" = mkIf genAercConf { 209text = joinCfg [ 210header 211( mkINI cfg . extraConfig ) 212( joinContextual accountsExtraConfig ) 213]; 214}; 215 216" ${ configDir } /binds.conf" = mkIf genBindsConf { 217text = joinCfg [ 218header 219( mkINI cfg . extraBinds ) 220( joinContextual accountsExtraBinds ) 221]; 222}; 223} // ( mkStyleset cfg . stylesets ) // ( mkTemplates cfg . templates ); 224}; 225}