Version-Controlled Dotfiles With chezmoi: One Repo, Every Machine, No Symlink Mess

The real test of a dotfiles setup is not how clever your shell prompt looks. It is how long it takes to get a brand-new laptop back to your working environment after the old one dies, gets stolen, or finally refuses to boot.

If the honest answer is "a day of copying files from a backup and remembering which settings I changed", you do not have a dotfiles setup. You have archaeology. This post walks through doing it properly with chezmoi: one git repository that holds your shell, git, and editor configuration, templates the bits that differ between machines, pulls secrets from a password manager instead of committing them, and rebuilds a fresh box with a single command. I will also say where the older symlink approach, GNU stow, still earns its place.

What chezmoi actually does

chezmoi keeps a source directory — by default ~/.local/share/chezmoi, which is a normal git repository — and applies its contents to your home directory. It does not symlink. It renders each source file and writes the result to the target path, which is what lets it template files and decrypt secrets on the way through. The naming convention carries the metadata: dot_zshrc becomes ~/.zshrc, a .tmpl suffix marks a file as a Go template, private_ forces 0600 permissions, and encrypted_ marks a file that is stored encrypted in the repo and decrypted on apply.

The daily loop is small. chezmoi add ~/.gitconfig pulls a file into the source repo. chezmoi edit ~/.gitconfig edits the source, not the target. chezmoi diff shows what would change before it changes it, and chezmoi apply writes it out. On another machine, chezmoi update does a git pull and applies in one step. Because the source is just git, your history, branches and rollback are the ones you already know.

Templating the bits that differ per machine

The reason people give up on a shared dotfiles repo is that machines are not identical. Work email is not personal email. macOS puts Homebrew in /opt/homebrew; Linux does not have it at all. Committing one version and hand-patching the other on each box is exactly the drift you were trying to escape.

chezmoi handles this with a config file it generates per machine and a set of built-in template variables. Put a .chezmoi.toml.tmpl in the source root and chezmoi runs it once, at init time, prompting for anything it does not already know:

# ~/.local/share/chezmoi/.chezmoi.toml.tmpl
# Rendered once by `chezmoi init`; the answers are stored in
# ~/.config/chezmoi/chezmoi.toml and reused on every apply.
{{- $email := promptStringOnce . "email" "Git email for this machine" -}}
{{- $work  := promptBoolOnce   . "work"  "Is this a work machine" -}}

[data]
    email = {{ $email | quote }}
    work  = {{ $work }}

Now any templated dotfile can branch on that data, or on chezmoi's own variables such as .chezmoi.os, .chezmoi.hostname and .chezmoi.arch. A single dot_gitconfig.tmpl and dot_zshrc.tmpl cover every machine you own:

# dot_gitconfig.tmpl  ->  ~/.gitconfig
[user]
    name  = Richard King
    email = {{ .email }}
{{ if .work }}
# Rewrite work org clones over SSH without touching public repos
[url "git@github.com:acme-corp/"]
    insteadOf = https://github.com/acme-corp/
{{ end }}

# dot_zshrc.tmpl  ->  ~/.zshrc  (OS-specific PATH handling)
{{ if eq .chezmoi.os "darwin" -}}
export HOMEBREW_PREFIX="/opt/homebrew"
eval "$({{ .chezmoi.homeDir }}/../../opt/homebrew/bin/brew shellenv)"
{{ else if eq .chezmoi.os "linux" -}}
export PATH="$HOME/.local/bin:$PATH"
{{ end -}}

Run chezmoi diff and you see the rendered result for this machine, not the template. That is the whole trick: the repository is uniform, the output is machine-specific, and nothing is edited in place after checkout.

Secrets: pull them, do not commit them

The one rule that matters: a dotfiles repo, even a private one, is not a secrets store. Tokens and keys leak through forks, backups, and the day you make the repo public by accident. chezmoi gives you two disciplined options, and they compose. The same logic that makes API keys a liability when mishandled applies to whatever you paste into a .env.

The first option is to read secrets from a password manager at apply time, so the plaintext never lands in git. chezmoi ships template functions for the common ones — 1Password, Bitwarden, pass, and others:

# dot_config/private_shell/env.tmpl  ->  ~/.config/shell/env
# Values are fetched from the password manager when `chezmoi apply` runs.
export GITHUB_TOKEN={{ onepasswordRead "op://Personal/GitHub Token/credential" | quote }}
export STRIPE_KEY={{ pass "stripe/live-key" | quote }}

The second option, for files that must physically exist on disk (an SSH config, a service credential a tool reads directly), is age encryption. Generate a key, tell chezmoi to use it, and add files with --encrypt:

# Generate an age key once, keep the private key OFF the repo
chezmoi age-keygen --output ~/.config/chezmoi/key.txt

# ~/.config/chezmoi/chezmoi.toml
encryption = "age"
[age]
    identity  = "~/.config/chezmoi/key.txt"
    recipient = "age1ql3z7hjy54pw3hyww5ayyfg7zqgvc7w3j2elw8zmrj2kg5sfn9aqmcac8p"

# Add a file encrypted; it lands in the repo as encrypted_...age
chezmoi add --encrypt ~/.ssh/config

The encrypted blob is safe to commit; the age private key is the one thing you carry out of band — in your password manager, on a hardware key, wherever your recovery process already lives. Lose it and you re-key; that is the deliberate trade.

Where GNU stow still fits

GNU stow predates chezmoi and does one thing well: it symlinks. You organise dotfiles into "packages" under a stow directory, and stow zsh creates symlinks from ~/.zshrc back into the repo. stow -D zsh removes them. There is no templating, no secrets handling, and no rendering — the file in your home directory is the file in the repo, via a symlink.

That simplicity is a genuine virtue when your machines are effectively identical — one OS, one identity, no per-host differences — and you want edits to a live file to be edits to the repo with zero indirection. The moment you have two operating systems, a work and a personal identity, or a single credential you would rather not commit, symlinks force you into per-machine branches or a pile of .local override files. That is the point at which chezmoi's rendering model stops being overhead and starts being the reason it works. Keeping this configuration in version control at all is the same discipline that makes a pre-commit baseline worth enforcing: the environment becomes reviewable, diffable, and reproducible rather than remembered.

Rebuilding a fresh machine in one command

Here is the payoff, and the reason to do any of this. On a box with nothing but a shell and curl, one line installs chezmoi, clones your repo, prompts for the machine-specific answers, and applies everything:

sh -c "$(curl -fsLS get.chezmoi.io)" -- init --apply $GITHUB_USERNAME

chezmoi installs itself to ./bin (use get.chezmoi.io/lb for ~/.local/bin), pulls github.com/$GITHUB_USERNAME/dotfiles, runs the .chezmoi.toml.tmpl prompts, decrypts age files, resolves password-manager secrets, and writes your environment out. For a private repo, swap the username for git@github.com:$GITHUB_USERNAME/dotfiles.git so it clones over SSH.

That command is the whole argument. A laptop reinstall stops being a day of archaeology and becomes a coffee-length wait, and every machine you own converges on the same reviewed, version-controlled state. The dotfiles are not the point — the recoverability is.

Free interactive tool

Website compliance checklist

What your site has to do, based on what it actually does

Answer as much or as little as you like — the list builds as you go. Nothing is stored against your name and no email is required.

Most technology problems are not technology problems. They are control problems.

The systems exist. The investment has been made. The question is whether leadership can understand, direct, evidence, and sustain what those systems produce. Find out where control exists — and where it only appears to.