nixos-managing
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseNixOS Management
NixOS管理
Quick Decision: What do you need?
快速决策:你需要做什么?
| Task | Go to |
|---|---|
| Rebuild / activate / rollback system | vm-management.md |
| Install NixOS on new machine | installation.md |
| Build ISO / disk image / VM image | image-building.md |
| Configure modules, flakes, packages, services | configuration.md |
| Ephemeral root / wipe on boot / impermanence | impermanence.md |
| LUKS encryption / remote unlock (SSH, Tailscale) | luks.md |
| Health checks / Telegram (or webhook) alerts for filesystem, disk, SMART, services | monitoring.md |
| Something isn't working / weird behavior | anti-patterns.md |
| 任务 | 查看文档 |
|---|---|
| 重建/激活/回滚系统 | vm-management.md |
| 在新机器上安装NixOS | installation.md |
| 构建ISO/磁盘镜像/VM镜像 | image-building.md |
| 配置模块、flakes、软件包、服务 | configuration.md |
| 临时根目录/开机重置/无持久化存储 | impermanence.md |
| LUKS加密/远程解锁(SSH、Tailscale) | luks.md |
| 文件系统、磁盘、SMART、服务的健康检查/Telegram(或Webhook)告警 | monitoring.md |
| 功能异常/奇怪行为排查 | anti-patterns.md |
Execution Context — Ask First
执行上下文 — 先确认环境
Before suggesting any commands, establish where they will run.
NixOS management involves multiple machines. Commands that work on the NixOS host will fail on macOS or non-NixOS Linux. Always determine the setup before suggesting commands.
If unsure — ask: "Where are you editing configs, and where does nixos-rebuild run?"
在推荐任何命令之前,先确定命令的运行环境。
NixOS管理涉及多台机器。在NixOS主机上可用的命令,在macOS或非NixOS Linux上可能会执行失败。在推荐命令前,务必先确认用户的环境配置。
若不确定,请询问:“你在哪个机器上编辑配置文件,又在哪个机器上运行?”
nixos-rebuildAll common setups
常见环境场景
Setup A — NixOS local (everything on one machine)
NixOS machine: edit → nixos-rebuild switch (local)- All commands run locally
- ,
nixos-option,man configuration.nix— all localnixos-rebuild
Setup B — NixOS workstation → NixOS remote server
NixOS workstation (edit) → nixos-rebuild --target-host → NixOS server- runs on workstation, deploys to server
nixos-rebuild - Option verification: workstation (has nixpkgs) or server via SSH
- on server reflects what's actually active there
nixos-option
Setup C — macOS → NixOS remote (Linux rebuilds locally)
macOS (edit .nix files) → push/rsync → NixOS Linux host → nixos-rebuild switch (local)- runs on the Linux host (SSH in, or via remote trigger)
nixos-rebuild - Option verification: SSH into Linux host — macOS has no
nixos-option - always works from macOS
search.nixos.org/options
Setup D — macOS → NixOS remote (macOS drives rebuild)
macOS (edit) → nixos-rebuild --target-host root@linux-host --flake .#host- runs on macOS but activates on Linux
nixos-rebuild - Requires Nix installed on macOS (CLI, not NixOS)
nix - Option verification: works on macOS if Nix is installed; otherwise use
nix evalsearch.nixos.org
Setup E — Linux (non-NixOS) → NixOS remote
Ubuntu/Debian/Arch (edit + run nixos-rebuild) → NixOS remote server- Same as Setup D — Nix must be installed on the source machine
- not available unless NixOS; use
nixos-optionornix evalsearch.nixos.org
Setup F — CI/CD → NixOS remote
CI agent (GitHub Actions / GitLab CI) → deploy to NixOS server- CI agent needs Nix installed (use or similar)
cachix/install-nix-action - No interactive verification — all options must be pre-validated
- Use in CI to catch errors before deploy
nix flake check
场景A — 本地NixOS(所有操作在同一台机器)
NixOS机器:编辑配置 → 本地执行nixos-rebuild switch- 所有命令均在本地运行
- 、
nixos-option、man configuration.nix— 全部支持本地执行nixos-rebuild
场景B — NixOS工作站 → NixOS远程服务器
NixOS工作站(编辑配置) → 执行nixos-rebuild --target-host → NixOS服务器- 在工作站运行,部署到服务器
nixos-rebuild - 配置项验证:可在工作站(需安装nixpkgs)或通过SSH在服务器上完成
- 服务器上的反映当前生效的配置
nixos-option
场景C — macOS → NixOS远程服务器(Linux本地执行重建)
macOS(编辑.nix文件) → 推送/rsync → NixOS Linux主机 → 本地执行nixos-rebuild switch- 在Linux主机上运行(需SSH登录或远程触发)
nixos-rebuild - 配置项验证:必须SSH登录到Linux主机 — macOS没有命令
nixos-option - 可在macOS上使用查询配置项
search.nixos.org/options
场景D — macOS → NixOS远程服务器(macOS驱动重建)
macOS(编辑配置) → 执行nixos-rebuild --target-host root@linux-host --flake .#host- 在macOS运行,但在Linux主机激活配置
nixos-rebuild - 要求macOS上安装Nix(命令行工具,而非NixOS系统)
nix - 配置项验证:若macOS安装了Nix,可使用;否则使用
nix evalsearch.nixos.org
场景E — 非NixOS Linux → NixOS远程服务器
Ubuntu/Debian/Arch(编辑配置+运行nixos-rebuild) → NixOS远程服务器- 与场景D一致 — 源机器必须安装Nix
- 除非是NixOS系统,否则无法使用;可使用
nixos-option或nix evalsearch.nixos.org
场景F — CI/CD → NixOS远程服务器
CI代理(GitHub Actions / GitLab CI) → 部署到NixOS服务器- CI代理需要安装Nix(可使用或类似工具)
cachix/install-nix-action - 无交互式验证 — 所有配置项必须预先验证
- 在CI中使用在部署前捕获错误
nix flake check
Verification availability by machine type
不同机器类型的验证工具可用性
| Machine | | | | |
|---|---|---|---|---|
| NixOS host | ✅ | ✅ | ✅ | ✅ |
| macOS with Nix | ❌ | ✅ | ❌ | ✅ |
| Linux (non-NixOS) with Nix | ❌ | ✅ | ❌ | ✅ |
| macOS without Nix | ❌ | ❌ | ❌ | ✅ |
| CI agent with Nix | ❌ | ✅ | ❌ | ✅ |
| 机器类型 | | | | |
|---|---|---|---|---|
| NixOS主机 | ✅ | ✅ | ✅ | ✅ |
| 安装Nix的macOS | ❌ | ✅ | ❌ | ✅ |
| 安装Nix的非NixOS Linux | ❌ | ✅ | ❌ | ✅ |
| 未安装Nix的macOS | ❌ | ❌ | ❌ | ✅ |
| 安装Nix的CI代理 | ❌ | ✅ | ❌ | ✅ |
MCP Server: mcp-nixos
(optional accelerator)
mcp-nixosMCP服务器:mcp-nixos
(可选加速工具)
mcp-nixosIf the MCP server (utensils/mcp-nixos) is configured in the environment, prefer it for lookups over SSH/web round-trips — but ask the user first before using it for a given task: "I can use the mcp-nixos MCP server to look this up directly — want me to use it?"
mcp-nixosWhat it's good at:
- Exact NixOS package names, versions, metadata (130K+ packages)
- NixOS options (23K+) — verify option paths before suggesting config
- Home Manager options (5K+)
- nix-darwin macOS settings (1K+)
- Nixvim Neovim configuration (5K+)
- FlakeHub registry discovery (600+ flakes)
- Noogle Nix function lookup (2K+)
- NixOS Wiki / nix.dev documentation
- Binary cache status checks
- Local flake input exploration
- Package version history with nixpkgs commit hashes (reproducible pins)
Use it instead of: guessing option names, scraping , or SSHing into a host just to run . Works on macOS without Nix installed.
search.nixos.orgnixos-optionTools exposed: (unified query), (version history).
nixnix_versionsWhen NOT to ask: the user has already said they want (or don't want) to use it this session — honor that preference.
Before installing — security review required. Any MCP server runs with your shell privileges. Before adding (or any MCP) to the configuration:
mcp-nixos- Download the source (or inspect the PyPI tarball via
git clone https://github.com/utensils/mcp-nixos/uvx --from mcp-nixos --print).pip download mcp-nixos --no-deps - Review it for anything harmful: unexpected network endpoints, filesystem writes outside its own cache, shell/exec calls, credential reads (,
~/.ssh, env vars), obfuscated code, suspicious post-install hooks in~/.aws/pyproject.toml.setup.py - Pin to a known-good version/commit rather than a floating tag.
- Only after review — add it to the MCP config.
Tell the user what you reviewed and what you found before they approve the install.
若环境中配置了 MCP服务器(utensils/mcp-nixos),优先使用它进行查询,以替代SSH/网页往返操作 — 但在为特定任务使用前,请先询问用户:“我可以使用mcp-nixos MCP服务器直接查询相关内容 — 是否需要启用该工具?”
mcp-nixos适用场景:
- 精确查询NixOS软件包的名称、版本、元数据(超过13万个软件包)
- NixOS配置项(超过23000个) — 在推荐配置前验证配置项路径
- Home Manager配置项(超过5000个)
- nix-darwin macOS设置(超过1000个)
- Nixvim Neovim配置(超过5000个)
- FlakeHub注册表发现(超过600个flakes)
- Noogle Nix函数查询(超过2000个)
- NixOS Wiki / nix.dev文档
- 二进制缓存状态检查
- 本地flake输入探索
- 基于nixpkgs提交哈希的软件包版本历史(可复现的版本锁定)
替代方案: 无需猜测配置项名称、爬取,或仅为运行而SSH登录主机。即使macOS未安装Nix也可使用。
search.nixos.orgnixos-option提供的工具: (统一查询)、(版本历史)。
nixnix_versions无需询问的情况: 用户已明确表示本次会话是否要使用该工具 — 尊重用户的选择。
安装前注意 — 需进行安全审查。 任何MCP服务器都会以你的Shell权限运行。在将(或任何MCP工具)添加到配置前:
mcp-nixos- 下载源代码(,或通过
git clone https://github.com/utensils/mcp-nixos/uvx --from mcp-nixos --print检查PyPI压缩包)。pip download mcp-nixos --no-deps - 检查是否存在有害内容:意外的网络端点、超出自身缓存目录的文件系统写入、Shell/执行调用、读取凭据(、
~/.ssh、环境变量)、混淆代码、~/.aws/pyproject.toml中可疑的安装后钩子。setup.py - 锁定到已知安全的版本/提交记录,而非浮动标签。
- 审查通过后 — 再将其添加到MCP配置中。
告知用户你审查的内容和结果,再等待用户批准安装。
Option Verification — Always Verify Before Suggesting
配置项验证 — 推荐前务必验证
Never suggest a NixOS option from memory alone. Option names change between NixOS versions, and incorrect options fail silently or produce confusing errors.
Fastest path (if available): the MCP server — see section above. Ask the user before using it.
mcp-nixosRun on the NixOS host (not macOS, not CI unless it runs NixOS):
bash
undefined切勿仅凭记忆推荐NixOS配置项。 不同NixOS版本的配置项名称可能会变化,错误的配置项会静默失败或产生令人困惑的错误。
最快验证方式(若可用): 使用 MCP服务器 — 见上文。使用前需询问用户。
mcp-nixos在NixOS主机上运行(不要在macOS、CI环境中运行,除非CI环境是NixOS):
bash
undefinedSearch available options interactively
交互式搜索可用配置项
nixos-option services.openssh.settings
nixos-option services.openssh.settings
Evaluate option existence in current nixpkgs
评估当前nixpkgs中是否存在该配置项
nix eval nixpkgs#nixosOptionsDoc --apply 'x: builtins.attrNames x' 2>/dev/null | grep -o '"[^"]PasswordAuth[^"]"'
nix eval nixpkgs#nixosOptionsDoc --apply 'x: builtins.attrNames x' 2>/dev/null | grep -o '"[^"]PasswordAuth[^"]"'
Quickest: man page on the NixOS machine
最快捷方式:在NixOS机器上查看手册页
man configuration.nix | grep -A3 "PasswordAuthentication"
**Or use the web** (version-specific, always available):
- `https://search.nixos.org/options` — search by option name, filterable by channel
- Check the channel matching your `nixpkgs.url` (e.g. `nixos-25.05`)
**Workflow when unsure about an option:**
1. Check `search.nixos.org/options` for the option name and correct path
2. Note the NixOS version it applies to
3. Only then include it in configurationman configuration.nix | grep -A3 "PasswordAuthentication"
**或使用网页工具**(按版本区分,始终可用):
- `https://search.nixos.org/options` — 按配置项名称搜索,可按频道筛选
- 选择与你的`nixpkgs.url`匹配的频道(例如`nixos-25.05`)
**不确定配置项时的工作流程:**
1. 在`search.nixos.org/options`上查询配置项名称和正确路径
2. 记录该配置项适用的NixOS版本
3. 确认无误后再将其添加到配置中Core Mental Model
核心思维模型
NixOS is declarative and atomic. Every change produces a new generation. You can always roll back.
Key workflow:
- Edit files
.nix - (in flakes — untracked files are invisible to Nix)
git add - (activate without committing to bootloader)
nixos-rebuild test - (set as default boot)
nixos-rebuild switch
NixOS是声明式且原子化的系统。每一次变更都会生成一个新的系统代次。你始终可以回滚到之前的状态。
关键工作流程:
- 编辑文件
.nix - (在flakes模式下 — 未跟踪的文件对Nix不可见)
git add - (激活配置但不修改引导加载项 — 安全的第一步)
nixos-rebuild test - (设置为默认引导项)
nixos-rebuild switch
Flakes vs Channels
Flakes vs Channels
Use flakes for reproducibility. Channels are impure (machine-dependent lookup paths).
Enable flakes once:
nix
nix.settings.experimental-features = [ "nix-command" "flakes" ];优先使用flakes以确保可复现性。Channels是非纯净的(依赖机器特定的查找路径)。
只需启用一次flakes:
nix
nix.settings.experimental-features = [ "nix-command" "flakes" ];Common nixos-rebuild
Commands
nixos-rebuild常用nixos-rebuild
命令
nixos-rebuild| Command | When to use |
|---|---|
| Apply + set as default boot |
| Apply now, skip bootloader — safe first step |
| Set as next boot without activating now |
| Preview changes without applying |
| Build only, creates |
| Build QEMU VM for local testing |
Always test before switch — especially on remote servers:
bash
nixos-rebuild test --flake .#hostname --target-host root@server
nixos-rebuild switch --flake .#hostname --target-host root@server| 命令 | 使用场景 |
|---|---|
| 应用配置并设置为默认引导项 |
| 立即应用配置,不修改引导加载项 — 安全的测试步骤 |
| 设置为下次引导项,但不立即激活 |
| 预览变更但不实际应用 |
| 仅构建系统,生成 |
| 构建QEMU虚拟机用于本地测试 |
在执行switch前务必先测试 — 尤其是远程服务器:
bash
nixos-rebuild test --flake .#hostname --target-host root@server
nixos-rebuild switch --flake .#hostname --target-host root@serverRemote Deployment Tools
远程部署工具
| Tool | Best for |
|---|---|
| 1-3 machines, simplest |
| Small fleet, auto-rollback on failure |
| Large fleet, parallel, cross-host config |
| Initial install on non-NixOS machine |
| 工具 | 最佳适用场景 |
|---|---|
| 1-3台机器,最简单的方式 |
| 小型机器集群,失败时自动回滚 |
| 大型机器集群,支持并行部署、跨主机配置 |
| 在非NixOS机器上初始安装NixOS |
Secret Management
密钥管理
| Tool | When to use |
|---|---|
| agenix | Simple setup, SSH key workflow, small secret count |
| sops-nix | Cloud KMS needed, templating, multiple formats |
Secrets live at or — never in Nix store.
/run/agenix//run/secrets/| 工具 | 使用场景 |
|---|---|
| agenix | 配置简单,基于SSH密钥工作流,密钥数量较少的场景 |
| sops-nix | 需要云KMS、模板化支持、多格式密钥的场景 |
密钥存储在或目录 — 切勿存储在Nix存储库中。
/run/agenix//run/secrets/Most Frequently Changed Elements
最常修改的配置元素
- — installed packages
environment.systemPackages - — user accounts and SSH keys
users.users.<name> - — hostname, IPs, firewall ports
networking.* - — enable/configure systemd services
services.* - — bootloader and kernel settings
boot.loader.* - — substituters, trusted users, features
nix.settings.*
See configuration.md for patterns.
- — 已安装的软件包
environment.systemPackages - — 用户账户和SSH密钥
users.users.<name> - — 主机名、IP地址、防火墙端口
networking.* - — 启用/配置systemd服务
services.* - — 引导加载项和内核设置
boot.loader.* - — 替代源、可信用户、实验性功能
nix.settings.*
更多配置模式请查看configuration.md。