69 lines
1.7 KiB
Bash
69 lines
1.7 KiB
Bash
#!/usr/bin/env bash
|
|
set -e
|
|
|
|
# use_nix.sh - Script to update NixOS configuration
|
|
# Usage: use_nix.sh [--rebuild] [--first-run]
|
|
|
|
function check_root() {
|
|
if [[ $EUID -ne 0 ]]; then
|
|
echo "This script must be run as root" >&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
function show_usage() {
|
|
echo "Usage: $0 [--rebuild] [--first-run]"
|
|
echo " --rebuild Automatically run nixos-rebuild after copying files"
|
|
echo " --first-run Indicate this is the first system configuration"
|
|
}
|
|
|
|
AUTO_REBUILD=0
|
|
FIRST_RUN=0
|
|
|
|
# Parse arguments
|
|
for arg in "$@"; do
|
|
case $arg in
|
|
--rebuild) AUTO_REBUILD=1 ;;
|
|
--first-run) FIRST_RUN=1 ;;
|
|
--help|-h) show_usage; exit 0 ;;
|
|
*) echo "Unknown option: $arg"; show_usage; exit 1 ;;
|
|
esac
|
|
done
|
|
|
|
check_root
|
|
|
|
# Check if source directory exists
|
|
if [[ ! -d "root-conf/nixos" ]]; then
|
|
echo "Error: Source directory 'root-conf/nixos' not found!" >&2
|
|
exit 1
|
|
fi
|
|
|
|
# Backup existing configuration
|
|
if [[ -d /etc/nixos ]]; then
|
|
echo "Backing up existing configuration to /etc/nixos.bak"
|
|
rm -rf /etc/nixos.bak 2>/dev/null || true
|
|
cp -r /etc/nixos /etc/nixos.bak
|
|
rm -rf /etc/nixos/*
|
|
else
|
|
mkdir -p /etc/nixos
|
|
fi
|
|
|
|
# Copy new configuration
|
|
echo "Copying files to /etc/nixos/"
|
|
cp -r root-conf/nixos/* /etc/nixos/
|
|
ls -l /etc/nixos/
|
|
|
|
# Rebuild if requested
|
|
if [[ $AUTO_REBUILD -eq 1 ]]; then
|
|
echo "Running nixos-rebuild switch..."
|
|
if [[ $FIRST_RUN -eq 1 ]]; then
|
|
echo "First run: running nixos-rebuild with --upgrade"
|
|
nixos-rebuild switch --upgrade
|
|
else
|
|
echo "Running nixos-rebuild switch with flake"
|
|
nixos-rebuild switch --flake /etc/nixos
|
|
fi
|
|
cp /etc/nixos/flake.lock root-conf/nixos/flake.lock
|
|
fi
|
|
|
|
echo "Done!" |