- Simplified command cases by removing unnecessary semicolons.

- Improved error handling in rebuild.nix for clarity and consistency.
This commit is contained in:
2026-05-29 09:29:31 +02:00
parent d989792774
commit 134fc441a5
8 changed files with 435 additions and 447 deletions

View File

@ -18,6 +18,7 @@
# --- Help text --- # --- Help text ---
print_help = '' print_help = ''
print_help() {
echo "NixOS CLI Utility -- version 2.1.3" echo "NixOS CLI Utility -- version 2.1.3"
echo "" echo ""
echo "Usage: ncli [command]" echo "Usage: ncli [command]"
@ -45,6 +46,7 @@
echo "" echo ""
echo " help - Show this help message." echo " help - Show this help message."
echo "" echo ""
}
''; '';
in in
pkgs.writeShellScriptBin "ncli" '' pkgs.writeShellScriptBin "ncli" ''
@ -70,6 +72,7 @@ in
${lib.print_success} ${lib.print_success}
${lib.print_error} ${lib.print_error}
${lib.print_info} ${lib.print_info}
${print_help}
# --- Main Logic --- # --- Main Logic ---
if [ "$#" -eq 0 ]; then if [ "$#" -eq 0 ]; then
@ -122,7 +125,7 @@ in
${dev.dev_case} ${dev.dev_case}
;; ;;
help) help)
${print_help} print_help
;; ;;
*) *)
echo "Error: Invalid command '$1'" >&2 echo "Error: Invalid command '$1'" >&2

View File

@ -1,7 +1,5 @@
# ncli/commands/dev.nix — dev init / track / untrack # ncli/commands/dev.nix — dev init / track / untrack
lib: lib: {
{
dev_case = '' dev_case = ''
# Only flake files need assume-unchanged (Nix requires them in the index). # Only flake files need assume-unchanged (Nix requires them in the index).
# .envrc and .direnv are handled via .git/info/exclude instead. # .envrc and .direnv are handled via .git/info/exclude instead.
@ -323,14 +321,14 @@ lib:
if [[ "$GIT_CHOICE" =~ ^[Yy]$ ]]; then if [[ "$GIT_CHOICE" =~ ^[Yy]$ ]]; then
cat > "$TARGET_ABS/flake.lock" << 'FLAKE_LOCK_EOF' cat > "$TARGET_ABS/flake.lock" << 'FLAKE_LOCK_EOF'
{ {
"nodes": { "nodes": {
"root": {} "root": {}
}, },
"root": "root", "root": "root",
"version": 7 "version": 7
} }
FLAKE_LOCK_EOF FLAKE_LOCK_EOF
print_success "flake.lock ready (Nix will populate it on first run if empty)" print_success "flake.lock ready (Nix will populate it on first run if empty)"
git -C "$TARGET_ABS" add --intent-to-add flake.nix flake.lock git -C "$TARGET_ABS" add --intent-to-add flake.nix flake.lock
@ -362,6 +360,5 @@ FLAKE_LOCK_EOF
;; ;;
esac esac
;;
''; '';
} }

View File

@ -1,10 +1,7 @@
# ncli/commands/git.nix — commit, push, pull, status, format # ncli/commands/git.nix — commit, push, pull, status, format
lib: lib: let
let
inherit (lib) project; inherit (lib) project;
in in {
{
commit_case = '' commit_case = ''
cd "$HOME/${project}" || { echo "Error: Could not change to $HOME/${project}"; exit 1; } cd "$HOME/${project}" || { echo "Error: Could not change to $HOME/${project}"; exit 1; }
if [ "$#" -lt 2 ]; then if [ "$#" -lt 2 ]; then
@ -20,29 +17,24 @@ in
fi fi
git add -A && git commit -m "$commit_msg" git add -A && git commit -m "$commit_msg"
;;
''; '';
push_case = '' push_case = ''
cd "$HOME/${project}" || { echo "Error: Could not change to $HOME/${project}"; exit 1; } cd "$HOME/${project}" || { echo "Error: Could not change to $HOME/${project}"; exit 1; }
git push origin $(git branch --show-current) git push origin $(git branch --show-current)
;;
''; '';
pull_case = '' pull_case = ''
cd "$HOME/${project}" || { echo "Error: Could not change to $HOME/${project}"; exit 1; } cd "$HOME/${project}" || { echo "Error: Could not change to $HOME/${project}"; exit 1; }
git pull origin $(git branch --show-current) git pull origin $(git branch --show-current)
;;
''; '';
status_case = '' status_case = ''
cd "$HOME/${project}" || { echo "Error: Could not change to $HOME/${project}"; exit 1; } cd "$HOME/${project}" || { echo "Error: Could not change to $HOME/${project}"; exit 1; }
git status git status
;;
''; '';
format_case = '' format_case = ''
nix fmt . nix fmt .
;;
''; '';
} }

View File

@ -1,10 +1,7 @@
# ncli/commands/maintenance.nix — cleanup, diag, list-gens, trim, home-backups # ncli/commands/maintenance.nix — cleanup, diag, list-gens, trim, home-backups
lib: lib: let
let
inherit (lib) project; inherit (lib) project;
in in {
{
cleanup_case = '' cleanup_case = ''
echo "Warning! This will remove old generations of your system." echo "Warning! This will remove old generations of your system."
read -p "How many generations to keep (default: all)? " keep_count read -p "How many generations to keep (default: all)? " keep_count
@ -33,7 +30,6 @@ in
echo "Cleaning up old log files..." >> "$LOG_FILE" echo "Cleaning up old log files..." >> "$LOG_FILE"
find "$LOG_DIR" -type f -mtime +3 -name "*.log" -delete >> "$LOG_FILE" 2>&1 find "$LOG_DIR" -type f -mtime +3 -name "*.log" -delete >> "$LOG_FILE" 2>&1
echo "Cleanup process logged to $LOG_FILE" echo "Cleanup process logged to $LOG_FILE"
;;
''; '';
diag_case = '' diag_case = ''
@ -50,7 +46,6 @@ in
echo "" echo ""
} > "$HOME/diag.txt" } > "$HOME/diag.txt"
echo "Diagnostic report saved to $HOME/diag.txt" echo "Diagnostic report saved to $HOME/diag.txt"
;;
''; '';
list_gens_case = '' list_gens_case = ''
@ -59,7 +54,6 @@ in
echo "" echo ""
echo "--- System Generations ---" echo "--- System Generations ---"
nix profile history --profile /nix/var/nix/profiles/system | cat || echo "Could not list system generations." nix profile history --profile /nix/var/nix/profiles/system | cat || echo "Could not list system generations."
;;
''; '';
trim_case = '' trim_case = ''
@ -73,11 +67,9 @@ in
else else
echo "Trim operation cancelled." echo "Trim operation cancelled."
fi fi
;;
''; '';
home_backups_case = '' home_backups_case = ''
ls -a ~ | grep backup ls -a ~ | grep backup
;;
''; '';
} }

View File

@ -1,10 +1,7 @@
# ncli/commands/rebuild.nix — Rebuild + Update logic # ncli/commands/rebuild.nix — Rebuild + Update logic
lib: lib: let
let
inherit (lib) host project handle_backups handle_build_error; inherit (lib) host project handle_backups handle_build_error;
in in rec {
{
# Shared rebuild snippet used by both `rebuild` and `update` # Shared rebuild snippet used by both `rebuild` and `update`
rebuild_logic = '' rebuild_logic = ''
handle_backups handle_backups
@ -21,7 +18,10 @@ in
${rebuild_logic} ${rebuild_logic}
echo -e "Starting NixOS rebuild for current host: ${host} on generation: $YELLOW$geno$NOCOLOR" echo -e "Starting NixOS rebuild for current host: ${host} on generation: $YELLOW$geno$NOCOLOR"
sudo nixos-rebuild switch --flake . ; _rebuild_exit=$? set +e
sudo nixos-rebuild switch --flake .
_rebuild_exit=$?
set -e
if [ "$_rebuild_exit" -eq 0 ]; then if [ "$_rebuild_exit" -eq 0 ]; then
echo " Rebuild finished successfully for ${host}" echo " Rebuild finished successfully for ${host}"
@ -38,7 +38,6 @@ in
echo " Rebuild failed for ${host}" >&2 echo " Rebuild failed for ${host}" >&2
exit 1 exit 1
fi fi
;;
''; '';
update_case = '' update_case = ''
@ -110,7 +109,10 @@ in
echo "Rebuilding system... Staying on current specialization" echo "Rebuilding system... Staying on current specialization"
fi fi
sudo nixos-rebuild switch --flake . ; _rebuild_exit=$? set +e
sudo nixos-rebuild switch --flake .
_rebuild_exit=$?
set -e
if [ "$_rebuild_exit" -eq 0 ]; then if [ "$_rebuild_exit" -eq 0 ]; then
echo " Update and rebuild finished successfully for ${host}" echo " Update and rebuild finished successfully for ${host}"
@ -127,6 +129,5 @@ in
echo " Update and rebuild failed for ${host}" >&2 echo " Update and rebuild failed for ${host}" >&2
exit 1 exit 1
fi fi
;;
''; '';
} }

View File

@ -1,9 +1,5 @@
# ncli/commands/switch.nix — Specialization switching # ncli/commands/switch.nix — Specialization switching
lib: lib: {
{}:
{
switch_case = '' switch_case = ''
current="" current=""
if [ -f /etc/nixos-tags ]; then if [ -f /etc/nixos-tags ]; then
@ -35,6 +31,5 @@ lib:
echo "To switch to a specialization, run: 'ncli switch <tag>'" echo "To switch to a specialization, run: 'ncli switch <tag>'"
fi fi
fi fi
;;
''; '';
} }

View File

@ -1,7 +1,3 @@
# ncli/default.nix — Entry point
# Usage in your flake:
# ncli = import ./ncli { inherit pkgs host project; };
# environment.systemPackages = [ ncli ];
{ {
pkgs, pkgs,
host, host,

View File

@ -13,23 +13,26 @@ in
# --- Backup helper --- # --- Backup helper ---
handle_backups = '' handle_backups = ''
if [ ''${#BACKUP_FILES[@]} -eq 0 ]; then handle_backups() {
echo "No backup files configured to check."
return
fi
echo "Checking for backup files to remove..." echo "Checking for backup files to remove..."
found=0
for file_path in "''${BACKUP_FILES[@]}"; do for file_path in "''${BACKUP_FILES[@]}"; do
full_path="$HOME/$file_path" full_path="$HOME/$file_path"
if [ -f "$full_path" ]; then if [ -f "$full_path" ]; then
echo "Removing stale backup file: $full_path" echo "Removing stale backup file: $full_path"
rm "$full_path" rm "$full_path"
found=1
fi fi
done done
if [ "$found" -eq 0 ]; then
echo "No stale backup files found."
fi
}
''; '';
# --- OOM / build error handler --- # --- OOM / build error handler ---
handle_build_error = '' handle_build_error = ''
handle_build_error() {
local exit_code=$? local exit_code=$?
if [ "$exit_code" -eq 137 ]; then if [ "$exit_code" -eq 137 ]; then
echo "" echo ""
@ -51,26 +54,35 @@ in
echo " journalctl -k --since '5 minutes ago' | grep -i oom" echo " journalctl -k --since '5 minutes ago' | grep -i oom"
echo "" echo ""
fi fi
}
''; '';
# --- Print helpers (used by dev init) --- # --- Print helpers (used by dev init) ---
print_header = '' print_header = ''
print_header() {
echo "" echo ""
echo -e "''${BLUE}==============================================''${NOCOLOR}" echo -e "''${BLUE}==============================================''${NOCOLOR}"
echo -e "''${BLUE} Nix Flake Development Environment Initializer''${NOCOLOR}" echo -e "''${BLUE} Nix Flake Development Environment Initializer''${NOCOLOR}"
echo -e "''${BLUE}==============================================''${NOCOLOR}" echo -e "''${BLUE}==============================================''${NOCOLOR}"
echo "" echo ""
}
''; '';
print_success = '' print_success = ''
print_success() {
echo -e "''${GREEN}[OK]''${NOCOLOR} $1" echo -e "''${GREEN}[OK]''${NOCOLOR} $1"
}
''; '';
print_error = '' print_error = ''
print_error() {
echo -e "''${RED}[ERR]''${NOCOLOR} $1" echo -e "''${RED}[ERR]''${NOCOLOR} $1"
}
''; '';
print_info = '' print_info = ''
print_info() {
echo -e "''${YELLOW}[->]''${NOCOLOR} $1" echo -e "''${YELLOW}[->]''${NOCOLOR} $1"
}
''; '';
} }