diff options
Diffstat (limited to 'home-manager/modules/programs/mbsync.nix')
-rw-r--r-- | home-manager/modules/programs/mbsync.nix | 198 |
1 files changed, 87 insertions, 111 deletions
diff --git a/home-manager/modules/programs/mbsync.nix b/home-manager/modules/programs/mbsync.nix index 908a1add715..6ade10986fe 100644 --- a/home-manager/modules/programs/mbsync.nix +++ b/home-manager/modules/programs/mbsync.nix @@ -4,22 +4,21 @@ with lib; let - dag = config.lib.dag; - cfg = config.programs.mbsync; # Accounts for which mbsync is enabled. mbsyncAccounts = filter (a: a.mbsync.enable) (attrValues config.accounts.email.accounts); - genTlsConfig = tls: { - SSLType = - if !tls.enable then "None" - else if tls.useStartTls then "STARTTLS" - else "IMAPS"; - } - // - optionalAttrs (tls.enable && tls.certificatesFile != null) { + genTlsConfig = tls: + { + SSLType = if !tls.enable then + "None" + else if tls.useStartTls then + "STARTTLS" + else + "IMAPS"; + } // optionalAttrs (tls.enable && tls.certificatesFile != null) { CertificateFile = toString tls.certificatesFile; }; @@ -32,79 +31,61 @@ let genSection = header: entries: let - escapeValue = escape [ "\"" ]; + escapeValue = escape [ ''"'' ]; hasSpace = v: builtins.match ".* .*" v != null; genValue = n: v: - if isList v - then concatMapStringsSep " " (genValue n) v - else if isBool v then (if v then "yes" else "no") - else if isInt v then toString v - else if isString v && hasSpace v then "\"${escapeValue v}\"" - else if isString v then v + if isList v then + concatMapStringsSep " " (genValue n) v + else if isBool v then + (if v then "yes" else "no") + else if isInt v then + toString v + else if isString v && hasSpace v then + ''"${escapeValue v}"'' + else if isString v then + v else - let prettyV = lib.generators.toPretty {} v; - in throw "mbsync: unexpected value for option ${n}: '${prettyV}'"; - in - '' - ${header} - ${concatStringsSep "\n" - (mapAttrsToList (n: v: "${n} ${genValue n v}") entries)} - ''; - - genAccountConfig = account: with account; - genSection "IMAPAccount ${name}" ( - { - Host = imap.host; - User = userName; - PassCmd = toString passwordCommand; - } - // genTlsConfig imap.tls - // optionalAttrs (imap.port != null) { Port = toString imap.port; } - // mbsync.extraConfig.account - ) - + "\n" - + genSection "IMAPStore ${name}-remote" ( - { - Account = name; - } - // mbsync.extraConfig.remote - ) - + "\n" - + genSection "MaildirStore ${name}-local" ( - { - Path = "${maildir.absPath}/"; - Inbox = "${maildir.absPath}/${folders.inbox}"; - SubFolders = "Verbatim"; - } - // optionalAttrs (mbsync.flatten != null) { Flatten = mbsync.flatten; } - // mbsync.extraConfig.local - ) - + "\n" - + genSection "Channel ${name}" ( - { - Master = ":${name}-remote:"; - Slave = ":${name}-local:"; - Patterns = mbsync.patterns; - Create = masterSlaveMapping.${mbsync.create}; - Remove = masterSlaveMapping.${mbsync.remove}; - Expunge = masterSlaveMapping.${mbsync.expunge}; - SyncState = "*"; - } - // mbsync.extraConfig.channel - ) - + "\n"; + let prettyV = lib.generators.toPretty { } v; + in throw "mbsync: unexpected value for option ${n}: '${prettyV}'"; + in '' + ${header} + ${concatStringsSep "\n" + (mapAttrsToList (n: v: "${n} ${genValue n v}") entries)} + ''; + + genAccountConfig = account: + with account; + genSection "IMAPAccount ${name}" ({ + Host = imap.host; + User = userName; + PassCmd = toString passwordCommand; + } // genTlsConfig imap.tls + // optionalAttrs (imap.port != null) { Port = toString imap.port; } + // mbsync.extraConfig.account) + "\n" + + genSection "IMAPStore ${name}-remote" + ({ Account = name; } // mbsync.extraConfig.remote) + "\n" + + genSection "MaildirStore ${name}-local" ({ + Path = "${maildir.absPath}/"; + Inbox = "${maildir.absPath}/${folders.inbox}"; + SubFolders = "Verbatim"; + } // optionalAttrs (mbsync.flatten != null) { Flatten = mbsync.flatten; } + // mbsync.extraConfig.local) + "\n" + genSection "Channel ${name}" ({ + Master = ":${name}-remote:"; + Slave = ":${name}-local:"; + Patterns = mbsync.patterns; + Create = masterSlaveMapping.${mbsync.create}; + Remove = masterSlaveMapping.${mbsync.remove}; + Expunge = masterSlaveMapping.${mbsync.expunge}; + SyncState = "*"; + } // mbsync.extraConfig.channel) + "\n"; genGroupConfig = name: channels: let genGroupChannel = n: boxes: "Channel ${n}:${concatStringsSep "," boxes}"; - in - concatStringsSep "\n" ( - [ "Group ${name}" ] ++ mapAttrsToList genGroupChannel channels - ); - -in + in concatStringsSep "\n" + ([ "Group ${name}" ] ++ mapAttrsToList genGroupChannel channels); -{ +in { options = { programs.mbsync = { enable = mkEnableOption "mbsync IMAP4 and Maildir mailbox synchronizer"; @@ -119,7 +100,7 @@ in groups = mkOption { type = types.attrsOf (types.attrsOf (types.listOf types.str)); - default = {}; + default = { }; example = literalExample '' { inboxes = { @@ -144,45 +125,40 @@ in }; config = mkIf cfg.enable { - assertions = - let - checkAccounts = pred: msg: - let - badAccounts = filter pred mbsyncAccounts; - in { - assertion = badAccounts == []; - message = "mbsync: ${msg} for accounts: " - + concatMapStringsSep ", " (a: a.name) badAccounts; - }; - in - [ - (checkAccounts (a: a.maildir == null) "Missing maildir configuration") - (checkAccounts (a: a.imap == null) "Missing IMAP configuration") - (checkAccounts (a: a.passwordCommand == null) "Missing passwordCommand") - (checkAccounts (a: a.userName == null) "Missing username") - ]; + assertions = let + checkAccounts = pred: msg: + let badAccounts = filter pred mbsyncAccounts; + in { + assertion = badAccounts == [ ]; + message = "mbsync: ${msg} for accounts: " + + concatMapStringsSep ", " (a: a.name) badAccounts; + }; + in [ + (checkAccounts (a: a.maildir == null) "Missing maildir configuration") + (checkAccounts (a: a.imap == null) "Missing IMAP configuration") + (checkAccounts (a: a.passwordCommand == null) "Missing passwordCommand") + (checkAccounts (a: a.userName == null) "Missing username") + ]; home.packages = [ cfg.package ]; programs.notmuch.new.ignore = [ ".uidvalidity" ".mbsyncstate" ]; - home.file.".mbsyncrc".text = - let - accountsConfig = map genAccountConfig mbsyncAccounts; - groupsConfig = mapAttrsToList genGroupConfig cfg.groups; - in - concatStringsSep "\n" ( - [ "# Generated by Home Manager.\n" ] - ++ optional (cfg.extraConfig != "") cfg.extraConfig - ++ accountsConfig - ++ groupsConfig - ) + "\n"; - - home.activation.createMaildir = - dag.entryBetween [ "linkGeneration" ] [ "writeBoundary" ] '' - $DRY_RUN_CMD mkdir -m700 -p $VERBOSE_ARG ${ - concatMapStringsSep " " (a: a.maildir.absPath) mbsyncAccounts - } - ''; + home.file.".mbsyncrc".text = let + accountsConfig = map genAccountConfig mbsyncAccounts; + groupsConfig = mapAttrsToList genGroupConfig cfg.groups; + in concatStringsSep "\n" (['' + # Generated by Home Manager. + ''] ++ optional (cfg.extraConfig != "") cfg.extraConfig ++ accountsConfig + ++ groupsConfig) + "\n"; + + home.activation = mkIf (mbsyncAccounts != [ ]) { + createMaildir = + hm.dag.entryBetween [ "linkGeneration" ] [ "writeBoundary" ] '' + $DRY_RUN_CMD mkdir -m700 -p $VERBOSE_ARG ${ + concatMapStringsSep " " (a: a.maildir.absPath) mbsyncAccounts + } + ''; + }; }; } |