# look at dev/get_keys.py to generate a default mappings { config, lib, pkgs, ... }: with lib; let cfg = config.programs.astroid; astroidAccounts = filterAttrs (n: v: v.notmuch.enable) (config.accounts.email.accounts); sendMailCommand = account: if account.astroid.sendMailCommand != null then account.astroid.sendMailCommand else if account.msmtp.enable then cfg.msmtp.sendCommand else ""; accountAttr = account: with account; { email = address; name = realName; sendmail = sendMailCommand account; additional_sent_tags = ""; default = if primary then "true" else "false"; save_drafts_to = folders.drafts; save_sent = "true"; save_sent_to = folders.sent; select_query = ""; } // optionalAttrs (signature.showSignature != "none") { signature_attach= if (signature.showSignature == "attach") then "true" else "false"; signature_default_on = if (signature.showSignature != "none") then "true" else "false"; signature_file = pkgs.writeText "signature.txt" signature.text; signature_file_markdown = "false"; signature_separate = "true"; # prepends '--\n' to the signature } // optionalAttrs (gpg != null) { always_gpg_sign = if gpg.signByDefault then "true" else "false"; gpgkey = gpg.key; } // astroid.extraConfig ; # See https://github.com/astroidmail/astroid/wiki/Configuration-Reference configFile = mailAccounts: let template = builtins.fromJSON (builtins.readFile ./astroid-config-template.json); astroidConfig = recursiveUpdate (template // { astroid.notmuch_config = "${config.xdg.configHome}/notmuch/notmuchrc"; accounts = mapAttrs (name: account: (accountAttr account) ) astroidAccounts; crypto.gpg.path = "${pkgs.gnupg}/bin/gpg"; } // cfg.extraConfig ) cfg.externalEditor; in builtins.toJSON astroidConfig; in { options = { programs.astroid = { enable = mkEnableOption "Astroid"; pollScript = mkOption { type = types.str; default = ""; example = '' mbsync gmail ''; description = '' Script to run to fetch/update mails. ''; }; externalEditor = mkOption { type = types.str; default = '' gvim -c 'set ft=mail' '+set fileencoding=utf-8' '+set ff=unix' '+set enc=utf-8' '+set fo+=w' %1 ''; # converts it into json that can be appended to the config apply = cmd: { editor = { "external_editor" = "true"; "cmd" = cmd; }; }; example = '' nvim-qt -- -c 'set ft=mail' '+set fileencoding=utf-8' '+set ff=unix' '+set enc=utf-8' '+set fo+=w' %1 ''; description = '' You can use %1 / %2 / %3 to refer respectively to: - filename - servername - socketid See ''; }; extraConfig = mkOption { type = types.attrs; default = {}; example = literalExample '' { poll.interval = 0; } ''; description = '' JSON config that will override the default astroid configuration. ''; }; }; }; config = mkIf cfg.enable { home.packages = [ pkgs.astroid ]; xdg.configFile."astroid/config".source = pkgs.runCommandNoCC "out.json" {x = configFile astroidAccounts; } ''echo "$x" | ${pkgs.jq}/bin/jq . > $out''; xdg.configFile."astroid/poll.sh" = { executable = true; text = "# Generated by home-manager\n" + cfg.pollScript; }; }; }