{ pkgs, ... }:

let
  solarized-xresources = ./xresources;
  homedir = builtins.getEnv "HOME";
in
{
  accounts = {
    email = {
      maildirBasePath = "${homedir}/Mail";
      accounts = {
        "ben@bsima.me" = {
          primary = true;
          realName = "Ben Sima";
          address = "ben@bsima.me";
          folders = {
            inbox = "INBOX";
          };
          imap = {
            host = "mail.bsima.me";
            port = 993;
          };
          smtp = {
            host = "mail.bsima.me";
            port = 587;
          };
          userName = "ben@bsima.me";
          mbsync = {
            enable = true;
            create = "both";
            expunge = "none";
          };
          notmuch.enable = true;
          msmtp.enable = false;
          passwordCommand = "pass bnet/helium/mail/ben";
        };
      };
    };
  };
  home = {
    packages = import ./packages.nix { inherit pkgs; };
    sessionVariables = {
      EDITOR = "vim";
      LANG = "en_US.UTF-8";
      PATH = "${homedir}/bin:${homedir}/.cabal/bin:${homedir}/.local/bin:$PATH";
      PAGER = "less";
    };
    file = {
      mutt = {
        text = (builtins.readFile ./muttrc) +
          (builtins.readFile ./mutt/solarized.muttrc);
        target = ".muttrc";
      };
      mailcap = {
        source = ./mailcap;
        target = ".mailcap";
      };
      tmux = {
        source = ./tmux;
        target = ".tmux.conf";
      };
    };
  };

  fonts.fontconfig.enableProfileFonts = true;

  xresources = {
    properties = {
      "XTerm*font" = "*-fixed-*-*-*-*-*";
      "XTerm*faceName" = "mononoki";
      "XTerm*faceSize" = "10";
      "XTerm*termName" = "xterm-256color";
      "XTerm*metaSendsEscape" = true;
    };
    extraConfig = builtins.readFile(solarized-xresources + "/Xresources.dark");
  };

  services = {
    network-manager-applet.enable = false;

    polybar = {
      enable = false;
      config = {
        "bar/top" = {
          font-0 = "mononoki:size-10";
          monitor = "\${env:MONITOR:eDP1}";
          monitor-fallback = "HDMI1";
          width = "100%";
          height = "2%";
          radius = 0;
          background = "#fdf6e3"; # solarized base3
          foreground = "#657b83"; # solarized base00
          tray-position = "right";
          tray-detached = false;
          tray-maxsize = 16;
          tray-transparent = false;
          tray-background = "#fdf6e3";
          tray-offset-x = 0;
          tray-offset-y = 0;
          tray-padding = 0;
          tray-scale = 1.0;
          module-margin = 4;
          modules-center = "date";
          modules-right = "battery";
        };
        "module/date" = {
          type = "internal/date";
          internal = 5;
          date = "%Y.%m.%d";
          time = "%H.%M";
          label = "%date%..%time%";
        };
        "module/battery" = {
          type = "internal/battery";
          battery = "BAT0";
          adapter = "AC";
          full-at = 99;
        };
        "module/ewmh" = {
          type = "internal/xworkspaces";
          pin-workspaces = true;
          enable-click = true;
          enable-scroll = false;
        };
      };
      script = ''
        #!/usr/bin/env sh
        systemctl --user daemon-reload
        polybar top &
      '';
    };

    taffybar = {
      enable = false;
    };

    redshift = {
      enable = false;
      latitude = "33.044444";
      longitude = "-117.271667";
    };

    gpg-agent = {
      enable = true;
      defaultCacheTtl = 600;
      enableSshSupport = true;
      extraConfig = ''
        allow-emacs-pinentry
        pinentry-program /home/ben/.nix-profile/bin/pinentry-tty
      '';
    };
  };

  xsession = {
    enable = false;
    windowManager = {
      xmonad = {
        enable = false;
        extraPackages = hpkgs: [
          hpkgs.xmonad-contrib
          hpkgs.xmonad-extras
          hpkgs.monad-logger
          hpkgs.taffybar
        ];
        enableContribAndExtras = true;
        config = ./xmonad.hs;
      };
    };
  };

  programs = {
    home-manager = {
      enable = true;
      path = "./home-manager";
    };

    direnv = {
      enable = true;
    };

    vim = {
      enable = true;
      plugins = [
        "ctrlp"
        "fugitive"
        "editorconfig-vim"
        "gitgutter"
        "surround"
        "vim-colorschemes"
      ];
      extraConfig = builtins.readFile ./vimrc;
    };

    git = {
      enable = true;
      userName  = "Ben Sima";
      userEmail = "ben@bsima.me";
      ignores = [ "*~" "*.swp" ];
      package = pkgs.gitAndTools.gitFull;
      aliases = {
        authors = "shortlog -s -n";
      };
      extraConfig = ''
        [push]
        default = simple

        [commit]
        template = ~/.config/nixpkgs/git-commit-template

        [sendemail]
        smtpuser = ben@bsima.me
        smptserverport = 587
        smptserver = mail.bsima.me
        chainreplyto = false
        composeencoding = UTF-8
      '';
    };

    bash = {
      enable = true;
      initExtra = ''
        DIR=~/.nix-profile/etc/profile.d
        [[ -f "$DIR/nix.sh" ]] && . "$DIR/nix.sh"
        [[ -f "$DIR/hm-session-vars.sh" ]] && . "$DIR/hm-session-vars.sh"
      '';
      shellAliases = {
        #z = "fasd_cd -d"; # cd, same functionality as j in autojump
        #zz = "fasd_cd -d -i"; # cd with interactive selection
          a = "fasd -a"; # any
          add = "git add --ignore-removal";
          ci = "git commit";
          d = "fasd -d"; # directory
          day = "date +%a";
          ddate = "date +%Y.%m.%d";
          dday = "date +%A";
          et = "emacs -nw"; # emacs in a terminal
        f = "fasd -f"; # file
        fetch = "git fetch";
        g = "grep -in";
        ga = "git add -A";
        gb = "git branch";
        gc = "git commit";
        gca = "git commit -a";
        gco = "git checkout";
        gd = "git diff";
        gl = "git pull --prune";
        glog = ''git log --decorate --all --graph --pretty=format:"%Cred%h%Creset %an: %s - %Creset %C(yellow)%d%Creset %Cgreen(%cr)%Creset" --abbrev-commit --date=relative'';
        gp = "git push origin HEAD";
        gs = "git status -sb";
        hs = "home-manager switch";
        ll = "ls -l";
        pull = "git pull";
        push = "git push";
        rm = "rm -i";
        s = "fasd -si"; # show / search / select
        sd = "fasd -sid"; # interactive directory selection
        sf = "fasd -sif"; # interactive file selection
        showpath = "echo $PATH | sed 's/:/\n/g'";
        tdate = "date +%Y.%m.%d..%H.%M";
        ttime = "date +%H.%M";
        typeless = "history | tail -n 20000 | sed 's/.*  //' | sort | uniq -c | sort -g | tail -n 100";
        v = "vim";
        "v." = "vim .";
        vimdiff = "vim -d";
      };
    };

    emacs = {
      enable = true;
      extraPackages = epkgs: import ./emacs-packages.nix { inherit epkgs; };
    };

    firefox = {
      enable = true;
    };

    notmuch = {
      enable = true;
      new.tags = [ "unread" "inbox" ];
      hooks = {
        preNew = ''
          msmtp-queue -r
          mbsync --all
        '';
        postNew = ''
          notmuch tag -inbox +haskell -- to:haskell-cafe@haskell.org
          notmuch tag -inbox +clojure -- to:clojure@googlegroups.com
          notmuch tag -inbox +ai -- to:scikit-learn@python.org OR to:tensorflow.org
          notmuch tag -inbox +bitcoin -- to:bitcoin-dev@lists.linuxfoundation.org OR to:bitcoin-discuss@lists.linuxfoundation.org
          notmuch tag -inbox +diybio -- to:diybio@googlegroups.com
          notmuch tag -inbox +meetups -- from:meetup.com
          afew -tn
        '';

      };
      extraConfig = {
        search = {
          exclude_tags = "deleted;spam;";
        };
      };
    };

    afew = {
      enable = true;
      extraConfig = ''
        [ArchiveSentMailsFilter]

        [SpamFilter]
        spam_tag = spam

        # This filter looks for the List-Id header, and if it finds it, adds a
        # tag lists and a tag named lists/<list-id>.
        [ListMailsFilter]

        [Filter.0]
        message = mobile todos
        query = 'from:ben@bsima.me AND to:ben@bsima.me AND subject:TODO'
        tags = +todo;-inbox;+unread

        [Filter.1]
        message = logwatch
        query = subject:Logwatch
        tags = +logwatch;-new;-inbox

        [Filter.2]
        message = newsletters
        query =
        from:the-morning-paper@onelanday.co.uk OR
        from:newsletter@fightaging.org OR
        from:newsletter@fightaging.org or
        subject:'The Reading List Email*' OR
        from:info@haskellweekly.news OR
        from:kale@hackernewsletter.com OR
        from:newsletter@farnamstreetblog.com
        tags = -unread +deals

        [Filter.3]
        message = job emails
        query =
        from:alist.co OR
        from:angel.co OR
        from:stackoverflow.com OR
        from:stackoverflow.email OR
        from:cdmtechnology.com OR
        subject:job
        tags = +jobs;-inbox

        [Filter.4]
        message = github
        query = from:github.com
        tags = +github;-inbox

        [InboxFilter]
      '';
    };

    mbsync = {
      enable = true;
    };

    msmtp = {
      enable = false;
      extraConfig = builtins.readFile ./msmtprc;
    };
  };
}