{ config, pkgs, lib, ... }:
let
  passcmd = id: "${pkgs.pass}/bin/pass ${id}";
  ticks = "''";

in {
  # Maildir <-> Server communication
  # --------------------------------
  # mbsync: MailDir <-> IMAP
  # msmtp: sendmail interface sending mails through your provider.

  # client backend
  # --------------
  # notmuch: Tagdatabase for Emails
  # afew: Autotagger using notmuch
  # muchsync: notmuch database synctool

  # clients
  # -------
  # alot
  # astroid
  # neomutt

  home-manager.users.mainUser.accounts.email.accounts = {
    palo_van_dalo-gmx = {
      primary = false;
      address = "palo_van_dalo@gmx.de";
      aliases = [ ];
      realName = "Ingolf Wagner";
      userName = "palo_van_dalo@gmx.de";
      passwordCommand = passcmd "mail/gmx/palo_van_dalo@gmx.de";
      smtp = {
        host = "smtp.gmx.net";
        port = 465;
      };
      notmuch.enable = true;
      msmtp = { enable = true; };
    };
    ingolf-wagner-gmx = {
      primary = false;
      address = "ingolf.wagner@gmx.de";
      aliases = [ ];
      realName = "Ingolf Wagner";
      userName = "ingolf.wagner@gmx.de";
      passwordCommand = passcmd "mail/gmx/ingolf.wagner@gmx.de";
      smtp = {
        host = "smtp.gmx.net";
        port = 465;
      };
      notmuch.enable = true;
      msmtp = { enable = true; };
    };
    pali_palo = {
      primary = false;
      address = "pali_palo@web.de";
      aliases = [ ];
      realName = "Ingolf Wagner";
      userName = "pali_palo@web.de";
      passwordCommand = passcmd "mail/web.de/pali_palo@web.de";
      smtp = {
        host = "smtp.web.de";
        port = 465;
      };
      notmuch.enable = true;
      msmtp = {
        enable = true;
        # msmtp --serverinfo --tls --tls-certcheck=off -a gmail
        # tls.fingerprint = "77:2F:E1:F0:1C:9C:00:45:36:D5:0B:25:17:76:AC:7F:0E:79:68:27:8C:E9:E1:F6:BD:DF:F1:6F:1E:8C:85:18";
        # tls.fingerprint = "3F:B7:F9:7A:AC:9C:D7:C4:2E:8A:C1:F9:90:B5:D7:D1:8E:E2:F7:7D:9D:DB:FA:01:55:27:D2:79:5F:F8:C1:64";
      };
    };
    gmail = {
      # for google accounts you have to allow 'less secure apps' in accounts.google.com
      primary = true;
      address = "palipalo9@googlemail.com";
      aliases = [ ];
      realName = "Ingolf Wagner";
      userName = "palipalo9@googlemail.com";
      passwordCommand = passcmd "mail/gmail/palipalo9@googlemail.com";
      smtp = {
        host = "smtp.gmail.com";
        port = 465;
      };
      notmuch.enable = true;
      msmtp = {
        enable = true;
        # msmtp --serverinfo --tls --tls-certcheck=off -a gmail
        # tls.fingerprint = "77:2F:E1:F0:1C:9C:00:45:36:D5:0B:25:17:76:AC:7F:0E:79:68:27:8C:E9:E1:F6:BD:DF:F1:6F:1E:8C:85:18";
        # tls.fingerprint = "3F:B7:F9:7A:AC:9C:D7:C4:2E:8A:C1:F9:90:B5:D7:D1:8E:E2:F7:7D:9D:DB:FA:01:55:27:D2:79:5F:F8:C1:64";
      };
      gpg = {
        encryptByDefault = true;
        signByDefault = true;
        key = "42AC51C9482D0834CF488AF1389EC2D64AC71EAC";
      };
    };
    ingolf-wagner = {
      primary = false;
      address = "contact@ingolf-wagner.de";
      aliases = [ ];
      realName = "Ingolf Wagner";
      userName = "contact@ingolf-wagner.de";
      passwordCommand = passcmd "mail/siteground/contact@ingolf-wagner.de";
      notmuch.enable = true;
      smtp = {
        host = "securees5.sgcpanel.com";
        port = 587;
        tls.useStartTls = true;
      };
      msmtp = {
        enable = true;
        # msmtp --serverinfo --tls --tls-certcheck=off -a ingolf-wagner
        tls.fingerprint =
          "33:B9:77:B6:69:2C:0C:BC:ED:28:12:79:02:AC:00:4E:E3:EE:31:EF:5E:C8:12:1C:02:5D:13:0B:BA:C0:1E:5F";
      };
      gpg = {
        encryptByDefault = true;
        signByDefault = true;
        key = "42AC51C9482D0834CF488AF1389EC2D64AC71EAC";
      };
    };
    gaykraft = {
      primary = false;
      address = "root@gaykraft.com";
      aliases = [ ];
      realName = "Ingolf Wagner";
      userName = "root@gaykraft.com";
      passwordCommand = "mail/gaykraft.com/root@gaykraft.com";
      mbsync = {
        enable = true;
        create = "both";
      };
      smtp = {
        host = "mail.gaykraft.com";
        port = 587;
        tls.useStartTls = true;
      };
      msmtp = {
        enable = true;
        # msmtp --serverinfo --tls --tls-certcheck=off -a gaykraft
        tls.fingerprint =
          "08:42:73:72:3E:68:7D:55:89:7A:30:02:4B:CD:30:35:F1:6D:3E:7A:DD:A8:B6:84:67:25:37:F0:4F:7F:86:FE";
      };
      notmuch.enable = true;
    };
    c-base = {
      primary = false;
      address = "palo@c-base.org";
      aliases = [ ];
      realName = "Ingolf Wagner";
      userName = "palo";
      passwordCommand = passcmd "mail/c-base/palo@c-base.org";
      smtp = {
        host = "c-mail.c-base.org";
        port = 465;
      };
      notmuch.enable = true;
      msmtp = {
        enable = true;
        # msmtp --serverinfo --tls --tls-certcheck=off -a c-base
        tls.fingerprint =
          "9C:82:3B:0F:31:CE:1B:8E:96:00:CC:C9:FF:E7:BE:66:95:92:4F:22:DD:D6:2E:0E:1D:90:76:BE:8E:9E:8E:16";
      };
      gpg = {
        encryptByDefault = true;
        signByDefault = true;
        key = "42AC51C9482D0834CF488AF1389EC2D64AC71EAC";
      };
    };
  };

  # install mail programs
  home-manager.users.mainUser.programs.msmtp.enable = true;
  home-manager.users.mainUser.programs.notmuch.enable = true;

  # not working for some reason
  # maybe update home-manager
  #home-manager.users.mainUser.services.muchsync = {
  #  remotes = {
  #    server = {
  #      frequency = "*:0/10";
  #      remote.host = "mailfetcher@sterni.private";
  #    };
  #  };
  #};

  # configure astroid ui
  home-manager.users.mainUser.programs.astroid = {
    enable = true;
    extraConfig = { startup.queries.inbox = "tag:inbox AND NOT tag:killed"; };
    externalEditor =
      "${pkgs.neovim-qt}/bin/nvim-qt -- -c 'set ft=mail' '+set fileencoding=utf-8' '+set ff=unix' '+set enc=utf-8' '+set fo+=w' %1";
  };

  # configure alot
  home-manager.users.mainUser.programs.alot = {
    enable = true;
    #name    mono fg     mono bg   16c fg                        16c bg         256c fg                 256c bg
    #        |                 |   |                             |              |                             |
    #        v                 v   v                             v              v                             v
    #normal= 'bold,underline', '', 'light red, bold, underline', 'light green', 'light red, bold, underline', '#8f6'
    bindings = {
      envelope = { e = "edit"; };
      thread = {
        r = "reply --all";
        R = "reply --all";
        g = "reply";
        h = "toggleheaders";
      };
    };
    extraConfig = ''
      theme = "solarized_light_real"
      auto_remove_unread = True
      ask_subject = True
      handle_mouse = False
      initial_command = "search tag:inbox AND NOT tag:discourse"
      input_timeout = 0.3
      exclude_tags = "killed"
      prefer_plaintext = True
      thread_indent_replies = 4

      [tags]
        [[flagged]]
          translated = ⚑
          normal = ${ticks},${ticks},'light red',${ticks},'light red',${ticks}
          focus = ${ticks},${ticks},'light red',${ticks},'light red',${ticks}
        [[replied]]
          translated = ⏎
          normal = ${ticks},${ticks},'light red',${ticks},'light red',${ticks}
          focus = ${ticks},${ticks},'light red',${ticks},'light red',${ticks}
    '';
  };

  home-manager.users.mainUser.xdg.configFile."alot/themes/solarized_light_real".text =
    ''


      ###############################################################################
      # SOLARIZED LIGHT
      #
      # colour theme for alot. © 2012 Patrick Totzke, GNU GPL3+
      # http://ethanschoonover.com/solarized
      # https://github.com/pazz/alot
      ###############################################################################
      #
      # Define mappings from solarized colour names to urwid attribute names for 16
      # and 256 colour modes. These work well assuming you use the solarized term
      # colours via Xressources/Xdefaults. You might want to change this otherwise

      16_base3 = 'dark gray'
      16_base2 = 'black'
      16_base1 = 'light green'
      16_base0 = 'yellow'
      16_base00 = 'light blue'
      16_base01 = 'light cyan'
      16_base02 = 'light gray'
      16_base03 = 'white'
      16_yellow = 'brown'
      16_orange = 'light red'
      16_red = 'dark red'
      16_magenta = 'dark magenta'
      16_violet = 'light magenta'
      16_blue = 'dark blue'
      16_cyan = 'dark cyan'
      16_green = 'dark green'

      # Use a slightly different mapping here to be able to use "bold" in 256c mode

      256_base3 = 'dark gray'
      256_base2 = 'black'
      256_base1 = 'light green'
      256_base0 = 'yellow'
      256_base00 = 'g50'		#808080
      256_base01 = 'g52'		#848484 - approximates #8a8a8a
      256_base02 = 'light gray'
      256_base03 = 'white'
      256_yellow = 'brown'
      256_orange = 'light red'
      256_red = 'dark red'
      256_magenta = 'dark magenta'
      256_violet = 'light magenta'
      256_blue = 'dark blue'
      256_cyan = '#0aa'		#00afaf
      256_green = 'dark green'

      # This is the actual alot theme
      [global]
          footer = 'standout','default','%(16_base01)s','%(16_base2)s','%(256_base01)s','%(256_base2)s'
          body = 'default','default','%(16_base00)s','%(16_base3)s','%(256_base00)s','%(256_base3)s'
          notify_error = 'standout','default','%(16_base3)s','%(16_red)s','%(256_base3)s','%(256_red)s'
          notify_normal = 'default','default','%(16_base00)s','%(16_base2)s','%(256_base00)s','%(256_base2)s'
          prompt = 'default','default','%(16_base00)s','%(16_base2)s','%(256_base00)s','%(256_base2)s'
          tag = 'default','default','%(16_yellow)s','%(16_base3)s','%(256_yellow)s','%(256_base3)s'
          tag_focus = 'standout','default','%(16_base3)s','%(16_yellow)s','%(256_base3)s','%(256_yellow)s'
      [help]
          text = 'default','default','%(16_base00)s','%(16_base2)s','%(256_base00)s','%(256_base2)s'
          section = 'underline','default','%(16_base01)s,underline','%(16_base2)s','%(256_base01)s,underline','%(256_base2)s'
          title = 'standout','default','%(16_base01)s','%(16_base2)s','%(256_base01)s','%(256_base2)s'
      [namedqueries]
          line_focus = 'standout','default','%(16_base2)s','%(16_yellow)s','%(256_base2)s','%(256_yellow)s'
          line_even = 'default','default','%(16_base00)s','%(16_base3)s','%(256_base00)s','%(256_base3)s'
          line_odd = 'default','default','%(16_base00)s','%(16_base2)s','%(256_base00)s','%(256_base2)s'
      [taglist]
          line_focus = 'standout','default','%(16_base2)s','%(16_yellow)s','%(256_base2)s','%(256_yellow)s'
          line_even = 'default','default','%(16_base00)s','%(16_base3)s','%(256_base00)s','%(256_base3)s'
          line_odd = 'default','default','%(16_base00)s','%(16_base2)s','%(256_base00)s','%(256_base2)s'
      [bufferlist]
          line_focus = 'standout','default','%(16_base2)s','%(16_yellow)s','%(256_base2)s','%(256_yellow)s'
          line_even = 'default','default','%(16_base00)s','%(16_base3)s','%(256_base00)s','%(256_base3)s'
          line_odd = 'default','default','%(16_base00)s','%(16_base2)s','%(256_base00)s','%(256_base2)s'
      [thread]
          attachment = 'default','default','%(16_base00)s','%(16_base3)s','%(256_base00)s','%(256_base3)s'
          attachment_focus = 'underline','default','%(16_base2)s','%(16_yellow)s','%(256_base2)s','%(256_yellow)s'
          body = 'default','default','%(16_base00)s','%(16_base3)s','%(256_base00)s','%(256_base3)s'
          body_focus = 'default','default','%(16_base00)s','%(16_base3)s','%(256_base00)s','%(256_base2)s'
          arrow_bars = 'default','default','%(16_yellow)s','%(16_base3)s','%(256_yellow)s','%(256_base3)s'
          arrow_heads = 'default','default','%(16_yellow)s','%(16_base3)s','%(256_yellow)s','%(256_base3)s'
          header = 'default','default','%(16_base00)s','%(16_base2)s','%(256_base00)s','%(256_base2)s'
          header_key = 'default','default','%(16_magenta)s','%(16_base2)s','%(256_magenta)s','%(256_base2)s'
          header_value = 'default','default','%(16_blue)s','%(16_base2)s','%(256_blue)s','%(256_base2)s'
          [[summary]]
              even = 'default','default','%(16_base00)s','%(16_base2)s','%(256_base00)s','%(256_base2)s'
              focus = 'standout','default','%(16_base3)s','%(16_yellow)s','%(256_base3)s','%(256_yellow)s'
              odd = 'default','default','%(16_base00)s','%(16_base3)s','%(256_base00)s','%(256_base3)s'
      [envelope]
          body = 'default','default','%(16_base00)s','%(16_base3)s','%(256_base00)s','%(256_base3)s'
          header = 'default','default','%(16_base00)s','%(16_base2)s','%(256_base00)s','%(256_base2)s'
          header_key = 'default','default','%(16_orange)s','%(16_base2)s','%(256_orange)s','%(256_base2)s'
          header_value = 'default','default','%(16_violet)s','%(16_base2)s','%(256_violet)s','%(256_base2)s'
      [search]
          [[threadline]]
              normal = 'default','default','%(16_base01)s','%(16_base3)s','%(256_base01)s','%(256_base3)s'
              focus = 'default','default','%(16_base01)s,underline','%(16_base3)s','%(256_base01)s,underline','%(256_base3)s'
              parts = mailcount,date,tags,authors,subject
              [[[date]]]
                  normal = 'default','default','%(16_base01)s','%(16_base3)s','%(256_base01)s','%(256_base3)s'
                  focus = 'default','default','%(16_base01)s,underline','%(16_base3)s','%(256_base01)s,underline','%(256_base3)s'
              [[[mailcount]]]
                  normal = 'default','default','%(16_base01)s','%(16_base3)s','%(256_base01)s','%(256_base3)s'
                  focus = 'default','default','%(16_base01)s,underline','%(16_base3)s','%(256_base01)s,underline','%(256_base3)s'
              [[[tags]]]
                  normal = 'bold','default','%(16_yellow)s','%(16_base3)s','%(256_yellow)s','%(256_base3)s'
                  focus = 'bold','default','%(16_yellow)s,underline','%(16_base3)s','%(256_yellow)s,underline','%(256_base3)s'
              [[[authors]]]
                  normal = 'default,underline','default','%(16_blue)s','%(16_base3)s','%(256_blue)s','%(256_base3)s'
                  focus = 'default,underline','default','%(16_blue)s,underline','%(16_base3)s','%(256_blue)s,underline','%(256_base3)s'
                  width = 'fit',0,30
              [[[subject]]]
                  normal = 'default','default','%(16_base00)s','%(16_base3)s','%(256_base00)s','%(256_base3)s'
                  focus = 'default','default','%(16_base00)s,underline','%(16_base3)s','%(256_base00)s,underline','%(256_base3)s'
                  width = 'weight',1
              [[[content]]]
                  normal = 'default','default','%(16_base1)s','%(16_base3)s','%(256_base1)s','%(256_base3)s'
                  focus = 'default','default','%(16_base1)s,underline','%(16_base3)s','%(256_base1)s,underline','%(256_base3)s'
          [[threadline-unread]]
              normal = 'default','default','%(16_base01)s,bold','%(16_base2)s','%(256_base01)s,bold','%(256_base2)s'
              tagged_with = 'unread'
              [[[date]]]
                  normal = 'default','default','%(16_base01)s,bold','%(16_base2)s','%(256_base01)s,bold','%(256_base2)s'
              [[[mailcount]]]
                  normal = 'default','default','%(16_base01)s,bold','%(16_base2)s','%(256_base01)s,bold','%(256_base2)s'
              [[[tags]]]
                  normal = 'bold','default','%(16_yellow)s','%(16_base2)s','%(256_yellow)s','%(256_base2)s'
              [[[authors]]]
                  normal = 'default,underline','default','%(16_violet)s','%(16_base2)s','%(256_violet)s','%(256_base2)s'
              [[[subject]]]
                  normal = 'default','default','%(16_base02)s,bold','%(16_base2)s','%(256_base02)s,bold','%(256_base2)s'
              [[[content]]]
                  normal = 'default','default','%(16_base1)s,bold','%(16_base2)s','%(256_base1)s,bold','%(256_base2)s'
        '';

  # enable html emails
  home-manager.users.mainUser.home.file.".mailcap".text = ''
    text/html; ${pkgs.elinks}/bin/elinks -dump ; copiousoutput;
  '';

  home-manager.users.mainUser.home.file.".muttrc".text = ''
    # gpg
    set crypt_use_gpgme=yes
    set crypt_autosign=yes
    set crypt_verify_sig=yes
    set crypt_replysign=yes
    set crypt_replyencrypt=yes
    set crypt_replysignencrypted=yes

    # mutt is not smart enough to see if I have a key or not
    #set crypt_autoencrypt = yes

    set pgp_check_gpg_decrypt_status_fd
    set pgp_use_gpg_agent = yes
    set pgp_self_encrypt = yes # needs default key
    set pgp_default_key = 42AC51C9482D0834CF488AF1389EC2D64AC71EAC

    # read html mails
    auto_view text/html
    set mailcap_path = ~/.mailcap

    # notmuch
    set nm_default_uri="notmuch://$HOME/Maildir" # path to the maildir
    set nm_record = yes
    set nm_record_tags = "-inbox me archive"
    set virtual_spoolfile=yes                    # enable virtual folders

    set sendmail=${
      pkgs.writers.writeBash "msmtp" ''
        ${pkgs.coreutils}/bin/tee >(${pkgs.notmuch}/bin/notmuch insert --create-folder +sent) | \
        ${pkgs.msmtp}/bin/msmtpq "$@"
      ''
    }

    set from="contact@ingolf-wagner.de"
    #alternates ^.*@ingolf-wagner\.de$ ^.*@.*\.r$
    set use_from=yes
    set envelope_from=yes
    set reverse_name

    set sort=threads

    set index_format="${
      pkgs.writers.writeDash "mutt-index" ''
        # http://www.mutt.org/doc/manual/#formatstrings
        recipent="$(echo $1 | sed 's/[^,]*<\([^>]*\)[^,]*/ \1/g')"
        #     output to mutt
        #           V
        echo "%4C %Z %?GI?%GI& ? %[%y-%m-%d %H:%M] %-20.20a %?M?(%3M)& ? %s %> $recipent %?g?%g?%"
        # args to mutt-index dash script
        # V
      ''
    } %r |"

    virtual-mailboxes "INBOX" "notmuch://?query=(tag:inbox or (tag:sent and not tag:archive)) AND NOT tag:discourse"
    virtual-mailboxes "Unread" "notmuch://?query=tag:unread"
    virtual-mailboxes "Discourse" "notmuch://?query=tag:discourse"
    virtual-mailboxes "Starred" "notmuch://?query=tag:flagged"
    virtual-mailboxes "Archive" "notmuch://?query=tag:archive"
    virtual-mailboxes "Sent" "notmuch://?query=tag:sent"
    virtual-mailboxes "Junk" "notmuch://?query=tag:junk"
    virtual-mailboxes "All" "notmuch://?query=*"

    tag-transforms "junk"     "k" \
                   "unread"   "u" \
                   "replied"  "↻" \
                   "TODO"     "T" \

    # notmuch bindings
    bind index \\\\ noop
    bind pager \\ noop
    bind pager \\\\ noop
    macro index \\\\ "<vfolder-from-query>"                   # looks up a hand made query
    macro pager \\\\ "<vfolder-from-query>"                   # looks up a hand made query

    macro index + "<modify-labels>+flagged\n<sync-mailbox>"         # tag as starred
    macro pager + "<modify-labels>+flagged\n<sync-mailbox>"         # tag as starred
    macro index ! "<modify-labels>+flagged\n<sync-mailbox>"         # tag as starred
    macro pager ! "<modify-labels>+flagged\n<sync-mailbox>"         # tag as starred
    macro index - "<modify-labels>-flagged\n<sync-mailbox>"         # tag as unstarred
    macro pager - "<modify-labels>-flagged\n<sync-mailbox>"         # tag as unstarred

    # Address Book
    # ------------
    set query_command="${pkgs.notmuch-addrlookup}/bin/notmuch-addrlookup --mutt '%s'"

    # keys bindings
    # -------------
    #killed
    bind index d noop
    bind pager d noop

    bind index S noop
    bind index s noop
    bind pager S noop
    bind pager s noop
    macro index S "<modify-labels-then-hide>-inbox -unread +junk\n" # tag as Junk mail
    macro index s "<modify-labels>-junk\n" # tag as Junk mail
    macro pager S "<modify-labels-then-hide>-inbox -unread +junk\n" # tag as Junk mail
    macro pager s "<modify-labels>-junk\n" # tag as Junk mail


    bind index A noop
    bind index a noop
    bind pager A noop
    bind pager a noop
    macro index A "<modify-labels>+archive -unread -inbox\n"  # tag as Archived
    macro index a "<modify-labels>-archive\n"  # tag as Archived
    macro pager A "<modify-labels>+archive -unread -inbox\n"  # tag as Archived
    macro pager a "<modify-labels>-archive\n"  # tag as Archived

    bind index U noop
    bind index u noop
    bind pager U noop
    bind pager u noop
    macro index u "<modify-labels>+unread\n"
    macro index U "<modify-labels>-unread\n"
    macro pager u "<modify-labels>+unread\n"
    macro pager U "<modify-labels>-unread\n"

    bind index l noop
    bind pager l noop
    macro index l "<modify-labels>" # set tags manual
    macro pager l "<modify-labels>" # set tags manual

    # taskwarrior
    # -----------
    # from https://www.nixternal.com/mark-e-mails-in-mutt-as-tasks-in-taskwarrior/
    bind index t noop
    bind pager t noop
    bind index T noop
    bind pager T noop
    macro index T "<pipe-message>${
      pkgs.writers.writeDash "mutt2task" ''
        ${pkgs.taskwarrior}/bin/task add +email E-mail: $( ${pkgs.gnugrep}/bin/grep 'Subject' $* | awk -F: '{print $2}' )
      ''
    }<enter>"

    # top index bar in email view
    set pager_index_lines=7
    # top_index_bar toggle
    macro pager ,@1 "<enter-command> set pager_index_lines=0; macro pager ] ,@2 'Toggle indexbar<Enter>"
    macro pager ,@2 "<enter-command> set pager_index_lines=3; macro pager ] ,@3 'Toggle indexbar<Enter>"
    macro pager ,@3 "<enter-command> set pager_index_lines=7; macro pager ] ,@1 'Toggle indexbar<Enter>"
    macro pager ] ,@1 'Toggle indexbar

    # sidebar
    # -------
    set sidebar_width   = 20
    set sidebar_visible = yes               # set to "no" to disable sidebar view at startup
    color sidebar_new yellow default
    # sidebar bindings
    bind index <left> sidebar-prev          # got to previous folder in sidebar
    bind index <right> sidebar-next         # got to next folder in sidebar
    bind index <space> sidebar-open         # open selected folder from sidebar

    # sidebar toggle
    #macro index,pager ,@) "<enter-command> set sidebar_visible=no; macro index,pager [ ,@( 'Toggle sidebar'<Enter>"
    #macro index,pager ,@( "<enter-command> set sidebar_visible=yes; macro index,pager [ ,@) 'Toggle sidebar'<Enter>"
    #macro index,pager [ ,@( 'Toggle sidebar'      # toggle the sidebar
  '';

  environment.systemPackages = let

    mailSync = pkgs.writers.writeDashBin "mail-sync" ''
      ${pkgs.muchsync}/bin/muchsync mailfetcher@workhorse.private --nonew
    '';
    mailSend = pkgs.writers.writeDashBin "mail-send" ''
      ${pkgs.msmtp}/bin/msmtp-queue -r
    '';
    mailView = pkgs.writers.writeDashBin "mail-view" ''
      ${pkgs.neomutt}/bin/neomutt"$@"
    '';
    mutt = pkgs.writers.writeDashBin "mutt" ''
      ${pkgs.neomutt}/bin/neomutt"$@"
    '';
    mail = pkgs.writers.writeDashBin "mail" ''
      ${mailSync}/bin/mail-sync
      ${mailView}/bin/mail-view
      ${mailSend}/bin/mail-send
      ${mailSync}/bin/mail-sync
    '';
  in [
    pkgs.notmuch
    pkgs.alot
    pkgs.muchsync
    mail
    mailSync
    mailView
    mailSend
    pkgs.neomutt
    mutt
  ];

}