{ pkgs, config, ... }:

let
  xdg_data = config.xdg.dataHome;
  xdg_config = config.xdg.configHome;
  xdg_cache = config.xdg.cacheHome;
  xdg_state = config.xdg.stateHome;
in
rec {
  xdg.enable = true;

  home = {
    sessionVariables = {
      EDITOR = "nvim";
      GHCUP_USE_XDG_DIRS = "true";
      HISTFILE = "${xdg_state}/bash/history";
      CABAL_CONFIG = "${xdg_config}/cabal/config";
      CABAL_DIR = "${xdg_data}/cabal";
      DOCKER_CONFIG = "${xdg_config}/docker";
      IPYTHONDIR = "${xdg_config}/ipython";
      JUPYTER_CONFIG_DIR = "${xdg_config}/jupyter";
      LESSHISTFILE = "${xdg_cache}/less/history";
      MPLCONFIGDIR = "${xdg_config}/matplotlib";
      TERMINFO = "${xdg_data}/terminfo";
      PSQL_HISTORY = "${xdg_data}/psql_history";
      BUNDLE_USER_CONFIG = "${xdg_config}/bundle";
      BUNDLE_USER_CACHE = "${xdg_cache}/bundle";
      BUNDLE_USER_PLUGIN = "${xdg_data}/bundle";
      GRADLE_USER_HOME = "${xdg_data}/gradle";
      ANDROID_USER_HOME = "${xdg_data}/android";
      XDG_CONFIG_HOME = "${xdg_config}";
      GOPATH = "${xdg_data}/go";
      MINT_PATH = "${xdg_data}/mint";
      MINT_LINK_PATH = "${xdg_data}/mint/bin";
    };
    shellAliases = {
      cat = "bat --plain";
      fopen = "open (fzf)";
      rebuild = "darwin-rebuild switch --flake ~/.config/darwin#weethet";
      yarn = "yarn --use-yarnrc ~/.config/yarnrc";
      brew = "/opt/homebrew/bin/brew";
      mint = "/opt/homebrew/bin/mint";
    };
  };
  programs.nushell = {
    enable = true;
    environmentVariables = {
      EDITOR = "nvim";
      CARGO_HOME = "${xdg_data}/cargo";
      RUSTUP_HOME = "${xdg_data}/rustup";
    };
    shellAliases = home.shellAliases;
    configFile = {
      text = ''
        $env.config = {
          show_banner: false,
          filesize: {
            metric: true,
          }
          table: {
            mode: rounded
          }
          rm: {
            always_trash: true # always act as if -t was given. Can be overridden with -p
          }
        }
      '';
    };
    envFile = {
      text = ''
        $env.NIX_SSL_CERT_FILE = '/etc/ssl/certs/ca-certificates.crt'
        $env.PATH = ($env.PATH 
          | split row (char esep) 
          | prepend [
              '/etc/profiles/per-user/weethet/bin', 
              '/nix/var/nix/profiles/default/bin', 
              '/run/current-system/sw/bin/'
            ]
          | append [
            '${xdg_data}/cargo/bin',
          ]
        )

        $env.NIX_PROFILES = [
          '/nix/var/nix/profiles/default', 
          '/etc/profiles/per-user/weethet/bin'
        ]
      '';
    };
  };
  programs.fish = {
    enable = true;
    interactiveShellInit = ''
      set fish_greeting

      set tmp_paths $fish_user_paths

      set -x fish_user_paths

      for p in (string split ":" $PATH)
        if string match -q '/nix/store/*/bin' $p
          set -a fish_user_paths $p 
        end
      end

      # for p in (string split ":" $PATH)
      #   if string match -q '*/.devenv/*/bin' $p
      #     set -a fish_user_paths $p 
      #   end
      # end

      for p in (string split " " $NIX_PROFILES)
        set -a fish_user_paths $p/bin
      end

      set -a fish_user_paths $HOME/.local/bin
      set -a fish_user_paths $HOME/Library/Python/3.9/bin
      set -a fish_user_paths $HOME/.local/share/cargo/bin
      set -a fish_user_paths ${xdg_data}/mint/bin

      for p in $tmp_paths
        if not contains $p $fish_user_paths
          set -a fish_user_paths $p
        end
      end
    '';
  };
  xdg.configFile."fish/themes" = {
    source =
      pkgs.fetchFromGitHub {
        owner = "vitallium";
        repo = "tokyonight-fish";
        rev = "26a5f1d02ee6bdae1d7793a656d276de1402d315";
        sha256 = "M2eP7qLhS+Iw/QaKtyoAFqgVgRIUkaSmoC6xe2mhhcM=";
      }
      + "/themes";
  };
}