← Pablo Murad

chezmoi on Linux and Windows, from the first file to a new machine

A practical chezmoi manual for Linux and Windows: installing, the daily edit and apply loop, attributes, templates, .chezmoiignore, scripts, externals, secrets with age and password managers, a new machine in one command, and what to do when something breaks.

Contents

In Dotfiles, the files that make a computer mine I told the story of how my configuration ended up in a repo and how chezmoi1 keeps Omarchy and Windows in sync. SSH, dotfiles and secrets covered commit signing and the secret side of things. This post is the manual: how to operate chezmoi day to day on both systems, from the first chezmoi init to bringing up a fresh machine with one command.

Every command here works the same on Linux and Windows unless the section says otherwise. In the Linux examples the home folder is ~. In PowerShell the safe way to write it is $HOME, as in chezmoi add $HOME\.gitconfig.

How chezmoi thinks #

There are three places involved, and once they're clear the rest of the tool makes sense.

The source directory lives at ~/.local/share/chezmoi on both systems (on Windows that's C:\Users\<name>\.local\share\chezmoi). It's a normal Git repo, and the file names in it are encoded: ~/.bashrc is stored as dot_bashrc, a private folder gets a private_ prefix, a template ends in .tmpl.

The config file lives at ~/.config/chezmoi/chezmoi.toml. It holds the settings and the data that are specific to one machine, like an email address or whether the machine is for work. It never goes to the repo.

The home folder is the destination. chezmoi reads the source directory and the config, renders the templates, works out what the home folder should look like (the target state) and then writes only what differs.

chezmoi remote Git remote source Source directory ~/.local/share/chezmoi remote->source chezmoi update source->remote git push target Target state (computed) source->target render templates config Config and data chezmoi.toml config->target home Home folder target->home chezmoi apply home->source add / re-add
Source
digraph chezmoi {
rankdir=LR;
node [shape=box, style=rounded, fontname="Helvetica"];
edge [fontname="Helvetica", fontsize=10];
remote [label="Git remote"];
source [label="Source directory\n~/.local/share/chezmoi"];
config [label="Config and data\nchezmoi.toml"];
target [label="Target state\n(computed)"];
home [label="Home folder"];
remote -> source [label="chezmoi update"];
source -> remote [label="git push"];
source -> target [label="render templates"];
config -> target;
target -> home [label="chezmoi apply"];
home -> source [label="add / re-add", style=dashed];
}

The files in the home folder are real copies. There's no symlink farm like with Stow, so editing ~/.bashrc directly leaves the source behind until it's brought back with re-add or merge. That one detail explains most of the confusion people have in the first week.

Installing #

On Linux the package manager is the easy route:

  • Arch and derivatives: sudo pacman -S chezmoi
  • Debian and Ubuntu: sudo apt-get install chezmoi
  • Fedora: sudo dnf install chezmoi
  • openSUSE: sudo zypper install chezmoi
  • Alpine: apk add chezmoi
  • Void: sudo xbps-install -S chezmoi
  • Nix: nix-env -i chezmoi
  • Termux: pkg install chezmoi
  • Snap: snap install chezmoi --classic

Distro repositories can lag behind the current release (2.74.2 as I write this). When that matters, the official script drops the latest binary in place:

sh -c "$(curl -fsLS https://get.chezmoi.io)"

By default it installs to ./bin. To pick the folder, pass -b inside the quotes, or use the /lb address, which installs to ./.local/bin:

sh -c "$(curl -fsLS https://get.chezmoi.io)" -- -b "$HOME/.local/bin"
sh -c "$(curl -fsLS https://get.chezmoi.io/lb)"

The script doesn't touch PATH, so the folder has to be there already.

On Windows any of the three package managers works:

winget install twpayne.chezmoi
scoop install chezmoi
choco install chezmoi

There's also a PowerShell installer that fetches the binary directly:

iex "&{$(irm 'https://get.chezmoi.io/ps1')}"

And mise use --global chezmoi@2.74.2 works on both systems for anyone already managing tools with mise.

After installing, open a new terminal and check:

chezmoi --version
chezmoi doctor

doctor prints a table of everything chezmoi found: config file, source directory, Git, editor, age, password manager tools. Any line marked as a warning or error is worth reading before going further.

Starting a repo from scratch #

chezmoi init creates the source directory and turns it into a Git repo. Then files go in with chezmoi add:

chezmoi init
chezmoi add ~/.bashrc ~/.gitconfig
chezmoi add ~/.config/nvim

Adding a folder adds everything inside it. Now the source directory has dot_bashrc, dot_gitconfig and dot_config/nvim/....

On Windows it's the same idea with Windows paths:

chezmoi init
chezmoi add $HOME\.gitconfig
chezmoi add $HOME\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

To push it somewhere, step into the source directory with chezmoi cd. It opens a new shell there, and exit brings you back:

chezmoi cd
git add .
git commit -m "First dotfiles"
git branch -M main
git remote add origin git@github.com:your-username/dotfiles.git
git push -u origin main
exit

The repo can live on GitHub, Forgejo, GitLab or any Git server. Naming it dotfiles saves typing later, because chezmoi init your-username assumes https://github.com/your-username/dotfiles.git. Any other address works if written in full:

chezmoi init https://codeberg.org/your-username/my-config.git
chezmoi init git@github.com:your-username/dotfiles.git

When the repo already exists, chezmoi init clones it instead of creating an empty one. Nothing is written to the home folder until chezmoi apply, so it's a good moment to run chezmoi diff and look at what would change.

The daily loop #

This is the part that gets used every day, so it's worth doing it the chezmoi way from the start.

Changing a file. chezmoi edit ~/.bashrc opens the source file (dot_bashrc) in the editor. When the editor closes, nothing has happened to the home folder yet. chezmoi apply writes it. To do both in one go:

chezmoi edit --apply ~/.bashrc

Looking before writing. These three commands are the safety net:

chezmoi diff
chezmoi status
chezmoi apply --dry-run --verbose

diff shows exactly what apply would change. status gives a short list with a letter per file (A added, D deleted, M modified, R a script that would run). The dry run goes through the whole apply without touching anything.

When the file was edited directly. It happens all the time: an app rewrites its own settings, or a quick fix goes straight into ~/.bashrc. chezmoi re-add copies the current version of every managed file back into the source:

chezmoi re-add
chezmoi re-add ~/.bashrc

re-add leaves templates alone, because it can't turn a rendered file back into a template. For those, chezmoi merge ~/.gitconfig opens a three-way merge between the home file, the source template and the rendered target. The default tool is vimdiff, and the [merge] section of the config can point to another one.

If apply finds that a file in the home folder changed since chezmoi last wrote it, it stops and asks what to do: show the diff, overwrite, overwrite all, skip or quit. Skip is the right answer when unsure, followed by re-add or merge.

Saving to the repo. chezmoi git runs Git inside the source directory without leaving the current folder. Anything with flags needs -- so chezmoi doesn't try to read the flags itself:

chezmoi git add .
chezmoi git -- commit -m "Add fzf keybindings"
chezmoi git push

Bringing changes to another machine. chezmoi update pulls the repo and applies it. To look first, pull and diff, then apply:

chezmoi git pull -- --autostash --rebase
chezmoi diff
chezmoi apply

Finding things. A few commands answer the "where is this" questions:

chezmoi managed
chezmoi unmanaged
chezmoi source-path ~/.bashrc
chezmoi cat ~/.gitconfig

managed lists everything under chezmoi. unmanaged lists what's in the home folder and isn't, which is handy when hunting for a config that should be in the repo. source-path shows the encoded name of a file. cat prints the target version of a file, with templates already rendered.

Stopping managing a file. chezmoi forget ~/.some-file removes it from the source and leaves the home copy alone. There's also chezmoi destroy, which removes the file from both places. I'd avoid it until the difference is very clear in your head.

The config file #

The config file starts empty and grows as needed. These are the settings worth knowing early:

[edit]
command = "code"
args = ["--wait"]

[git]
autoCommit = true
autoPush = true

[diff]
exclude = ["scripts"]

[edit] picks the editor. Without it chezmoi uses $VISUAL or $EDITOR. Graphical editors need their "wait" flag, otherwise chezmoi thinks the edit is done the moment the window opens.

autoCommit makes every add, edit and re-add turn into a commit, and autoPush pushes it right away. It's comfortable. It's also a way to push a secret to a remote in one keystroke, so I'd only turn autoPush on after the secrets are handled (see the section on secrets below).

[diff] exclude = ["scripts"] hides scripts from chezmoi diff, which keeps the output readable once there are a few of them. [status] exclude does the same for status.

A config template for every machine #

Writing the config by hand on each machine gets old quickly. If the repo has a file called .chezmoi.toml.tmpl at the root, chezmoi init renders it and saves the result as the config. Combined with the prompt functions, each machine answers a few questions once and never again:

{{- $email := promptStringOnce . "email" "Email address" -}}
{{- $work := promptBoolOnce . "work" "Is this a work machine" -}}

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

[git]
autoCommit = true

The Once part means the question only shows up if the value isn't already in the config. Everything under [data] becomes available to templates as .email and .work.

When the template changes later, chezmoi prints a warning saying the config template has changed. Running chezmoi init again regenerates the config and asks only the new questions.

Attributes: what the file names mean #

The prefixes and suffixes in the source directory are how chezmoi stores everything that isn't content: the leading dot, permissions, whether a file is a template, whether it's encrypted, whether it's a script.

AttributeEffect
dot_Becomes a leading dot: dot_bashrc is ~/.bashrc
private_Removes group and world permissions (0600 files, 0700 folders)
executable_Sets the execute bit
readonly_Removes write permissions
empty_Keeps the file even when it's empty
create_Writes the file only if it doesn't exist yet
modify_A script that receives the current file and prints the new one
encrypted_Stored encrypted in the repo
symlink_Creates a symlink; the file content is the link target
exact_On a folder: deletes anything in it that isn't in the source
remove_Deletes the target
external_On a folder: ignores attributes on the files inside
literal_Stops chezmoi from reading the rest of the name as attributes
run_A script, run on every apply
once_With run_: runs once per content
onchange_With run_: runs again when the content changes
before_With run_: runs before the files are written
after_With run_: runs after the files are written
.tmplThe file is a template
.literalStops chezmoi from reading the end of the name as a suffix
Source
| Attribute | Effect |
|-----------+--------|
| dot_ | Becomes a leading dot: dot_bashrc is ~/.bashrc |
| private_ | Removes group and world permissions (0600 files, 0700 folders) |
| executable_ | Sets the execute bit |
| readonly_ | Removes write permissions |
| empty_ | Keeps the file even when it's empty |
| create_ | Writes the file only if it doesn't exist yet |
| modify_ | A script that receives the current file and prints the new one |
| encrypted_ | Stored encrypted in the repo |
| symlink_ | Creates a symlink; the file content is the link target |
| exact_ | On a folder: deletes anything in it that isn't in the source |
| remove_ | Deletes the target |
| external_ | On a folder: ignores attributes on the files inside |
| literal_ | Stops chezmoi from reading the rest of the name as attributes |
| run_ | A script, run on every apply |
| once_ | With run_: runs once per content |
| onchange_ | With run_: runs again when the content changes |
| before_ | With run_: runs before the files are written |
| after_ | With run_: runs after the files are written |
| .tmpl | The file is a template |
| .literal | Stops chezmoi from reading the end of the name as a suffix |

The order matters. For a regular file it's encrypted_, private_, readonly_, empty_, executable_, then dot_. For a folder it's remove_, external_, exact_, private_, readonly_, dot_. For a script, run_, then once_ or onchange_, then before_ or after_. A few real examples:

  • dot_bashrc: ~/.bashrc
  • private_dot_ssh/config: ~/.ssh/config inside a 0700 folder
  • dot_local/bin/executable_backup.sh: ~/.local/bin/backup.sh, executable
  • dot_gitconfig.tmpl: ~/.gitconfig, rendered from a template
  • encrypted_private_dot_netrc.age: ~/.netrc, encrypted in the repo, 0600 at home
  • run_once_before_10-install-tools.sh: a script that runs once, before the files

Renaming by hand works, but chezmoi chattr is safer because it gets the order right:

chezmoi chattr +executable ~/.local/bin/backup.sh
chezmoi chattr +private ~/.ssh/config
chezmoi chattr +template ~/.gitconfig
chezmoi chattr -template ~/.gitconfig

Two attributes deserve a closer look. create_ is for files that an app keeps editing on its own: chezmoi writes the first version on a new machine and then leaves it alone. exact_ is the opposite, it makes a folder look exactly like the source and deletes the rest. It's great for a folder of shell functions and dangerous on a folder where apps also write.

One more rule that catches everybody: files in the source directory that start with a dot are ignored, except the .chezmoi* ones. So .git and .gitattributes stay out of the home folder on their own. A README.md or a LICENSE doesn't start with a dot, though, and will land in the home folder unless it's listed in .chezmoiignore.

Templates #

Templates are what let one repo serve several machines. Any file ending in .tmpl goes through Go's template engine before being written. To turn an existing file into a template:

chezmoi add --template ~/.gitconfig

chezmoi data prints every variable available. The ones that come up most:

  • .chezmoi.os: linux, windows or darwin
  • .chezmoi.arch: amd64, arm64 and so on
  • .chezmoi.hostname: the machine name
  • .chezmoi.username and .chezmoi.homeDir
  • .chezmoi.osRelease.id: on Linux, the distro from /etc/os-release (arch, debian, ubuntu, fedora...)
  • .chezmoi.sourceDir: where the source directory is

A .gitconfig that changes on Windows looks like this:

[user]
name = Your Name
email = {{ .email }}
{{- if eq .chezmoi.os "windows" }}
[core]
sshCommand = C:/Windows/System32/OpenSSH/ssh.exe
{{- end }}

The dashes inside the braces trim whitespace. {{- eats the line break before the tag and -}} eats the one after it. Without them the rendered file fills up with blank lines where the tags were. When a template looks strange, whitespace is usually the reason.

Branching by machine name works the same way:

{{ if eq .chezmoi.hostname "workstation" -}}
export MONITOR_LAYOUT=dual
{{ end -}}

eq also accepts several values, so {{ if eq .chezmoi.osRelease.id "debian" "ubuntu" }} is true on either.

Data that isn't per machine, like a list of packages or a font name, can go in .chezmoidata.toml at the root of the repo (or in several files inside a .chezmoidata/ folder). It's merged with the [data] from the config:

[fonts]
mono = "JetBrains Mono"

[packages.arch]
pacman = ["git", "ripgrep", "fzf", "neovim"]

[packages.debian]
apt = ["git", "ripgrep", "fzf", "neovim"]

[packages.windows]
winget = ["Git.Git", "BurntSushi.ripgrep.MSVC", "junegunn.fzf", "Neovim.Neovim"]

Any template can now use {{ .fonts.mono }}. The package lists come back in the scripts section.

A few template functions solve problems that show up quickly:

  • lookPath "nvim" returns the path of a program, or nothing if it isn't installed: {{ if lookPath "nvim" }}export EDITOR=nvim{{ else }}export EDITOR=vi{{ end }}
  • env "VAR" reads an environment variable
  • include "file" inserts another file from the source directory
  • joinPath builds paths with the right separator
  • output "cmd" "arg" runs a command and inserts what it prints

Testing templates without applying anything:

chezmoi execute-template '{{ .chezmoi.os }}/{{ .chezmoi.arch }}'
chezmoi execute-template < "$(chezmoi source-path ~/.gitconfig)"
chezmoi cat ~/.gitconfig

The first prints a quick expression, the second renders a template file from standard input, and cat shows the final result exactly as apply would write it.

.chezmoiignore #

.chezmoiignore sits at the root of the repo and lists what chezmoi must not touch on this machine. The patterns refer to paths in the home folder (not to encoded source names), and the file is a template, which is what makes it so useful between Linux and Windows:

README.md
LICENSE
meta/

{{ if ne .chezmoi.os "windows" }}
AppData
Documents/PowerShell
{{ end }}

{{ if eq .chezmoi.os "windows" }}
.bashrc
.config/hypr
.config/waybar
{{ end }}

{{ if not .work }}
.config/work-vpn
{{ end }}

An ignored file isn't deleted from the home folder. chezmoi just pretends it doesn't exist. To remove files on purpose, use the remove_ attribute.

Windows, specifically #

Most of chezmoi feels identical on Windows. These are the places where it doesn't.

Where things live. The source directory and config use the same paths under the user profile. Many Windows apps keep their settings under AppData, and chezmoi manages those like any other folder. Windows Terminal, for example, lives at AppData/Local/Packages/Microsoft.WindowsTerminal_8wekyb3d8bbwe/LocalState/settings.json. Since the Terminal rewrites that file itself, it's a good candidate for re-add or for create_.

The PowerShell profile. $PROFILE shows the real path. PowerShell 7 uses Documents\PowerShell\Microsoft.PowerShell_profile.ps1 and Windows PowerShell 5.1 uses Documents\WindowsPowerShell\. If OneDrive is backing up the Documents folder, the path becomes OneDrive\Documents\... instead, and that's the path that has to go into chezmoi.

Permissions. Windows has no Unix execute bit and no 0600. private_ and executable_ are about the Linux side of the repo, and keeping them in the names is what makes the same repo work on both.

Scripts. On Windows chezmoi picks the interpreter by the file extension. A .ps1 script runs with powershell -NoLogo by default, which is Windows PowerShell 5.1. To use PowerShell 7:

[interpreters.ps1]
command = "pwsh"
args = ["-NoLogo"]

If PowerShell scripts refuse to run, check Get-ExecutionPolicy. On a development machine the usual setting is:

Set-ExecutionPolicy -Scope CurrentUser RemoteSigned

Some tasks need administrator rights. chezmoi's documentation has a snippet that makes a script relaunch itself elevated, placed at the top of the .ps1:

# Self-elevate the script if required
if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {
$CommandLine = "-NoExit -File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments
Start-Process -Wait -FilePath pwsh.exe -Verb Runas -ArgumentList $CommandLine
Exit
}
}

Symlinks. symlink_ works on Windows, but creating symlinks there needs Developer Mode turned on or an elevated shell. Plain files avoid the problem, and chezmoi works perfectly well with plain files.

Line endings. When the same repo goes between Linux and Windows, Git can rewrite line endings on checkout, and a shell script with CRLF breaks on the shebang line. A .gitattributes at the root of the source directory settles it, and chezmoi ignores the file because it starts with a dot:

* text=auto eol=lf
*.bat text eol=crlf
*.cmd text eol=crlf

WSL. WSL is a separate Linux with its own home folder, so it gets its own chezmoi install and its own chezmoi init. Templates can tell it apart from a regular Linux:

{{ if eq .chezmoi.os "linux" }}
{{ if (.chezmoi.kernel.osrelease | lower | contains "microsoft") }}
# WSL-specific settings
{{ end }}
{{ end }}

The shell of chezmoi cd. On Windows, chezmoi cd can open PowerShell 7 instead of the default shell:

[cd]
command = "pwsh"

Scripts #

Scripts are what turn a dotfiles repo into a machine setup: install packages, set the default shell, load desktop settings. Any file starting with run_ is a script. chezmoi runs scripts in alphabetical order, so a number after the attributes keeps them in line (run_once_before_10-..., run_once_before_20-...).

How often a script runs depends on its name:

  • run_: on every chezmoi apply
  • run_once_: once for each distinct content. chezmoi stores a hash of the script, so a new version of the script runs once again
  • run_onchange_: whenever the content changes

And when it runs:

  • before_: before chezmoi writes the files
  • after_: after the files are in place
  • neither: in the middle, in alphabetical order with the files

A few rules:

  • Scripts need a shebang (on Linux) but don't need the execute bit.
  • A script ending in .tmpl is rendered first. If it renders to nothing but whitespace, chezmoi skips it. This is the cleanest way to have Linux-only and Windows-only scripts in the same repo.
  • Scripts can live in a .chezmoiscripts/ folder at the root of the repo. They run normally and don't create a folder in the home directory.
  • chezmoi sets CHEZMOI=1, CHEZMOI_OS and CHEZMOI_ARCH in the environment of every script. The [scriptEnv] section of the config adds more variables.

Installing packages on both systems #

With the package lists from .chezmoidata.toml, one script per system covers it. On Linux, .chezmoiscripts/run_onchange_before_10-install-packages.sh.tmpl:

{{ if eq .chezmoi.os "linux" -}}
#!/bin/sh
set -e
{{ if eq .chezmoi.osRelease.id "arch" -}}
sudo pacman -S --needed --noconfirm {{ .packages.arch.pacman | join " " }}
{{ else if eq .chezmoi.osRelease.id "debian" "ubuntu" -}}
sudo apt-get update
sudo apt-get install -y {{ .packages.debian.apt | join " " }}
{{ end -}}
{{ end -}}

On Windows, .chezmoiscripts/run_onchange_before_10-install-packages.ps1.tmpl:

{{ if eq .chezmoi.os "windows" -}}
{{ range .packages.windows.winget -}}
winget install --id {{ . }} --exact --silent --accept-package-agreements --accept-source-agreements
{{ end -}}
{{ end -}}

On Linux the PowerShell script renders empty and gets skipped, and on Windows the shell script does. Because the package list is rendered into the script, adding a package to .chezmoidata.toml changes the script's content, and onchange runs it again on the next apply.

Running a script when another file changes #

Sometimes the script should run when a data file changes, not when the script itself does. The trick is to put the file's hash in a comment, so a new version of the file changes the script's content. On GNOME, loading dconf settings:

#!/bin/bash
# dconf.ini hash: {{ include "dconf.ini" | sha256sum }}
dconf load / < {{ joinPath .chezmoi.sourceDir "dconf.ini" | quote }}

Saved as run_onchange_after_dconf-load.sh.tmpl, with dconf.ini listed in .chezmoiignore so it doesn't land in the home folder.

The same idea works with a winget export on Windows. winget export -o packages.json saves the installed apps, the file goes in meta/ in the repo, and this script imports it whenever it changes:

{{ if eq .chezmoi.os "windows" -}}
# packages.json hash: {{ include "meta/packages-winget.json" | sha256sum }}
winget import --import-file "{{ joinPath .chezmoi.sourceDir "meta/packages-winget.json" }}" --accept-package-agreements --accept-source-agreements
{{ end -}}

Seeing and resetting scripts #

chezmoi status lists scripts that would run with an R. To apply only the files and skip every script, chezmoi apply --exclude=scripts.

chezmoi remembers which once and onchange scripts already ran. To make them run again on a machine, clear that state:

chezmoi state delete-bucket --bucket=entryState
chezmoi state delete-bucket --bucket=scriptState

The first one resets onchange scripts, the second resets once scripts.

Externals: files that live elsewhere #

Plugins, themes and frameworks shouldn't be copied into the repo. .chezmoiexternal.toml at the root tells chezmoi to download them and keep them updated:

[".oh-my-zsh"]
type = "archive"
url = "https://github.com/ohmyzsh/ohmyzsh/archive/master.tar.gz"
exact = true
stripComponents = 1
refreshPeriod = "168h"

[".vim/autoload/plug.vim"]
type = "file"
url = "https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim"
refreshPeriod = "168h"

[".vim/pack/alker0/chezmoi.vim"]
type = "git-repo"
url = "https://github.com/alker0/chezmoi.vim.git"
refreshPeriod = "168h"

The types are file, archive (a tar or zip unpacked into a folder), archive-file (one file pulled out of an archive) and git-repo. stripComponents = 1 drops the top folder that GitHub puts inside every archive. refreshPeriod says how often chezmoi downloads again: 168h is once a week. Without it, the download happens once. The file is read as a template, so the same OS checks from .chezmoiignore work here too.

To force every external to download now:

chezmoi --refresh-externals apply

Secrets #

There are two good ways to keep secrets out of a public (or even private) repo, and they mix well.

Pulling secrets from a password manager #

Templates can read from password managers at apply time. The repo only has a reference, and the secret is fetched when the file is written:

# 1Password
token = {{ onepasswordRead "op://Personal/GitHub/token" }}

# Bitwarden
password = {{ (bitwarden "item" "GitHub").login.password }}

# pass
token = {{ pass "github/token" }}

The password manager's own CLI has to be installed and unlocked, since chezmoi calls it every time it renders the template. That includes chezmoi diff and chezmoi cat.

Encrypting files with age #

For whole files (a .netrc, a VPN config, an API credentials file), chezmoi can encrypt them with age before they go to the repo. First a key:

chezmoi age-keygen --output="$HOME/.config/chezmoi/key.txt"

It prints the public key, which starts with age1. Then the config:

encryption = "age"
[age]
identity = "~/.config/chezmoi/key.txt"
recipient = "age1..."

From then on, files go in with --encrypt:

chezmoi add --encrypt ~/.netrc

The source gets encrypted_private_dot_netrc.age. chezmoi edit decrypts it to a temporary file, opens the editor and encrypts it again on save, and apply writes the decrypted version to the home folder.

The key file never goes into the repo. Every machine needs a copy of it, and a password manager is a good place to keep one. For several machines with their own keys, identities and recipients take lists:

encryption = "age"
[age]
identities = ["~/.config/chezmoi/key.txt"]
recipients = ["age1...", "age1..."]

Two other options exist. symmetric = true uses the identity itself as the key, which allows reusing an existing SSH key. passphrase = true drops the key file and asks for a passphrase every time a file is encrypted or decrypted. It's simpler to carry around and tiring when there are many encrypted files.

Recent chezmoi releases also include a built-in age implementation, so the separate age program isn't always needed. chezmoi doctor shows what it found.

For SSH keys, commit signing and the 1Password side, the SSH post goes into detail.

A new machine in one command #

With the repo in place, a fresh Linux machine needs one line. It installs chezmoi, clones the repo, asks the config template's questions and applies everything:

sh -c "$(curl -fsLS https://get.chezmoi.io)" -- init --apply your-username

For a private repo, use the SSH address. The machine needs an SSH key that the Git server accepts:

sh -c "$(curl -fsLS https://get.chezmoi.io)" -- init --apply git@github.com:your-username/dotfiles.git

On Windows the cleanest path is winget, then a new terminal, then the same init:

winget install twpayne.chezmoi
chezmoi init --apply your-username

The PowerShell installer can do both steps in one line, with the arguments inside the quotes:

iex "&{$(irm 'https://get.chezmoi.io/ps1')} -- init --apply your-username"

The order of a first apply is always the same: the config template is rendered (with its questions), the before_ scripts run, the files are written, and the after_ scripts run. That order is why package installs belong in before_ and anything that depends on the config files belongs in after_.

For containers and throwaway VMs, --one-shot applies the dotfiles and then removes chezmoi's own source directory and config, leaving only the files:

sh -c "$(curl -fsLS https://get.chezmoi.io)" -- init --one-shot your-username

When something goes wrong #

  • Start with chezmoi doctor. Half the problems are a missing editor, a missing age key or a password manager that isn't signed in.
  • Before an apply that feels risky, chezmoi apply --dry-run --verbose shows everything without doing anything.
  • A template that won't render: test the expression with chezmoi execute-template, then look at the final file with chezmoi cat.
  • "config file template has changed": run chezmoi init to regenerate the config.
  • A file that shouldn't be managed: chezmoi forget, then add it to .chezmoiignore if it keeps coming back.
  • A script that should run again: clear the state buckets shown in the scripts section.
  • Inside scripts and automation, --no-tty stops chezmoi from trying to open the terminal for prompts.
  • To start over on one machine, chezmoi purge removes chezmoi's source directory and config there. The files in the home folder stay where they are.

Cheat sheet #

  • chezmoi init: create the source directory, or clone an existing repo
  • chezmoi init --apply your-username: clone and apply in one step
  • chezmoi add FILE: start managing a file
  • chezmoi add --template FILE: add it as a template
  • chezmoi add --encrypt FILE: add it encrypted
  • chezmoi edit FILE: edit the source version
  • chezmoi edit --apply FILE: edit and apply on close
  • chezmoi diff: show what apply would change
  • chezmoi status: short list of pending changes
  • chezmoi apply: write the target state to the home folder
  • chezmoi apply --dry-run --verbose: rehearse an apply
  • chezmoi re-add: copy edited home files back into the source
  • chezmoi merge FILE: three-way merge for templates
  • chezmoi update: pull the repo and apply
  • chezmoi cd: open a shell in the source directory
  • chezmoi git ARGS: run Git in the source directory
  • chezmoi managed: list managed files
  • chezmoi unmanaged: list files chezmoi doesn't manage
  • chezmoi source-path FILE: show a file's source name
  • chezmoi cat FILE: print the rendered target version
  • chezmoi chattr +ATTR FILE: change attributes safely
  • chezmoi forget FILE: stop managing, keep the home copy
  • chezmoi data: show every template variable
  • chezmoi execute-template 'EXPR': test a template expression
  • chezmoi --refresh-externals apply: download externals again
  • chezmoi state delete-bucket --bucket=scriptState: rerun once scripts
  • chezmoi doctor: check the installation
  • chezmoi purge: remove chezmoi's own files from this machine

Notes

  1. The name is French for "at my place", and it's pronounced more or less "shay mwah". ↩