fedora-csb-system-manager
  1#!/usr/bin/env bash
  2
  3# Fedora CSB Bootstrap Script
  4# Description: Installs Nix and sets up system-manager on Fedora CSB
  5
  6set -euo pipefail
  7
  8# Color output for better readability
  9readonly RED='\033[0;31m'
 10readonly GREEN='\033[0;32m'
 11readonly YELLOW='\033[1;33m'
 12readonly NC='\033[0m' # No Color
 13
 14# Configuration
 15REPO_URL="${REPO_URL:-https://github.com/vdemeester/home.git}"
 16REPO_PATH="${REPO_PATH:-$HOME/src/home}"
 17SYSTEM_CONFIG="${SYSTEM_CONFIG:-fedora-csb}"
 18
 19# Logging functions
 20log_info() {
 21	echo -e "${GREEN}[INFO]${NC} $*"
 22}
 23
 24log_warn() {
 25	echo -e "${YELLOW}[WARN]${NC} $*"
 26}
 27
 28log_error() {
 29	echo -e "${RED}[ERROR]${NC} $*" >&2
 30}
 31
 32check_root() {
 33	if [[ $EUID -eq 0 ]]; then
 34		log_error "This script should NOT be run as root"
 35		log_error "Run as your regular user - it will use sudo when needed"
 36		exit 1
 37	fi
 38}
 39
 40install_dependencies() {
 41	log_info "Installing dependencies..."
 42	sudo dnf install -y git curl xz
 43}
 44
 45install_nix() {
 46	if command -v nix &>/dev/null; then
 47		log_info "Nix is already installed"
 48		return 0
 49	fi
 50
 51	log_info "Installing Nix..."
 52
 53	# Use the Determinate Systems installer for better multi-user experience
 54	# It handles SELinux and systemd integration well on Fedora
 55	curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install
 56
 57	log_info "Nix installed successfully"
 58	log_warn "You may need to restart your shell or run: . /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh"
 59}
 60
 61setup_nix_config() {
 62	log_info "Configuring Nix..."
 63
 64	# Ensure Nix is in PATH for this session
 65	if [[ -f /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh ]]; then
 66		# shellcheck disable=SC1091
 67		. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
 68	fi
 69
 70	# Create XDG config directory for Nix
 71	mkdir -p "${XDG_CONFIG_HOME:-$HOME/.config}/nix"
 72
 73	# Enable flakes and nix-command
 74	cat >"${XDG_CONFIG_HOME:-$HOME/.config}/nix/nix.conf" <<EOF
 75experimental-features = nix-command flakes
 76use-xdg-base-directories = true
 77EOF
 78
 79	log_info "Nix configured with flakes enabled"
 80}
 81
 82clone_repo() {
 83	if [[ -d "$REPO_PATH" ]]; then
 84		log_info "Repository already exists at $REPO_PATH"
 85		log_info "Pulling latest changes..."
 86		git -C "$REPO_PATH" pull --ff-only || log_warn "Could not pull, continuing with existing repo"
 87		return 0
 88	fi
 89
 90	log_info "Cloning repository to $REPO_PATH..."
 91	mkdir -p "$(dirname "$REPO_PATH")"
 92	git clone "$REPO_URL" "$REPO_PATH"
 93	log_info "Repository cloned successfully"
 94}
 95
 96build_system_manager() {
 97	log_info "Building system-manager configuration..."
 98
 99	cd "$REPO_PATH"
100
101	# Ensure Nix is available
102	if ! command -v nix &>/dev/null; then
103		if [[ -f /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh ]]; then
104			# shellcheck disable=SC1091
105			. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
106		else
107			log_error "Nix not found in PATH. Please restart your shell and run this script again."
108			exit 1
109		fi
110	fi
111
112	log_info "Building system configuration: $SYSTEM_CONFIG"
113	nix build ".#systemConfigs.${SYSTEM_CONFIG}" --no-link --print-out-paths
114}
115
116activate_system_manager() {
117	log_info "Activating system-manager..."
118
119	cd "$REPO_PATH"
120
121	# Get the built system path
122	local system_path
123	system_path=$(nix build ".#systemConfigs.${SYSTEM_CONFIG}" --no-link --print-out-paths)
124
125	log_info "Activating configuration from: $system_path"
126
127	# Activate the system configuration
128	sudo "${system_path}/bin/activate"
129
130	log_info "System-manager activated successfully!"
131}
132
133print_next_steps() {
134	log_info ""
135	log_info "Bootstrap complete!"
136	log_info ""
137	log_info "Next steps:"
138	log_info "  1. Restart your shell or run: exec \$SHELL"
139	log_info "  2. Navigate to repo: cd $REPO_PATH"
140	log_info "  3. To rebuild and activate: nix build .#systemConfigs.${SYSTEM_CONFIG} && sudo ./result/bin/activate"
141	log_info ""
142	log_info "Useful commands:"
143	log_info "  - Build only: nix build .#systemConfigs.${SYSTEM_CONFIG}"
144	log_info "  - Show flake outputs: nix flake show"
145	log_info ""
146}
147
148main() {
149	log_info "Starting Fedora CSB bootstrap..."
150	log_info "Configuration: $SYSTEM_CONFIG"
151	log_info "Repository: $REPO_URL -> $REPO_PATH"
152	log_info ""
153
154	check_root
155	install_dependencies
156	install_nix
157	setup_nix_config
158	clone_repo
159	build_system_manager
160	activate_system_manager
161	print_next_steps
162}
163
164main "$@"