This repository manages dotfiles across multiple machines using Chezmoi templating.
Already configured! Machine type: personal
When setting up on your work laptop:
-
Install and initialize chezmoi:
chezmoi init https://github.com/YOUR_USERNAME/YOUR_REPO.git
-
Create/edit
~/.config/chezmoi/chezmoi.toml:[git] autoAdd = true autoCommit = true autoPush = true [diff] pager = "less -R" [edit] apply = true [data] machine_type = "work" # <-- Set to "work" here
-
Apply the configuration:
chezmoi apply
Files with .tmpl suffix use Go templates with Chezmoi data:
{{ .chezmoi.homeDir }}- Current user's home directory{{ .chezmoi.username }}- Current username{{ .machine_type }}- Custom variable from config{{- if eq .machine_type "work" }}...{{- end }}- Conditional sections
dot_zsh/integrations.zsh.tmpl: Work-specific shell integrations- On work machine: Loads tunnel-goat and kubejumper
- On personal machine: Empty (header only)
From ~/.config/chezmoi/chezmoi.toml [data] section:
.machine_type- "personal" or "work"
Built-in Chezmoi variables:
.chezmoi.homeDir- Home directory path.chezmoi.username- Current username.chezmoi.hostname- Machine hostname.chezmoi.os- Operating system
{{- if eq .machine_type "work" }}
# Work-specific configuration
export WORK_VAR="value"
{{- else }}
# Personal configuration
export PERSONAL_VAR="value"
{{- end }}# Instead of hardcoded paths like /Users/nrebello
source "{{ .chezmoi.homeDir }}/.config/tool/config"Two approaches:
-
Templates with conditionals (current approach)
- Single file with
{{- if }}blocks - Pro: All logic in one place
- Con: File exists on all machines
- Single file with
-
Separate files with
.chezmoiignore- Create
file.workandfile.personal - Use
.chezmoiignoreto exclude based on machine - Pro: Cleaner separation
- Con: More files to manage
- Create
- Never commit secrets - Use
~/.config/chezmoi/chezmoi.tomlfor machine-specific data - Test templates - Run
chezmoi execute-template < file.tmplto test - Preview changes - Use
chezmoi diffbeforechezmoi apply - Use built-in variables - Prefer
.chezmoi.homeDirover hardcoded paths - Keep it simple - Only template what needs to differ between machines
# Preview changes
chezmoi diff
# Apply changes
chezmoi apply
# Edit a file (auto-applies on save)
chezmoi edit ~/.zshrc
# Add a new file
chezmoi add ~/.config/newfile
# See current data/variables
chezmoi data
# Test a template
chezmoi execute-template < dot_file.tmpl
# Check what chezmoi manages
chezmoi managedConsider templating these files based on your needs:
- Git config - Different emails for work/personal
- SSH config - Different hosts and keys
- Shell aliases - Work-specific commands
- Tool configs - Different settings per environment
dot_filename→.filenamedot_config/tool/config→.config/tool/configfilename.tmpl→ Processed as template,.tmplremovedprivate_filename→ Permissions set to 600executable_filename→ Permissions set to 755