Version 1.0.0

This commit is contained in:
Peritia 2025-08-08 13:38:34 +02:00
parent 0084c7295d
commit 0ae67a9897
11 changed files with 764 additions and 568 deletions

249
nyx/bash/nyx-cleanup.sh Normal file
View file

@ -0,0 +1,249 @@
#!/usr/bin/env bash
# nyx-cleanup.sh — tokenized template
# Tokens replaced by Nix:
# @LOG_DIR@ @KEEP_GENERATIONS@ @AUTO_PUSH@ @GIT_BIN@ @VERSION@
nyx-cleanup() {
set -euo pipefail
########################################################################
# CONFIG (injected by Nix)
########################################################################
log_dir="@LOG_DIR@"
keep_generations="@KEEP_GENERATIONS@"
auto_push="@AUTO_PUSH@"
git_bin="@GIT_BIN@"
version="@VERSION@"
# Paths
log_dir_rebuild="${log_dir}/rebuild"
log_dir_cleanup="${log_dir}/cleanup"
timestamp=$(date '+%Y-%m-%d_%H-%M-%S')
summary_log="${log_dir_cleanup}/cleanup-${timestamp}.log"
########################################################################
# COLORS
########################################################################
if [[ -t 1 ]]; then
RED=$'\e[31m'; GREEN=$'\e[32m'; YELLOW=$'\e[33m'
BLUE=$'\e[34m'; MAGENTA=$'\e[35m'; CYAN=$'\e[36m'
BOLD=$'\e[1m'; RESET=$'\e[0m'
else
RED=""; GREEN=""; YELLOW=""; BLUE=""; MAGENTA=""; CYAN=""; BOLD=""; RESET=""
fi
########################################################################
# UTIL
########################################################################
say() { echo -e "$*"; }
action(){ say "${BOLD}${BLUE}$*${RESET}"; }
ok() { say "${GREEN}${RESET} $*"; }
warn() { say "${YELLOW}!${RESET} $*"; }
nope() { say "${RED}${RESET} $*"; }
log() { echo -e "$*" | tee -a "$summary_log"; }
print_line() { log "\n${BOLD}==================================================${RESET}\n"; }
# Lightweight git helpers (mirrors rebuilds style)
g() { "$git_bin" "$@"; }
git_in_repo() {
local dir="$1"
[[ -d "$dir/.git" ]] || (cd "$dir" && g rev-parse --git-dir >/dev/null 2>&1)
}
git_commit_if_staged() {
if ! g diff --cached --quiet; then
g commit -m "$1" || true
return 0
fi
return 1
}
git_push_if_enabled() {
if [[ "$auto_push" == "true" ]]; then g push || true; fi
}
########################################################################
# ARGS
########################################################################
DRYRUN=false
OVERRIDE_KEEP=""
while [[ $# -gt 0 ]]; do
case "$1" in
--dry-run) DRYRUN=true; shift;;
--keep) OVERRIDE_KEEP="${2:-}"; shift 2;;
-h|--help)
cat <<EOF
nyx-cleanup [--dry-run] [--keep N]
Prunes old *system* generations, runs GC (and store optimise), and tidies logs.
Options:
--dry-run Show actions without doing them.
--keep N Override configured generations to keep (default: ${keep_generations}).
-h, --help Show this help.
EOF
return 0
;;
*)
warn "Unknown arg: $1"
shift;;
esac
done
[[ -n "$OVERRIDE_KEEP" ]] && keep_generations="$OVERRIDE_KEEP"
########################################################################
# BANNER (requires nyx-tool in PATH)
########################################################################
if command -v nyx-tool >/dev/null 2>&1; then
nyx-tool "Nyx" "nyx-cleanup" "$version" \
"Prune old NixOS generations, GC store, tidy logs" \
"by Peritia-System" \
"https://github.com/Peritia-System/Nyx-Tools" \
"https://github.com/Peritia-System/Nyx-Tools/issues" \
"Clean. Lean. Serene."
else
say "Nyx Tools — nyx-cleanup v${version}"
fi
########################################################################
# PREP
########################################################################
mkdir -p "$log_dir_cleanup"
start_human=$(date '+%Y-%m-%d %H:%M:%S')
start_s=$(date +%s)
log "Started: ${start_human}"
print_line
########################################################################
# STEP 1: Ensure sudo ticket
########################################################################
action "Checking sudo access…"
if sudo -n true 2>/dev/null; then
ok "Sudo already available."
else
say "Getting sudo ticket (you may be prompted)…"
if ! sudo -v; then nope "Cannot get sudo."; exit 1; fi
fi
########################################################################
# STEP 2: Prune old *system* generations (keep newest K)
########################################################################
print_line
action "Pruning NixOS system generations (keeping ${keep_generations})…"
# List generations oldest->newest
mapfile -t gens < <(sudo nix-env -p /nix/var/nix/profiles/system --list-generations | awk '{print $1}')
if (( ${#gens[@]} == 0 )); then
ok "No system generations found."
else
if (( ${#gens[@]} > keep_generations )); then
to_del_count=$(( ${#gens[@]} - keep_generations ))
to_del=( "${gens[@]:0:to_del_count}" )
if [[ "$DRYRUN" == true ]]; then
log "[dry-run] sudo nix-env -p /nix/var/nix/profiles/system --delete-generations ${to_del[*]}"
else
sudo nix-env -p /nix/var/nix/profiles/system --delete-generations "${to_del[@]}"
ok "Removed ${to_del_count}; kept newest ${keep_generations}."
fi
else
ok "Generations (${#gens[@]}) ≤ keep (${keep_generations}); nothing to prune."
fi
fi
########################################################################
# STEP 3: Garbage collect unreferenced store paths
########################################################################
print_line
action "Running Nix GC (and store optimise)…"
if [[ "$DRYRUN" == true ]]; then
log "[dry-run] sudo nix-collect-garbage -d"
log "[dry-run] sudo nix store optimise"
else
sudo nix-collect-garbage -d
# Optimise: dedup store (if subcommand exists)
if command -v nix >/dev/null 2>&1 && nix --help 2>&1 | grep -q 'store optimise'; then
sudo nix store optimise || true
fi
ok "GC complete."
fi
########################################################################
# STEP 4: Tidy logs (rebuild + cleanup)
########################################################################
print_line
action "Tidying logs…"
removed_any=false
# (a) Remove unfinished rebuild logs
if [[ -d "$log_dir_rebuild" ]]; then
for pat in "rebuild-*.log" "Current-Error*.txt"; do
if compgen -G "${log_dir_rebuild}/${pat}" >/dev/null; then
if [[ "$DRYRUN" == true ]]; then
log "[dry-run] rm ${log_dir_rebuild}/${pat}"
else
rm -f ${log_dir_rebuild}/${pat}
removed_any=true
fi
fi
done
# (b) Keep newest K final rebuild logs (nixos-gen_*-switch-*.log)
mapfile -t final_logs < <(ls -1 "${log_dir_rebuild}"/nixos-gen_*-switch-*.log 2>/dev/null | sort)
if (( ${#final_logs[@]} > keep_generations )); then
del_count=$(( ${#final_logs[@]} - keep_generations ))
to_del=( "${final_logs[@]:0:del_count}" )
if [[ "$DRYRUN" == true ]]; then
log "[dry-run] rm ${to_del[*]}"
else
rm -f "${to_del[@]}"
removed_any=true
fi
ok "Rebuild logs: kept newest ${keep_generations}, removed ${del_count}."
else
ok "Rebuild logs count (${#final_logs[@]}) ≤ keep (${keep_generations}); nothing to delete."
fi
else
warn "Rebuild log dir not found: ${log_dir_rebuild}"
fi
# (c) Keep cleanup dir itself trimmed (optional: keep last 10 summaries)
mapfile -t cleanup_logs < <(ls -1 "${log_dir_cleanup}"/cleanup-*.log 2>/dev/null | sort)
if (( ${#cleanup_logs[@]} > 10 )); then
del_count=$(( ${#cleanup_logs[@]} - 10 ))
to_del=( "${cleanup_logs[@]:0:del_count}" )
if [[ "$DRYRUN" == true ]]; then
log "[dry-run] rm ${to_del[*]}"
else
rm -f "${to_del[@]}"
removed_any=true
fi
ok "Cleanup logs: kept newest 10, removed ${del_count}."
fi
# (d) Commit/push log changes if logs live in a git repo
if [[ "$DRYRUN" == false && "$removed_any" == true ]]; then
log_root="$(dirname "$log_dir")"
if git_in_repo "$log_root"; then
(
cd "$log_root"
g add "$(basename "$log_dir")"
git_commit_if_staged "cleanup: pruned logs & system generations"
git_push_if_enabled
)
ok "Logged cleanup committed${auto_push:+ and pushed}."
fi
fi
########################################################################
# SUMMARY
########################################################################
print_line
end_s=$(date +%s)
log "${BOLD}${CYAN}Cleanup Summary${RESET}"
log " Started: ${start_human}"
log " Duration: $(( end_s - start_s )) sec"
(( ${#gens[@]:-0} > 0 )) && log " Gens kept: ${keep_generations} (of ${#gens[@]})"
ok "Done."
}
# Execute when sourced as a script
nyx-cleanup "$@"

387
nyx/bash/nyx-rebuild.sh Normal file
View file

@ -0,0 +1,387 @@
#!/usr/bin/env bash
nyx-rebuild() {
set -euo pipefail
########################################################################
# CONFIGURATION (injected by Nix)
########################################################################
nix_dir="@NIX_DIR@"
log_dir="@LOG_DIR@"
start_editor="@START_EDITOR@"
enable_formatting="@ENABLE_FORMATTING@"
editor_cmd="@EDITOR@"
formatter_cmd="@FORMATTER@"
git_bin="@GIT_BIN@"
nom_bin="@NOM_BIN@"
auto_push="@AUTO_PUSH@"
version="@VERSION@"
########################################################################
# ARGUMENT PARSING
########################################################################
do_repair=false
do_update=false
while [[ $# -gt 0 ]]; do
case "$1" in
--repair) do_repair=true; shift;;
--update) do_update=true; shift;;
-h|--help)
cat <<EOF
nyx-rebuild [--repair] [--update]
--repair Stage & commit the nix_dir with "rebuild - repair <timestamp>"
and remove any unfinished logs (Current-Error*.txt and rebuild-*.log
that are not final nixos-gen_* logs).
--update Before rebuilding, update the flake in nix_dir using:
nix flake update
EOF
return 0
;;
*) echo "Unknown argument: $1" >&2; return 2;;
esac
done
########################################################################
# COLORS (TTY only)
########################################################################
if [[ -t 1 ]]; then
RED=$'\e[31m'; GREEN=$'\e[32m'; YELLOW=$'\e[33m'
BLUE=$'\e[34m'; MAGENTA=$'\e[35m'; CYAN=$'\e[36m'
BOLD=$'\e[1m'; RESET=$'\e[0m'
else
RED=""; GREEN=""; YELLOW=""
BLUE=""; MAGENTA=""; CYAN=""
BOLD=""; RESET=""
fi
########################################################################
# LIGHTWEIGHT GIT HELPERS
########################################################################
g() { "$git_bin" "$@"; }
git_in_repo() {
local dir="$1"
[[ -d "$dir/.git" ]] || (cd "$dir" && g rev-parse --git-dir >/dev/null 2>&1)
}
git_has_uncommitted_changes() {
# prints true if there are changes
[[ -n "$(g status --porcelain)" ]]
}
git_pause_if_dirty() {
local attempts=0
while git_has_uncommitted_changes; do
if (( attempts == 0 )); then
echo "${YELLOW}Uncommitted changes detected!${RESET}"
echo "${RED}Pausing for 5 seconds to allow cancel (Ctrl-C) before attempting repair...${RESET}"
sleep 5
echo "Attempting repair..."
repair || true # never let a no-op commit kill the script
echo "repair ran"
((attempts++)) || true
# loop will re-check cleanliness
else
echo "${YELLOW}Uncommitted changes still present after repair.${RESET}"
echo "${RED}Needs manual review. Continuing in 5 seconds...${RESET}"
sleep 5
break
fi
done
}
git_pull_rebase() {
g pull --rebase
}
git_add_path() {
g add "$@"
}
git_commit_if_staged() {
# commit if there is something staged; ignore empty
if ! g diff --cached --quiet; then
g commit -m "$1" || true
fi
}
git_commit_message() {
local msg="$1"
g commit -m "$msg"
}
git_push_if_enabled() {
if [[ "${auto_push}" == "true" ]]; then
g push
fi
}
git_safe_add_commit_push() {
# Convenience: add paths, commit message, optional push
local msg="$1"; shift
git_add_path "$@"
if git_commit_if_staged "$msg"; then
git_push_if_enabled
fi
}
########################################################################
# REPAIR MODE
########################################################################
repair() {
cd "$nix_dir" || { echo "ERROR: Cannot cd into nix_dir: $nix_dir" >&2; return 1; }
ts="$(date '+%Y-%m-%d_%H-%M-%S')"
echo "Starting repair at ${ts}..."
# Remove unfinished logs (not final logs)
log_dir_rebuild="${log_dir}/rebuild"
if [[ -d "$log_dir_rebuild" ]]; then
echo "Checking for unfinished logs in: $log_dir_rebuild"
if find "$log_dir_rebuild" -type f \
! -name 'nixos-gen_*' \
\( -name 'rebuild-*.log' -o -name 'Current-Error*.txt' \) | grep -q .; then
echo "Removing unfinished logs..."
find "$log_dir_rebuild" -type f \
! -name 'nixos-gen_*' \
\( -name 'rebuild-*.log' -o -name 'Current-Error*.txt' \) \
-exec rm -v {} +
echo "Unfinished logs removed."
else
echo "No unfinished logs found."
fi
else
echo "No rebuild log directory found."
fi
echo "Staging all changes in $nix_dir..."
g add -A
# Oed; avoid set nly commit if something is stag-e failure on empty commit
if ! g diff --cached --quiet --; then
echo "Committing repair changes..."
g commit -m "rebuild - repair ${ts}"
echo "Repair commit created."
else
echo "No changes to commit."
fi
}
########################################################################
# LOGGING / COMMON HELPERS
########################################################################
start_time=$(date +%s)
start_human=$(date '+%Y-%m-%d %H:%M:%S')
stats_duration=0
stats_gen="?"
stats_errors=0
stats_last_error_lines=""
rebuild_success=false
exit_code=1
timestamp=$(date '+%Y-%m-%d_%H-%M-%S')
log_dir_rebuild="${log_dir}/rebuild"
build_log="${log_dir_rebuild}/rebuild-${timestamp}.log"
error_log="${log_dir_rebuild}/Current-Error-${timestamp}.txt"
console-log() { echo -e "$@" | tee -a "$build_log"; }
print_line() { console-log ""; console-log "${BOLD}==================================================${RESET}"; console-log ""; }
run_with_log() {
local tmp; tmp=$(mktemp)
( "$@" 2>&1; echo $? > "$tmp" ) | tee -a "$build_log"
local s; s=$(<"$tmp"); rm "$tmp"; return "$s"
}
run_with_log_rebuild() {
local tmp; tmp=$(mktemp)
( "$@" 2>&1; echo $? > "$tmp" ) | tee -a "$build_log" | $nom_bin
local s; s=$(<"$tmp"); rm "$tmp"; return "$s"
}
########################################################################
# EARLY REPAIR MODE CHECK
########################################################################
if [[ "$do_repair" == true ]]; then
########################################################################
# BANNER
########################################################################
echo
nyx-tool "Nyx" "nyx-rebuild --repair" "$version" \
"Smart NixOS configuration repair" \
"by Peritia-System" \
"https://github.com/Peritia-System/Nyx-Tools" \
"https://github.com/Peritia-System/Nyx-Tools/issues" \
"Fixing our mistake... or yours"
echo
repair
rebuild_success=true
return 0
fi
finish_nyx_rebuild() {
stats_duration=$(( $(date +%s) - start_time ))
echo
if [[ "$rebuild_success" == true ]]; then
echo "${GREEN}${BOLD}NixOS Rebuild Complete${RESET}"
echo "${BOLD}${CYAN}Summary:${RESET}"
echo " Started: $start_human"
echo " Duration: ${stats_duration} sec"
echo " Generation: $stats_gen"
else
echo "${RED}${BOLD}NixOS Rebuild Failed${RESET}"
echo "${BOLD}${RED}Error Stats:${RESET}"
echo " Started: $start_human"
echo " Duration: ${stats_duration} sec"
echo " Error lines: ${stats_errors}"
[[ -n "$stats_last_error_lines" ]] && echo -e "${YELLOW}Last few errors:${RESET}$stats_last_error_lines"
fi
}
trap finish_nyx_rebuild EXIT
########################################################################
# BANNER
########################################################################
echo
nyx-tool "Nyx" "nyx-rebuild" "$version" \
"Smart NixOS configuration rebuilder" \
"by Peritia-System" \
"https://github.com/Peritia-System/Nyx-Tools" \
"https://github.com/Peritia-System/Nyx-Tools/issues" \
"Always up to date for you!"
echo
########################################################################
# PREP
########################################################################
mkdir -p "$log_dir_rebuild"
cd "$nix_dir" || { echo "Cannot cd into nix_dir: $nix_dir" >&2; exit_code=1; return $exit_code; }
########################################################################
# GIT DIRTY CHECK
########################################################################
echo -e "${BOLD}${BLUE}Checking Git status...${RESET}"
git_pause_if_dirty
########################################################################
# NORMAL REBUILD FLOW...
########################################################################
console-log "${BOLD}${BLUE}Pulling latest changes...${RESET}"
if ! run_with_log git pull --rebase; then
exit_code=1; return $exit_code
fi
########################################################################
# OPTIONAL: editor
########################################################################
if [[ "$start_editor" == "true" ]]; then
console-log "${BOLD}${BLUE}Opening editor...${RESET}"
console-log "Started editing: $(date)"
run_with_log "$editor_cmd"
console-log "Finished editing: $(date)"
console-log "${BOLD}${CYAN}Changes summary:${RESET}"
run_with_log git diff --compact-summary
fi
########################################################################
# OPTIONAL: formatter
########################################################################
if [[ "$enable_formatting" == "true" ]]; then
console-log "${BOLD}${MAGENTA}Running formatter...${RESET}"
run_with_log "$formatter_cmd" .
fi
########################################################################
# REBUILD
########################################################################
# Check if update:
print_line
if [[ "$do_update" == true ]]; then
console-log "${BOLD}${BLUE}Updating flake...${RESET}"
print_line
run_with_log nix flake update --verbose
if git_has_uncommitted_changes; then
git_add_path flake.lock
git_commit_if_staged "flake update: $(date '+%Y-%m-%d %H:%M:%S')"
fi
print_line
fi
console-log "${BOLD}${BLUE}Starting system rebuild...${RESET}"
if find ~ -type f -name '*delme-HMbackup' | grep -q .; then
print_line
console-log "Removing old HM conf"
run_with_log find ~ -type f -name '*delme-HMbackup' -exec rm -v {} +
print_line
fi
if sudo -n true 2>/dev/null; then
console-log "Sudo rights already available"
else
console-log "Getting sudo ticket (please enter your password)"
run_with_log sudo whoami > /dev/null
fi
print_line
console-log "Rebuild started: $(date)"
print_line
run_with_log_rebuild sudo nixos-rebuild switch --flake "$nix_dir"
rebuild_status=$?
if [[ $rebuild_status -ne 0 ]]; then
echo "${RED}Rebuild failed at $(date).${RESET}" > "$error_log"
stats_errors=$(grep -Ei -A 1 'error|failed' "$build_log" | tee -a "$error_log" | wc -l || true)
stats_last_error_lines=$(tail -n 10 "$error_log" || true)
# capture and push error artifacts
git_add_path "$log_dir_rebuild"
g commit -m "Rebuild failed: errors logged" || true
git_push_if_enabled
exit_code=1
return $exit_code
fi
########################################################################
# SUCCESS PATH
########################################################################
rebuild_success=true
exit_code=0
gen=$(nixos-rebuild list-generations | grep True | awk '{$1=$1};1' || true)
stats_gen=$(echo "$gen" | awk '{printf "%04d", $1}' || echo "0000")
# Append summary to build log (before rotating file name)
finish_nyx_rebuild >> "$build_log"
# Commit config changes (if any)
git_add_path -u
if git_commit_if_staged "Rebuild: $gen"; then
echo "${BLUE}Commit message:${RESET}${GREEN}Rebuild: $gen${RESET}"
fi
# Move and add final log
final_log="$log_dir_rebuild/nixos-gen_${stats_gen}-switch-${timestamp}.log"
mv "$build_log" "$final_log"
git_add_path "$final_log"
git_commit_if_staged "log for $gen" || echo "${YELLOW}No changes in logs to commit.${RESET}"
git_push_if_enabled && echo "${GREEN}Changes pushed to remote.${RESET}" || true
}
# Execute when sourced as a script
nyx-rebuild "$@"

54
nyx/bash/nyx-tool.sh Normal file
View file

@ -0,0 +1,54 @@
#!/usr/bin/env bash
# nyx-tool: reusable metadata banner printer with Base16 theme
nyx-tool() {
local logo="${1:-Nyx}"
local name="${2:-nix-script}"
local version="${3:-Version Unknown - Please Open Issue}"
local description="${4:-A Nix utility}"
local credit="${5:-Peritia-System}"
local github="${6:-https://github.com/example/repo}"
local issues="${7:-${github}/issues}"
local message="${8:-Use responsibly}"
# Base16 color palette (ANSI escape codes)
local RESET="\033[0m"
local BOLD="\033[1m"
local HEADER="\033[38;5;33m" # blue
local LABEL="\033[38;5;70m" # green
local VALUE="\033[38;5;250m" # gray
local EMPHASIS="\033[38;5;196m" # red
local CYAN="\033[38;5;51m"
local GREEN="\033[38;5;82m"
local line
line=$(printf '=%.0s' $(seq 1 35))
echo ""
echo -e "${HEADER}${line}${RESET}"
echo -e "${HEADER}=====[ ${BOLD}Peritia System Tools${RESET}${HEADER} ]=====${RESET}"
echo -e "${VALUE}${BOLD}"
# Figlet logo rendering
if command -v figlet &>/dev/null; then
figlet -f banner3 "$logo" | sed 's/#/█/g'
else
echo "$logo"
fi
echo -e "${RESET}${HEADER}by Peritia-System${RESET}"
echo -e "${HEADER}${line}${RESET}"
echo ""
echo -e "${LABEL}🛠️ Name: ${VALUE}${name}${RESET}"
echo -e "${LABEL}🏷️ Version: ${VALUE}${version}${RESET}"
echo -e "${LABEL}📝 Description: ${VALUE}${description}${RESET}"
echo -e "${LABEL}👤 Credit: ${VALUE}${credit}${RESET}"
echo -e "${LABEL}🌐 GitHub: ${VALUE}${github}${RESET}"
echo -e "${LABEL}🐛 Issues: ${VALUE}${issues}${RESET}"
echo ""
echo -e "${LABEL}📌 Message: ${BOLD}${message}${RESET}"
echo ""
}
nyx-tool "$@"