#!/bin/sh # Set up a new macOS or Linux machine with the dotfiles: # # curl -fsSL https://get.amitprasad.dev | sh # # It installs Determinate Nix if necessary, logs in to GitHub (the dotfiles # repo is private), clones the dotfiles and applies home-manager. Only the Nix # install uses sudo. # # Env: DOTFILES_TARGET (flake output, e.g. amit@magnolia), DOTFILES_REPO # (default AmitPr/dotfiles), DOTFILES_DIR (default ~/.config/dotfiles), # GH_TOKEN or GITHUB_TOKEN (skips the interactive login) set -eu DOTFILES_REPO="${DOTFILES_REPO:-AmitPr/dotfiles}" DOTFILES_DIR="${DOTFILES_DIR:-$HOME/.config/dotfiles}" DOTFILES_TARGET="${DOTFILES_TARGET:-}" # stdin is the curl pipe, so prompts use /dev/tty. Open it, because an access # check passes in CI without a terminal, and the prompt then hangs. tty_ok() { (exec 3<>/dev/tty) 2>/dev/null; } if [ -t 2 ] || tty_ok; then c_step=$(printf '\033[1;34m'); c_ok=$(printf '\033[1;32m') c_warn=$(printf '\033[1;33m'); c_err=$(printf '\033[1;31m'); c_off=$(printf '\033[0m') else c_step=''; c_ok=''; c_warn=''; c_err=''; c_off='' fi step() { printf '%s==>%s %s\n' "$c_step" "$c_off" "$*" >&2; } ok() { printf '%s ok%s %s\n' "$c_ok" "$c_off" "$*" >&2; } warn() { printf '%swarn%s %s\n' "$c_warn" "$c_off" "$*" >&2; } die() { printf '%serror%s %s\n' "$c_err" "$c_off" "$*" >&2; exit 1; } OS="$(uname -s)" case "$OS" in Darwin|Linux) : ;; *) die "unsupported OS: $OS (expected Darwin or Linux)" ;; esac if [ "$(id -u)" = 0 ] && [ -z "${DOTFILES_TARGET}" ]; then warn "running as root: home-manager will target /root. Set DOTFILES_TARGET=root@server if that is intended." fi step "Bootstrapping $OS host as $(id -un) -> repo $DOTFILES_REPO into $DOTFILES_DIR" ensure_nix_on_path() { command -v nix >/dev/null 2>&1 && return 0 for p in \ /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh \ "$HOME/.nix-profile/etc/profile.d/nix.sh"; do # shellcheck disable=SC1090 [ -e "$p" ] && . "$p" done command -v nix >/dev/null 2>&1 } if ensure_nix_on_path; then ok "Nix already installed ($(nix --version 2>/dev/null || echo present))" else step "Installing Determinate Nix (will prompt for sudo)" curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix \ | sh -s -- install --determinate --no-confirm \ || die "Nix install failed" ensure_nix_on_path || die "Nix installed but not on PATH; open a new shell and re-run" ok "Nix installed ($(nix --version 2>/dev/null))" fi # A stock Nix daemon from an earlier install may not enable flakes. export NIX_CONFIG="${NIX_CONFIG:-} extra-experimental-features = nix-command flakes" # Use gh and git from nixpkgs for this run if they are missing. gh runs git, # so both must be on the same PATH. have_tools() { command -v gh >/dev/null 2>&1 && command -v git >/dev/null 2>&1; } gh_run() { if have_tools; then gh "$@"; else nix shell nixpkgs#gh nixpkgs#git -c gh "$@"; fi; } git_run() { if command -v git >/dev/null 2>&1; then git "$@"; else nix shell nixpkgs#git -c git "$@"; fi; } step "Checking GitHub authentication" if [ -n "${GH_TOKEN:-}${GITHUB_TOKEN:-}" ]; then ok "using token from environment" elif gh_run auth status >/dev/null 2>&1; then ok "gh already authenticated" elif tty_ok; then step "Launching 'gh auth login' (choose GitHub.com > HTTPS)" gh_run auth login < /dev/tty || die "gh auth login failed" else die "not authenticated, no TTY, and no GH_TOKEN set. Re-run with GH_TOKEN= in the environment." fi gh_run auth setup-git >/dev/null 2>&1 || warn "could not configure git credential helper (continuing)" if [ -d "$DOTFILES_DIR/.git" ]; then step "Updating existing checkout at $DOTFILES_DIR" git_run -C "$DOTFILES_DIR" pull --ff-only || warn "pull failed; using existing checkout as-is" else [ -e "$DOTFILES_DIR" ] && die "$DOTFILES_DIR exists but is not a git repo; move it aside and re-run" step "Cloning $DOTFILES_REPO -> $DOTFILES_DIR" mkdir -p "$(dirname "$DOTFILES_DIR")" gh_run repo clone "$DOTFILES_REPO" "$DOTFILES_DIR" || die "clone failed" fi ok "dotfiles at $DOTFILES_DIR" if [ -z "$DOTFILES_TARGET" ]; then case "$OS" in Darwin) suggested="amit@macbook" ;; *) suggested="" ;; esac if tty_ok; then { printf '\nKnown targets: amit@amit-mbp amit@amit-modal-mbp ec2-user@devbox amit@magnolia server\n' if [ -n "$suggested" ]; then printf 'home-manager target [%s]: ' "$suggested"; else printf 'home-manager target (user@host): '; fi } > /dev/tty IFS= read -r DOTFILES_TARGET < /dev/tty || DOTFILES_TARGET='' [ -z "$DOTFILES_TARGET" ] && DOTFILES_TARGET="$suggested" else DOTFILES_TARGET="$suggested" fi fi [ -n "$DOTFILES_TARGET" ] || die "no flake target; re-run with DOTFILES_TARGET=user@host" step "Applying home-manager configuration: #$DOTFILES_TARGET" # -b backup renames existing dotfiles to *.backup, so a collision does not stop # the switch. nix run github:nix-community/home-manager -- \ switch -b backup --flake "$DOTFILES_DIR#$DOTFILES_TARGET" \ || die "home-manager switch failed for target '$DOTFILES_TARGET'" ok "home-manager applied for $DOTFILES_TARGET" step "Done. Start a new shell (or 'exec zsh') to pick up the new environment."