Adding Importing functionality
This commit is contained in:
parent
62594d312c
commit
2f9ff34ad4
17 changed files with 1188 additions and 307 deletions
304
other/bash/nyx-rebuild.sh
Normal file
304
other/bash/nyx-rebuild.sh
Normal file
|
|
@ -0,0 +1,304 @@
|
|||
#!/usr/bin/env bash
|
||||
nyx-rebuild () {
|
||||
set -euo pipefail
|
||||
|
||||
# === CONFIGURATION ===
|
||||
nix_dir="${nixDirStr}"
|
||||
log_dir="${toString cfg.logDir}"
|
||||
start_editor="${if cfg.startEditor then "true" else "false"}"
|
||||
enable_formatting="${if cfg.enableFormatting then "true" else "false"}"
|
||||
editor_cmd="${cfg.editor}"
|
||||
formatter_cmd="${cfg.formatter}"
|
||||
auto_push_log="${if cfg.autoPushLog then "true" else "false"}"
|
||||
auto_push_nixdir="${if cfg.autoPushNixDir then "true" else "false"}"
|
||||
git_bin="${pkgs.git}/bin/git"
|
||||
|
||||
# === INITIAL SETUP ===
|
||||
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
|
||||
|
||||
# === 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
|
||||
|
||||
# === LOGGING SETUP ===
|
||||
hostname=$(hostname)
|
||||
log_dir="$nix_dir/Misc/nyx/logs/$hostname"
|
||||
mkdir -p "$log_dir"
|
||||
timestamp=$(date '+%Y-%m-%d_%H-%M-%S')
|
||||
build_log="$log_dir/build-''${timestamp}.log"
|
||||
error_log="$log_dir/Current-Error-''${timestamp}.txt"
|
||||
|
||||
# === HELPERS ===
|
||||
console-log() {
|
||||
echo -e "$@" | tee -a "$build_log"
|
||||
}
|
||||
|
||||
|
||||
# === LOGGING ===
|
||||
console-log() {
|
||||
echo -e "$@" | tee -a "$build_log"
|
||||
}
|
||||
|
||||
print_line() {
|
||||
console-log ""
|
||||
console-log "''${BOLD}==================================================''${RESET}"
|
||||
console-log ""
|
||||
}
|
||||
|
||||
|
||||
run_with_log() {
|
||||
local cmd_output
|
||||
cmd_output=$(mktemp)
|
||||
(
|
||||
"$@" 2>&1
|
||||
echo $? > "$cmd_output"
|
||||
) | tee -a "$build_log"
|
||||
local status
|
||||
status=$(<"$cmd_output")
|
||||
rm "$cmd_output"
|
||||
return "$status"
|
||||
}
|
||||
|
||||
run_with_log_rebuild() {
|
||||
local cmd_output
|
||||
cmd_output=$(mktemp)
|
||||
(
|
||||
"$@" 2>&1
|
||||
echo $? > "$cmd_output"
|
||||
) | tee -a "$build_log" | nom
|
||||
local status
|
||||
status=$(<"$cmd_output")
|
||||
rm "$cmd_output"
|
||||
return "$status"
|
||||
}
|
||||
|
||||
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 -e "\n''${YELLOW}🧾 Last few errors:''${RESET}\n$stats_last_error_lines"
|
||||
fi
|
||||
}
|
||||
|
||||
trap finish_nyx_rebuild EXIT
|
||||
|
||||
# === TOOL INFO ===
|
||||
|
||||
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
|
||||
|
||||
# === INITIAL SETUP ===
|
||||
mkdir -p "$log_dir"
|
||||
timestamp=$(date '+%Y-%m-%d_%H-%M-%S')
|
||||
build_log="$log_dir/rebuild-''${timestamp}.log"
|
||||
repo_dir="$(dirname "$(dirname "$log_dir")")"
|
||||
|
||||
|
||||
# === PROJECT PREP ===
|
||||
cd "$nix_dir" || { exit_code=1; return $exit_code; }
|
||||
|
||||
|
||||
console-log "\n''${BOLD}''${BLUE}📁 Checking Git status...''${RESET}"
|
||||
if [[ -n $(git status --porcelain) ]]; then
|
||||
echo "''${YELLOW}⚠️ Uncommitted changes detected!''${RESET}"
|
||||
echo "''${RED}⏳ 5s to cancel...''${RESET}"
|
||||
sleep 5
|
||||
fi
|
||||
|
||||
# === SCRIPT START ===
|
||||
print_line
|
||||
console-log "''${BLUE}''${BOLD}🚀 Starting Nyx Rebuild...''${RESET}"
|
||||
|
||||
# === GIT PULL ===
|
||||
console-log "\n''${BOLD}''${BLUE}⬇️ Pulling latest changes...''${RESET}"
|
||||
if ! run_with_log $git_bin --rebase; then
|
||||
exit_code=1; return $exit_code
|
||||
fi
|
||||
|
||||
# === OPTIONAL: OPEN EDITOR ===
|
||||
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: FORMAT FILES ===
|
||||
if [[ "$enable_formatting" == "true" ]]; then
|
||||
console-log "\n''${BOLD}''${MAGENTA}🎨 Running formatter...''${RESET}"
|
||||
run_with_log "$formatter_cmd" .
|
||||
fi
|
||||
|
||||
# === GIT DIFF ===
|
||||
console-log "\n''${BOLD}''${CYAN}🔍 Changes summary:''${RESET}"
|
||||
run_with_log git diff --compact-summary
|
||||
|
||||
# === 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
|
||||
console-log "Getting sudo ticket (please enter your password)"
|
||||
run_with_log sudo whoami > /dev/null
|
||||
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)
|
||||
stats_last_error_lines=$(tail -n 10 "$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 FLOW ===
|
||||
rebuild_success=true
|
||||
exit_code=0
|
||||
|
||||
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
|
||||
if ! git diff --cached --quiet; then
|
||||
git commit -m "Rebuild: $gen"
|
||||
console-log "''${BLUE}🔧 Commit message:''${RESET}\n''${GREEN}Rebuild: $gen''${RESET}"
|
||||
fi
|
||||
|
||||
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 "''${YELLOW}ℹ️ Added changes to git''${RESET}"
|
||||
else
|
||||
echo "''${YELLOW}ℹ️ No changes in logs to commit.''${RESET}"
|
||||
fi
|
||||
|
||||
if [[ "$auto_push" == "true" ]]; then
|
||||
git push && echo "''${GREEN}✅ Changes pushed to remote.''${RESET}"
|
||||
fi
|
||||
|
||||
echo -e "\n''${GREEN}🎉 Nyx rebuild completed successfully!''${RESET}"
|
||||
finish_nyx_rebuild
|
||||
#return $exit_code
|
||||
}
|
||||
nyx-rebuild
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# === SUCCESS FLOW ===
|
||||
rebuild_success=true
|
||||
exit_code=0
|
||||
|
||||
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
|
||||
if ! git diff --cached --quiet; then
|
||||
git commit -m "Rebuild: $gen"
|
||||
console-log "''${BLUE}🔧 Commit message:''${RESET}\n''${GREEN}Rebuild: $gen''${RESET}"
|
||||
fi
|
||||
|
||||
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 "''${YELLOW}ℹ️ Added changes to git''${RESET}"
|
||||
else
|
||||
echo "''${YELLOW}ℹ️ No changes in logs to commit.''${RESET}"
|
||||
fi
|
||||
|
||||
echo -e "\n''${GREEN}🎉 Nyx rebuild completed successfully!''${RESET}"
|
||||
finish_nyx_rebuild
|
||||
|
||||
|
||||
|
||||
if [[ $? -ne 0 ]]; then
|
||||
console-log "''${RED}''${BOLD}❌ Rebuild failed. See log: $build_log''${RESET}"
|
||||
$git_bin add "$build_log"
|
||||
$git_bin commit -m "chore(rebuild): failed rebuild on $(date)" || true
|
||||
if [[ "$auto_push_nixdir" == "true" ]]; then
|
||||
(
|
||||
cd "$nix_dir"
|
||||
if $git_bin remote | grep -q .; then
|
||||
$git_bin push && console-log "''${GREEN}✅ Nix config pushed to remote.''${RESET}"
|
||||
else
|
||||
console-log "''${YELLOW}⚠️ No Git remote configured in nixDirectory.''${RESET}"
|
||||
fi
|
||||
)
|
||||
fi
|
||||
exit 1
|
||||
fi
|
||||
|
||||
print_line
|
||||
console-log "''${GREEN}''${BOLD}✅ NixOS rebuild complete!''${RESET}"
|
||||
|
||||
101
other/bash/nyx-tool.sh
Normal file
101
other/bash/nyx-tool.sh
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
#!/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 ""
|
||||
}
|
||||
|
||||
# Spinner with timing
|
||||
nyx-show_spinner() {
|
||||
local pid=$1
|
||||
local spinner_delay=0.1
|
||||
local spinstr='|/-\'
|
||||
local start_time
|
||||
start_time=$(date +%s%3N)
|
||||
|
||||
local CYAN="\033[38;5;51m"
|
||||
local GREEN="\033[38;5;82m"
|
||||
local RESET="\033[0m"
|
||||
|
||||
echo -ne "${CYAN}⏳ Starting rebuild...${RESET} "
|
||||
|
||||
(
|
||||
while kill -0 "$pid" 2>/dev/null; do
|
||||
local now elapsed seconds milliseconds clock_time
|
||||
now=$(date +%s%3N)
|
||||
elapsed=$((now - start_time))
|
||||
seconds=$((elapsed / 1000))
|
||||
milliseconds=$((elapsed % 1000))
|
||||
clock_time=$(date +%T)
|
||||
|
||||
for i in $(seq 0 3); do
|
||||
printf "\r${CYAN}⏳ [%s] %s [%d.%03ds] ${RESET}" "$clock_time" "${spinstr:$i:1}" "$seconds" "$milliseconds"
|
||||
sleep "$spinner_delay"
|
||||
done
|
||||
done
|
||||
) &
|
||||
local spinner_pid=$!
|
||||
|
||||
wait "$pid"
|
||||
local exit_status=$?
|
||||
|
||||
kill "$spinner_pid" 2>/dev/null
|
||||
wait "$spinner_pid" 2>/dev/null
|
||||
|
||||
local end_time total_elapsed total_sec total_ms end_clock_time
|
||||
end_time=$(date +%s%3N)
|
||||
total_elapsed=$((end_time - start_time))
|
||||
total_sec=$((total_elapsed / 1000))
|
||||
total_ms=$((total_elapsed % 1000))
|
||||
end_clock_time=$(date +%T)
|
||||
|
||||
echo -e "\r${GREEN}✅ Completed at ${end_clock_time}, total: ${total_sec}.${total_ms}s${RESET} "
|
||||
|
||||
return $exit_status
|
||||
}
|
||||
37
other/example/configuration.nix
Normal file
37
other/example/configuration.nix
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
{ config, pkgs, host, lib, inputs, userconf, ... }:
|
||||
|
||||
let
|
||||
username = "YOUR_USER";
|
||||
nixDirectory = "/home/${username}/NixOS";
|
||||
in {
|
||||
################################################################
|
||||
# Module Imports
|
||||
################################################################
|
||||
|
||||
imports = [
|
||||
# Home Manager integration
|
||||
inputs.home-manager.nixosModules.home-manager
|
||||
];
|
||||
|
||||
################################################################
|
||||
# Home Manager Configuration
|
||||
################################################################
|
||||
|
||||
home-manager = {
|
||||
useGlobalPkgs = true;
|
||||
useUserPackages = true;
|
||||
backupFileExtension = "delme-HMbackup";
|
||||
users.${username} = import ./home.nix;
|
||||
|
||||
extraSpecialArgs = {
|
||||
inherit inputs nixDirectory username;
|
||||
};
|
||||
};
|
||||
################################################################
|
||||
# System Version
|
||||
################################################################
|
||||
|
||||
system.stateVersion = "25.05";
|
||||
|
||||
# ... Add more
|
||||
}
|
||||
30
other/example/flake.nix
Normal file
30
other/example/flake.nix
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
{
|
||||
description = "EXAMPLE-flake";
|
||||
|
||||
inputs = {
|
||||
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
|
||||
home-manager.url = "github:nix-community/home-manager";
|
||||
home-manager.inputs.nixpkgs.follows = "nixpkgs";
|
||||
nyx.url = "github:Peritia-System/Nyx-Tools";
|
||||
};
|
||||
outputs = inputs @ {
|
||||
nixpkgs,
|
||||
home-manager,
|
||||
nyx,
|
||||
...
|
||||
}: {
|
||||
nixosConfigurations = {
|
||||
default = nixpkgs.lib.nixosSystem {
|
||||
system = "x86_64-linux";
|
||||
specialArgs = {
|
||||
inherit inputs self;
|
||||
host = "default";
|
||||
};
|
||||
modules = [
|
||||
./Configurations/Hosts/Default/configuration.nix
|
||||
# No need to include nyx here
|
||||
];
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
53
other/example/home.nix
Normal file
53
other/example/home.nix
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
{ config, nixDirectory, username, pkgs, inputs, ... }:
|
||||
|
||||
{
|
||||
|
||||
################################################################
|
||||
# Module Imports
|
||||
################################################################
|
||||
|
||||
imports = [
|
||||
# Other Home Manager Modules
|
||||
# ......
|
||||
inputs.nyx.homeManagerModules.default
|
||||
];
|
||||
|
||||
################################################################
|
||||
# Nyx Tools Configuration
|
||||
################################################################
|
||||
|
||||
nyx.nyx-rebuild = {
|
||||
enable = true;
|
||||
inherit username nixDirectory;
|
||||
editor = "nvim";
|
||||
formatter = "alejandra";
|
||||
enableAlias = false;
|
||||
autoPushLog = false;
|
||||
autoPushNixDir = false;
|
||||
startEditor = false;
|
||||
logDir = "/home/${username}/.nyx/nyx-rebuild/logs";
|
||||
};
|
||||
|
||||
nyx.nyx-cleanup = {
|
||||
enable = true;
|
||||
inherit username nixDirectory;
|
||||
autoPush = false;
|
||||
keepGenerations = 5;
|
||||
enableAlias = false;
|
||||
logDir = "/home/${username}/.nyx/nyx-rebuild/logs";
|
||||
};
|
||||
|
||||
nyx.nyx-tool = {
|
||||
enable = true;
|
||||
inherit nixDirectory;
|
||||
};
|
||||
|
||||
|
||||
################################################################
|
||||
# Basic Home Manager Setup
|
||||
################################################################
|
||||
|
||||
home.username = username;
|
||||
home.homeDirectory = "/home/${username}";
|
||||
home.stateVersion = "25.05";
|
||||
}
|
||||
108
other/zsh/nyx-cleanup.zsh
Normal file
108
other/zsh/nyx-cleanup.zsh
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
# Extract cfg values into local variables
|
||||
nix_dir="${cfg.nixDirectory}"
|
||||
start_editor="${toString cfg.startEditor}"
|
||||
enable_formatting="${toString cfg.enableFormatting}"
|
||||
editor_cmd="${cfg.editor}"
|
||||
formatter_cmd="${cfg.formatter}"
|
||||
auto_push="${toString cfg.autoPush}"
|
||||
source "${scriptTargetPath}"
|
||||
|
||||
function nyx-cleanup() {
|
||||
##### 🛠️ CONFIGURATION #####
|
||||
local version="1.3.1"
|
||||
local keep_generations="${keep_generations:-5}"
|
||||
local start_human=$(date '+%Y-%m-%d %H:%M:%S')
|
||||
local nix_cleanup_log="nixos-cleanup.log"
|
||||
local optimize_store="${optimize_store:-false}"
|
||||
local auto_push="${auto_push:-false}"
|
||||
|
||||
local RED=$'\e[31m'; local GREEN=$'\e[32m'; local YELLOW=$'\e[33m'
|
||||
local BLUE=$'\e[34m'; local MAGENTA=$'\e[35m'; local CYAN=$'\e[36m'
|
||||
local BOLD=$'\e[1m'; local RESET=$'\e[0m'
|
||||
|
||||
##### 📁 PATH SETUP #####
|
||||
local timestamp=$(date '+%Y-%m-%d_%H-%M-%S')
|
||||
local hostname_id=$(hostname)
|
||||
local log_dir="$nix_dir/Misc/nyx/logs/$hostname_id"
|
||||
mkdir -p "$log_dir"
|
||||
local cleanup_log="$log_dir/cleanup-$timestamp.log"
|
||||
local log_file="$log_dir/nixos-gen-cleanup-$timestamp.log"
|
||||
|
||||
##### 🧰 HELPERS #####
|
||||
console-log() {
|
||||
echo -e "$@" | tee -a "$cleanup_log"
|
||||
}
|
||||
|
||||
print_line() {
|
||||
console-log "${BOLD}$(printf '%*s\n' "${COLUMNS:-40}" '' | tr ' ' '=')${RESET}"
|
||||
}
|
||||
|
||||
format_bytes() {
|
||||
num=$1
|
||||
echo $(numfmt --to=iec-i --suffix=B "$num")
|
||||
}
|
||||
|
||||
disk_usage() {
|
||||
df --output=used /nix/store | tail -1
|
||||
}
|
||||
|
||||
##### 📘 TOOL INFO #####
|
||||
print_line
|
||||
nyx-tool "Nyx" "nyx-cleanup" "$version" \
|
||||
"Smart NixOS configuration cleanup" \
|
||||
"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
|
||||
echo -e "${BOLD}${CYAN}🧼 Nyx Cleanup v$version — Starting...${RESET}"
|
||||
print_line
|
||||
|
||||
##### 📊 STATS: BEFORE #####
|
||||
local disk_before=$(disk_usage)
|
||||
console-log "${CYAN}📊 Disk used before cleanup: $(format_bytes $disk_before)${RESET}"
|
||||
|
||||
##### 🧹 EXECUTION #####
|
||||
console-log "\n${BLUE}🗑️ Collecting Nix garbage...${RESET}"
|
||||
sudo nix-collect-garbage -d >> "$cleanup_log" 2>&1
|
||||
|
||||
console-log "\n${BLUE}🧹 Deleting old generations (keep $keep_generations)...${RESET}"
|
||||
sudo nix-collect-garbage --delete-older-than "${keep_generations}d" >> "$cleanup_log" 2>&1
|
||||
|
||||
if [[ "$optimize_store" == "true" ]]; then
|
||||
console-log "\n${MAGENTA}🔧 Optimizing the Nix store...${RESET}"
|
||||
sudo nix-store --optimize >> "$cleanup_log" 2>&1
|
||||
fi
|
||||
|
||||
##### 📊 STATS: AFTER #####
|
||||
local disk_after=$(disk_usage)
|
||||
local space_freed=$((disk_before - disk_after))
|
||||
print_line
|
||||
console-log "${GREEN}${BOLD}✅ Cleanup Completed Successfully!${RESET}"
|
||||
console-log "${CYAN}🕒 Finished at: $(date)${RESET}"
|
||||
console-log "${CYAN}📊 Disk used after cleanup: $(format_bytes $disk_after)${RESET}"
|
||||
console-log "${CYAN}💾 Space freed: $(format_bytes $space_freed)${RESET}"
|
||||
print_line
|
||||
|
||||
##### 📝 GIT LOGGING #####
|
||||
local gen_nmbr=$(nixos-rebuild list-generations | grep True | awk '{print $1}' | tail -1 | xargs printf "%04d\n")
|
||||
|
||||
cd "$nix_dir" || return 1
|
||||
mv "$nix_cleanup_log" "$log_file"
|
||||
git add "$log_file"
|
||||
|
||||
if ! git diff --cached --quiet; then
|
||||
git commit -m "Cleanup log on $timestamp"
|
||||
console-log "${GREEN}✅ Cleanup log committed.${RESET}"
|
||||
else
|
||||
console-log "${YELLOW}ℹ️ No new changes in logs to commit.${RESET}"
|
||||
fi
|
||||
|
||||
if [[ "$auto_push" == "true" ]]; then
|
||||
console-log "${BLUE}🚀 Auto-push enabled. Pushing to remote...${RESET}"
|
||||
git push && console-log "${GREEN}✅ Changes pushed to remote.${RESET}"
|
||||
fi
|
||||
|
||||
console-log "\n${GREEN}🎉 Nyx cleanup finished!${RESET}"
|
||||
print_line
|
||||
}
|
||||
204
other/zsh/nyx-rebuild.zsh
Normal file
204
other/zsh/nyx-rebuild.zsh
Normal file
|
|
@ -0,0 +1,204 @@
|
|||
function nyx-rebuild() {
|
||||
|
||||
#################### 🔧 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
|
||||
|
||||
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'
|
||||
else
|
||||
RED=""; GREEN=""; YELLOW=""; BLUE=""
|
||||
MAGENTA=""; CYAN=""; BOLD=""; RESET=""
|
||||
fi
|
||||
|
||||
#################### 📁 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"
|
||||
|
||||
#################### 🧰 HELPERS ####################
|
||||
console-log() {
|
||||
echo -e "$@" | tee -a "$build_log"
|
||||
}
|
||||
|
||||
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
|
||||
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
|
||||
|
||||
#################### 📁 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 "${YELLOW}⚠️ Uncommitted changes detected!${RESET}"
|
||||
echo "${RED}⏳ 5s to cancel...${RESET}"
|
||||
sleep 5
|
||||
fi
|
||||
|
||||
#################### 🔄 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
|
||||
|
||||
#################### 📝 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
|
||||
|
||||
#################### 🎨 FORMAT ####################
|
||||
if [[ "$enable_formatting" == "true" ]]; then
|
||||
console-log "\n${BOLD}${MAGENTA}🎨 Running formatter...${RESET}"
|
||||
run_with_log $formatter_cmd .
|
||||
fi
|
||||
|
||||
#################### 🧾 GIT DIFF ####################
|
||||
console-log "\n${BOLD}${CYAN}🔍 Changes summary:${RESET}"
|
||||
run_with_log git diff --compact-summary
|
||||
|
||||
#################### 🛠️ 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
|
||||
|
||||
run_with_log_rebuild sudo nixos-rebuild switch --flake "$nix_dir"
|
||||
local rebuild_status=$?
|
||||
|
||||
if [[ $rebuild_status -ne 0 ]]; then
|
||||
#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 FLOW ####################
|
||||
rebuild_success=true
|
||||
exit_code=0
|
||||
|
||||
|
||||
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
|
||||
if ! git diff --cached --quiet; then
|
||||
git commit -m "Rebuild: $gen"
|
||||
console-log "${BLUE}🔧 Commit message:${RESET}\n${GREEN}Rebuild: $gen${RESET}"
|
||||
fi
|
||||
|
||||
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 "${YELLOW}ℹ️ Added changes to git${RESET}"
|
||||
else
|
||||
echo "${YELLOW}ℹ️ No changes in logs to commit.${RESET}"
|
||||
fi
|
||||
|
||||
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
|
||||
|
||||
}
|
||||
92
other/zsh/nyx-tool.zsh
Normal file
92
other/zsh/nyx-tool.zsh
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
#!/usr/bin/env zsh
|
||||
|
||||
# nyx-tool: reusable metadata banner printer with Base16 theme
|
||||
function 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 (using tput for portability or ANSI codes)
|
||||
local RESET="\033[0m"
|
||||
local BOLD="\033[1m"
|
||||
local HEADER="\033[38;5;33m" # Approx base0D (blue)
|
||||
local LABEL="\033[38;5;70m" # Approx base0B (green)
|
||||
local VALUE="\033[38;5;250m" # Approx base05 (gray)
|
||||
local EMPHASIS="\033[38;5;196m" # Approx base08 (red)
|
||||
|
||||
local line
|
||||
line=$(printf '=%.0s' {1..35})
|
||||
|
||||
echo ""
|
||||
echo -e "${HEADER}${line}${RESET}"
|
||||
echo -e "${HEADER}=====[ ${BOLD}Peritia System Tools${RESET}${HEADER} ]=====${RESET}"
|
||||
echo -e "
|
||||
${VALUE}${BOLD}
|
||||
|
||||
$(figlet -f banner3 "${logo}" | sed 's/#/█/g') ${RESET}${HEADER}by Peritia-System${RESET}
|
||||
|
||||
${RESET}"
|
||||
|
||||
#cat ../Logo-68px.txt
|
||||
|
||||
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-show_spinner() {
|
||||
local pid=$1
|
||||
local spinner_delay=0.1
|
||||
local spinstr='|/-\'
|
||||
local start_time=$(date +%s%3N)
|
||||
|
||||
echo -ne "${CYAN}⏳ Starting rebuild...${RESET} "
|
||||
|
||||
(
|
||||
while kill -0 "$pid" 2>/dev/null; do
|
||||
local now=$(date +%s%3N)
|
||||
local elapsed=$((now - start_time))
|
||||
local seconds=$((elapsed / 1000))
|
||||
local milliseconds=$((elapsed % 1000))
|
||||
local clock_time=$(date +%T) # format: HH:MM:SS
|
||||
|
||||
for i in $(seq 0 3); do
|
||||
printf "\r${CYAN}⏳ [%s] %s [%d.%03ds] ${RESET}" "$clock_time" "${spinstr:$i:1}" "$seconds" "$milliseconds"
|
||||
sleep $spinner_delay
|
||||
done
|
||||
done
|
||||
) &
|
||||
local spinner_pid=$!
|
||||
|
||||
# Wait for the main process
|
||||
wait "$pid"
|
||||
local exit_status=$?
|
||||
|
||||
# Kill spinner and wait
|
||||
kill "$spinner_pid" 2>/dev/null
|
||||
wait "$spinner_pid" 2>/dev/null
|
||||
|
||||
local end_time=$(date +%s%3N)
|
||||
local total_elapsed=$((end_time - start_time))
|
||||
local total_sec=$((total_elapsed / 1000))
|
||||
local total_ms=$((total_elapsed % 1000))
|
||||
local end_clock_time=$(date +%T)
|
||||
|
||||
echo -e "\r${GREEN}✅ Completed at ${end_clock_time}, total: ${total_sec}.${total_ms}s${RESET} "
|
||||
|
||||
return $exit_status
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue