{
  lib,
  pkgs,
  config,
  ...
}: let
  cfg = config.roles.email;
  terminal = config.programs.terminal;
  termcmd = "${terminal}/bin/${terminal.pname}";
in {
  options.roles = with lib; {
    email = {
      enable = mkEnableOption "email settings";
      email = mkOption {
        description = "email address";
      };
      terminal = mkOption {
        type = types.bool;
        default = true;
      };
    };
  };

  imports = [./aerc/.];

  config = lib.mkIf cfg.enable {
    programs.scripts = [
      {
        name = "email";
        text = ''
          ${termcmd} -e aerc
        '';
        install = false;
        hotkeys = [{key = "E";}];
      }
    ];

    programs.git = {
      userName = config.home.userFullname;
      userEmail = cfg.email;
    };

    accounts.email.accounts.${cfg.email} = lib.mkIf cfg.terminal {
      notmuch.enable = true;
      neomutt.enable = true;
      aerc.enable = true;
      mbsync.enable = true;
      mbsync.create = "both";
      userName = cfg.email;
      realName = config.home.userFullname;
      address = cfg.email;
      primary = true;
      passwordCommand = "${pkgs.rbw}/bin/rbw get privateemail";
      smtp = {
        host = "mail.privateemail.com";
        port = 465;
        tls.enable = true;
      };
      imap = {
        host = "mail.privateemail.com";
        port = 993;
        tls.enable = true;
      };
    };

    programs.notmuch.enable = cfg.terminal;
    programs.neomutt = {
      enable = cfg.terminal;
      vimKeys = true;
      sort = "reverse-date";
      sidebar.enable = true;
    };
    programs.mbsync.enable = cfg.terminal;
    services.mbsync.enable = cfg.terminal;

    programs.aerc = {
      enable = cfg.terminal;
    };
  };
}
