# List all available recipes
default:
    @just --list

# Rebuild and switch to the new configuration (uses current hostname by default)
switch hostname=`hostname`:
    sudo nixos-rebuild switch --flake .#{{hostname}}

# Build without switching — useful to check the result before applying
build hostname=`hostname`:
    nixos-rebuild build --flake .#{{hostname}}

# Apply the new configuration temporarily (reverts on next boot)
test hostname=`hostname`:
    sudo nixos-rebuild test --flake .#{{hostname}}

# Update all flake inputs to their latest locked versions
update:
    nix flake update

# Update a single flake input (e.g. just update-input nixpkgs)
update-input input:
    nix flake update {{input}}

# Check the flake for evaluation errors
check:
    nix flake check

# Open a Nix REPL with the flake loaded
repl:
    nix repl --expr 'builtins.getFlake (toString ./.)'

# Delete Nix store paths older than 7 days and run the garbage collector
gc:
    sudo nix-collect-garbage --delete-older-than 7d
    nix-collect-garbage --delete-older-than 7d

# List all system generations
generations:
    sudo nix-env --list-generations --profile /nix/var/nix/profiles/system

# Build the installer ISO (bootable USB image)
iso:
    nix build .#nixosConfigurations.installer.config.system.build.isoImage
    @echo ""
    @echo "ISO built at: $(readlink -f result)/iso/*.iso"
    @echo "Flash with:   sudo dd if=result/iso/*.iso of=/dev/sdX bs=4M status=progress && sync"

# Edit the SOPS-encrypted secrets file
secrets:
    sops secrets/secrets.yaml

# Show a diff between the running system and the current build
diff hostname=`hostname`:
    nix build .#nixosConfigurations.{{hostname}}.config.system.build.toplevel --no-link
    nvd diff /run/current-system $(nix path-info .#nixosConfigurations.{{hostname}}.config.system.build.toplevel)
