export GUM_FILTER_HEIGHT := "15"

@_default:
  just "$(gum filter --placeholder "Pick a recipie..." $(just --summary --unsorted))"

eval hostname=`just _a-host`:
  nix eval .#nixosConfigurations.{{hostname}}.config.system.build.toplevel.outPath

report hostname=`just _a-host`:
  nix eval .#nixosReports.{{hostname}} --json | yq . --yaml-output | bat --language yaml --style plain --paging never

reports:
  nix eval .#nixosReports --json | yq . --yaml-output | bat --language yaml --style plain --paging never

update:
  git reset flake.lock
  git restore flake.lock
  git pull --rebase --autostash
  nix flake update --commit-lock-file
  @mkdir -p .direnv/
  @echo "$(date +%Y-%m-%d)" > .direnv/pull-date

build:
  nixos-rebuild build --flake .

test:
  sudo nixos-rebuild test --flake .

switch:
  sudo nixos-rebuild switch --flake .

boot:
  sudo nixos-rebuild boot --flake .

push hostname=`just _a-host` cmd=`gum choose test switch boot --header "Select mode..."`:
  nixos-rebuild build --flake .#{{hostname}}
  @echo pushing...
  NIX_NO_NOM=1 NIX_SSHOPTS="-tt" nixos-rebuild {{cmd}} --flake .#{{hostname}} --use-remote-sudo --target-host $(nix eval .#nixosReports.{{hostname}}.fqdn --json | jq . -r) --use-substitutes

@_a-host:
  nix eval .#nixosReports --apply builtins.attrNames --json 2>/dev/null | jq '.[]' -r | gum filter --placeholder "Pick a host..."

@remote-current:
  >&2 echo Current remote: $(remote-host)

@remote-list:
  tomlq <.remote.toml '.hosts | to_entries[] | "remote-set \(.key+1) # -> \(.value.host)"' -r

@_a-remote-label:
  just remote-list | gum filter --placeholder "Pick a remote..." | cut -d' ' -f2

@_a-remote-fqdn:
  just remote-list | gum filter --placeholder "Pick a remote..." | cut -d' ' -f5-

@remote-set remote=`just remote-current && just _a-remote-label`:
  remote-set {{remote}}

@gorgon:
  # https://codeberg.org/gorgon/gorgon/src/branch/main/nix-web
  # https://discourse.nixos.org/t/a-web-ui-for-the-nix-store-early-beta/35762
  nix run 'git+https://codeberg.org/gorgon/gorgon#nix-web'