Version 1.1.0
Added: - TUI - A few comments
This commit is contained in:
parent
f26e6f5020
commit
7e5f29cf9f
9 changed files with 442 additions and 331 deletions
77
README.md
77
README.md
|
|
@ -1,19 +1,19 @@
|
|||
# ⚙️ Nyx: NixOS System Management Toolkit
|
||||
# Nyx: NixOS System Management Toolkit
|
||||
|
||||
**Nyx** is a modular toolkit that simplifies and automates various NixOS system management tasks, from enhanced rebuilds to cleanup and shell customization.
|
||||
|
||||
---
|
||||
|
||||
## ✨ Features
|
||||
## Features
|
||||
|
||||
* 🔁 **Enhanced NixOS Rebuilds** — via `nyx-rebuild.nix`
|
||||
* 🧹 **Automated System Cleanup** — via `nyx-cleanup.nix`
|
||||
* 🛠️ **Shell Customization & Tooling** — banners and helpers via `nyx-tool.nix`
|
||||
* 🧩 **All-in-One Integration** — enable everything with a single import: `nyx.nix`
|
||||
* **Enhanced NixOS Rebuilds** — via `nyx-rebuild.nix`
|
||||
* **Automated System Cleanup** — via `nyx-cleanup.nix`
|
||||
* **Shell Customization & Tooling** — banners and helpers via `nyx-tool.nix`
|
||||
* **All-in-One Integration** — enable everything with a single import: `nyx.nix`
|
||||
|
||||
---
|
||||
|
||||
## 📦 Dependencies
|
||||
## Dependencies
|
||||
|
||||
| Tool / Service | Required | Notes |
|
||||
| -------------------- | -------- | -------------------------------------------------------- |
|
||||
|
|
@ -25,37 +25,51 @@
|
|||
|
||||
---
|
||||
|
||||
## 📁 Project Structure
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
Nyx-Tools
|
||||
├── default.nix # Top-level module
|
||||
├── nyx-tool.nix # Shell enhancements and banners
|
||||
├── nyx-rebuild.nix # Enhanced nixos-rebuild logic
|
||||
├── nyx-cleanup.nix # System cleanup automation
|
||||
└── other/ # Legacy scripts (to be removed soon)
|
||||
├── nyx
|
||||
│ ├── bash
|
||||
│ │ ├── nyx-cleanup.sh
|
||||
│ │ ├── nyx-rebuild.sh
|
||||
│ │ ├── nyx-tool.sh
|
||||
│ │ └── nyx-tui.sh
|
||||
│ ├── default.nix
|
||||
│ ├── nyx-cleanup.nix
|
||||
│ ├── nyx-rebuild.nix
|
||||
│ ├── nyx-tool.nix
|
||||
│ └── nyx-tui.nix
|
||||
└── other/
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## ⚙️ How It Works
|
||||
## How It Works
|
||||
|
||||
* **`nyx-tool.nix`**
|
||||
* **`default.nix`**
|
||||
Importing the other Modules.
|
||||
|
||||
* **`nyx-tool.nix`**
|
||||
Sets up shell visuals (e.g. banners) and Zsh helpers.
|
||||
|
||||
* **`nyx-rebuild.nix`**
|
||||
* **`nyx-rebuild.nix`**
|
||||
Enhances `nixos-rebuild` with:
|
||||
|
||||
* Git auto-push support
|
||||
* Optional code formatting before builds
|
||||
* Rebuild logging
|
||||
|
||||
* **`nyx-cleanup.nix`**
|
||||
* **`nyx-cleanup.nix`**
|
||||
Automates system cleanup and tracks logs (optionally pushes to GitHub).
|
||||
|
||||
* **`nyx-tui.nix`**
|
||||
Making a TUI for the other tools.
|
||||
|
||||
|
||||
---
|
||||
|
||||
## 🚀 Quick Start
|
||||
## Quick Start
|
||||
|
||||
### 1. Add Nyx to your Flake
|
||||
|
||||
|
|
@ -117,6 +131,12 @@ Nyx-Tools
|
|||
nyx-tool = {
|
||||
enable = true;
|
||||
};
|
||||
|
||||
nyx-tui = {
|
||||
enable = true;
|
||||
enableAlias = false;
|
||||
};
|
||||
|
||||
};
|
||||
}
|
||||
```
|
||||
|
|
@ -127,9 +147,9 @@ See `other/example/example-home.nix` for a working example.
|
|||
|
||||
---
|
||||
|
||||
## 🔧 Module Options
|
||||
## Module Options
|
||||
|
||||
### `modules.nyx-rebuild`
|
||||
### `nyx.nyx-rebuild`
|
||||
|
||||
| Option | Description | Default |
|
||||
| ------------------ | -------------------------------------- | ------------------------- |
|
||||
|
|
@ -142,7 +162,7 @@ See `other/example/example-home.nix` for a working example.
|
|||
|
||||
---
|
||||
|
||||
### `modules.nyx-cleanup`
|
||||
### `nyx.nyx-cleanup`
|
||||
|
||||
| Option | Description | Default |
|
||||
| ----------------- | ----------------------------- | ------------------------- |
|
||||
|
|
@ -152,7 +172,16 @@ See `other/example/example-home.nix` for a working example.
|
|||
|
||||
---
|
||||
|
||||
### `modules.nyx-tool`
|
||||
### `nyx.nyx-tui`
|
||||
|
||||
| Option | Description | Default |
|
||||
| ----------------- | ----------------------------- | ------------------------- |
|
||||
| `enable` | Enable the module | `false` |
|
||||
| `enableAlias` | Add CLI alias for the tui | `false` |
|
||||
|
||||
---
|
||||
|
||||
### `nyx.nyx-tool`
|
||||
|
||||
| Option | Description | Default |
|
||||
| -------- | ------------------------------- | ------- |
|
||||
|
|
@ -162,7 +191,7 @@ See `other/example/example-home.nix` for a working example.
|
|||
|
||||
---
|
||||
|
||||
## 🤝 Contributing
|
||||
## Contributing
|
||||
|
||||
You're welcome to contribute:
|
||||
|
||||
|
|
@ -176,7 +205,7 @@ Open an issue or pull request at:
|
|||
|
||||
---
|
||||
|
||||
## 📄 License
|
||||
## License
|
||||
|
||||
Licensed under the [MIT License](./LICENSE)
|
||||
|
||||
|
|
|
|||
331
nyx/bash/nyx-tui.sh
Executable file
331
nyx/bash/nyx-tui.sh
Executable file
|
|
@ -0,0 +1,331 @@
|
|||
#!/usr/bin/env bash
|
||||
# nyx-tui: interactive TUI for Nyx tasks
|
||||
|
||||
if [ -z "${BASH_VERSION:-}" ]; then
|
||||
echo "This script must be run with bash, not $SHELL" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
########################################################################
|
||||
# CONFIGURATION (injected by Nix)
|
||||
########################################################################
|
||||
log_dir="@LOG_DIR@"
|
||||
nix_dir="@NIX_DIR@"
|
||||
version="@VERSION@"
|
||||
dialog_bin="${DIALOG_BIN:-@DIALOG_BIN@}"
|
||||
|
||||
# Fallbacks if Nix didn't substitute
|
||||
if [[ -z "${dialog_bin//@DIALOG_BIN@/}" ]]; then
|
||||
# If placeholder remained, try common defaults
|
||||
if command -v dialog >/dev/null 2>&1; then
|
||||
dialog_bin="$(command -v dialog)"
|
||||
elif command -v whiptail >/dev/null 2>&1; then
|
||||
dialog_bin="$(command -v whiptail)"
|
||||
else
|
||||
echo "Error: neither 'dialog' nor 'whiptail' found. Please install one." >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
if ! command -v "$dialog_bin" >/dev/null 2>&1; then
|
||||
echo "Error: dialog binary '$dialog_bin' is not executable." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
mkdir -p "$log_dir"
|
||||
|
||||
########################################################################
|
||||
# CLI args
|
||||
########################################################################
|
||||
do_startup=false
|
||||
|
||||
print_help() {
|
||||
cat <<'EOF'
|
||||
nyx-tui [--pretty] [--help]
|
||||
|
||||
--pretty Show a simple artificial startup screen (optional).
|
||||
--help Show this help.
|
||||
EOF
|
||||
}
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
--pretty) do_startup=true; shift;;
|
||||
-h|--help) print_help; exit 0;;
|
||||
*) echo "Unknown argument: $1" >&2; exit 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
|
||||
|
||||
pause() { read -r -p "Press Enter to continue..." _; }
|
||||
|
||||
########################################################################
|
||||
# Dialog wrappers
|
||||
########################################################################
|
||||
d_msg() {
|
||||
local msg="${1:-}"
|
||||
"$dialog_bin" --title "Nyx TUI" --msgbox "$msg" 8 60
|
||||
clear
|
||||
}
|
||||
|
||||
d_textbox() {
|
||||
local title="${1:-}"; local file="${2:-}"
|
||||
"$dialog_bin" --title "$title" --textbox "$file" 20 100
|
||||
clear
|
||||
}
|
||||
|
||||
d_menu() {
|
||||
local title="$1"; shift
|
||||
local prompt="$1"; shift
|
||||
local choice
|
||||
choice=$("$dialog_bin" --title "$title" --menu "$prompt" 20 70 10 "$@" 3>&1 1>&2 2>&3) || return 1
|
||||
echo "$choice"
|
||||
}
|
||||
|
||||
########################################################################
|
||||
# Helpers
|
||||
########################################################################
|
||||
run_with_spinner() {
|
||||
local label="$1"; shift
|
||||
echo "${CYAN}${BOLD}${label}${RESET}"
|
||||
( "$@" ) &
|
||||
local pid=$!
|
||||
local spin='|/-\'; local i=0
|
||||
while kill -0 "$pid" 2>/dev/null; do
|
||||
i=$(( (i+1) % 4 ))
|
||||
printf "\r%s" "${spin:$i:1}"
|
||||
sleep 0.1
|
||||
done
|
||||
wait "$pid"; local status=$?; printf "\r"
|
||||
return $status
|
||||
}
|
||||
|
||||
########################################################################
|
||||
# Actions
|
||||
########################################################################
|
||||
action_rebuild() {
|
||||
clear
|
||||
if command -v nyx-rebuild >/dev/null 2>&1; then
|
||||
run_with_spinner "Rebuilding (nyx-rebuild)..." nyx-rebuild || true
|
||||
if ! check_last_log_for_error; then
|
||||
return
|
||||
fi
|
||||
d_msg "Rebuild finished."
|
||||
elif command -v nixos-rebuild >/dev/null 2>&1; then
|
||||
run_with_spinner "nixos-rebuild switch --flake ${nix_dir} ..." \
|
||||
sudo nixos-rebuild switch --flake "$nix_dir" || true
|
||||
sleep 1
|
||||
d_msg "nixos-rebuild finished."
|
||||
else
|
||||
d_msg "No rebuild tool found (nyx-rebuild / nixos-rebuild). Skipping."
|
||||
fi
|
||||
}
|
||||
|
||||
action_update() {
|
||||
clear
|
||||
if command -v nyx-rebuild >/dev/null 2>&1; then
|
||||
run_with_spinner "Updating (nyx-rebuild --update)..." nyx-rebuild --update || true
|
||||
if ! check_last_log_for_error; then
|
||||
return
|
||||
fi
|
||||
d_msg "Update finished."
|
||||
elif command -v nixos-rebuild >/dev/null 2>&1; then
|
||||
(
|
||||
cd "$nix_dir"
|
||||
run_with_spinner "nix flake update..." nix flake update || true
|
||||
)
|
||||
run_with_spinner "nixos-rebuild switch --flake ${nix_dir} ..." \
|
||||
sudo nixos-rebuild switch --flake "$nix_dir" || true
|
||||
sleep 1
|
||||
d_msg "nixos-rebuild finished."
|
||||
else
|
||||
d_msg "No update tool found. Skipping."
|
||||
fi
|
||||
}
|
||||
|
||||
action_repair() {
|
||||
clear
|
||||
if command -v nyx-rebuild >/dev/null 2>&1; then
|
||||
run_with_spinner "Repairing (nyx-rebuild --repair)..." nyx-rebuild --repair || true
|
||||
if ! check_last_log_for_error; then
|
||||
return
|
||||
fi
|
||||
d_msg "Repair finished."
|
||||
else
|
||||
d_msg "No repair tool found. Skipping."
|
||||
fi
|
||||
}
|
||||
|
||||
action_cleanup() {
|
||||
clear
|
||||
if command -v nyx-cleanup >/dev/null 2>&1; then
|
||||
run_with_spinner "Cleaning up..." nyx-cleanup || true
|
||||
sleep 1
|
||||
d_msg "Cleanup finished."
|
||||
else
|
||||
d_msg "nyx-cleanup not found; nothing to do."
|
||||
fi
|
||||
}
|
||||
|
||||
action_update_flake() {
|
||||
clear
|
||||
if command -v nix >/dev/null 2>&1 && [[ -d "$nix_dir" ]]; then
|
||||
( cd "$nix_dir" && run_with_spinner "nix flake update..." nix flake update ) || true
|
||||
d_msg "Flake update finished."
|
||||
else
|
||||
d_msg "nix not installed or flake dir missing: ${nix_dir}"
|
||||
fi
|
||||
}
|
||||
|
||||
action_git_pull() {
|
||||
clear
|
||||
if [[ -d "$nix_dir/.git" ]]; then
|
||||
( cd "$nix_dir" && run_with_spinner "git pull --rebase..." git pull --rebase ) || true
|
||||
d_msg "Git pull completed."
|
||||
else
|
||||
d_msg "No git repo at: ${nix_dir}"
|
||||
fi
|
||||
}
|
||||
|
||||
action_system_info() {
|
||||
local tmp; tmp="$(mktemp)"
|
||||
{
|
||||
echo "Host: $(hostname)"
|
||||
echo "Kernel: $(uname -srmo)"
|
||||
echo "Uptime: $(uptime -p)"
|
||||
echo
|
||||
echo "Disk (root):"
|
||||
df -h /
|
||||
echo
|
||||
if command -v nix >/dev/null 2>&1; then
|
||||
echo "Nix profiles:"
|
||||
nix profile list || true
|
||||
else
|
||||
echo "Nix not installed."
|
||||
fi
|
||||
} > "$tmp"
|
||||
d_textbox "System Info" "$tmp"
|
||||
rm -f "$tmp"
|
||||
}
|
||||
|
||||
action_view_logs() {
|
||||
if [[ -d "$log_dir" ]]; then
|
||||
local lastlog tmp
|
||||
tmp="$(mktemp)"
|
||||
lastlog="$(find "$log_dir" -type f -name '*.log' -printf '%T@ %p\n' 2>/dev/null | sort -nr | awk 'NR==1{print $2}')"
|
||||
if [[ -n "${lastlog:-}" && -f "$lastlog" ]]; then
|
||||
tail -n 300 "$lastlog" > "$tmp"
|
||||
d_textbox "Last Rebuild Log: $(basename "$lastlog")" "$tmp"
|
||||
else
|
||||
d_msg "No logs found in ${log_dir}"
|
||||
fi
|
||||
rm -f "$tmp"
|
||||
else
|
||||
d_msg "Log directory not found: ${log_dir}"
|
||||
fi
|
||||
}
|
||||
|
||||
check_last_log_for_error() {
|
||||
local lastlog
|
||||
lastlog="$(find "$log_dir" -type f -name '*.log' -printf '%T@ %p\n' 2>/dev/null |
|
||||
sort -nr | awk 'NR==1{print $2}')"
|
||||
if [[ -n "${lastlog:-}" && -f "$lastlog" ]]; then
|
||||
if grep -qi "error" "$lastlog"; then
|
||||
local tmp
|
||||
tmp="$(mktemp)"
|
||||
echo "Error detected in: $(basename "$lastlog")" > "$tmp"
|
||||
echo >> "$tmp"
|
||||
grep -A99999 -i "error" "$lastlog" >> "$tmp"
|
||||
d_textbox "Last Build Error" "$tmp"
|
||||
rm -f "$tmp"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
|
||||
|
||||
startup() {
|
||||
clear
|
||||
if "$do_startup"; then
|
||||
echo
|
||||
nyx-tool "Nyx" "nyx-tui" "$version" \
|
||||
"A better way to nyx" \
|
||||
"by Peritia-System" \
|
||||
"https://github.com/Peritia-System/Nyx-Tools" \
|
||||
"https://github.com/Peritia-System/Nyx-Tools/issues" \
|
||||
"Because who doesn't love a good TUI"
|
||||
echo "Loading Nyx TUI..."
|
||||
echo
|
||||
|
||||
local bar_length=25
|
||||
for ((i=0; i<=bar_length; i++)); do
|
||||
local filled empty percent
|
||||
filled=$(printf "%${i}s" | tr ' ' '#')
|
||||
empty=$(printf "%$((bar_length - i))s" | tr ' ' ' ')
|
||||
percent=$(( i * 100 / bar_length ))
|
||||
printf "\r[%s%s] %d%%" "$filled" "$empty" "$percent"
|
||||
sleep 0.2
|
||||
# Slow down after 70% (i > 17 when bar_length = 25)
|
||||
if [[ $i -gt 17 ]]; then
|
||||
sleep 0.1
|
||||
fi
|
||||
done
|
||||
echo -e "\nAll Loaded!\n"
|
||||
read -r -p "Press Enter to continue..."
|
||||
clear
|
||||
else
|
||||
echo
|
||||
nyx-tool "Nyx" "nyx-tui" "$version" \
|
||||
"A better way to nyx" \
|
||||
"by Peritia-System" \
|
||||
"https://github.com/Peritia-System/Nyx-Tools" \
|
||||
"https://github.com/Peritia-System/Nyx-Tools/issues" \
|
||||
"Because who doesn't love a good TUI"
|
||||
read -r -p "Press Enter to continue..."
|
||||
clear
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
########################################################################
|
||||
# Menu Loop
|
||||
########################################################################
|
||||
startup
|
||||
while true; do
|
||||
choice=$(d_menu "Nyx TUI ${version}" "Select an action:" \
|
||||
1 "Update" \
|
||||
2 "Rebuild" \
|
||||
3 "Repair" \
|
||||
4 "Cleanup (nyx-cleanup)" \
|
||||
5 "Flake: nix flake update" \
|
||||
6 "Git pull (in nix dir)" \
|
||||
7 "System info" \
|
||||
8 "View latest rebuild log" \
|
||||
X "Exit") || { clear; exit 0; }
|
||||
|
||||
case "$choice" in
|
||||
1) action_update ;;
|
||||
2) action_rebuild ;;
|
||||
3) action_repair ;;
|
||||
4) action_cleanup ;;
|
||||
5) action_update_flake ;;
|
||||
6) action_git_pull ;;
|
||||
7) action_system_info ;;
|
||||
8) action_view_logs ;;
|
||||
X) clear; exit 0 ;;
|
||||
esac
|
||||
done
|
||||
|
|
@ -39,6 +39,8 @@ with lib;
|
|||
./nyx-rebuild.nix
|
||||
./nyx-cleanup.nix
|
||||
./nyx-tool.nix
|
||||
./nyx-tui.nix
|
||||
|
||||
];
|
||||
|
||||
################################################################
|
||||
|
|
@ -47,6 +49,7 @@ with lib;
|
|||
config = mkIf (!config.nyx.enable) {
|
||||
nyx.nyx-rebuild.enable = false;
|
||||
nyx.nyx-cleanup.enable = false;
|
||||
nyx.nyx-tui.enable = false;
|
||||
nyx.nyx-tool.enable = false;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ let
|
|||
(toString (cfg.keepGenerations or 5))
|
||||
(if (nyxCfg.autoPush or false) then "true" else "false")
|
||||
"${pkgs.git}/bin/git"
|
||||
"1.0.0"
|
||||
"1.1.0"
|
||||
]
|
||||
src;
|
||||
in
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ let
|
|||
"${pkgs.git}/bin/git"
|
||||
"${pkgs.nix-output-monitor}/bin/nom"
|
||||
(if nyxCfg.autoPush then "true" else "false")
|
||||
"1.0.0"
|
||||
"1.1.0"
|
||||
]
|
||||
src;
|
||||
in
|
||||
|
|
|
|||
45
nyx/nyx-tui.nix
Normal file
45
nyx/nyx-tui.nix
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
{ config, lib, pkgs, ... }:
|
||||
|
||||
let
|
||||
nyxCfg = config.nyx or {};
|
||||
cfg = nyxCfg."nyx-tui" or {};
|
||||
|
||||
# Read the tui script template and replace tokens
|
||||
tuiScript =
|
||||
let
|
||||
src = builtins.readFile ./bash/nyx-tui.sh; # Script with @TOKENS@
|
||||
in
|
||||
builtins.replaceStrings
|
||||
[
|
||||
"@LOG_DIR@" "@NIX_DIR@" "@VERSION@" "@DIALOG_BIN@"
|
||||
]
|
||||
[
|
||||
(toString nyxCfg.logDir)
|
||||
(toString nyxCfg.nixDirectory)
|
||||
"1.1.0"
|
||||
"${pkgs.dialog}/bin/dialog"
|
||||
]
|
||||
src;
|
||||
in
|
||||
{
|
||||
options.nyx."nyx-tui" = {
|
||||
enable = lib.mkEnableOption "Enable nyx-tui script";
|
||||
enableAlias = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = true;
|
||||
description = "If true, add alias 'nyx' for 'nyx-tui'.";
|
||||
};
|
||||
};
|
||||
|
||||
config = lib.mkIf ((nyxCfg.enable or false) && (cfg.enable or false)) {
|
||||
home.packages = [
|
||||
(pkgs.writeShellScriptBin "nyx-tui" tuiScript)
|
||||
];
|
||||
|
||||
home.shellAliases = lib.mkIf (cfg.enableAlias or true) {
|
||||
nyx = "nyx-tui";
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -1,304 +0,0 @@
|
|||
#!/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}"
|
||||
|
||||
|
|
@ -20,7 +20,10 @@ in {
|
|||
home-manager = {
|
||||
useGlobalPkgs = true;
|
||||
useUserPackages = true;
|
||||
backupFileExtension = "delme-HMbackup";
|
||||
backupFileExtension = "delme-HMbackup";
|
||||
# Please use this backup File Extension to ensure that the bug won't cause any problems
|
||||
# nyx-rebuild deletes the backups on each rebuild before rebuilding
|
||||
# HM Bug: https://discourse.nixos.org/t/nixos-rebuild-fails-on-backup-up-config-file-by-home-manager/45992
|
||||
users.${username} = import ./home.nix;
|
||||
|
||||
extraSpecialArgs = {
|
||||
|
|
|
|||
|
|
@ -41,6 +41,10 @@ nyx = {
|
|||
enable = true;
|
||||
};
|
||||
|
||||
nyx-tui = {
|
||||
enable = true;
|
||||
enableAlias = false;
|
||||
};
|
||||
}
|
||||
################################################################
|
||||
# Basic Home Manager Setup
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue