# just is a command runner, Justfile is very similar to Makefile, but simpler.

# Use nushell for shell commands
# To use this justfile, you need to enter a shell with just & nushell installed:
# 
#   nix shell nixpkgs#just nixpkgs#nushell
set shell := ["nu", "-c"]

utils_nu := absolute_path("utils.nu")

############################################################################
#
#  Common commands(suitable for all machines)
#
############################################################################

# List all the just commands
default:
    @just --list

# Run eval tests
[group('nix')]
test:
  nix eval .#evalTests --show-trace --print-build-logs --verbose

# Update all the flake inputs
[group('nix')]
up:
  nix flake update --commit-lock-file

# Update specific input
# Usage: just upp nixpkgs
[group('nix')]
upp input:
  nix flake update {{input}} --commit-lock-file

# List all generations of the system profile
[group('nix')]
history:
  nix profile history --profile /nix/var/nix/profiles/system

# Open a nix shell with the flake
[group('nix')]
repl:
  nix repl -f flake:nixpkgs

# remove all old generations
# on darwin, you may need to switch to root user to run this command
[group('nix')]
clean:
  # Wipe out NixOS's history
  sudo nix profile wipe-history --profile /nix/var/nix/profiles/system
  # Wipe out home-manager's history
  nix profile wipe-history --profile $"($env.XDG_STATE_HOME)/nix/profiles/home-manager"

# Garbage collect all unused nix store entries
[group('nix')]
gc:
  # garbage collect all unused nix store entries(system-wide)
  sudo nix-collect-garbage --delete-older-than 7d
  # garbage collect all unused nix store entries(for the user - home-manager)
  # https://github.com/NixOS/nix/issues/8508
  nix-collect-garbage --delete-older-than 7d

# Enter a shell session which has all the necessary tools for this flake
[linux]
[group('nix')]
shell:
  nix shell nixpkgs#git nixpkgs#neovim nixpkgs#colmena

# Enter a shell session which has all the necessary tools for this flake
[macos]
[group('nix')]
shell:
  nix shell nixpkgs#git nixpkgs#neovim

# upgrade determinate nix
[macos]
[group('nix')]
nix-upgrade:
  sudo determinate-nixd upgrade

[group('nix')]
fmt:
  # format the nix files in this repo
  ls **/*.nix | each { |it| nixfmt $it.name }

# Show all the auto gc roots in the nix store
[group('nix')]
gcroot:
  ls -al /nix/var/nix/gcroots/auto/

# Verify all the store entries
# Nix Store can contains corrupted entries if the nix store object has been modified unexpectedly.
# This command will verify all the store entries,
# and we need to fix the corrupted entries manually via `sudo nix store delete <store-path-1> <store-path-2> ...`
[group('nix')]
verify-store:
  nix store verify --all

# Repair Nix Store Objects
[group('nix')]
repair-store *paths:
  nix store repair {{paths}}

# Update all Nixpkgs inputs
[group('nix')]
up-nix:
  nix flake update --commit-lock-file nixpkgs-stable nixpkgs-master nixpkgs-darwin nixpkgs-patched

# override nixpkgs's commit hash
[group('nix')]
override-pkgs hash:
  nix flake update --commit-lock-file nixpkgs --override-input nixpkgs github:NixOS/nixpkgs/{{hash}}

############################################################################
#
#  NixOS Desktop related commands
#
############################################################################

# Deploy the nixosConfiguration by hostname match
[linux]
[group('homelab')]
local mode="switch" verbosity="normal":
  #!/usr/bin/env nu
  use {{utils_nu}} *;
  nixos-switch (hostname) {{mode}} {{verbosity}}

# Deploy the niri nixosConfiguration by hostname match
[linux]
[group('desktop')]
niri mode="switch" verbosity="normal":
  #!/usr/bin/env nu
  use {{utils_nu}} *;
  nixos-switch $"(hostname)-niri" {{mode}} {{verbosity}}

############################################################################
#
#  Darwin related commands
#
############################################################################

[macos]
[group('desktop')]
brew-upgrade:
  brew upgrade --cask --greedy

[macos]
[group('desktop')]
darwin-set-proxy:
  sudo python3 scripts/darwin_set_proxy.py
  sleep 1sec

[macos]
[group('desktop')]
darwin-rollback:
  #!/usr/bin/env nu
  use {{utils_nu}} *;
  darwin-rollback

# Deploy the darwinConfiguration by hostname match
[macos]
[group('desktop')]
local verbosity="normal":
  #!/usr/bin/env nu
  use {{utils_nu}} *;
  darwin-build (hostname) {{verbosity}};
  darwin-switch (hostname) {{verbosity}}


# Reset launchpad to force it to reindex Applications
[macos]
[group('desktop')]
reset-launchpad:
  defaults write com.apple.dock ResetLaunchPad -bool true
  killall Dock

############################################################################
#
#  Homelab - Kubevirt Cluster related commands
#
############################################################################

# Remote deployment via colmena
[linux]
[group('homelab')]
col tag mode="switch":
  colmena apply {{mode}} --on '@{{tag}}' --verbose --show-trace

# Build and upload a vm image
[linux]
[group('homelab')]
upload-vm name verbosity="normal":
  #!/usr/bin/env nu
  use {{utils_nu}} *;
  upload-vm {{name}} {{verbosity}}

# Deploy all the KubeVirt nodes(Physical machines running KubeVirt)
[linux]
[group('homelab')]
lab mode="switch":
  colmena apply {{mode}} --on '@virt-*' --verbose --show-trace

[linux]
[group('homelab')]
shoryu mode="switch":
  colmena apply {{mode}} --on '@kubevirt-shoryu' --verbose --show-trace

[linux]
[group('homelab')]
shushou mode="switch":
  colmena apply {{mode}} --on '@kubevirt-shushou' --verbose --show-trace

[linux]
[group('homelab')]
youko mode="switch":
  colmena apply {{mode}} --on '@kubevirt-youko' --verbose --show-trace

############################################################################
#
# Commands for other Virtual Machines
#
############################################################################

# Build and upload a vm image
[linux]
[group('homelab')]
upload-idols verbosity="normal":
  #!/usr/bin/env nu
  use {{utils_nu}} *; 
  upload-vm aquamarine {{verbosity}}
  upload-vm ruby {{verbosity}}
  upload-vm kana {{verbosity}}

[linux]
[group('homelab')]
aqua mode="switch":
  colmena apply {{mode}} --on '@aqua' --verbose --show-trace

[linux]
[group('homelab')]
ruby mode="switch":
  colmena apply {{mode}} --on '@ruby' --verbose --show-trace

[linux]
[group('homelab')]
kana mode="switch":
  colmena apply {{mode}} --on '@kana' --verbose --show-trace

############################################################################
#
# Kubernetes related commands
#
############################################################################

# Build and upload a vm image
[linux]
[group('homelab')]
upload-k3s-prod verbosity="normal":
  #!/usr/bin/env nu
  use {{utils_nu}} *; 
  upload-vm k3s-prod-1-master-1 {{verbosity}};
  upload-vm k3s-prod-1-master-2 {{verbosity}};
  upload-vm k3s-prod-1-master-3 {{verbosity}};
  upload-vm k3s-prod-1-worker-1 {{verbosity}};
  upload-vm k3s-prod-1-worker-2 {{verbosity}};
  upload-vm k3s-prod-1-worker-3 {{verbosity}};

[linux]
[group('homelab')]
upload-k3s-test verbosity="normal":
  #!/usr/bin/env nu
  use {{utils_nu}} *; 
  upload-vm k3s-test-1-master-1 {{verbosity}};
  upload-vm k3s-test-1-master-2 {{verbosity}};
  upload-vm k3s-test-1-master-3 {{verbosity}};

[linux]
[group('homelab')]
k3s-prod mode="switch":
  colmena apply {{mode}} --on '@k3s-prod-*' --verbose --show-trace

[linux]
[group('homelab')]
k3s-test mode="switch":
  colmena apply {{mode}} --on '@k3s-test-*' --verbose --show-trace

# =================================================
#
# Other useful commands
#
# =================================================

[group('common')]
path:
   $env.PATH | split row ":"

[group('common')]
trace-access app *args:
  strace -f -t -e trace=file {{app}} {{args}} | complete | $in.stderr | lines | find -v -r "(/nix/store|/newroot|/proc)" | parse --regex '"(/.+)"' | sort | uniq

[linux]
[group('common')]
penvof pid:
  sudo cat $"/proc/($pid)/environ" | tr '\0' '\n'

# Remove all reflog entries and prune unreachable objects
[group('git')]
ggc:
  git reflog expire --expire-unreachable=now --all
  git gc --prune=now

# Amend the last commit without changing the commit message
[group('git')]
game:
  git commit --amend -a --no-edit

# Delete all failed pods
[group('k8s')]
del-failed:
  kubectl delete pod --all-namespaces --field-selector="status.phase==Failed"

[linux]
[group('services')]
list-inactive:
  systemctl list-units -all --state=inactive

[linux]
[group('services')]
list-failed:
  systemctl list-units -all --state=failed

[linux]
[group('services')]
list-systemd:
  systemctl list-units systemd-*


# =================================================
#
# GitHub CLI + Nixpkgs Review via Github Action
# https://github.com/ryan4yin/nixpkgs-review-gha
#
# =================================================

[group('github')]
gh-login:
  gh auth login -h github.com --skip-ssh-key --git-protocol ssh

# Run nixpkgs-review for PR
[group('nixpkgs')]
pkg-review pr:
  gh workflow run review.yml --repo ryan4yin/nixpkgs-review-gha -f x86_64-darwin=no -f post-result=true -f pr={{pr}}

# Run package tests for PR
[group('nixpkgs')]
pkg-test pr pname:
  gh workflow run review.yml --repo ryan4yin/nixpkgs-review-gha -f x86_64-darwin=no -f post-result=true -f pr={{pr}} -f extra-args="-p {{pname}}.passthru.tests"

# View the summary of a workflow
[group('nixpkgs')]
pkg-summary:
  gh workflow view review.yml --repo ryan4yin/nixpkgs-review-gha
