- Simplified command cases by removing unnecessary semicolons.
- Improved error handling in rebuild.nix for clarity and consistency.
This commit is contained in:
@ -18,6 +18,7 @@
|
||||
|
||||
# --- Help text ---
|
||||
print_help = ''
|
||||
print_help() {
|
||||
echo "NixOS CLI Utility -- version 2.1.3"
|
||||
echo ""
|
||||
echo "Usage: ncli [command]"
|
||||
@ -45,6 +46,7 @@
|
||||
echo ""
|
||||
echo " help - Show this help message."
|
||||
echo ""
|
||||
}
|
||||
'';
|
||||
in
|
||||
pkgs.writeShellScriptBin "ncli" ''
|
||||
@ -70,6 +72,7 @@ in
|
||||
${lib.print_success}
|
||||
${lib.print_error}
|
||||
${lib.print_info}
|
||||
${print_help}
|
||||
|
||||
# --- Main Logic ---
|
||||
if [ "$#" -eq 0 ]; then
|
||||
@ -122,7 +125,7 @@ in
|
||||
${dev.dev_case}
|
||||
;;
|
||||
help)
|
||||
${print_help}
|
||||
print_help
|
||||
;;
|
||||
*)
|
||||
echo "Error: Invalid command '$1'" >&2
|
||||
|
||||
@ -1,7 +1,5 @@
|
||||
# ncli/commands/dev.nix — dev init / track / untrack
|
||||
lib:
|
||||
|
||||
{
|
||||
lib: {
|
||||
dev_case = ''
|
||||
# Only flake files need assume-unchanged (Nix requires them in the index).
|
||||
# .envrc and .direnv are handled via .git/info/exclude instead.
|
||||
@ -362,6 +360,5 @@ FLAKE_LOCK_EOF
|
||||
;;
|
||||
|
||||
esac
|
||||
;;
|
||||
'';
|
||||
}
|
||||
|
||||
@ -1,10 +1,7 @@
|
||||
# ncli/commands/git.nix — commit, push, pull, status, format
|
||||
lib:
|
||||
|
||||
let
|
||||
lib: let
|
||||
inherit (lib) project;
|
||||
in
|
||||
{
|
||||
in {
|
||||
commit_case = ''
|
||||
cd "$HOME/${project}" || { echo "Error: Could not change to $HOME/${project}"; exit 1; }
|
||||
if [ "$#" -lt 2 ]; then
|
||||
@ -20,29 +17,24 @@ in
|
||||
fi
|
||||
|
||||
git add -A && git commit -m "$commit_msg"
|
||||
;;
|
||||
'';
|
||||
|
||||
push_case = ''
|
||||
cd "$HOME/${project}" || { echo "Error: Could not change to $HOME/${project}"; exit 1; }
|
||||
git push origin $(git branch --show-current)
|
||||
;;
|
||||
'';
|
||||
|
||||
pull_case = ''
|
||||
cd "$HOME/${project}" || { echo "Error: Could not change to $HOME/${project}"; exit 1; }
|
||||
git pull origin $(git branch --show-current)
|
||||
;;
|
||||
'';
|
||||
|
||||
status_case = ''
|
||||
cd "$HOME/${project}" || { echo "Error: Could not change to $HOME/${project}"; exit 1; }
|
||||
git status
|
||||
;;
|
||||
'';
|
||||
|
||||
format_case = ''
|
||||
nix fmt .
|
||||
;;
|
||||
'';
|
||||
}
|
||||
|
||||
@ -1,10 +1,7 @@
|
||||
# ncli/commands/maintenance.nix — cleanup, diag, list-gens, trim, home-backups
|
||||
lib:
|
||||
|
||||
let
|
||||
lib: let
|
||||
inherit (lib) project;
|
||||
in
|
||||
{
|
||||
in {
|
||||
cleanup_case = ''
|
||||
echo "Warning! This will remove old generations of your system."
|
||||
read -p "How many generations to keep (default: all)? " keep_count
|
||||
@ -33,7 +30,6 @@ in
|
||||
echo "Cleaning up old log files..." >> "$LOG_FILE"
|
||||
find "$LOG_DIR" -type f -mtime +3 -name "*.log" -delete >> "$LOG_FILE" 2>&1
|
||||
echo "Cleanup process logged to $LOG_FILE"
|
||||
;;
|
||||
'';
|
||||
|
||||
diag_case = ''
|
||||
@ -50,7 +46,6 @@ in
|
||||
echo ""
|
||||
} > "$HOME/diag.txt"
|
||||
echo "Diagnostic report saved to $HOME/diag.txt"
|
||||
;;
|
||||
'';
|
||||
|
||||
list_gens_case = ''
|
||||
@ -59,7 +54,6 @@ in
|
||||
echo ""
|
||||
echo "--- System Generations ---"
|
||||
nix profile history --profile /nix/var/nix/profiles/system | cat || echo "Could not list system generations."
|
||||
;;
|
||||
'';
|
||||
|
||||
trim_case = ''
|
||||
@ -73,11 +67,9 @@ in
|
||||
else
|
||||
echo "Trim operation cancelled."
|
||||
fi
|
||||
;;
|
||||
'';
|
||||
|
||||
home_backups_case = ''
|
||||
ls -a ~ | grep backup
|
||||
;;
|
||||
'';
|
||||
}
|
||||
|
||||
@ -1,10 +1,7 @@
|
||||
# ncli/commands/rebuild.nix — Rebuild + Update logic
|
||||
lib:
|
||||
|
||||
let
|
||||
lib: let
|
||||
inherit (lib) host project handle_backups handle_build_error;
|
||||
in
|
||||
{
|
||||
in rec {
|
||||
# Shared rebuild snippet used by both `rebuild` and `update`
|
||||
rebuild_logic = ''
|
||||
handle_backups
|
||||
@ -21,7 +18,10 @@ in
|
||||
${rebuild_logic}
|
||||
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
|
||||
echo "✓ Rebuild finished successfully for ${host}"
|
||||
|
||||
@ -38,7 +38,6 @@ in
|
||||
echo "✗ Rebuild failed for ${host}" >&2
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
'';
|
||||
|
||||
update_case = ''
|
||||
@ -110,7 +109,10 @@ in
|
||||
echo "Rebuilding system... Staying on current specialization"
|
||||
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
|
||||
echo "✓ Update and rebuild finished successfully for ${host}"
|
||||
|
||||
@ -127,6 +129,5 @@ in
|
||||
echo "✗ Update and rebuild failed for ${host}" >&2
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
'';
|
||||
}
|
||||
|
||||
@ -1,9 +1,5 @@
|
||||
# ncli/commands/switch.nix — Specialization switching
|
||||
lib:
|
||||
|
||||
{}:
|
||||
|
||||
{
|
||||
lib: {
|
||||
switch_case = ''
|
||||
current=""
|
||||
if [ -f /etc/nixos-tags ]; then
|
||||
@ -35,6 +31,5 @@ lib:
|
||||
echo "To switch to a specialization, run: 'ncli switch <tag>'"
|
||||
fi
|
||||
fi
|
||||
;;
|
||||
'';
|
||||
}
|
||||
|
||||
@ -1,7 +1,3 @@
|
||||
# ncli/default.nix — Entry point
|
||||
# Usage in your flake:
|
||||
# ncli = import ./ncli { inherit pkgs host project; };
|
||||
# environment.systemPackages = [ ncli ];
|
||||
{
|
||||
pkgs,
|
||||
host,
|
||||
|
||||
@ -13,23 +13,26 @@ in
|
||||
|
||||
# --- Backup helper ---
|
||||
handle_backups = ''
|
||||
if [ ''${#BACKUP_FILES[@]} -eq 0 ]; then
|
||||
echo "No backup files configured to check."
|
||||
return
|
||||
fi
|
||||
|
||||
handle_backups() {
|
||||
echo "Checking for backup files to remove..."
|
||||
found=0
|
||||
for file_path in "''${BACKUP_FILES[@]}"; do
|
||||
full_path="$HOME/$file_path"
|
||||
if [ -f "$full_path" ]; then
|
||||
echo "Removing stale backup file: $full_path"
|
||||
rm "$full_path"
|
||||
found=1
|
||||
fi
|
||||
done
|
||||
if [ "$found" -eq 0 ]; then
|
||||
echo "No stale backup files found."
|
||||
fi
|
||||
}
|
||||
'';
|
||||
|
||||
# --- OOM / build error handler ---
|
||||
handle_build_error = ''
|
||||
handle_build_error() {
|
||||
local exit_code=$?
|
||||
if [ "$exit_code" -eq 137 ]; then
|
||||
echo ""
|
||||
@ -51,26 +54,35 @@ in
|
||||
echo " journalctl -k --since '5 minutes ago' | grep -i oom"
|
||||
echo ""
|
||||
fi
|
||||
}
|
||||
'';
|
||||
|
||||
# --- Print helpers (used by dev init) ---
|
||||
print_header = ''
|
||||
print_header() {
|
||||
echo ""
|
||||
echo -e "''${BLUE}==============================================''${NOCOLOR}"
|
||||
echo -e "''${BLUE} Nix Flake Development Environment Initializer''${NOCOLOR}"
|
||||
echo -e "''${BLUE}==============================================''${NOCOLOR}"
|
||||
echo ""
|
||||
}
|
||||
'';
|
||||
|
||||
print_success = ''
|
||||
print_success() {
|
||||
echo -e "''${GREEN}[OK]''${NOCOLOR} $1"
|
||||
}
|
||||
'';
|
||||
|
||||
print_error = ''
|
||||
print_error() {
|
||||
echo -e "''${RED}[ERR]''${NOCOLOR} $1"
|
||||
}
|
||||
'';
|
||||
|
||||
print_info = ''
|
||||
print_info() {
|
||||
echo -e "''${YELLOW}[->]''${NOCOLOR} $1"
|
||||
}
|
||||
'';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user