From 7f583a7ed9ba2854ca93a5233ce67f18b9b4de22 Mon Sep 17 00:00:00 2001 From: iawdwa Date: Fri, 11 Jul 2025 09:13:09 +0200 Subject: [PATCH] init --- nyx-cleanup.nix | 56 +++++++++++++++++ nyx-rebuild.nix | 87 ++++++++++++++++++++++++++ nyx-tool.nix | 21 +++++++ nyx.nix | 16 +++++ zsh/nyx-cleanup.zsh | 73 ++++++++++++++++++++++ zsh/nyx-rebuild.zsh | 144 ++++++++++++++++++++++++++++++++++++++++++++ zsh/nyx-tool.zsh | 92 ++++++++++++++++++++++++++++ 7 files changed, 489 insertions(+) create mode 100644 nyx-cleanup.nix create mode 100644 nyx-rebuild.nix create mode 100644 nyx-tool.nix create mode 100644 nyx.nix create mode 100644 zsh/nyx-cleanup.zsh create mode 100644 zsh/nyx-rebuild.zsh create mode 100755 zsh/nyx-tool.zsh diff --git a/nyx-cleanup.nix b/nyx-cleanup.nix new file mode 100644 index 0000000..56d31b6 --- /dev/null +++ b/nyx-cleanup.nix @@ -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}" + ''; + }; +} diff --git a/nyx-rebuild.nix b/nyx-rebuild.nix new file mode 100644 index 0000000..a701d9e --- /dev/null +++ b/nyx-rebuild.nix @@ -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}" + ''; + }; +} diff --git a/nyx-tool.nix b/nyx-tool.nix new file mode 100644 index 0000000..32803d2 --- /dev/null +++ b/nyx-tool.nix @@ -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}" + ''; + }; +} diff --git a/nyx.nix b/nyx.nix new file mode 100644 index 0000000..f2c54e5 --- /dev/null +++ b/nyx.nix @@ -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 + ]; +} diff --git a/zsh/nyx-cleanup.zsh b/zsh/nyx-cleanup.zsh new file mode 100644 index 0000000..8bd93b6 --- /dev/null +++ b/zsh/nyx-cleanup.zsh @@ -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 +} diff --git a/zsh/nyx-rebuild.zsh b/zsh/nyx-rebuild.zsh new file mode 100644 index 0000000..0a867ad --- /dev/null +++ b/zsh/nyx-rebuild.zsh @@ -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 +} diff --git a/zsh/nyx-tool.zsh b/zsh/nyx-tool.zsh new file mode 100755 index 0000000..7b97b5a --- /dev/null +++ b/zsh/nyx-tool.zsh @@ -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 +}