Files

78 lines
3.0 KiB
Makefile

# Justfile to safely clean up NixOS profiles, keeping only the last 3 generations.
# Uses nh (https://github.com/nix-community/nh) when available, falling back to plain nix commands.
host := "prouk"
keep := "3"
nh := `command -v nh || true`
hm_profile := env("HOME") / ".local/state/nix/profiles/home-manager"
# Default recipe displays the help menu.
help:
@echo "NixOS Profile Cleanup Utility"
@echo "============================="
@echo "nh: {{ if nh != "" { "found (" + nh + ")" } else { "not found, using plain nix commands" } }}"
@echo "Available recipes:"
@echo " just dry-run - Safe preview of what will be deleted"
@echo " just clean-all - Clean System, User and Home Manager generations (requires sudo)"
@echo " just build - Rebuild and switch the system"
@echo " just update - Update flake inputs"
# Dry run to preview changes without deleting anything.
dry-run:
#!/usr/bin/env bash
set -euo pipefail
if [ -n "{{ nh }}" ]; then
echo "=== [DRY RUN] All profiles (nh) ==="
nh clean all --keep {{ keep }} --dry
exit 0
fi
echo "=== [DRY RUN] System Profile ==="
sudo nix-env --profile /nix/var/nix/profiles/system --delete-generations +{{ keep }} --dry-run
echo -e "\n=== [DRY RUN] User Profile ==="
nix-env --delete-generations +{{ keep }} --dry-run
echo -e "\n=== [DRY RUN] Home Manager Profile ==="
if [ -e "{{ hm_profile }}" ]; then
nix-env --profile "{{ hm_profile }}" --delete-generations +{{ keep }} --dry-run
elif [ -d "$HOME/.nix-profile" ]; then
echo "No Home Manager directory found, but user profile exists."
else
echo "No Home Manager profile detected."
fi
# Clean system, user and Home Manager generations, then update the bootloader (requires sudo).
clean-all:
#!/usr/bin/env bash
set -euo pipefail
if [ -n "{{ nh }}" ]; then
echo "--- Cleaning All Profiles (nh) ---"
nh clean all --keep {{ keep }}
echo "--- Updating Bootloader Menu ---"
nh os boot . --hostname {{ host }}
else
echo "--- Cleaning System Profile (SUDO) ---"
sudo nix-env --profile /nix/var/nix/profiles/system --delete-generations +{{ keep }}
echo "--- Cleaning User Profile ---"
nix-env --delete-generations +{{ keep }}
echo "--- Cleaning Home Manager Profile ---"
if [ -e "{{ hm_profile }}" ]; then
nix-env --profile "{{ hm_profile }}" --delete-generations +{{ keep }}
else
echo "No Home Manager profile found. Skipping."
fi
sudo nix-collect-garbage
nix-collect-garbage
echo "--- Updating Bootloader Menu ---"
sudo nixos-rebuild boot --flake .#{{ host }}
fi
echo "All cleanups successfully completed!"
# Build.
build:
@echo "--- Rebuilding System ---"
{{ if nh != "" { "nh os switch . --hostname " + host } else { "sudo nixos-rebuild switch --flake .#" + host } }}
# Update flake.
update:
@echo "--- Updating Flake ---"
nix flake update