前言
作为开发者,你是否经历过这些痛苦?
- 换新 Mac,花一整天重新安装配置各种工具
- 公司电脑和个人电脑的配置不同步,经常忘记某个好用的 alias
.zshrc越改越乱,不敢轻易动,怕改坏了- 想把配置分享给同事,却发现根本跑不起来
我也深受其苦。直到我发现了 chezmoi + nix-darwin 这个组合——现在,我可以用一条命令在任意一台新 Mac 上部署完整的开发环境:
sh -c "$(curl -fsLS get.chezmoi.io)" -- init --apply signalridge这篇文章会介绍我是如何做到的,以及为什么这个组合如此强大。
什么是 chezmoi?
chezmoi 是一个用 Go 编写的 dotfiles 管理工具。
传统 dotfiles 管理的问题
大多数人管理 dotfiles 的方式是:
- 创建一个 git 仓库
- 把配置文件移进去
- 用 symlink 链接回原位置
这种方式有几个致命问题:
- 不支持多机器差异:公司电脑和个人电脑需要不同配置怎么办?
- 不支持模板:想根据机器名动态生成配置?做不到
- 不支持敏感信息:API key 要不要提交?不提交就不完整,提交了又不安全
- 符号链接脆弱:误删、移动都会破坏链接
chezmoi 如何解决
chezmoi 采用了完全不同的思路:
源文件 (git 仓库) → chezmoi apply → 目标文件 (home 目录)它不是用符号链接,而是真正复制文件。这带来了巨大的灵活性:
1. 模板支持
文件名加 .tmpl 后缀,就可以使用 Go 模板语法:
# dot_gitconfig.tmpl
[user]
name = {{ .name }}
email = {{ .email }}
{{- if .work }}
signingkey = {{ .work_gpg_key }}
{{- end }}运行 chezmoi apply 时,会根据当前机器的数据渲染成真正的 .gitconfig。
2. 多配置文件支持
通过 .chezmoidata.yaml 定义变量:
homebrew:
casks:
shared:
- visual-studio-code
- ghostty
work:
- slack
- zoom
private:
- discord
- steam初始化时选择 profile:
chezmoi init --data='{"work": true}' # 工作电脑
chezmoi init --data='{"private": true}' # 个人电脑3. 脚本钩子
可以定义在应用配置前后执行的脚本:
.chezmoiscripts/
├── run_once_before_01_install-nix.sh # 首次运行前安装 Nix
├── run_onchange_after_02_init.sh.tmpl # 配置变更后执行
└── run_after_05_update_homebrew.sh.tmpl # 每次 apply 后更新 Homebrew
什么是 nix-darwin?
nix-darwin 是 NixOS 的 macOS 版本,用 Nix 语言声明式地管理系统配置。
声明式 vs 命令式
传统安装软件(命令式):
brew install ripgrep bat eza fd
# 下次换电脑,你还记得装了什么吗?nix-darwin(声明式):
# apps.nix
environment.systemPackages = with pkgs; [
ripgrep
bat
eza
fd
];所有要安装的包都写在配置文件里。运行 darwin-rebuild switch,Nix 会:
- 读取配置
- 计算需要安装/卸载什么
- 原子性地应用变更
nix-darwin 的优势
1. 完全可复现
同样的配置文件,在任何 Mac 上都会得到完全相同的环境。不是"差不多",是完全相同。
2. 原子性更新
要么全部成功,要么全部失败。不会出现"装到一半断网了,现在环境坏了"的情况。
3. 回滚能力
# 列出所有历史版本
nix profile history --profile /nix/var/nix/profiles/system
# 回滚到上一版本
nix profile rollback --profile /nix/var/nix/profiles/system装了个软件把系统搞坏了?一秒回滚。
4. 系统设置也能管理
# system.nix
system.defaults = {
dock.autohide = true;
finder.ShowPathbar = true;
NSGlobalDomain.AppleShowAllExtensions = true;
};连 Dock 自动隐藏、Finder 显示路径栏这些系统设置都能声明式管理!
chezmoi + nix-darwin:完美组合
为什么要两个工具一起用?因为它们各有分工:
| 职责 | 工具 |
|---|---|
| 用户配置文件 (.zshrc, .gitconfig 等) | chezmoi |
| 系统级包管理 | nix-darwin |
| macOS 系统设置 | nix-darwin |
| GUI 应用 | Homebrew (通过 nix-darwin 管理) |
| 敏感信息 | chezmoi (支持加密) |
| 多机器差异 | chezmoi (模板) |
流程如下:
chezmoi init --apply
↓
1. 下载 dotfiles 仓库
2. 运行 run_once_before 脚本安装 Nix
3. 应用配置文件
4. 运行 run_after 脚本执行 darwin-rebuild
↓
完整环境就绪!
我的 dotfiles 特性
介绍完理论,来看看我的实际配置:
GitHub: https://github.com/signalridge/dotfiles
现代 CLI 工具链
我用 Rust 编写的现代工具替换了传统 Unix 命令:
| 传统命令 | 现代替代 | 改进 |
|---|---|---|
ls | eza | Git 集成、图标、树形视图 |
cat | bat | 语法高亮、Git 集成 |
grep | ripgrep | 快 10 倍、自动忽略 .gitignore |
find | fd | 更友好的语法 |
du | dust | 可视化磁盘占用 |
cd | zoxide | 智能跳转,学习你的习惯 |
man | tldr | 实用示例而非冗长文档 |
Shell 环境
- Starship - 用 Rust 写的极快 prompt,显示 git 状态、语言版本
- Sheldon - Rust 写的 zsh 插件管理器,比 oh-my-zsh 快很多
- Atuin - 魔法般的历史搜索,存储在 SQLite,支持模糊搜索
- direnv - 进入目录自动加载环境变量
- fzf - 模糊搜索一切
开发工具
- mise - 统一管理 Node.js、Python、Go、Rust 等运行时版本
- lazygit - 终端里的 Git GUI,复杂操作变简单
- yazi - 极快的终端文件管理器,支持图片预览
- tmux - 终端复用,配置了 vim 键位和弹出窗口
多配置支持
一套代码,两种配置:
# .chezmoidata.yaml
homebrew:
casks:
shared: # 所有机器
- visual-studio-code
- ghostty
work: # 工作机器
- slack
- cursor
private: # 个人机器
- discord
- steamAI 集成
- aicommit - 用 Claude/GPT 自动生成 commit message
- Claude Code - AI 编程助手
- Starship 显示当日 Claude API 用量
统一主题
全部工具使用 Dracula 深色主题,视觉统一:
- Starship prompt
- tmux 状态栏
- bat 语法高亮
- lazygit 界面
- yazi 文件管理器
一键命令
just full-upgrade # 更新所有:nix + homebrew + mise + sheldon
just darwin # 重建 nix-darwin 配置
just gc # 清理旧版本,释放空间
如何开始?
使用我的配置
# 1. 安装 Nix
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install
# 2. 一键部署
sh -c "$(curl -fsLS get.chezmoi.io)" -- init --apply signalridge
# 3. 构建 nix-darwin
cd ~/.local/share/chezmoi
just darwin创建自己的配置
如果想从零开始:
# 安装 chezmoi
sh -c "$(curl -fsLS get.chezmoi.io)"
# 初始化
chezmoi init
# 添加第一个文件
chezmoi add ~/.zshrc
# 编辑
chezmoi edit ~/.zshrc
# 应用
chezmoi apply
# 推送到 GitHub
chezmoi cd
git add -A && git commit -m "Initial commit"
git remote add origin git@github.com:你的用户名/dotfiles.git
git push -u origin main
总结
chezmoi + nix-darwin 这个组合解决了 dotfiles 管理的所有痛点:
| 痛点 | 解决方案 |
|---|---|
| 换电脑要重新配置 | 一条命令部署 |
| 多机器配置不同 | chezmoi 模板 + 多 profile |
| 忘记装了什么软件 | nix-darwin 声明式管理 |
| 配置改坏了 | Nix 原子更新 + 回滚 |
| 敏感信息 | chezmoi 加密 |
一次投入,终身受益。以后换电脑再也不用花一整天配环境了。
我的配置开源在 GitHub,欢迎 Star ⭐
https://github.com/signalridge/dotfiles
有问题欢迎评论或者提 Issue!
