# https://nix-community.github.io/home-manager/options.html
{ pkgs, config, ... }:
{
  #nixpkgs.config.allowUnfree = true;
  home.stateVersion  = "22.11"; # TODO: what uses this?

  imports = [
    ./profiles/bat.nix
    ./profiles/git.nix
    ./profiles/gtk.nix
    ./profiles/micro.nix
    ./profiles/shell.nix
    ./profiles/gh-dash.nix
    ./profiles/ssh.nix
    ./profiles/tmate.nix
    ./profiles/update-diff.nix
  ];

  # TODO: add remote-exec once packaged
  programs.bash.initExtra = ''
    # remote-exec
    if command -v remote >/dev/null && command -v remote-quick >/dev/null; then
      complete -F _command remote
      complete -F _command remote-quick
    fi

    # TODO: remove
    function atom_nix {
      #nix-shell -p atom --run "atom $(printf "%q " "$@") --in-process-gpu --no-sandbox"
      #nix-shell -p atom --run "atom $(printf "%q " "$@") --no-sandbox"
      nix run nixpkgs#atom -- --disable-gpu --in-process-gpu --no-sandbox "$@"
    }
  '';

  home.packages = with pkgs; [
    xclip
    #rmate-sh # TODO: add to nixpkgs, add to ssh config
    #remote-exec # TODO: add to nixpkgs

    zip
    unrar
    unzip
    atool
    p7zip
    bzip2
    gzip
    atool

    dos2unix

    pandoc
    graphviz
    vgmstream
    gallery-dl
    youtube-dl
    yt-dlp
    ffmpeg-full

    visidata

    lolcat
    toilet
    boxes
    tewisay
    ponysay

    # TODO: move to graphical? kiosk?
    cage
    mesa-demos

    librespeed-cli

    cachix
    #nix-template
    nix-output-monitor
    nixpkgs-review
    nixpkgs-hammering
    nix-update
    nix-init
    nurl
    manix
    comma

    #sshuttle

    #cargo
    #cargo-edit

    #nim
    #nimble

    poetry
    (python3.withPackages (python-packages: with python-packages; [
      matplotlib
      more-itertools
      numpy
      #objexplore # TODO: package this, used in python interactive shell
      ptpython
      python-lsp-server
      pyyaml
      requests
      rich
      scipy
      toml
      virtualenv
    ]))
  ];

  home.shellAliases = {
    #flexget = "ssh -t knut.pbsds.net sudo -u flexget flexget";
    flexget = "sudo --user=flexget flexget -c /var/lib/flexget/flexget.yml"; # TODO: only applies to nox...
    hman = "man -H "; # HTML opened in a browser
    tmux = "systemd-run --scope --user tmux"; # detach from ssh scope, surviving logout

    #TODO: vpn

    # TODO: wayland
    clip = "xclip -sel clip -t text/plain -rmlastnl -i";

    # TODO: ps1?
    pwd-fqdn = ''echo "$(whoami)@$(hostname -f):$(printf "%q" "$(realpath .)/")"'';

    http-server = "${pkgs.python3}/bin/python -m http.server";
    manix-fzf = ''manix "" 2>/dev/null | grep '^# ' | sed 's/^# \(.*\) (.*/\1/;s/ (.*//;s/^# //' | fzf --preview="manix '{}'" | xargs manix'';

    # TODO: move to /run/user/...
    # TODO: add --nom once released - https://github.com/Mic92/nixpkgs-review/pull/303
    nixpkgs-review = ''mkdir -p /dev/shm/nixpkgs-review && ln -sfn /dev/shm/nixpkgs-review $HOME/.cache/nixpkgs-review && nixpkgs-review'';

    killall = "killall -v";
    #bin = "perl -pe 'chomp' | curl -is -X POST https://i.kuklef.se/upload -F content=@- -F expiration=10d | grep '^location:' | cut -d' ' -f2-";
    bin = "python -c 'import sys; sys.stdout.write(sys.stdin.read().strip())' | curl -is -X POST https://i.kuklef.se/upload -F content=@- -F expiration=10d | grep '^location:' | cut -d' ' -f2-";

    #atom = "atom --no-sandbox";
    atom = "atom --disable-gpu --in-process-gpu --no-sandbox";
  };

  programs.nix-index.enable = true;

  xdg.enable = true;
  #TODO: xdg.desktopEntries

  /** /
  programs.beets = {
    enable = true;
    settings = {
      directory = "/mnt/meconium/beets_preprocessed/data";
      #library = "/mnt/meconium/beets_preprocessed/library.db";
      library = "${config.xdg.configHome}/beets/library_preprocessed.db";

      #directory = "/mnt/meconium/beets_music/library";
      #library = "${config.xdg.configHome}/beets/library_meconium.db";
      ##library = "/mnt/meconium/beets_music/data.db";
    };
  };
  /**/

}