Detecting Machine Characteristics
Determine Laptop vs. Desktop
You can automatically detect whether a machine is a laptop or desktop and adjust configurations accordingly..chezmoi.toml.tmpl
dot_bashrc.tmpl
Detect CPU Cores and Threads
Optimize configurations based on available CPU resources:.chezmoi.toml.tmpl
dot_config/make/settings.mk.tmpl
Operating System Conditionals
Basic OS Detection
Use the.chezmoi.os variable to handle different operating systems:
dot_bashrc.tmpl
Linux Distribution Detection
Handle differences between Linux distributions:.chezmoi.toml.tmpl
run_once_install_packages.sh.tmpl
Platform-Specific Guides
macOS
Use Homebrew Bundle
Manage packages declaratively with arun_onchange_ script:
run_onchange_before_install-packages-darwin.sh.tmpl
Get Stable Hostname
Thehostname command can change based on network. Use scutil instead:
Run Scripts After macOS Updates
Automate tasks after system updates:run_onchange_after_macos_update.sh.tmpl
Linux
Combine OS and Distribution Checks
Simplify nested conditionals:dot_bashrc.tmpl
Handle Package Managers
Different distributions use different package managers:run_once_install_tools.sh.tmpl
Windows
Detect Windows Subsystem for Linux (WSL)
Detect if running in WSL:dot_bashrc.tmpl
Run PowerShell Scripts as Admin
Self-elevate PowerShell scripts:run_once_install_tools.ps1.tmpl
Create Symlinks on Windows
Enable symlink creation by turning on Developer Mode or setting the appropriate permission.Containers and VMs
Detect Container Environments
dot_bashrc.tmpl
Minimal Configurations for Containers
Use.chezmoiignore to skip unnecessary files in containers:
dot_chezmoiignore
Hostname-Based Configuration
Simple Hostname Check
dot_gitconfig.tmpl
Hostname Patterns
Match multiple machines with similar names:dot_bashrc.tmpl
Interactive Configuration
Prompt for machine-specific data duringchezmoi init:
.chezmoi.toml.tmpl
dot_gitconfig.tmpl
Architecture-Specific Configuration
Handle different CPU architectures:dot_bashrc.tmpl
Best Practices
Use Data Variables
Store complex logic in your config file template and expose simple variables:.chezmoi.toml.tmpl
dot_bashrc.tmpl
Keep Templates Simple
Avoid complex logic in individual file templates. Put it in.chezmoi.toml.tmpl instead.
Test on All Machines
Before committing changes, test on all your machines:Document Your Variables
Add comments to your config template:.chezmoi.toml.tmpl
Troubleshooting
Debug Template Variables
Create a debug file to inspect available variables:Test Templates
Test template rendering without applying:Check OS Release Info
On Linux, inspect/etc/os-release: