Chezmoi dotfiles backup
Short Introduction to chezmoi
- Desired State: chezmoi works by storing the desired state of your dotfiles in a dedicated source directory (usually
~/.local/share/chezmoi), which is also a Git repository. - No Symlinks (by default): Unlike some older methods, chezmoi typically doesn’t use symbolic links. It copies and manages the actual files in your home directory, avoiding issues like broken links.
- Cross-Machine Consistency: It uses templating (Go’s text/template) to handle differences between machines. You can write one file (
.gitconfig.tmpl) and have it automatically apply the correct settings (like different emails or hostnames) on your personal laptop versus your work desktop. - Security: It has built-in support for encrypting secrets (using tools like GPG or age) and integrating with password managers, so you can safely store sensitive data within your public dotfiles repository.
Basic Workflow for Backup and Management
The process involves two main phases: Initial Setup and Daily Use/Syncing.
Phase 1: Initial Setup
-
Initialize chezmoi:
chezmoi initThis creates the source directory (the Git repository) where chezmoi will manage your files (
~/.local/share/chezmoi). -
Add Your First Dotfile:
chezmoi add ~/.zshrcThis copies your existing
~/.zshrcinto the chezmoi source directory, putting it under management. chezmoi will automatically rename it internally (e.g., todot_zshrc) to handle the leading dot. Your original~/.zshrcfile is now “managed.” -
Check and Apply (Optional but Recommended):
chezmoi diff chezmoi applychezmoi diffshows you the difference between the managed file in the source directory and the file in your home directory. If you just ranchezmoi add, the file in your home directory is now the “target,” and the diff should be empty.chezmoi applymakes the necessary changes to your home directory files to match the source state.
-
Commit and Push to Git (The Backup!):
chezmoi cd # Now you are inside the source directory (~/.local/share/chezmoi) git add . git commit -m "Initial commit of zshrc" git remote add origin <URL-of-your-dotfiles-repo> git push -u origin mainThis is your backup! By committing and pushing the source directory, your managed dotfiles are now securely version-controlled and backed up in your remote Git repository (e.g., GitHub, GitLab).
Phase 2: Daily Use
-
Make a Change to a Managed File:
The best way to edit a managed file is with:
chezmoi edit ~/.zshrcThis opens the source file inside the chezmoi source directory in your
$EDITOR. -
Apply the Change: When you save and exit the editor, the change is only in the source repository. To update the live file in your home directory:
chezmoi apply # This updates the live ~/.zshrc from the managed copy. -
Sync the Backup: To save the changes to your Git backup:
chezmoi cd git commit -am "Update zshrc alias" git push
Phase 3: Setting Up a New Machine (Restore)
- Install chezmoi on the new machine.
- Initialize and Apply with URL:
This single command clones your repository and applies all your dotfiles to the new machine, setting up your environment instantly!
chezmoi init --apply <URL-of-your-dotfiles-repo> # OR: chezmoi init --apply your_github_username