--- name: chezmoi description: Chezmoi dotfile management expertise --- # Chezmoi Expertise Comprehensive guidance on managing dotfiles with chezmoi, including templating, cross-platform configuration, and secret management. ## File Naming Conventions ### Prefix System - `dot_` → `.` (hidden files) - `private_` → Sets 0600 permissions - `readonly_` → Sets 0444 permissions - `executable_` → Sets executable bit - `run_` → Scripts that run (once, onchange, always) - `modify_` → Modifies existing files - `create_` → Creates files if they don't exist - `symlink_` → Creates symbolic links ### Suffix System - `.tmpl` → Go template files - `.literal` → Treat as literal (no templating) ### Examples ``` dot_zshrc.tmpl → ~/.zshrc (templated) private_dot_ssh/config.tmpl → ~/.ssh/config (0600, templated) run_onchange_install-packages.sh.tmpl → Runs when content changes ``` ## Templating System ### Template Variables ```go {{ .chezmoi.hostname }} // Machine hostname {{ .chezmoi.os }} // Operating system (darwin, linux, windows) {{ .chezmoi.arch }} // Architecture (amd64, arm64) {{ .chezmoi.username }} // Current username {{ .chezmoi.homeDir }} // Home directory path {{ .chezmoi.sourceDir }} // Chezmoi source directory ``` ### Conditional Logic ```go {{ if eq .chezmoi.os "darwin" }} # macOS-specific configuration {{ else if eq .chezmoi.os "linux" }} # Linux-specific configuration {{ end }} {{ if .is_work_machine }} # Work-specific settings {{ end }} ``` ### Advanced Templates ```go {{- $email := promptStringOnce . "email" "Email address" -}} export EMAIL="{{ $email }}" {{- if hasKey . "gpg_key" }} export GPG_KEY="{{ .gpg_key }}" {{- end }} ``` ## Data Management ### Configuration File (.chezmoi.toml.tmpl) ```toml [data] email = "user@example.com" is_work_machine = false [data.github] username = "myusername" [diff] exclude = ["scripts"] [merge] command = "nvim" args = ["-d", "{{ .Destination }}", "{{ .Source }}"] ``` ### External Data Sources ```toml [data] weather = """{{ output "curl" "-s" "wttr.in/?format=%c+%t" }}""" hostname = """{{ output "hostname" "-s" }}""" ``` ## Secret Management ### Password Manager Integration ```go # Bitwarden export GITHUB_TOKEN="{{ (bitwarden "item" "github-token").login.password }}" # 1Password export API_KEY="{{ onepasswordRead "op://Personal/API Key/password" }}" # Encrypted files {{ includeTemplate "private_dot_ssh/id_rsa.tmpl" . | decrypt }} ``` ### Encryption ```bash # Encrypt a file chezmoi add --encrypt ~/.ssh/id_rsa # Decrypt for editing chezmoi edit ~/.ssh/id_rsa # Set encryption method in .chezmoi.toml encryption = "age" # or "gpg" ``` ## Scripts and Hooks ### Script Types - `run_once_` → Runs once only - `run_onchange_` → Runs when script content changes - `run_always_` → Runs every time ### Script Ordering - `run_before_` → Before applying dotfiles - `run_after_` → After applying dotfiles - Numeric prefixes for order: `run_onchange_before_10-install.sh` ### Example Script ```bash # run_onchange_before_install-packages.sh.tmpl #!/bin/bash {{ if eq .chezmoi.os "darwin" -}} brew bundle --file=- <