Skip to content

Instantly share code, notes, and snippets.

@jorgeav527
Last active September 10, 2026 16:59
Show Gist options
  • Select an option

  • Save jorgeav527/b791460eadeb1ceb31869c4959073c41 to your computer and use it in GitHub Desktop.

Select an option

Save jorgeav527/b791460eadeb1ceb31869c4959073c41 to your computer and use it in GitHub Desktop.
bootstrap for dotfiles ansible repo
#!/usr/bin/env bash
set -euo pipefail
GITHUB_USER="jorgeav527"
REPO_NAME="dotfiles"
PROJECTS_DIR="$HOME/Projects"
DOTFILES_DIR="$PROJECTS_DIR/$REPO_NAME"
KEY_PATH="$HOME/.ssh/id_ed25519_github"
# Force read from /dev/tty so curl | bash handles interactive input
read -re -p "Enter machine identifier [default: desktop]: " MACHINE < /dev/tty
MACHINE="${MACHINE:-desktop}"
echo "==> Machine set to: $MACHINE"
# -----------------------------------------------------------------------------
# 1. Detect OS & Install Base Packages
# -----------------------------------------------------------------------------
if [ -f /etc/os-release ]; then
. /etc/os-release
OS_ID="$ID"
else
echo "Error: /etc/os-release not found. Cannot determine OS." >&2
exit 1
fi
echo "==> Installing base system packages ($NAME)..."
if [[ "$OS_ID" == "arch" || "$OS_ID" == "omarchy" || "${OS_ID_LIKE:-}" == *"arch"* ]]; then
echo "--> Detected Arch-based distribution. Updating pacman databases..."
sudo pacman -Sy --needed --noconfirm git ansible openssh
elif [[ "$OS_ID" == "debian" || "$OS_ID" == "ubuntu" || "${OS_ID_LIKE:-}" == *"debian"* ]]; then
echo "--> Detected Debian-based distribution. Updating apt-get..."
sudo apt-get update
sudo apt-get install -y git ansible openssh-client
else
echo "Error: Unsupported distribution: $OS_ID" >&2
exit 1
fi
# -----------------------------------------------------------------------------
# 2. Check & Generate GitHub SSH Key
# -----------------------------------------------------------------------------
echo "==> Checking GitHub SSH key..."
if [ ! -f "$KEY_PATH" ]; then
echo "Creating new GitHub SSH key at $KEY_PATH..."
mkdir -p "$HOME/.ssh"
chmod 700 "$HOME/.ssh"
ssh-keygen -t ed25519 -C "${GITHUB_USER}@${MACHINE}" -f "$KEY_PATH" -N ""
if ! grep -q "Host github.com" "$HOME/.ssh/config" 2>/dev/null; then
cat <<EOF >> "$HOME/.ssh/config"
Host github.com
HostName github.com
User git
IdentityFile $KEY_PATH
IdentitiesOnly yes
EOF
fi
chmod 600 "$HOME/.ssh/config"
echo ""
echo "=========================================================================="
echo " Add this public key to GitHub: https://github.com/settings/keys"
echo "=========================================================================="
cat "${KEY_PATH}.pub"
echo "=========================================================================="
echo ""
read -rp "Press [Enter] once added to GitHub..." _ < /dev/tty
else
echo "GitHub SSH key already exists at $KEY_PATH. Skipping."
fi
# Fetch GitHub host keys safely
ssh-keyscan github.com >> "$HOME/.ssh/known_hosts" 2>/dev/null || true
# -----------------------------------------------------------------------------
# 3. Sync Dotfiles Repository
# -----------------------------------------------------------------------------
echo "==> Syncing repository..."
mkdir -p "$PROJECTS_DIR"
if [ ! -d "$DOTFILES_DIR" ]; then
# Clone public repo into ~/Projects/dotfiles via HTTPS
git clone "https://github.com/${GITHUB_USER}/${REPO_NAME}.git" "$DOTFILES_DIR"
# Switch remote URL to SSH for future pushes
git -C "$DOTFILES_DIR" remote set-url origin "git@github.com:${GITHUB_USER}/${REPO_NAME}.git"
else
echo "Repository already exists at $DOTFILES_DIR. Updating..."
git -C "$DOTFILES_DIR" pull origin main
fi
# Last gist update
# curl -fsSL https://gist.githubusercontent.com/jorgeav527/b791460eadeb1ceb31869c4959073c41/raw/dotfiles_bootstrap.sh | bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment