{ pkgs, ... }:

let
  solarized-xresources = ./xresources;
  homedir = builtins.getEnv "HOME";
in
{
  # Waiting for the accounts feature to land in the 18.03 release branch...
  #
  #accounts = {
  #  email = {
  #    maildirBasePath = "$HOME/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;
  #        notmuch.enable = true;
  #        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";
      };
      msmtp = {
        source = ./msmtprc;
        target = ".msmtprc";
      };
      notmuch = {
        source = ./notmuch;
        target = ".notmuch-config";
      };
      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.light");
  };

  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 = false;
      defaultCacheTtl = 600;
      enableSshSupport = true;
    };
  };

  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 = https://github.com/rycee/home-manager/archive/release-18.03.tar.gz;
    };

    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;
      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";
        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 --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
        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;
    };
  };

}