Skip to content

Instantly share code, notes, and snippets.

@jmmaranan
Last active May 26, 2025 21:37
Show Gist options
  • Save jmmaranan/01beab9e72b81c355161f873a93768b7 to your computer and use it in GitHub Desktop.
Save jmmaranan/01beab9e72b81c355161f873a93768b7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# --- Configuration ---
# Set your Chezmoi user/repo.
# For a PRIVATE GitHub repository, it's highly recommended to use an SSH URL
# after ensuring your SSH keys are set up with GitHub.
# Example SSH URL: CHEZMOI_USER_REPO="[email protected]:your_username/your_private_dotfiles.git"
#
# If you use an HTTPS URL for a private repo (e.g., "https://github.com/your_username/private_dots.git"),
# you will likely be prompted for credentials by Git, or you'll need a Git credential helper
# configured (e.g., by running `gh auth login` if you use the GitHub CLI).
#
# If CHEZMOI_USER_REPO is not set as an environment variable, it will use the default below.
: "${CHEZMOI_USER_REPO:="[email protected]:jmmaranan/dotfiles.git"}" # Example default, adjust as needed
# --- Variables ---
CHEZMOI_INSTALL_SCRIPT_URL="https://get.chezmoi.io"
# Default Chezmoi source path (for informational check if binary is missing)
DEFAULT_CHEZMOI_SOURCE_DIR="${HOME}/.local/share/chezmoi"
if [ -n "$XDG_DATA_HOME" ]; then
DEFAULT_CHEZMOI_SOURCE_DIR="${XDG_DATA_HOME}/chezmoi"
fi
# --- Helper Functions ---
print_info() {
echo "INFO: $1"
}
print_note() {
echo "NOTE: $1"
}
print_action() {
echo "ACTION: $1"
}
print_success() {
echo "SUCCESS: $1"
}
print_error() {
echo "ERROR: $1" >&2
}
# --- Prerequisite Checks ---
check_prerequisites() {
# Check for Git
if ! command -v git &> /dev/null; then
print_error "Git is not installed, but it's required by Chezmoi."
print_error "Please install Git first and then re-run this script."
# Instructions for common systems (can be expanded)
if [[ "$(uname)" == "Darwin" ]]; then
print_error "On macOS, you can install Git with Homebrew: brew install git"
print_error "Or by installing Xcode Command Line Tools: xcode-select --install"
elif command -v apt-get &> /dev/null; then
print_error "On Debian/Ubuntu, try: sudo apt-get update && sudo apt-get install git"
elif command -v yum &> /dev/null; then
print_error "On Fedora/RHEL, try: sudo yum install git"
fi
return 1
fi
print_info "Git is installed."
return 0
}
# --- Main Logic ---
main() {
print_info "Starting Chezmoi setup for user/repo: ${CHEZMOI_USER_REPO}"
echo "---"
if ! check_prerequisites; then
return 1
fi
echo "---"
# Guidance for private repositories
if [[ "${CHEZMOI_USER_REPO}" == git@* ]] || [[ "${CHEZMOI_USER_REPO}" == ssh://* ]]; then
print_info "Using SSH URL for Chezmoi repository. Ensure your SSH keys are configured with GitHub and loaded."
elif [[ "${CHEZMOI_USER_REPO}" == https://*github.com* ]]; then
print_note "Using HTTPS URL for a GitHub repository."
print_note "If this is a private repository, Git will require authentication."
print_note "This might be an interactive prompt for username/password (not recommended for scripting),"
print_note "or it might use a Git credential helper (e.g., if you've used 'gh auth login' or Git Credential Manager)."
print_note "For a smoother experience with private repos, consider using SSH URLs."
fi
echo "---"
if command -v chezmoi &> /dev/null; then
print_info "Chezmoi binary is already installed."
else
print_info "Chezmoi binary not found."
if [ -d "${DEFAULT_CHEZMOI_SOURCE_DIR}" ]; then
print_note "An existing directory was found at a common Chezmoi source path: ${DEFAULT_CHEZMOI_SOURCE_DIR}."
else
print_info "No pre-existing directory found at the common Chezmoi source path."
fi
print_action "Installing Chezmoi binary..."
if sh -c "$(curl -fsSL ${CHEZMOI_INSTALL_SCRIPT_URL})"; then # Just install binary first
print_success "Chezmoi binary installation attempt finished."
# Ensure chezmoi is in PATH for the next command if just installed this session
# This depends on how the installer adds it to PATH; often requires new shell or sourcing profile.
# For common installations, it might be in /usr/local/bin or ~/bin
# We will rely on the user having to potentially re-source or use the full path if it's a brand new install
# or hope the installer script makes it available.
# A more robust way if the installer doesn't update current shell's PATH:
# export PATH="$HOME/bin:$PATH" # If installed to ~/bin
# export PATH="/usr/local/bin:$PATH" # Common location
else
print_error "Chezmoi binary installation script failed. Please check the output."
return 1
fi
fi
# Now, initialize and apply
# This assumes `chezmoi` is now in PATH if it was just installed.
# If the installer script prompts to update PATH and exit, this script might need adjustment
# or user needs to re-run/source profile. get.chezmoi.io usually puts it in a standard PATH location.
print_action "Initializing Chezmoi and applying configuration for '${CHEZMOI_USER_REPO}'..."
if chezmoi init --apply "${CHEZMOI_USER_REPO}"; then
print_success "Chezmoi initialization and application attempt finished."
else
print_error "Chezmoi 'init --apply' failed. Please check the output."
print_error "If using a private repository, ensure your authentication (SSH key or HTTPS credentials/helper) is correctly set up."
return 1
fi
echo "---"
print_success "Chezmoi setup process complete."
print_info "To verify, try running: chezmoi --version"
print_info "Or check your dotfiles status: chezmoi status"
}
# --- Run Script ---
# Stop on errors for critical parts, but allow some informational steps to not halt everything
# set -e # Commented out to allow more granular error handling for now
main
# If main returns an error, exit with that error code
# exit $? # This line can be problematic if set -e is also used.
# Script will exit with the status of the last command in main if no explicit exit is called.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment