diff --git a/EXAMPLE_home.nix b/EXAMPLE_home.nix new file mode 100644 index 0000000..15fa400 --- /dev/null +++ b/EXAMPLE_home.nix @@ -0,0 +1,54 @@ +```nix + +{ config, nixDirectory, pkgs, ... }: + +let + nixDirectory = "/home/${username}/NixOS"; + +in +{ + + ################################################################ + # Module Imports + ################################################################ + + imports = [ + # Nyx Tools + /home/${username}/NixOS/Nyx-Tools + + ]; + + + ################################################################ + # Nyx Tools Configuration + ################################################################ + + modules.nyx-rebuild = { + enable = true; + inherit username nixDirectory; + editor = "nvim"; + formatter = "alejandra"; + enableAlias = false; + autoPush = false; + enableFormatting = false; + startEditor = false; + }; + + modules.nyx-cleanup = { + enable = true; + inherit username nixDirectory; + autoPush = false; + keepGenerations = 5; + enableAlias = false; + }; + + + modules.nix-tool = { + enable = true; + inherit nixDirectory; + }; + +} + + +``` \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..1bdc6a0 --- /dev/null +++ b/README.md @@ -0,0 +1,160 @@ +# โš™๏ธ Nyx: NixOS System Management Toolkit + +**Nyx** is a modular toolkit for managing NixOS systems. It streamlines NixOS rebuilds, system cleanups, and developer workflow enhancements through a unified and extensible interface. Simply import one file to enable all features. + +--- + +## โœจ Features + +* ๐Ÿ” **Enhanced NixOS Rebuilds** โ€” via `nyx-rebuild.nix` +* ๐Ÿงน **Automated System Cleanup** โ€” via `nyx-cleanup.nix` +* ๐Ÿ› ๏ธ **Custom Shell Tools & Visuals** โ€” like banners via `nyx-tool.nix` +* ๐Ÿงฉ **One-File Integration** โ€” import just `nyx.nix` to activate everything + +--- + +## ๐Ÿ“ฆ Dependencies + +* โœ… NixOS or compatible Nix environment +* โœ… `sudo` access (for system operations) +* โœ… [Home Manager](https://github.com/nix-community/home-manager) +* โœ… Git โ€” required if you use `autoPush` features (must be a Git repo) +* โœ… Zsh (included via Nyx modules) +* โœ… [`nix-output-monitor`](https://github.com/maralorn/nix-output-monitor) (included via Nyx) + +> โ„น๏ธ No need to preinstall Zsh or `nix-output-monitor`; Nyx provides them internally. + +--- + +## ๐Ÿ“ Project Structure + +```bash +Nyx-Tools +โ”œโ”€โ”€ nyx.nix # Master module +โ”œโ”€โ”€ nyx-tool.nix # Shell customizations (e.g., banners) +โ”œโ”€โ”€ nyx-rebuild.nix # Enhanced nixos-rebuild logic +โ”œโ”€โ”€ nyx-cleanup.nix # System cleanup logic +โ””โ”€โ”€ zsh/ + โ”œโ”€โ”€ nyx-cleanup.zsh + โ”œโ”€โ”€ nyx-rebuild.zsh + โ””โ”€โ”€ nyx-tool.zsh +```` + +--- + +## โš™๏ธ How It Works + +* `nyx.nix`: Central module that imports all others. +* `nyx-tool.nix`: Adds startup banners and sources Zsh helpers. +* `nyx-rebuild.nix`: Extends `nixos-rebuild` with logs, Git push, and optional formatting. +* `nyx-cleanup.nix`: Automates system cleanup and logs output. +* `zsh/*.zsh`: Shell scripts sourced into Zsh to handle CLI tooling. + +--- + +## ๐Ÿš€ Quick Start + +### 1. Import into `home.nix` + +```nix +imports = [ + ./path/to/Nyx-Tools/nyx.nix +]; +``` + +### 2. Enable desired modules + +```nix +modules.nyx-rebuild = { + enable = true; + inherit username nixDirectory; +}; + +modules.nyx-cleanup = { + enable = true; + inherit username nixDirectory; +}; + +modules.nix-tool = { + enable = true; + inherit nixDirectory; +}; +``` + +> โš ๏ธ **Note:** You must define `nixDirectory` in your configuration or import it. +> It must be a **full path** to your flake directory (e.g., `/home/${username}/NixOS/Nyx-Tools`). + +๐Ÿ‘‰ See the [example config](./EXAMPLE_home.nix) for a working setup. + +--- + +## ๐Ÿ› ๏ธ Module Options + +### `nyx-rebuild` + +| Option | Description | Default | +| ------------------ | ----------------------------------- | ------- | +| `enable` | Enable this module | `false` | +| `startEditor` | Launch an editor before rebuild | `false` | +| `editor` | Which editor to use (`nvim`, `vim`) | โ€” | +| `enableFormatting` | Format Nix files before rebuild | `false` | +| `formatter` | Formatter to use (`alejandra`) | โ€” | +| `enableAlias` | Add shell alias for rebuild | `false` | +| `autoPush` | Push rebuild logs or dir to GitHub | `false` | + +--- + +### `nyx-cleanup` + +| Option | Description | Default | +| ----------------- | ------------------------------------ | ------- | +| `enable` | Enable this module | `false` | +| `autoPush` | Push logs to GitHub after cleanup | `false` | +| `keepGenerations` | Number of system generations to keep | `5` | +| `enableAlias` | Add shell alias for cleanup | `false` | + +--- + +### `nix-tool` + +| Option | Description | Default | +| -------- | ------------------ | ------- | +| `enable` | Enable this module | `false` | + +--- + +## ๐ŸŽจ Customization + +Nyx is fully modular and extensible. You can: + +* Modify existing `.nix` or `.zsh` files +* Add new modules and source them in `nyx.nix` + +### Example: Adding a Custom Tool + +```nix +# Add to nyx.nix or your home.nix +imports = [ + ./nyx-rebuild.nix + ./my-custom-tool.nix +]; +``` + +Create `my-custom-tool.nix` and optionally pair it with a `.zsh` helper script in the `zsh/` folder. + +--- + +## ๐Ÿค Contributing + +Contributions are welcome! Feel free to open an issue or submit a pull request to: + +* Add new functionality +* Improve existing tools +* Fix bugs or typos + +--- + +## ๐Ÿ“„ License + +Licensed under the [MIT License](./LICENSE). + diff --git a/nyx-rebuild.nix b/nyx-rebuild.nix index 0f50284..7eb2364 100644 --- a/nyx-rebuild.nix +++ b/nyx-rebuild.nix @@ -2,7 +2,7 @@ let cfg = config.modules.nyx-rebuild; - scriptTargetPath = "${cfg.nixDirectory}/Misc/Nyx-Tools/zsh/nyx-cleanup.zsh"; + scriptTargetPath = "${cfg.nixDirectory}/Misc/Nyx-Tools/zsh/nyx-rebuild.zsh"; in { options.modules.nyx-rebuild = { diff --git a/nyx-tool.nix b/nyx-tool.nix index 21a1327..7969f5c 100644 --- a/nyx-tool.nix +++ b/nyx-tool.nix @@ -1,11 +1,18 @@ -{ config, lib, pkgs, ... }: +{ config, lib, pkgs,... }: let cfg = config.modules.nix-tool; - scriptTargetPath = "${cfg.nixDirectory}/Misc/Nyx-Tools/zsh/nyx-cleanup.zsh"; + scriptTargetPath = "${cfg.nixDirectory}/Misc/Nyx-Tools/zsh/nyx-tool.zsh"; in { - options.modules.nix-tool.enable = lib.mkEnableOption "Enable nix-tool Zsh function for Banner display."; + options.modules.nix-tool = { + enable = lib.mkEnableOption "Enable nix-tool Zsh function for Banner display."; + + nixDirectory = lib.mkOption { + type = lib.types.str; + description = "Path to the main Nix directory used for scripts."; + }; + }; config = lib.mkIf cfg.enable { home.packages = [ diff --git a/nyx.nix b/nyx.nix index f2c54e5..d28b01e 100644 --- a/nyx.nix +++ b/nyx.nix @@ -1,7 +1,7 @@ # Import all modules so only needs to Import nyx.nix -{ config, pkgs, lib, ... }: +{ config, pkgs, lib, nixDirectory, ... }: { imports = [ @@ -13,4 +13,10 @@ # Nyx cleanup ./nyx-cleanup.nix ]; + + home.packages = with pkgs; [ + nix-output-monitor + ]; + + } diff --git a/zsh/nyx-rebuild.zsh b/zsh/nyx-rebuild.zsh index 0a867ad..7fd5198 100644 --- a/zsh/nyx-rebuild.zsh +++ b/zsh/nyx-rebuild.zsh @@ -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 + } diff --git a/zsh/nyx-tool.zsh b/zsh/nyx-tool.zsh old mode 100755 new mode 100644