fork/home-manager
git clone https://git.julia.blue/fork/home-manager
455cc8cf
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5let 6 7cfg = config . programs . offlineimap ; 8 9accounts = filter ( a :a . offlineimap . enable ) 10( attrValues config . accounts . . accounts ); 11 12toIni = generators . toINI { 13mkKeyValue = key :value : 14let 15value' = 16( if isBool value then lib . hm . booleans . yesNo else toString ) value ; 17in " ${ key } = ${ value' } " ; 18}; 19 20accountStr = account : 21with account ; 22let 23postSyncHook = optionalAttrs ( offlineimap . postSyncHookCommand != "" ) { 24postsynchook = pkgs . writeShellScriptBin "postsynchook" 25offlineimap . postSyncHookCommand + "/bin/postsynchook" ; 26}; 27 28localType = 29if account . flavor == "gmail.com" then "GmailMaildir" else "Maildir" ; 30 31remoteType = if account . flavor == "gmail.com" then "Gmail" else "IMAP" ; 32 33remoteHost = 34optionalAttrs ( imap . host != null ) { remotehost = imap . host ; }; 35 36remotePort = 37optionalAttrs (( imap . port or null ) != null ) { remoteport = imap . port ; }; 38 39ssl = if imap . tls . enable then { 40ssl = true ; 41sslcacertfile = toString imap . tls . certificatesFile ; 42starttls = imap . tls . useStartTls ; 43} else { 44ssl = false ; 45}; 46 47remotePassEval = 48let arglist = concatMapStringsSep "," ( x :"' ${ x } '" ) passwordCommand ; 49in optionalAttrs ( passwordCommand != null ) { 50remotepasseval = ''get_pass(" ${ name } ", [ ${ arglist } ]).strip(b"\n")'' ; 51}; 52in toIni { 53"Account ${ name } " = { 54localrepository = " ${ name } -local" ; 55remoterepository = " ${ name } -remote" ; 56} // postSyncHook // offlineimap . extraConfig . account ; 57 58"Repository ${ name } -local" = { 59type = localType ; 60localfolders = maildir . absPath ; 61} // offlineimap . extraConfig . local ; 62 63"Repository ${ name } -remote" = { 64type = remoteType ; 65remoteuser = userName ; 66} // remoteHost // remotePort // remotePassEval // ssl 67// offlineimap . extraConfig . remote ; 68}; 69 70extraConfigType = with types ; attrsOf ( either ( either str int ) bool ); 71 72in { 73options = { 74programs . offlineimap = { 75enable = mkEnableOption "OfflineIMAP" ; 76 77package = mkPackageOption pkgs "offlineimap" { 78example = '' 79pkgs.offlineimap.overridePythonAttrs ( old: { 80propagatedBuildInputs = old.propagatedBuildInputs 81++ (with pkgs.python3Packages; [ 82requests_oauthlib xdg gpgme]); 83})'' ; 84extraDescription = "Can be used to specify extensions." ; 85}; 86 87pythonFile = mkOption { 88type = types . lines ; 89default = '' 90import subprocess 91 92def get_pass(service, cmd): 93return subprocess.check_output(cmd, ) 94'' ; 95description = '' 96Python code that can then be used in other parts of the 97configuration. 98'' ; 99}; 100 101extraConfig . general = mkOption { 102type = extraConfigType ; 103default = { }; 104example = { 105maxage = 30 ; 106ui = "blinkenlights" ; 107}; 108description = '' 109Extra configuration options added to the 110{option}`general` section. 111'' ; 112}; 113 114extraConfig . default = mkOption { 115type = extraConfigType ; 116default = { }; 117example = { gmailtrashfolder = "[Gmail]/Papierkorb" ; }; 118description = '' 119Extra configuration options added to the 120{option}`DEFAULT` section. 121'' ; 122}; 123 124extraConfig . mbnames = mkOption { 125type = extraConfigType ; 126default = { }; 127example = literalExpression '' 128{ 129filename = "~/.config/mutt/mailboxes"; 130header = "'mailboxes '"; 131peritem = "'+%(accountname)s/%(foldername)s'"; 132sep = "' '"; 133footer = "'\\n'"; 134} 135'' ; 136description = '' 137Extra configuration options added to the 138`mbnames` section. 139'' ; 140}; 141}; 142 143accounts . . accounts = mkOption { 144type = with types ; 145attrsOf ( submodule ( import ./offlineimap-accounts.nix )); 146}; 147}; 148 149config = mkIf cfg . enable { 150home . packages = [ cfg . package ]; 151 152xdg . configFile . "offlineimap/get_settings.py" . text = cfg . pythonFile ; 153xdg . configFile . "offlineimap/get_settings.pyc" . source = " ${ 154pkgs . runCommandLocal "get_settings-compile" { 155nativeBuildInputs = [ cfg . package ]; 156pythonFile = cfg . pythonFile ; 157passAsFile = [ "pythonFile" ]; 158} '' 159mkdir -p $ out /bin 160cp $ pythonFilePath $ out /bin/get_settings.py 161python -m py_compile $ out /bin/get_settings.py 162'' 163} /bin/get_settings.pyc" ; 164 165xdg . configFile . "offlineimap/config" . text = '' 166# Generated by Home Manager. 167# See https://github.com/OfflineIMAP/offlineimap/blob/master/offlineimap.conf 168# for an exhaustive list of options. 169'' + toIni ({ 170general = { 171accounts = concatMapStringsSep "," ( a :a . name ) accounts ; 172pythonfile = " ${ config . xdg . configHome } /offlineimap/get_settings.py" ; 173metadata = " ${ config . xdg . dataHome } /offlineimap" ; 174} // cfg . extraConfig . general ; 175} // optionalAttrs ( cfg . extraConfig . mbnames != { }) { 176mbnames = { enabled = true ; } // cfg . extraConfig . mbnames ; 177} // optionalAttrs ( cfg . extraConfig . default != { }) { 178DEFAULT = cfg . extraConfig . default ; 179}) + "\n" + concatStringsSep "\n" ( map accountStr accounts ); 180}; 181}