Target Types
chezmoi will create, update, and delete files, directories, and symbolic links in the destination directory, and run scripts. chezmoi deterministically performs actions in ASCII order of their target name.Given a file
dot_a, a script run_z, and a directory exact_dot_c, chezmoi will first create .a, create .c, and then execute run_z.Files
Files are represented by regular files in the source state. The file’s attributes control how it is processed and applied to the destination.Attributes
encrypted_: The file in the source state is encryptedexecutable_: Sets the executable bits in the target stateprivate_: Clears all group and world permissionsreadonly_: Clears all write permission bits in the target state.tmpl: The file is interpreted as a templateempty_: Ensures the file is not removed even if empty
empty_ prefix.
Example
Create Files
Files with thecreate_ prefix will be created in the target state with the contents of the file in the source state if they do not already exist.
If the file in the destination state already exists, then its contents will be left unchanged. This is useful for files that the user is expected to modify.
Example
Modify Files
Files with themodify_ prefix are treated as scripts that modify an existing file.
Template Modification
If the file contains the stringchezmoi:modify-template, then all lines containing that string will be removed, and the rest of the file will be interpreted as a template. The template is executed with the existing file’s contents passed as a string in .chezmoi.stdin. The result of the template execution becomes the new contents of the file.
modify_dot_bashrc.tmpl
Script Modification
Otherwise, the script receives the current contents of the target file on standard input and must write the new contents to standard output. If the target file does not exist, the script’s standard input will be empty, and the script is responsible for generating the complete file contents.modify_dot_profile
Remove Entries
Files with theremove_ prefix will cause the corresponding entry (file, directory, or symlink) to be removed in the target state.
Example
Directories
Directories are represented by regular directories in the source state.Attributes
exact_: Causes chezmoi to remove any entries in the target state that are not explicitly specified in the source stateprivate_: Causes chezmoi to clear all group and world permissionsreadonly_: Clears all write permission bitsexternal_: Ignores attributes in child entries
Example
Symbolic Links
Symbolic links are represented by regular files in the source state with the prefixsymlink_.
The contents of the file will have a trailing newline stripped, and the result will be interpreted as the target of the symbolic link.
Symbolic links with the .tmpl suffix in the source state are interpreted as templates. If the target of the symbolic link is empty or consists only of whitespace, then the target is removed.
Example
~/.local/share/chezmoi/symlink_dot_vimrc
~/.local/share/chezmoi/symlink_dot_config_nvim.tmpl
Scripts
Scripts are represented as regular files in the source state with prefixrun_. The file’s contents (after being interpreted as a template if it has a .tmpl suffix) are executed.
Execution Timing
- No timing attribute: Executed in ASCII order with respect to files, directories, and symlinks
before_: Executed before any files, directories, or symlinks are updatedafter_: Executed after all files, directories, and symlinks have been updated
Execution Frequency
- No frequency attribute: Executed on every
chezmoi apply once_: Executed only if a script with the same contents has not been run successfully beforeonchange_: Executed whenever the script contents change
Examples
run_once_install-packages.sh
run_onchange_update-brewfile.sh.tmpl
run_before_backup.sh
run_after_update-completion.sh
Working Directory
Scripts will normally run with their working directory set to their equivalent location in the destination directory. If the equivalent location in the destination directory either does not exist or is not a directory, then chezmoi will walk up the script’s directory hierarchy and run the script in the first directory that exists and is a directory.A script in
~/.local/share/chezmoi/dir/run_script will be run with a working directory of ~/dir.Environment Variables
chezmoi sets a number ofCHEZMOI* environment variables when running scripts, corresponding to commonly-used template data variables. Extra environment variables can be set in the env or scriptEnv configuration variables.
Interpreters
Scripts are executed using an interpreter, if configured. See the interpreters documentation for more details.Symlink Mode
By default, chezmoi will create regular files and directories. Settingmode = "symlink" in the configuration file will make chezmoi behave more like a dotfile manager that uses symlinks by default.
In symlink mode, chezmoi apply will make dotfiles symlinks to files in the source directory if the target is a regular file and is not encrypted, executable, private, or a template.
Example Configuration
~/.config/chezmoi/chezmoi.toml
Application Order
chezmoi applies changes in a deterministic order:- Read source state
- Read destination state
- Compute target state
- Run
run_before_scripts in alphabetical order - Update entries in alphabetical order of their target name (directories before their contents)
- Run
run_after_scripts in alphabetical order
Related Pages
- Source State Attributes - File naming conventions
- Application Order - Order of operations
- Templates - Template syntax and features