This commit is contained in:
Peritia 2025-07-15 11:25:00 +02:00
parent 49ae0d0f70
commit 26cef4ce83
7 changed files with 391 additions and 104 deletions

View file

@ -1,9 +1,19 @@
function nyx-rebuild() {
###### CONFIGURATION ######
local version="1.0.3" # ⚠️ EDIT VERSION HERE
#################### 🔧 INITIAL SETUP ####################
local version="1.3.0"
local start_time=$(date +%s)
local start_human=$(date '+%Y-%m-%d %H:%M:%S')
local stats_duration=0
local stats_gen="?"
local stats_errors=0
local stats_last_error_lines=""
local rebuild_success=false
local exit_code=1 # default to failure
# Setup 16-color ANSI (TTY-safe)
trap finish_nyx_rebuild
#################### 🎨 ANSI 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'
@ -12,133 +22,183 @@ function nyx-rebuild() {
MAGENTA=""; CYAN=""; BOLD=""; RESET=""
fi
print_line() { echo -e "${BOLD}$(printf '%*s\n' "${COLUMNS:-40}" '' | tr ' ' '=')${RESET}"; }
#################### 📁 PATH SETUP ####################
local log_dir="$nix_dir/Misc/nyx/logs/$(hostname)"
mkdir -p "$log_dir"
local timestamp=$(date '+%Y-%m-%d_%H-%M-%S')
local build_log="$log_dir/build-${timestamp}.log"
local error_log="$log_dir/Current-Error-${timestamp}.txt"
print_line
#################### 🧰 HELPERS ####################
console-log() {
echo -e "$@" | tee -a "$build_log"
}
###### TOOL DESCRIPTION ######
nix-tool \
"Nyx" \
"nyx-rebuild" \
"$version" \
print_line() {
console-log "${BOLD}$(printf '%*s\n' "${COLUMNS:-40}" '' | tr ' ' '=')${RESET}"
}
run_with_log() {
local cmd_output
cmd_output=$(mktemp)
(
"$@" 2>&1
echo $? > "$cmd_output"
) | tee -a "$build_log"
local exit_code=$(<"$cmd_output")
rm "$cmd_output"
return "$exit_code"
}
run_with_log_rebuild() {
local cmd_output
cmd_output=$(mktemp)
(
"$@" 2>&1
echo $? > "$cmd_output"
) | tee -a "$build_log" | nom
local exit_code=$(<"$cmd_output")
rm "$cmd_output"
return "$exit_code"
}
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}🎯 Success Stats:${RESET}"
echo " 🕒 Started: $start_human"
echo " ⏱️ Duration: ${stats_duration} sec"
echo " 📦 Gen: $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 "\n${YELLOW}🧾 Last few errors:${RESET}\n$stats_last_error_lines"
fi
echo
return $exit_code
}
#################### 📘 TOOL INFO ####################
echo
nix-tool "Nyx" "nyx-rebuild" "$version" \
"Smart NixOS configuration rebuilder" \
"by Peritia-System" \
"https://github.com/Peritia-System/nix-os-private" \
"https://github.com/Peritia-System/nix-os-private/issues" \
"Always up to date for you!"
print_line
###### GIT PRECHECKS ######
cd "$nix_dir" || return 1
echo -e "\n${BOLD}${BLUE}📁 Checking Git status...${RESET}"
echo
#################### 📁 PROJECT PREP ####################
cd "$nix_dir" || { exit_code=1; return $exit_code; }
echo "\n${BOLD}${BLUE}📁 Checking Git status...${RESET}"
if [[ -n $(git status --porcelain) ]]; then
echo -e "${YELLOW}⚠️ Uncommitted changes detected!${RESET}" | tee -a nixos-switch.log
echo -e "${RED}⏳ 5s to cancel...${RESET}"
echo "${YELLOW}⚠️ Uncommitted changes detected!${RESET}"
echo "${RED}⏳ 5s to cancel...${RESET}"
sleep 5
# return 1
fi
echo -e "\n${BOLD}${BLUE}⬇️ Pulling latest changes...${RESET}"
if ! git pull --rebase | tee -a nixos-switch.log; then
echo -e "${RED}❌ Git pull failed.${RESET}" | tee -a nixos-switch.log
return 1
#################### 🔄 GIT PULL ####################
console-log "\n${BOLD}${BLUE}⬇️ Pulling latest changes...${RESET}"
if ! run_with_log git pull --rebase; then
exit_code=1; return $exit_code
fi
###### OPTIONAL CONFIG EDITING ######
if [[ "${start_editor}" == "true" ]]; then
echo -e "\n${BOLD}${BLUE}📝 Editing configuration...${RESET}"
echo "Started editing: $(date)" | tee -a nixos-switch.log
$editor_cmd
echo "Finished editing: $(date)" | tee -a nixos-switch.log
#################### 📝 EDIT CONFIG ####################
if [[ "$start_editor" == "true" ]]; then
console-log "\n${BOLD}${BLUE}📝 Editing configuration...${RESET}"
console-log "Started editing: $(date)"
run_with_log $editor_cmd
console-log "Finished editing: $(date)"
fi
###### OPTIONAL FORMATTER ######
if [[ "${enable_formatting}" == "true" ]]; then
echo -e "\n${BOLD}${MAGENTA}🎨 Running formatter...${RESET}" | tee -a nixos-switch.log
$formatter_cmd . >/dev/null
#################### 🎨 FORMAT ####################
if [[ "$enable_formatting" == "true" ]]; then
console-log "\n${BOLD}${MAGENTA}🎨 Running formatter...${RESET}"
run_with_log $formatter_cmd .
fi
###### GIT DIFF SUMMARY ######
echo -e "\n${BOLD}${CYAN}🔍 Changes summary:${RESET}" | tee -a nixos-switch.log
git diff --compact-summary | tee -a nixos-switch.log
#################### 🧾 GIT DIFF ####################
console-log "\n${BOLD}${CYAN}🔍 Changes summary:${RESET}"
run_with_log git diff --compact-summary
###### SYSTEM REBUILD ######
echo -e "\n${BOLD}${BLUE}🔧 Starting system rebuild...${RESET}" | tee -a nixos-switch.log
local start_time=$(date +%s)
print_line | tee -a nixos-switch.log
echo "🛠️ Rebuild started: $(date)" | tee -a nixos-switch.log
print_line | tee -a nixos-switch.log
#################### 🛠️ SYSTEM REBUILD ####################
console-log "\n${BOLD}${BLUE}🔧 Starting system rebuild...${RESET}"
print_line
console-log "🛠️ Removing old HM conf "
run_with_log find ~ -type f -name '*delme-HMbackup' -exec rm -v {} +
print_line
### Sudo session ticket:
console-log "Getting an \`sudo\`-\"Ticket\" to use \`nixos-rebuild\` with \"nom\" "
console-log "please enter your sudo credentials:"
run_with_log sudo whoami > /dev/null
### Now rebuils_
print_line
console-log "🛠️ Rebuild started: $(date)"
print_line
# REBUILDING
sudo nixos-rebuild switch --flake "${nix_dir}" &>nixos-switch.log
run_with_log_rebuild sudo nixos-rebuild switch --flake "$nix_dir"
local rebuild_status=$?
if [[ $rebuild_status -ne 0 ]]; then
echo -e "\n${BOLD}${RED}❌ Rebuild failed at $(date). Showing errors:${RESET}" | tee -a nixos-switch.log
echo "${RED}❌ Rebuild failed at $(date). Showing errors:${RESET}" > Current-Error.txt
grep --color=auto -Ei 'error|failed' nixos-switch.log || true
grep --color=auto -Ei 'error|failed' nixos-switch.log || true >> Current-Error.txt
git add Current-Error.txt
git commit -m "Rebuild failed"
return 1
#console-log "\n${BOLD}${RED}❌ Rebuild failed at $(date). Showing errors:${RESET}"
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)
stats_last_error_lines=$(tail -n 10 "$error_log")
finish_nyx_rebuild | tee -a "$error_log"
git add "$log_dir"
git commit -m "Rebuild failed: errors logged"
if [[ "$auto_push" == "true" ]]; then
run_with_log git push && console-log "${GREEN}✅ Error log pushed to remote.${RESET}"
fi
exit_code=1
return $exit_code
fi
###### SUCCESS SUMMARY ######
local end_time=$(date +%s)
local duration=$((end_time - start_time))
#################### ✅ SUCCESS FLOW ####################
rebuild_success=true
exit_code=0
print_line | tee -a nixos-switch.log
echo -e "${GREEN}${BOLD}✅ NixOS rebuild completed successfully!${RESET}" | tee -a nixos-switch.log
echo -e "${CYAN}⏱️ Total rebuild time: $((duration / 60)) min $((duration % 60)) sec${RESET}" | tee -a nixos-switch.log
print_line | tee -a nixos-switch.log
local gen
gen=$(nixos-rebuild list-generations | grep True | awk '{$1=$1};1')
gen_nmbr=$(nixos-rebuild list-generations | grep True | awk '{$1=$1};1' | awk '{printf "%04d\n", $1}')
echo -e "${BOLD}${GREEN}🎉 Done. Enjoy your freshly rebuilt system!${RESET}" | tee -a nixos-switch.log
print_line | tee -a nixos-switch.log
###### GENERATION INFO + GIT COMMIT ######
gen=$(nixos-rebuild list-generations | grep True | awk '{$1=$1};1')
stats_gen=$(echo "$gen" | awk '{printf "%04d\n", $1}')
finish_nyx_rebuild >> $build_log
git add -u
git diff --cached --quiet || git commit -m "Rebuild: $gen"
echo -e "${BLUE}🔧 Commit message:${RESET}" | tee -a nixos-switch.log
echo -e "${GREEN}Rebuild: $gen${RESET}" | tee -a nixos-switch.log
print_line | tee -a nixos-switch.log
echo -e "\n${GREEN}✅ Changes committed.${RESET}" | tee -a nixos-switch.log
###### AUTO PUSH ######
if [[ "${auto_push}" == "true" ]]; then
echo -e "${BLUE}🚀 Auto-push enabled:${RESET}" | tee -a nixos-switch.log
echo -e "\n${BOLD}${BLUE}🚀 Pushing to remote...${RESET}" | tee -a nixos-switch.log
git push && echo -e "${GREEN}✅ Changes pushed to remote.${RESET}" | tee -a nixos-switch.log
else
echo -e "${YELLOW}📌 Auto-push is disabled. Remember to push manually if needed.${RESET}" | tee -a nixos-switch.log
if ! git diff --cached --quiet; then
git commit -m "Rebuild: $gen"
console-log "${BLUE}🔧 Commit message:${RESET}\n${GREEN}Rebuild: $gen${RESET}"
fi
###### LOG ARCHIVING ######
local log_dir="$nix_dir/Misc/nyx/logs/$(hostname)"
mkdir -p "$log_dir"
local timestamp=$(date '+%Y-%m-%d_%H-%M-%S')
local log_file="$log_dir/nixos-gen_$gen_nmbr-switch-$timestamp.log"
mv nixos-switch.log "$log_file"
git add "$log_file"
echo -e "${YELLOW}Moved Logfile ${RESET}"
local final_log="$log_dir/nixos-gen_${stats_gen}-switch-${timestamp}.log"
mv "$build_log" "$final_log"
git add "$final_log"
if ! git diff --cached --quiet; then
git commit -m "log for $gen"
echo -e "${YELLOW} Added changes to git ${RESET}"
echo "${YELLOW} Added changes to git${RESET}"
else
echo -e "${YELLOW} No changes in logs to commit.${RESET}"
echo "${YELLOW} No changes in logs to commit.${RESET}"
fi
###### AUTO PUSH ######
if [[ "${auto_push}" == "true" ]]; then
git push && echo -e "${GREEN}✅ Changes pushed to remote.${RESET}"
fi
echo -e "\n${GREEN}🎉 Nyx rebuild completed successfully!${RESET}"
print_line
if [[ "$auto_push" == "true" ]]; then
git push && echo "${GREEN}✅ Changes pushed to remote.${RESET}"
fi
echo "\n${GREEN}🎉 Nyx rebuild completed successfully!${RESET}"
finish_nyx_rebuild
#return $exit_code
}