added some utilities and going to nh
This commit is contained in:
1 parent
3fb55bb091
commit
8ac7fd4862
8 files changed
+92
-56
No files matched your search
@@ -1,63 +1,75 @@
|
||||
# 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-system - Clean system generations and update bootloader (requires sudo)"
|
||||
@echo " just clean-user - Clean current user's profile generations"
|
||||
@echo " just clean-home-manager - Clean Home Manager generations (if applicable)"
|
||||
@echo " just clean-all - Clean everything (System, User, and Home Manager)"
|
||||
@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:
|
||||
@echo "=== [DRY RUN] System Profile ==="
|
||||
sudo nix-env --profile /nix/var/nix/profiles/system --delete-generations +3 --dry-run
|
||||
@echo "\n=== [DRY RUN] User Profile ==="
|
||||
nix-env --delete-generations +3 --dry-run
|
||||
@echo "\n=== [DRY RUN] Home Manager Profile ==="
|
||||
@if [ -d "$HOME/.local/state/nix/profiles/home-manager" ]; then \
|
||||
nix-env --profile "$HOME/.local/state/nix/profiles/home-manager" --delete-generations +3 --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."; \
|
||||
#!/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 generations (requires sudo).
|
||||
clean-system:
|
||||
@echo "--- Cleaning System Profile (SUDO) ---"
|
||||
sudo nix-env --profile /nix/var/nix/profiles/system --delete-generations +3
|
||||
sudo nix-collect-garbage
|
||||
@echo "--- Updating Bootloader Menu ---"
|
||||
sudo nixos-rebuild boot --flake .#prouk
|
||||
|
||||
# Clean user generations (no sudo).
|
||||
clean-user:
|
||||
@echo "--- Cleaning User Profile ---"
|
||||
nix-env --delete-generations +3
|
||||
nix-collect-garbage
|
||||
|
||||
# Clean Home Manager generations (no sudo).
|
||||
clean-home-manager:
|
||||
@echo "--- Cleaning Home Manager Profile ---"
|
||||
@if [ -d "$HOME/.local/state/nix/profiles/home-manager" ]; then \
|
||||
nix-env --profile "$HOME/.local/state/nix/profiles/home-manager" --delete-generations +3; \
|
||||
nix-collect-garbage; \
|
||||
else \
|
||||
echo "No Home Manager profile found. Skipping."; \
|
||||
# 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
|
||||
|
||||
# Clean everything in one go.
|
||||
clean-all: clean-system clean-user clean-home-manager
|
||||
@echo "All cleanups successfully completed!"
|
||||
echo "All cleanups successfully completed!"
|
||||
|
||||
# Build.
|
||||
build:
|
||||
@echo "--- Rebuilding System ---"
|
||||
sudo nixos-rebuild switch --flake .#prouk
|
||||
{{ if nh != "" { "nh os switch . --hostname " + host } else { "sudo nixos-rebuild switch --flake .#" + host } }}
|
||||
|
||||
# Update flake.
|
||||
update:
|
||||
|
||||
Reference in new issue
Block a user