set unstable
flake_path := "/etc/nixos"

privesc := if which("sudo") != "" { 
    "sudo" 
} else if which("doas") != "" { 
    "doas" 
} else if which("run0") != "" { 
    "run0 --background="
} else if which("pkexec") != "" { 
    "pkexec" 
} else { 
    error("No privilege escalation tool found")
}

default:
    @just --list

# Rebuild and switch
[group('nix')]
sw:
    nh os switch {{flake_path}}

# Update and switch
[group('nix')]
up:
    nh os switch --update {{flake_path}}

# Update specific input: `just upp nixpkgs`
[group('nix')]
upp input:
    nix flake update {{input}}

# Test
[group('nix')]
test:
    nh os test {{flake_path}}

# Collect garbage
[group('nix')]
gc:
    nix-collect-garbage --delete-old
    {{privesc}} nix-collect-garbage -d
    {{privesc}} /run/current-system/bin/switch-to-configuration boot

# Run `nh clean all`
[group('nix')]
ca:
    nh clean all
