Files
NixOS/modules/ncli/lib.nix

77 lines
2.7 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 = ''
if [ ''${#BACKUP_FILES[@]} -eq 0 ]; then
echo "No backup files configured to check."
return
fi
echo "Checking for backup files to remove..."
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"
fi
done
'';
# --- OOM / build error handler ---
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 = ''
echo ""
echo -e "''${BLUE}==============================================''${NOCOLOR}"
echo -e "''${BLUE} Nix Flake Development Environment Initializer''${NOCOLOR}"
echo -e "''${BLUE}==============================================''${NOCOLOR}"
echo ""
'';
print_success = ''
echo -e "''${GREEN}[OK]''${NOCOLOR} $1"
'';
print_error = ''
echo -e "''${RED}[ERR]''${NOCOLOR} $1"
'';
print_info = ''
echo -e "''${YELLOW}[->]''${NOCOLOR} $1"
'';
}