89 lines
2.9 KiB
Nix
89 lines
2.9 KiB
Nix
# ncli/lib.nix — Shared helpers and utilities
|
|
{ pkgs, host, project, backupFiles }:
|
|
|
|
let
|
|
backupFilesString = pkgs.lib.strings.concatStringsSep " " backupFiles;
|
|
in
|
|
{
|
|
# --- Injected config ---
|
|
inherit pkgs host project backupFiles backupFilesString;
|
|
|
|
# --- Color / banner source ---
|
|
colorsSource = "$HOME/${project}/other/colors.sh";
|
|
|
|
# --- Backup helper ---
|
|
handle_backups = ''
|
|
handle_backups() {
|
|
echo "Checking for backup files to remove..."
|
|
found=0
|
|
for file_path in "''${BACKUP_FILES[@]}"; do
|
|
full_path="$HOME/$file_path"
|
|
if [ -f "$full_path" ]; then
|
|
echo "Removing stale backup file: $full_path"
|
|
rm "$full_path"
|
|
found=1
|
|
fi
|
|
done
|
|
if [ "$found" -eq 0 ]; then
|
|
echo "No stale backup files found."
|
|
fi
|
|
}
|
|
'';
|
|
|
|
# --- OOM / build error handler ---
|
|
handle_build_error = ''
|
|
handle_build_error() {
|
|
local exit_code=$?
|
|
if [ "$exit_code" -eq 137 ]; then
|
|
echo ""
|
|
echo -e "''${RED}╔══════════════════════════════════════════════════════════╗''${NOCOLOR}"
|
|
echo -e "''${RED}║ BUILD KILLED — Signal 9 (SIGKILL) detected ║''${NOCOLOR}"
|
|
echo -e "''${RED}╚══════════════════════════════════════════════════════════╝''${NOCOLOR}"
|
|
echo ""
|
|
echo -e "''${YELLOW}What happened:''${NOCOLOR}"
|
|
echo " The build process was forcefully terminated by the OS."
|
|
echo " This is almost always the Linux kernel OOM killer running out"
|
|
echo " of RAM + swap during Nix evaluation or compilation."
|
|
echo ""
|
|
echo -e "''${YELLOW}Suggested fixes (try in order):''${NOCOLOR}"
|
|
echo ""
|
|
echo -e " ''${GREEN}1. Reduce parallel jobs''${NOCOLOR} (lowest RAM usage):"
|
|
echo " sudo nixos-rebuild switch --flake . --max-jobs 1 --cores 1"
|
|
echo ""
|
|
echo -e " ''${GREEN}2. Confirm OOM killer fired:''${NOCOLOR}"
|
|
echo " journalctl -k --since '5 minutes ago' | grep -i oom"
|
|
echo ""
|
|
fi
|
|
}
|
|
'';
|
|
|
|
# --- Print helpers (used by dev init) ---
|
|
print_header = ''
|
|
print_header() {
|
|
echo ""
|
|
echo -e "''${BLUE}==============================================''${NOCOLOR}"
|
|
echo -e "''${BLUE} Nix Flake Development Environment Initializer''${NOCOLOR}"
|
|
echo -e "''${BLUE}==============================================''${NOCOLOR}"
|
|
echo ""
|
|
}
|
|
'';
|
|
|
|
print_success = ''
|
|
print_success() {
|
|
echo -e "''${GREEN}[OK]''${NOCOLOR} $1"
|
|
}
|
|
'';
|
|
|
|
print_error = ''
|
|
print_error() {
|
|
echo -e "''${RED}[ERR]''${NOCOLOR} $1"
|
|
}
|
|
'';
|
|
|
|
print_info = ''
|
|
print_info() {
|
|
echo -e "''${YELLOW}[->]''${NOCOLOR} $1"
|
|
}
|
|
'';
|
|
}
|