chezmoi-config
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseChezmoi Configuration Management
Chezmoi配置文件管理
Comprehensive guide for managing chezmoi dotfiles with templates, scripts, external resources, and advanced configurations.
使用模板、脚本、外部资源和高级配置管理chezmoi配置文件的全面指南。
⚠️ Safety Constraints
⚠️ 安全约束
This skill is READ-ONLY and validation-only. It MUST NOT modify your actual environment.
本技能仅支持只读和验证操作,绝对不能修改你的实际环境。
Forbidden Operations
禁止操作
NEVER execute these commands - they will modify your system:
- - Applies changes to your home directory
chezmoi apply - - Forcefully applies changes
chezmoi apply --force - - Updates source state and may change managed files
chezmoi update - Any operations that directly execute scripts (scripts run automatically during )
chezmoi apply
绝不要执行以下命令——它们会修改你的系统:
- - 将变更应用到你的主目录
chezmoi apply - - 强制应用变更
chezmoi apply --force - - 更新源状态并可能修改受管文件
chezmoi update - 任何直接执行脚本的操作(脚本会在期间自动运行)
chezmoi apply
Allowed Safe Operations
允许的安全操作
ONLY use these read-only validation commands:
- - Preview what would change (safe)
chezmoi diff - - Simulate changes without applying (safe)
chezmoi apply --dry-run - - Check configuration validity (safe)
chezmoi doctor - - Display template data variables (safe)
chezmoi data - - List files currently managed by chezmoi (read-only, safe)
chezmoi managed - - List files currently unmanaged by chezmoi (read-only, safe)
chezmoi unmanaged - - List files ignored by chezmoi (read-only, safe)
chezmoi ignored - - Preview expanded template output (safe)
chezmoi execute-template < file.tmpl - - View script execution state (safe)
chezmoi state dump - - Verify source state integrity (safe)
chezmoi verify - - Enter source directory (safe)
chezmoi cd - - Show source directory path (safe)
chezmoi source-path
仅可使用以下只读验证命令:
- - 预览将会发生的变更(安全)
chezmoi diff - - 模拟变更而不实际应用(安全)
chezmoi apply --dry-run - - 检查配置有效性(安全)
chezmoi doctor - - 显示模板数据变量(安全)
chezmoi data - - 列出当前由chezmoi管理的文件(只读,安全)
chezmoi managed - - 列出当前未被chezmoi管理的文件(只读,安全)
chezmoi unmanaged - - 列出被chezmoi忽略的文件(只读,安全)
chezmoi ignored - - 预览展开后的模板输出(安全)
chezmoi execute-template < file.tmpl - - 查看脚本执行状态(安全)
chezmoi state dump - - 验证源状态完整性(安全)
chezmoi verify - - 进入源目录(安全)
chezmoi cd - - 显示源目录路径(安全)
chezmoi source-path
User Responsibility
用户责任
This skill helps you create and validate chezmoi configurations. To apply changes, you must manually run in your terminal after reviewing the output.
chezmoi apply本技能帮助你创建和验证chezmoi配置。若要应用变更,你必须在查看输出后手动在终端中运行。
chezmoi applyQuick Start
快速入门
Creating a New Template
创建新模板
-
Add existing file as template:bash
chezmoi add --template ~/.gitconfig -
Or create manually:bash
chezmoi cd $EDITOR dot_gitconfig.tmpl -
Use Go template syntax:go
[user] name = {{ .name }} {{- if eq .chezmoi.os "darwin" }} [credential] helper = osxkeychain {{- end }}
-
将现有文件添加为模板:bash
chezmoi add --template ~/.gitconfig -
或手动创建:bash
chezmoi cd $EDITOR dot_gitconfig.tmpl -
使用Go模板语法:go
[user] name = {{ .name }} {{- if eq .chezmoi.os "darwin" }} [credential] helper = osxkeychain {{- end }}
Managing External Resources
管理外部资源
Create :
.chezmoiexternal.toml.tmpltoml
[".oh-my-zsh"]
type = "archive"
url = "https://github.com/ohmyzsh/ohmyzsh/archive/master.tar.gz"
exact = true
stripComponents = 1创建:
.chezmoiexternal.toml.tmpltoml
[".oh-my-zsh"]
type = "archive"
url = "https://github.com/ohmyzsh/ohmyzsh/archive/master.tar.gz"
exact = true
stripComponents = 1Writing Scripts
编写脚本
Create :
run_onchange_install-packages.sh.tmplbash
{{ if eq .chezmoi.os "darwin" -}}
#!/bin/bash
brew install git vim ripgrep
{{ end -}}创建:
run_onchange_install-packages.sh.tmplbash
{{ if eq .chezmoi.os "darwin" -}}
#!/bin/bash
brew install git vim ripgrep
{{ end -}}Reference Documentation
参考文档
This skill includes comprehensive reference documentation. Consult these guides for detailed information:
- template-syntax.md - Go template syntax, variables, conditionals, loops, and examples
- external-files.md - .chezmoiexternal configuration for files, archives, and git repositories
- ignore-files.md - .chezmoiignore/.chezmoiignore.tmpl patterns, templates, and scope
- password-managers.md - Integrating 20+ password managers (1Password, Bitwarden, Vault, etc.)
- scripts.md - run_once, run_onchange, run_before, run_after scripts
- advanced-configuration.md - .chezmoi.toml, prompting, encryption, and advanced features
本技能包含完整的参考文档。如需详细信息,请查阅以下指南:
- template-syntax.md - Go模板语法、变量、条件语句、循环及示例
- external-files.md - 用于文件、归档和Git仓库的.chezmoiexternal配置
- ignore-files.md - .chezmoiignore/.chezmoiignore.tmpl的匹配模式、模板及作用范围
- password-managers.md - 集成20+款密码管理器(1Password、Bitwarden、Vault等)
- scripts.md - run_once、run_onchange、run_before、run_after脚本
- advanced-configuration.md - .chezmoi.toml、交互式提示、加密及高级功能
Common Tasks
常见任务
1. Create Platform-Specific Configuration
1. 创建平台专属配置
Template file (dot_gitconfig.tmpl):
go
[user]
name = {{ .name }}
email = {{ .email }}
{{- if eq .chezmoi.os "darwin" }}
[credential]
helper = osxkeychain
{{- else if eq .chezmoi.os "linux" }}
[credential]
helper = cache
{{- end }}Configuration (chezmoi.toml):
toml
[data]
name = "John Doe"
email = "john@example.com"模板文件(dot_gitconfig.tmpl):
go
[user]
name = {{ .name }}
email = {{ .email }}
{{- if eq .chezmoi.os "darwin" }}
[credential]
helper = osxkeychain
{{- else if eq .chezmoi.os "linux" }}
[credential]
helper = cache
{{- end }}配置文件(chezmoi.toml):
toml
[data]
name = "John Doe"
email = "john@example.com"2. Manage Secrets with Password Managers
2. 使用密码管理器管理敏感信息
go
{{- if (bitwarden "github") }}
[github]
token = {{ (bitwarden "github").login.password }}
{{- end }}go
{{- if (bitwarden "github") }}
[github]
token = {{ (bitwarden "github").login.password }}
{{- end }}3. Install Packages Conditionally
3. 条件化安装软件包
run_onchange_install-packages.sh.tmpl:
bash
{{ if eq .chezmoi.os "darwin" -}}
#!/bin/bash
brew bundle --file=/dev/stdin <<EOF
{{ range .packages.darwin.brews -}}
brew "{{ . }}"
{{ end -}}
EOF
{{ else if eq .chezmoi.os "linux" -}}
#!/bin/bash
sudo apt install -y {{ range .packages.linux }}{{ . }} {{ end }}
{{ end -}}.chezmoidata/packages.yaml:
yaml
darwin:
brews:
- git
- vim
- ripgrep
linux:
- git
- vim
- ripgrep
- fd-findrun_onchange_install-packages.sh.tmpl:
bash
{{ if eq .chezmoi.os "darwin" -}}
#!/bin/bash
brew bundle --file=/dev/stdin <<EOF
{{ range .packages.darwin.brews -}}
brew "{{ . }}"
{{ end -}}
EOF
{{ else if eq .chezmoi.os "linux" -}}
#!/bin/bash
sudo apt install -y {{ range .packages.linux }}{{ . }} {{ end }}
{{ end -}}.chezmoidata/packages.yaml:
yaml
darwin:
brews:
- git
- vim
- ripgrep
linux:
- git
- vim
- ripgrep
- fd-find4. Include External Resources
4. 引入外部资源
.chezmoiexternal.toml.tmpl:
toml
[".vim/autoload/plug.vim"]
type = "file"
url = "https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim"
[".oh-my-zsh"]
type = "archive"
url = "https://github.com/ohmyzsh/ohmyzsh/archive/master.tar.gz"
exact = true
stripComponents = 1
refreshPeriod = "168h"
[".local/bin/age"]
type = "archive-file"
url = "https://github.com/FiloSottile/age/releases/download/v1.1.1/age-v1.1.1-{{ .chezmoi.os }}-{{ .chezmoi.arch }}.tar.gz"
path = "age/age"
executable = true.chezmoiexternal.toml.tmpl:
toml
[".vim/autoload/plug.vim"]
type = "file"
url = "https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim"
[".oh-my-zsh"]
type = "archive"
url = "https://github.com/ohmyzsh/ohmyzsh/archive/master.tar.gz"
exact = true
stripComponents = 1
refreshPeriod = "168h"
[".local/bin/age"]
type = "archive-file"
url = "https://github.com/FiloSottile/age/releases/download/v1.1.1/age-v1.1.1-{{ .chezmoi.os }}-{{ .chezmoi.arch }}.tar.gz"
path = "age/age"
executable = true5. Configure Interactive Prompts
5. 配置交互式提示
.chezmoi.toml.tmpl:
toml
{{- $editor := promptChoice "favorite editor" ["vim" "neovim" "vscode"] -}}
[data]
name = "{{ promptString "your name" }}"
email = "{{ promptString "your email" }}"
editor = {{ $editor }}
{{ if promptBool "enable work config?" -}}
[data.work]
enabled = true
work_email = "{{ promptString "work email" }}"
{{ end }}.chezmoi.toml.tmpl:
toml
{{- $editor := promptChoice "favorite editor" ["vim" "neovim" "vscode"] -}}
[data]
name = "{{ promptString "your name" }}"
email = "{{ promptString "your email" }}"
editor = {{ $editor }}
{{ if promptBool "enable work config?" -}}
[data.work]
enabled = true
work_email = "{{ promptString "work email" }}"
{{ end }}6. Ignore Files Conditionally
6. 条件化忽略文件
.chezmoiignore (template-aware):
text
README.md
*.log
{{- if ne .chezmoi.hostname "work-laptop" }}
.work
{{- end }}
{{- if eq .chezmoi.os "windows" }}
Documents/*
!Documents/*PowerShell/
{{- end }}.chezmoiignore(支持模板):
text
README.md
*.log
{{- if ne .chezmoi.hostname "work-laptop" }}
.work
{{- end }}
{{- if eq .chezmoi.os "windows" }}
Documents/*
!Documents/*PowerShell/
{{- end }}7. Interpret chezmoi managed
/ chezmoi unmanaged
Output (Config Files)
chezmoi managedchezmoi unmanaged7. 解读chezmoi managed
/ chezmoi unmanaged
输出(配置文件)
chezmoi managedchezmoi unmanagedWhen reviewing , note that the destination config file is , while the source is typically managed as a template such as (or ) in the chezmoi source state.
chezmoi unmanaged~/.config/chezmoi/chezmoi.tomldot_config/chezmoi/chezmoi.toml.tmplprivate_dot_config/chezmoi/chezmoi.toml.tmplIf appears as unmanaged:
~/.config/chezmoi- First check to confirm whether the config file is already managed in source.
chezmoi managed - If it is not managed, add it as a template and manage it in source (recommended when you maintain machine-specific values in ).
chezmoi.toml
查看时需注意:目标配置文件为,而源文件通常以模板形式在chezmoi源状态中管理,例如(或)。
chezmoi unmanaged~/.config/chezmoi/chezmoi.tomldot_config/chezmoi/chezmoi.toml.tmplprivate_dot_config/chezmoi/chezmoi.toml.tmpl如果显示为未被管理:
~/.config/chezmoi- 首先查看确认该配置文件是否已在源状态中被管理。
chezmoi managed - 若未被管理,建议将其添加为模板并在源状态中管理(当你在中维护机器专属值时尤其推荐)。
chezmoi.toml
8. Manage run_once Scripts
8. 管理run_once脚本
run_once_000-bootstrap.sh.tmpl:
bash
#!/bin/bash
{{ if eq .chezmoi.os "darwin" -}}
if ! command -v brew &> /dev/null; then
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
{{ end -}}Reset run_once state:
bash
undefinedrun_once_000-bootstrap.sh.tmpl:
bash
#!/bin/bash
{{ if eq .chezmoi.os "darwin" -}}
if ! command -v brew &> /dev/null; then
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
{{ end -}}重置run_once状态:
bash
undefinedClear all run_once script state
清除所有run_once脚本状态
chezmoi state delete-bucket --bucket=scriptState
undefinedchezmoi state delete-bucket --bucket=scriptState
undefinedBuilt-in Variables
内置变量
Commonly used chezmoi variables available in all templates:
| Variable | Description | Example |
|---|---|---|
| Operating system | |
| Architecture | |
| Machine hostname | |
| Current user | |
| Home directory | |
| Source state directory | |
| Environment variables | |
所有模板中均可使用的常用chezmoi变量:
| 变量 | 描述 | 示例 |
|---|---|---|
| 操作系统 | |
| 系统架构 | |
| 机器主机名 | |
| 当前用户 | |
| 主目录 | |
| 源状态目录 | |
| 环境变量 | |
Template Syntax Reference
模板语法参考
Conditionals
条件语句
go
{{ if eq .chezmoi.os "darwin" }}go
{{ if eq .chezmoi.os "darwin" }}macOS-specific
macOS专属配置
{{ else if eq .chezmoi.os "linux" }}
{{ else if eq .chezmoi.os "linux" }}
Linux-specific
Linux专属配置
{{ end }}
undefined{{ end }}
undefinedLoops
循环语句
go
{{ range .packages.darwin.brews -}}
brew "{{ . }}"
{{ end -}}go
{{ range .packages.darwin.brews -}}
brew "{{ . }}"
{{ end -}}String Operations
字符串操作
go
{{ .name | quote }} # Add quotes
{{ .chezmoi.homeDir }} # No trimming
{{- "content" -}} # Trim whitespacego
{{ .name | quote }} # 添加引号
{{ .chezmoi.homeDir }} # 不修剪空格
{{- "content" -}} # 修剪空格Best Practices
最佳实践
- Use templates for cross-platform configs - Leverage ,
.chezmoi.os.chezmoi.arch - Store secrets in password managers - Never hardcode sensitive data
- Make scripts idempotent - Safe to run multiple times
- Use descriptive filenames -
run_once_000-bootstrap.sh.tmpl - Test templates locally - Use
chezmoi execute-template < file.tmpl - Organize .chezmoidata - Separate concerns into multiple files
- Document dependencies - Comment complex configurations
- Use appropriate refresh periods - Balance freshness and bandwidth
- 使用模板实现跨平台配置 - 利用、
.chezmoi.os变量.chezmoi.arch - 将敏感信息存储在密码管理器中 - 绝不硬编码敏感数据
- 确保脚本具有幂等性 - 可安全多次运行
- 使用描述性文件名 - 例如
run_once_000-bootstrap.sh.tmpl - 本地测试模板 - 使用
chezmoi execute-template < file.tmpl - 组织.chezmoidata文件 - 将不同关注点拆分到多个文件中
- 记录依赖关系 - 为复杂配置添加注释
- 设置合适的刷新周期 - 在新鲜度和带宽之间取得平衡
Consulting Latest Documentation
查阅最新文档
This skill can consult the latest chezmoi documentation via Context7 to ensure accuracy:
bash
undefined本技能可通过Context7查阅最新的chezmoi文档,确保信息准确性:
bash
undefinedWhen in doubt, ask for the latest docs
如有疑问,请求取最新文档
"Check the chezmoi documentation for [topic]"
If the question involves `.tmpl` behavior (especially `.chezmoiignore{,.tmpl}`), prefer checking the official docs first."查阅chezmoi关于[主题]的文档"
若问题涉及`.tmpl`的行为(尤其是`.chezmoiignore{,.tmpl}`),建议优先查阅官方文档。Useful Commands
实用命令
bash
undefinedbash
undefined⚠️ FORBIDDEN - Apply all changes (must run manually)
⚠️ 禁止操作 - 应用所有变更(必须手动运行)
chezmoi apply
chezmoi apply
Dry run (preview changes) - SAFE
试运行(预览变更)- 安全
chezmoi apply --dry-run
chezmoi apply --dry-run
⚠️ FORBIDDEN - Update source state (may change managed files)
⚠️ 禁止操作 - 更新源状态(可能修改受管文件)
chezmoi update
chezmoi update
Edit source state - SAFE
编辑源状态 - 安全
chezmoi cd
chezmoi cd
Add file as template - SAFE
将文件添加为模板 - 安全
chezmoi add --template ~/.config/file
chezmoi add --template ~/.config/file
Verify configuration - SAFE
验证配置 - 安全
chezmoi doctor
chezmoi doctor
View all template data - SAFE
查看所有模板数据 - 安全
chezmoi data
chezmoi data
List ignored files - SAFE
列出被忽略的文件 - 安全
chezmoi ignored
chezmoi ignored
Test template (preview output) - SAFE
测试模板(预览输出)- 安全
chezmoi execute-template < ~/.local/share/chezmoi/dot_file.tmpl
chezmoi execute-template < ~/.local/share/chezmoi/dot_file.tmpl
View run_once state - SAFE
查看run_once状态 - 安全
chezmoi state dump
chezmoi state dump
Clear run_once script state (use with caution) - SAFE
清除run_once脚本状态(谨慎使用)- 安全
chezmoi state delete-bucket --bucket=scriptState
chezmoi state delete-bucket --bucket=scriptState
Clear run_onchange script state (use with caution) - SAFE
清除run_onchange脚本状态(谨慎使用)- 安全
chezmoi state delete-bucket --bucket=entryState
chezmoi state delete-bucket --bucket=entryState
See what would change - SAFE
查看将会发生的变更 - 安全
chezmoi diff
chezmoi diff
Verify files match expected state - SAFE
验证文件是否符合预期状态 - 安全
chezmoi verify
undefinedchezmoi verify
undefinedFile Structure
文件结构
Standard chezmoi source state structure:
~/.local/share/chezmoi/
├── .chezmoi.toml.tmpl # Main configuration (optional)
├── .chezmoiexternal.toml.tmpl # External resources (optional)
├── .chezmoiignore.tmpl # Ignore rules (template-aware, optional)
├── .chezmoidata/
│ ├── packages.yaml # Custom data
│ └── work.yaml
├── dot_gitconfig.tmpl # Regular templates
├── dot_zshrc.tmpl
├── run_once_000-bootstrap.sh.tmpl
├── run_onchange_install-packages.sh.tmpl
└── run_after_dot_vimrc.sh.tmpl标准chezmoi源状态结构:
~/.local/share/chezmoi/
├── .chezmoi.toml.tmpl # 主配置(可选)
├── .chezmoiexternal.toml.tmpl # 外部资源(可选)
├── .chezmoiignore.tmpl # 忽略规则(支持模板,可选)
├── .chezmoidata/
│ ├── packages.yaml # 自定义数据
│ └── work.yaml
├── dot_gitconfig.tmpl # 常规模板
├── dot_zshrc.tmpl
├── run_once_000-bootstrap.sh.tmpl
├── run_onchange_install-packages.sh.tmpl
└── run_after_dot_vimrc.sh.tmplDebugging
调试
注意:调试时也不要执行 ,如需检查变更请用 或 。
chezmoi updatechezmoi diffchezmoi apply --dry-runVerbose output:
bash
chezmoi apply --verboseCheck what would change:
bash
chezmoi diffValidate configuration:
bash
chezmoi doctorSee expanded template:
bash
chezmoi execute-template < template-file注意:调试时也不要执行,如需检查变更请使用或。
chezmoi updatechezmoi diffchezmoi apply --dry-run详细输出:
bash
chezmoi apply --verbose查看将会发生的变更:
bash
chezmoi diff验证配置:
bash
chezmoi doctor查看展开后的模板:
bash
chezmoi execute-template < template-fileWhen to Use This Skill
何时使用本技能
Use this skill whenever you're:
- Creating new chezmoi templates with Go syntax
- Modifying existing .chezmoi configuration files
- Managing .chezmoiignore/.chezmoiignore.tmpl rules
- Setting up .chezmoiexternal for external resources
- Writing run_once/run_onchange scripts
- Integrating password managers for secrets
- Managing cross-platform dotfiles
- Debugging template issues
- Organizing .chezmoidata files
For specific concerns, refer to the detailed reference documentation listed above.
在以下场景中使用本技能:
- 使用Go语法创建新的chezmoi模板
- 修改现有chezmoi配置文件
- 管理.chezmoiignore/.chezmoiignore.tmpl规则
- 配置.chezmoiexternal以管理外部资源
- 编写run_once/run_onchange脚本
- 集成密码管理器处理敏感信息
- 管理跨平台配置文件
- 调试模板问题
- 组织.chezmoidata文件
如需特定问题的帮助,请参考上述详细参考文档。",