feat: adds error handling, environment checks, confirmation before rebuild & temporary clone

This commit is contained in:
Marcelo 2024-10-26 23:25:42 +01:00
parent ef6dec6de1
commit 29da49fedd

View file

@ -12,6 +12,7 @@ SCRIPT_DIR="${HOME}/.dotfiles"
USER_EMAIL="" USER_EMAIL=""
SKIP_REVIEW=0 SKIP_REVIEW=0
DISABLE_HARDEN=0 DISABLE_HARDEN=0
TEMP_CLONE=0
EDITOR="${EDITOR:-nano}" # Default to nano if EDITOR is not set EDITOR="${EDITOR:-nano}" # Default to nano if EDITOR is not set
# Helper function to display usage message # Helper function to display usage message
@ -22,10 +23,16 @@ show_help() {
printf " -e, --email <email> Provide an email to use for configuration (default: empty)\n" printf " -e, --email <email> Provide an email to use for configuration (default: empty)\n"
printf " -y, --yes Skip editor confirmation for flake.nix review\n" printf " -y, --yes Skip editor confirmation for flake.nix review\n"
printf " -n, --no-harden Skip the security hardening step\n" printf " -n, --no-harden Skip the security hardening step\n"
printf " -t, --temp-clone Clone into a temporary directory\n"
printf " -h, --help Show this help message\n" printf " -h, --help Show this help message\n"
exit 0 exit 0
} }
# Check dependencies
command -v nix-shell >/dev/null 2>&1 || { printf "${RED}Error: nix-shell is not installed.${NC}\n"; exit 1; }
command -v nixos-rebuild >/dev/null 2>&1 || { printf "${RED}Error: nixos-rebuild is not installed.${NC}\n"; exit 1; }
command -v nix >/dev/null 2>&1 || { printf "${RED}Error: nix is not installed.${NC}\n"; exit 1; }
# Parse arguments # Parse arguments
while [ "$#" -gt 0 ]; do while [ "$#" -gt 0 ]; do
case "$1" in case "$1" in
@ -33,6 +40,7 @@ while [ "$#" -gt 0 ]; do
-e|--email) USER_EMAIL="$2"; shift 2;; -e|--email) USER_EMAIL="$2"; shift 2;;
-y|--yes) SKIP_REVIEW=1; shift;; -y|--yes) SKIP_REVIEW=1; shift;;
-n|--no-harden) DISABLE_HARDEN=1; shift;; -n|--no-harden) DISABLE_HARDEN=1; shift;;
-t|--temp-clone) TEMP_CLONE=1; shift;;
-h|--help) show_help;; -h|--help) show_help;;
--) shift; break;; --) shift; break;;
*) printf "${RED}Error:${NC} Unknown option: $1\n"; show_help; exit 1;; *) printf "${RED}Error:${NC} Unknown option: $1\n"; show_help; exit 1;;
@ -40,12 +48,17 @@ while [ "$#" -gt 0 ]; do
done done
# Clone dotfiles repository # Clone dotfiles repository
printf "${CYAN}Cloning dotfiles to ${SCRIPT_DIR}...${NC}\n" if [ "$TEMP_CLONE" -eq 1 ]; then
SCRIPT_DIR=$(mktemp -d)
printf "${YELLOW}Cloning dotfiles to temporary directory ${SCRIPT_DIR}...${NC}\n"
else
printf "${CYAN}Cloning dotfiles to ${SCRIPT_DIR}...${NC}\n"
fi
nix-shell -p git --command "git clone https://gitlab.com/librephoenix/nixos-config $SCRIPT_DIR" || { printf "${RED}Failed to clone repository.${NC}\n"; exit 1; } nix-shell -p git --command "git clone https://gitlab.com/librephoenix/nixos-config $SCRIPT_DIR" || { printf "${RED}Failed to clone repository.${NC}\n"; exit 1; }
# Generate hardware configuration # Generate hardware configuration
printf "${CYAN}Generating hardware configuration...${NC}\n" printf "${CYAN}Generating hardware configuration...${NC}\n"
sudo nixos-generate-config --show-hardware-config > "$SCRIPT_DIR/system/hardware-configuration.nix" sudo nixos-generate-config --show-hardware-config > "$SCRIPT_DIR/system/hardware-configuration.nix" || { printf "${RED}Failed to generate hardware configuration.${NC}\n"; exit 1; }
# Determine boot mode (UEFI or BIOS) and set flake.nix accordingly # Determine boot mode (UEFI or BIOS) and set flake.nix accordingly
if [ -d /sys/firmware/efi/efivars ]; then if [ -d /sys/firmware/efi/efivars ]; then
@ -78,17 +91,25 @@ fi
# Apply security hardening if enabled # Apply security hardening if enabled
if [ "$DISABLE_HARDEN" -eq 0 ]; then if [ "$DISABLE_HARDEN" -eq 0 ]; then
printf "${CYAN}Applying security hardening...${NC}\n" printf "${CYAN}Applying security hardening...${NC}\n"
sudo "$SCRIPT_DIR/harden.sh" "$SCRIPT_DIR" sudo "$SCRIPT_DIR/harden.sh" "$SCRIPT_DIR" || { printf "${RED}Hardening failed.${NC}\n"; exit 1; }
else else
printf "${YELLOW}Skipping security hardening as requested.${NC}\n" printf "${YELLOW}Skipping security hardening as requested.${NC}\n"
fi fi
# Confirmation prompt for system rebuild
printf "${YELLOW}Ready to rebuild the system with nixos-rebuild. Do you want to proceed? (y/n) ${NC}"
read -r confirm
if [ "$confirm" != "y" ]; then
printf "${RED}Aborting system rebuild.${NC}\n"
exit 0
fi
# Rebuild system with new configuration # Rebuild system with new configuration
printf "${CYAN}Rebuilding system with nixos-rebuild...${NC}\n" printf "${CYAN}Rebuilding system with nixos-rebuild...${NC}\n"
sudo nixos-rebuild switch --flake "$SCRIPT_DIR#system" sudo nixos-rebuild switch --flake "$SCRIPT_DIR#system" || { printf "${RED}System rebuild failed.${NC}\n"; exit 1; }
# Build and switch to the user's home-manager configuration # Build and switch to the user's home-manager configuration
printf "${CYAN}Setting up home-manager configuration...${NC}\n" printf "${CYAN}Setting up home-manager configuration...${NC}\n"
nix run home-manager/master --extra-experimental-features nix-command --extra-experimental-features flakes -- switch --flake "$SCRIPT_DIR#user" nix run home-manager/master --extra-experimental-features nix-command --extra-experimental-features flakes -- switch --flake "$SCRIPT_DIR#user" || { printf "${RED}Home-manager setup failed.${NC}\n"; exit 1; }
printf "${GREEN}Installation and configuration complete!${NC}\n" printf "${GREEN}Installation and configuration complete!${NC}\n"