Application Order
chezmoi is deterministic in its order of application. Understanding this order is important when writing scripts that depend on other resources or when debugging issues.Application Phases
The order of application is:- Read the source state - chezmoi reads all files in the source directory
- Read the destination state - chezmoi reads the current state of the destination directory
- Compute the target state - chezmoi determines what changes need to be made
- Run
run_before_scripts - Scripts are executed in alphabetical order - Update entries - Files, directories, externals, scripts, symlinks are updated in alphabetical order of their target name
- Run
run_after_scripts - Scripts are executed in alphabetical order
Target Name Ordering
Target names are considered after all attributes are stripped.Given
create_alpha and modify_dot_beta in the source state, .beta will be updated before alpha because .beta sorts before alpha.Ordering Examples
Directory Handling
Directories (including those created by externals) are updated before the files they contain.Example
Script Execution Order
Before Scripts
Allrun_before_ scripts are executed in alphabetical order before any files, directories, or symlinks are updated.
During Updates
Scripts withoutbefore_ or after_ attributes are executed in alphabetical order along with other entry types.
After Scripts
Allrun_after_ scripts are executed in alphabetical order after all files, directories, and symlinks have been updated.
External Resources
External sources (defined in.chezmoiexternal.$FORMAT or .chezmoiexternals/ directories) are updated during the update phase.
Example
.chezmoiexternal.toml
Important Assumptions
chezmoi assumes that the source or destination states are not modified while chezmoi is being executed. This assumption permits significant performance improvements, including allowing chezmoi to only read files from the source and destination states if they are needed to compute the target state.Violation Examples
Practical Examples
Database Migration Pattern
Service Restart Pattern
Package Installation Pattern
Numbered Script Pattern
Use numeric prefixes to control exact execution order:Performance Implications
The deterministic ordering allows chezmoi to:- Lazy read files: Only read files when needed to compute target state
- Optimize I/O: Batch operations where possible
- Cache computations: Reuse computed values across phases
- Parallelize safely: Know which operations can run concurrently
Debugging Order Issues
Use these commands to understand execution order:Related Pages
- Target Types - Different types of managed resources
- Source State Attributes - File naming conventions
- Templates - Template syntax for dynamic content
chezmoi apply- Apply changes to destination