main
  1#!/usr/bin/env bash
  2
  3# Nagoya (Debian) bootstrap script.
  4# Installs Determinate Nix, checks out this repository, and applies the
  5# minimal host and Home Manager configurations. It intentionally leaves
  6# Debian-managed networking and services unchanged.
  7
  8if [[ "${BASH_SOURCE[0]:-}" == "${0}" ]]; then
  9	set -euo pipefail
 10fi
 11
 12readonly GREEN='\033[0;32m'
 13readonly YELLOW='\033[1;33m'
 14readonly RED='\033[0;31m'
 15readonly NC='\033[0m'
 16
 17REPO_URL="${REPO_URL:-https://git.sbr.pm/home.git}"
 18REPO_PATH="${REPO_PATH:-$HOME/src/home}"
 19SYSTEM_CONFIG="${SYSTEM_CONFIG:-nagoya}"
 20
 21log_info() { echo -e "${GREEN}[INFO]${NC} $*"; }
 22log_warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
 23log_error() { echo -e "${RED}[ERROR]${NC} $*" >&2; }
 24
 25check_root() {
 26	if [[ $EUID -eq 0 ]]; then
 27		log_error "Run as your regular user, not root (sudo is used when needed)"
 28		exit 1
 29	fi
 30}
 31
 32source_nix() {
 33	if ! command -v nix &>/dev/null && [[ -f /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh ]]; then
 34		# shellcheck disable=SC1091
 35		. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
 36	fi
 37}
 38
 39install_nix() {
 40	if command -v nix &>/dev/null; then
 41		log_info "Nix already installed: $(nix --version)"
 42		return 0
 43	fi
 44
 45	log_info "Installing Nix (Determinate Systems installer)..."
 46	curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install
 47	source_nix
 48	log_info "Nix installed: $(nix --version)"
 49}
 50
 51configure_nix() {
 52	local nix_conf="${XDG_CONFIG_HOME:-$HOME/.config}/nix/nix.conf"
 53	mkdir -p "$(dirname "$nix_conf")"
 54
 55	if [[ ! -f "$nix_conf" ]] || ! grep -q '^experimental-features[[:space:]]*=' "$nix_conf"; then
 56		echo 'experimental-features = nix-command flakes' >>"$nix_conf"
 57	fi
 58	if ! grep -q '^use-xdg-base-directories[[:space:]]*=' "$nix_conf"; then
 59		echo 'use-xdg-base-directories = true' >>"$nix_conf"
 60	fi
 61}
 62
 63clone_repo() {
 64	if [[ -d "$REPO_PATH/.git" ]]; then
 65		log_info "Repository exists at $REPO_PATH, pulling..."
 66		git -C "$REPO_PATH" pull --ff-only || log_warn "Pull failed, continuing with existing checkout"
 67		return 0
 68	fi
 69
 70	log_info "Cloning $REPO_URL → $REPO_PATH"
 71	mkdir -p "$(dirname "$REPO_PATH")"
 72	git clone "$REPO_URL" "$REPO_PATH"
 73}
 74
 75setup_host_configs() {
 76	local config_path
 77
 78	cd "$REPO_PATH"
 79	config_path=$(nix build ".#hostConfigs.${SYSTEM_CONFIG}" --no-link --print-out-paths)
 80	sudo "${config_path}/deploy"
 81}
 82
 83setup_home_manager() {
 84	cd "$REPO_PATH"
 85	nix run home-manager -- switch --flake ".#vincent@${SYSTEM_CONFIG}" -b backup
 86}
 87
 88main() {
 89	log_info "Bootstrapping Nagoya..."
 90	log_info "  Repo:   $REPO_URL → $REPO_PATH"
 91	log_info "  Config: $SYSTEM_CONFIG"
 92
 93	check_root
 94	install_nix
 95	configure_nix
 96	clone_repo
 97	setup_host_configs
 98	setup_home_manager
 99
100	log_info "Nagoya bootstrap completed. Existing Debian networking and services were left unchanged."
101}
102
103if [[ "${BASH_SOURCE[0]:-}" == "${0}" ]]; then
104	main "$@"
105fi