This commit is contained in:
iawdwa 2025-07-11 09:13:09 +02:00
commit 7f583a7ed9
7 changed files with 489 additions and 0 deletions

56
nyx-cleanup.nix Normal file
View file

@ -0,0 +1,56 @@
{ config, lib, pkgs, ... }:
let
cfg = config.modules.nyx-cleanup;
scriptTargetPath = "${cfg.nixDirectory}/Misc/zsh/nyx-cleanup.zsh";
in
{
options.modules.nyx-cleanup = {
enable = lib.mkEnableOption "Enable nyx-cleanup Zsh function and Zsh shell";
username = lib.mkOption {
type = lib.types.str;
description = "The name of the user this module applies to.";
};
nixDirectory = lib.mkOption {
type = lib.types.path;
description = "Path to the NixOS configuration directory.";
};
keepGenerations = lib.mkOption {
type = lib.types.int;
default = 5;
description = "Number of NixOS generations to keep during cleanup.";
};
autoPush = lib.mkOption {
type = lib.types.bool;
default = false;
description = "If true, push commits to Git remote after cleanup.";
};
enableAlias = lib.mkOption {
type = lib.types.bool;
default = true;
description = "If true, add `nc` alias for `nyx-cleanup`.";
};
};
config = lib.mkIf cfg.enable {
programs.zsh.enable = lib.mkDefault true;
home.shellAliases = lib.mkIf cfg.enableAlias {
nc = "nyx-cleanup";
};
programs.zsh.initContent = ''
# Nyx Cleanup
nix_dir="${cfg.nixDirectory}"
auto_push="${toString cfg.autoPush}"
keep_generations="${toString cfg.keepGenerations}"
source "${scriptTargetPath}"
'';
};
}

87
nyx-rebuild.nix Normal file
View file

@ -0,0 +1,87 @@
{ config, lib, pkgs, ... }:
let
cfg = config.modules.nyx-rebuild;
scriptTargetPath = "${cfg.nixDirectory}/Misc/zsh/nyx-rebuild.zsh";
in
{
options.modules.nyx-rebuild = {
enable = lib.mkEnableOption "Enable nyx-rebuild Zsh function and Zsh shell";
username = lib.mkOption {
type = lib.types.str;
description = "The name of the user this module applies to.";
};
nixDirectory = lib.mkOption {
type = lib.types.path;
description = "Path to the NixOS configuration directory.";
};
editor = lib.mkOption {
type = lib.types.str;
default = "nvim";
description = "Editor used in nyx-rebuild.";
};
formatter = lib.mkOption {
type = lib.types.str;
default = "alejandra";
description = "Formatter used for Nix files.";
};
startEditor = lib.mkOption {
type = lib.types.bool;
default = false;
description = "If true, starts editor while then rebuilds.";
};
enableFormatting = lib.mkOption {
type = lib.types.bool;
default = false;
description = "If true, uses set Formatter";
};
autoPush = lib.mkOption {
type = lib.types.bool;
default = false;
description = "If true, push commits to Git remote after rebuild.";
};
enableAlias = lib.mkOption {
type = lib.types.bool;
default = true;
description = "If true, add `nr` alias for `nyx-rebuild`.";
};
};
config = lib.mkIf cfg.enable {
programs.zsh.enable = lib.mkDefault true;
# Enable known formatters
## no enable function
home.packages = lib.mkIf (cfg.enableFormatting && cfg.formatter == "alejandra") [
pkgs.alejandra
];
# Add optional alias
home.shellAliases = lib.mkIf cfg.enableAlias {
nr = "nyx-rebuild";
};
# Add to .zshrc
programs.zsh.initContent = ''
# 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}"
'';
};
}

21
nyx-tool.nix Normal file
View file

@ -0,0 +1,21 @@
{ config, lib, pkgs, ... }:
let
cfg = config.modules.nix-tool;
scriptTargetPath = "${cfg.nixDirectory}/Misc/zsh/nyx-rebuild.zsh";
in
{
options.modules.nix-tool.enable = lib.mkEnableOption "Enable nix-tool Zsh function for tool metadata display.";
config = lib.mkIf cfg.enable {
home.packages = [
pkgs.figlet
];
programs.zsh.enable = lib.mkDefault true;
programs.zsh.initContent = ''
source "${scriptTargetPath}"
'';
};
}

16
nyx.nix Normal file
View file

@ -0,0 +1,16 @@
# Import all modules so only needs to Import nyx.nix
{ config, pkgs, lib, ... }:
{
imports = [
# System modules
# Rebuild
./nyx-rebuild.nix
# Custom Banner
./nyx-tool.nix
# Nyx cleanup
./nyx-cleanup.nix
];
}

73
zsh/nyx-cleanup.zsh Normal file
View file

@ -0,0 +1,73 @@
function nyx-cleanup() {
###### CONFIGURATION ######
local version="1.0.0"
local keep_generations="${keep_generations:-5}"
# Setup 16-color ANSI (TTY-safe)
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
print_line() { echo -e "${BOLD}$(printf '%*s\n' "${COLUMNS:-40}" '' | tr ' ' '=')${RESET}"; }
print_line
echo -e "${BOLD}${CYAN}🧼 Nyx Cleanup v${version}${RESET}"
print_line
###### TOOL DESCRIPTION ######
echo -e "${MAGENTA}Cleaning up old generations and Nix garbage...${RESET}"
echo "Started cleanup: $(date)" | tee nixos-cleanup.log
###### GARBAGE COLLECTION ######
echo -e "\n${BLUE}🗑️ Running Nix garbage collection...${RESET}" | tee -a nixos-cleanup.log
sudo nix-collect-garbage -d | tee -a nixos-cleanup.log
###### REMOVE OLD GENERATIONS ######
echo -e "\n${BLUE}🧹 Removing old generations (keeping last ${keep_generations})...${RESET}" | tee -a nixos-cleanup.log
sudo nix-collect-garbage --delete-older-than "${keep_generations}d" | tee -a nixos-cleanup.log
###### OPTIONAL STORE OPTIMIZATION ######
if [[ "${optimize_store}" == "true" ]]; then
echo -e "\n${MAGENTA}🔧 Optimizing the Nix store...${RESET}" | tee -a nixos-cleanup.log
sudo nix-store --optimize | tee -a nixos-cleanup.log
fi
###### SUCCESS SUMMARY ######
print_line | tee -a nixos-cleanup.log
echo -e "${GREEN}${BOLD}✅ Nix cleanup completed successfully!${RESET}" | tee -a nixos-cleanup.log
echo -e "${CYAN}🕒 Finished at: $(date)${RESET}" | tee -a nixos-cleanup.log
print_line | tee -a nixos-cleanup.log
###### OPTIONAL COMMIT LOG ######
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}')
cd "$nix_dir" || return 1
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-cleanup-$timestamp.log"
mv nixos-cleanup.log "$log_file"
git add "$log_file"
if ! git diff --cached --quiet; then
git commit -m "Cleanup log on $timestamp"
echo -e "${GREEN}✅ Cleanup log committed.${RESET}"
else
echo -e "${YELLOW} No new changes in logs to commit.${RESET}"
fi
if [[ "${auto_push}" == "true" ]]; then
echo -e "${BLUE}🚀 Auto-push enabled. Pushing to remote...${RESET}"
git push && echo -e "${GREEN}✅ Changes pushed to remote.${RESET}"
fi
echo -e "\n${GREEN}🎉 Nyx cleanup finished!${RESET}"
print_line
}

144
zsh/nyx-rebuild.zsh Normal file
View file

@ -0,0 +1,144 @@
function nyx-rebuild() {
###### CONFIGURATION ######
local version="1.0.3" # ⚠️ EDIT VERSION HERE
# Setup 16-color ANSI (TTY-safe)
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
print_line() { echo -e "${BOLD}$(printf '%*s\n' "${COLUMNS:-40}" '' | tr ' ' '=')${RESET}"; }
print_line
###### TOOL DESCRIPTION ######
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}"
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}"
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
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
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
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
###### 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
# REBUILDING
sudo nixos-rebuild switch --flake "${nix_dir}" &>nixos-switch.log
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
fi
###### SUCCESS SUMMARY ######
local end_time=$(date +%s)
local duration=$((end_time - start_time))
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 ######
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
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}"
if ! git diff --cached --quiet; then
git commit -m "log for $gen"
echo -e "${YELLOW} Added changes to git ${RESET}"
else
echo -e "${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
}

92
zsh/nyx-tool.zsh Executable file
View file

@ -0,0 +1,92 @@
#!/usr/bin/env zsh
# nix-tool: reusable metadata banner printer with Base16 theme
function nix-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
}