What is Chezmoi?
Chezmoi is a dotfiles manager that:- Maintains your dotfiles in a Git repository
- Applies them to your home directory
- Supports templating for per-machine customization
- Integrates with password managers and encryption tools
- Runs scripts to automate additional setup
Think of chezmoi as a “desired state engine” for your home directory, similar to how Kubernetes manages container deployments.
Configuration File
The.chezmoi.toml.tmpl file configures chezmoi’s behavior and stores machine-specific data.
Interactive Prompts
On first run, chezmoi asks questions to customize your environment:.chezmoi.toml.tmpl
promptStringOnce caches answers in ~/.config/chezmoi/chezmoi.toml so you’re only asked once per machine.Data Section
Prompted values are stored in the[data] section and become available to all templates:
Editor Integration
Chezmoi configures VS Code for editing, diffing, and merging:.chezmoi.toml.tmpl
chezmoi edit ~/.bashrc- Opens in VS Codechezmoi diff- Shows changes in VS Code diff viewchezmoi merge ~/.gitconfig- Three-way merge conflicts
Bitwarden Integration
Configuration
.chezmoi.toml.tmpl
bw CLI and automatically prompt for unlock if the vault is locked.
Template Functions
Chezmoi provides two Bitwarden template functions:- bitwarden
- bitwardenFields
Retrieves the entire item as JSON:Use
.notes for Secure Note contents or .login.password for passwords.Age Encryption
Configuration
.chezmoi.toml.tmpl
identity: Your private key for decryptionrecipient: Your public key for encryption (derived from the private key)
How It Works
The
.age file is safe to commit to Git. Only someone with your private key at ~/.config/chezmoi/key.txt can decrypt it.File Naming Conventions
Chezmoi uses special prefixes to control file handling:| Prefix | Effect | Example |
|---|---|---|
dot_ | Becomes . | dot_bashrc → ~/.bashrc |
private_ | Permissions 0600 | private_dot_ssh/ → ~/.ssh/ (mode 600) |
executable_ | Permissions 0755 | executable_script.sh → ~/script.sh (executable) |
.tmpl | Template processing | dot_gitconfig.tmpl → ~/.gitconfig (rendered) |
.age | Age encryption | config.age → config (decrypted on apply) |
Combined Examples
Template Syntax
Chezmoi uses Go’stext/template engine:
Variable Substitution
dot_bashrc.tmpl
Conditionals
dot_gitconfig.tmpl
Functions
Available Template Variables
Available Template Variables
.name- User’s full name.machine_type-personal,work, orhybrid.os- Operating system.editor- Default editor.personal_email- Personal email address.work_email- Work email address.chezmoi.homeDir- Home directory path.chezmoi.sourceDir- Chezmoi source directory.chezmoi.username- Current username.chezmoi.hostname- Machine hostname
Scripts
Chezmoi can run scripts at various lifecycle stages:Script Types
| Script Name | When It Runs |
|---|---|
run_once_*.sh | Only once ever (tracked in state) |
run_before_*.sh | Before applying files |
run_after_*.sh | After applying files |
run_onchange_*.sh | When the script content changes |
Example: Running Ansible
run_once_after_ansible.sh.tmpl
- Uses the
.tmplextension for template processing - Uses
run_once_afterto run once after files are applied - Changes to the Ansible directory using
{{ .chezmoi.sourceDir }} - Executes the playbook
Common Commands
Directory Structure
Best Practices
Secrets Handling
Secrets Handling
- Never commit
.config/chezmoi/key.txt - Use Bitwarden for credentials, not plaintext templates
- Mark sensitive files with
private_prefix - Use age encryption for configs containing secrets
Template Organization
Template Organization
- Use
{{ .machine_type }}conditionals for hybrid setups - Keep complex logic in separate script files
- Use
includefor shared template snippets - Comment template sections for clarity
Testing Changes
Testing Changes
- Always run
chezmoi diffbeforeapply - Use
--dry-runto preview script execution - Test in a VM before applying to production machines
- Commit and push changes regularly
Next Steps
Ansible Integration
Learn how Ansible automates system configuration
Secrets Management
Deep dive into Bitwarden and age encryption
Customization Guide
Add your own packages and configurations
Official Docs
Chezmoi reference documentation