Version 1.0.0
This commit is contained in:
parent
0084c7295d
commit
0ae67a9897
11 changed files with 764 additions and 568 deletions
|
|
@ -1,40 +1,35 @@
|
|||
{ config, lib, pkgs, ... }:
|
||||
|
||||
let
|
||||
cfg = config.nyx.nyx-cleanup;
|
||||
logDirDefault = "/home/${toString cfg.username}/.nyx/nyx-cleanup/logs";
|
||||
in
|
||||
nyxCfg = config.nyx or {};
|
||||
cfg = nyxCfg."nyx-cleanup" or {};
|
||||
|
||||
# Read the cleanup script template and replace tokens
|
||||
cleanupScript =
|
||||
let
|
||||
src = builtins.readFile ./bash/nyx-cleanup.sh; # Script with @TOKENS@
|
||||
in
|
||||
builtins.replaceStrings
|
||||
[
|
||||
"@LOG_DIR@" "@KEEP_GENERATIONS@" "@AUTO_PUSH@" "@GIT_BIN@" "@VERSION@"
|
||||
]
|
||||
[
|
||||
(toString nyxCfg.logDir)
|
||||
(toString (cfg.keepGenerations or 5))
|
||||
(if (nyxCfg.autoPush or false) then "true" else "false")
|
||||
"${pkgs.git}/bin/git"
|
||||
"1.0.0"
|
||||
]
|
||||
src;
|
||||
in
|
||||
{
|
||||
options.nyx.nyx-cleanup = {
|
||||
options.nyx."nyx-cleanup" = {
|
||||
enable = lib.mkEnableOption "Enable nyx-cleanup script";
|
||||
|
||||
username = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
description = "The user this module applies to.";
|
||||
};
|
||||
|
||||
nixDirectory = lib.mkOption {
|
||||
type = lib.types.path;
|
||||
description = "Path to NixOS flake configuration.";
|
||||
};
|
||||
|
||||
logDir = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = logDirDefault;
|
||||
description = "Directory for storing cleanup logs.";
|
||||
};
|
||||
|
||||
keepGenerations = lib.mkOption {
|
||||
type = lib.types.int;
|
||||
default = 5;
|
||||
description = "Number of NixOS generations to keep.";
|
||||
};
|
||||
|
||||
autoPush = lib.mkOption {
|
||||
type = lib.types.bool;
|
||||
default = false;
|
||||
description = "Whether to auto-push git commits after cleanup.";
|
||||
description = "Number of NixOS *system* generations to keep.";
|
||||
};
|
||||
|
||||
enableAlias = lib.mkOption {
|
||||
|
|
@ -44,132 +39,12 @@ in
|
|||
};
|
||||
};
|
||||
|
||||
config = lib.mkIf cfg.enable {
|
||||
config = lib.mkIf ((nyxCfg.enable or false) && (cfg.enable or false)) {
|
||||
home.packages = [
|
||||
(pkgs.writeShellScriptBin "nyx-cleanup" ''
|
||||
|
||||
#!/usr/bin/env bash
|
||||
nyx-cleanup(){
|
||||
set -euo pipefail
|
||||
|
||||
# === CONFIGURATION ===
|
||||
log_dir="${toString cfg.logDir}"
|
||||
keep_generations=${toString cfg.keepGenerations}
|
||||
auto_push=${if cfg.autoPush then "true" else "false"}
|
||||
git_bin="${pkgs.git}/bin/git"
|
||||
|
||||
# Derived repo dir (assumes: ~/.nyx/nyx-cleanup/logs → ~/.nyx)
|
||||
repo_dir="$(dirname "$(dirname "$log_dir")")"
|
||||
|
||||
# === INITIAL SETUP ===
|
||||
version="1.0.0"
|
||||
start_time=$(date +%s)
|
||||
start_human=$(date '+%Y-%m-%d %H:%M:%S')
|
||||
cleanup_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 ===
|
||||
mkdir -p "$log_dir"
|
||||
timestamp=$(date '+%Y-%m-%d_%H-%M-%S')
|
||||
cleanup_log="$log_dir/cleanup-''${timestamp}.log"
|
||||
|
||||
console-log() {
|
||||
echo -e "$@" | tee -a "$cleanup_log"
|
||||
}
|
||||
|
||||
print_line() {
|
||||
console-log ""
|
||||
console-log "''${BOLD}==================================================''${RESET}"
|
||||
console-log ""
|
||||
}
|
||||
|
||||
finish_cleanup() {
|
||||
duration=$(( $(date +%s) - start_time ))
|
||||
if [[ "$cleanup_success" == true ]]; then
|
||||
echo -e "''${GREEN}''${BOLD}
|
||||
##############################
|
||||
# ✅ Nyx Cleanup Complete! #
|
||||
##############################''${RESET}"
|
||||
echo -e "''${CYAN}''${BOLD}📋 Stats:''${RESET}"
|
||||
echo " 🕒 Started: $start_human"
|
||||
echo " ⏱️ Duration: ''${duration} sec"
|
||||
else
|
||||
echo -e "''${RED}''${BOLD}
|
||||
##############################
|
||||
# ❌ Nyx Cleanup Failed! #
|
||||
##############################''${RESET}"
|
||||
echo " 🕒 Started: $start_human"
|
||||
echo " ⏱️ Duration: ''${duration} sec"
|
||||
fi
|
||||
}
|
||||
|
||||
trap finish_cleanup EXIT
|
||||
|
||||
print_line
|
||||
console-log "''${BLUE}''${BOLD}🧹 Starting cleanup...''${RESET}"
|
||||
|
||||
# === REMOVE OLD LOGS ===
|
||||
console-log "''${CYAN}''${BOLD}🗑️ Removing logs older than 30 days...''${RESET}"
|
||||
find "$log_dir" -type f -mtime +30 -print -delete
|
||||
|
||||
# === REMOVE HOME MANAGER BACKUPS ===
|
||||
print_line
|
||||
console-log "''${CYAN}''${BOLD}📁 Deleting Home Manager backup files...''${RESET}"
|
||||
find ~ -type f -name '*delme-HMbackup' -print -delete
|
||||
|
||||
# === GARBAGE COLLECTION ===
|
||||
print_line
|
||||
console-log "''${MAGENTA}''${BOLD}🧼 Running Nix garbage collection...''${RESET}"
|
||||
sudo nix-collect-garbage -d | tee -a "$cleanup_log"
|
||||
|
||||
# === GIT SETUP ===
|
||||
print_line
|
||||
if [[ ! -d "$repo_dir/.git" ]]; then
|
||||
console-log "''${YELLOW}⚠️ No git repo in: $repo_dir. Initializing...''${RESET}"
|
||||
"$git_bin" -C "$repo_dir" init | tee -a "$cleanup_log"
|
||||
fi
|
||||
|
||||
# === GIT AUTO PUSH ===
|
||||
if [[ "$auto_push" == "true" ]]; then
|
||||
print_line
|
||||
console-log "''${BLUE}''${BOLD}🚀 Auto-pushing git commits in $repo_dir...''${RESET}"
|
||||
cd "$repo_dir"
|
||||
|
||||
if "$git_bin" remote | grep -q .; then
|
||||
"$git_bin" add .
|
||||
"$git_bin" commit -m "chore(cleanup): auto cleanup $(date)" || true
|
||||
"$git_bin" push
|
||||
else
|
||||
console-log "''${YELLOW}⚠️ No git remote configured. Skipping push.''${RESET}"
|
||||
console-log "''${YELLOW}📂 Check logs in: $log_dir''${RESET}"
|
||||
fi
|
||||
fi
|
||||
|
||||
cleanup_success=true
|
||||
exit_code=0
|
||||
|
||||
print_line
|
||||
console-log "''${GREEN}🎉 Cleanup finished successfully!''${RESET}"
|
||||
print_line
|
||||
}
|
||||
nyx-cleanup
|
||||
|
||||
|
||||
'')
|
||||
(pkgs.writeShellScriptBin "nyx-cleanup" cleanupScript)
|
||||
];
|
||||
|
||||
home.shellAliases = lib.mkIf cfg.enableAlias {
|
||||
home.shellAliases = lib.mkIf (cfg.enableAlias or true) {
|
||||
nc = "nyx-cleanup";
|
||||
};
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue