nix-mcp
# nix-mcp v2
A discovery-first, policy-aware MCP server for Nix development, inspection, diagnostics, and deployment. Version 2 exposes a small intent-oriented surface, while `discover` and `command` cover the installed Nix CLI without giving agents an arbitrary shell.
## Install and run
Requires Node.js 20+ and Nix 2.18 or newer.
```console
npx -y nix-mcp@2
```
Or run directly with Nix:
```console
nix run github:Digimuoto/nix-mcp
```
Local development:
```console
nix develop
npm ci
npm run verify
```
### MCP client configuration
Codex (`~/.codex/config.toml`):
```toml
[mcp_servers.nix]
command = "nix"
args = ["run", "github:Digimuoto/nix-mcp"]
env = { NIX_MCP_ROOTS = "/absolute/workspace/path" }
```
Claude Desktop:
```json
{
"mcpServers": {
"nix": {
"command": "nix",
"args": ["run", "github:Digimuoto/nix-mcp"],
"env": { "NIX_MCP_ROOTS": "/absolute/workspace/path" }
}
}
}
```
OpenCode:
```json
{
"mcp": {
"nix": {
"type": "local",
"command": ["nix", "run", "github:Digimuoto/nix-mcp"],
"environment": { "NIX_MCP_ROOTS": "/absolute/workspace/path" }
}
}
}
```
MCP client roots are added to the configured workspace boundary after initialization. `NIX_MCP_ROOTS` accepts colon-separated roots and is useful for clients that do not provide roots.
## Default tool catalog
The safe profile advertises 14 tools:
| Tool | Use it for |
|---|---|
| `workspace_inspect` | Detect the flake, outputs, Nix version, systems, and deployment adapters. |
| `discover` | Rank first-class tools and installed advanced Nix operations for a natural-language intent. |
| `build` | Focused builds, dry runs, output paths, overrides, builders, logs, and traces. |
| `check` | Repository-wide flake checks, including no-build and all-systems validation. |
| `run` | Run a flake app with a literal argument array. |
| `develop` | Run a literal command array in a selected dev shell. |
| `eval` | Evaluate an installable, expression, or file as typed raw/JSON output. |
| `syntax_check` | Parse Nix files with `nix-instantiate --parse`. |
| `search` | Search nixpkgs or another flake. |
| `flake_inspect` | Show outputs, lock metadata, or flake info. |
| `derivation_inspect` | Inspect builders, environments, inputs, outputs, and recursive derivation graphs. |
| `closure_inspect` | Inspect sizes/references, explain dependencies, and compare closures. |
| `run_inspect` | Retrieve diagnostics or bounded stdout/stderr from a durable `runId`. |
| `command` | Execute an operation returned by `discover` through validated positional fields and options. |
Use the flow `workspace_inspect` → intent tool → `run_inspect`. Use `discover` only when the first-class catalog does not fit. Every process result includes a stable envelope with its run ID, status, redacted command, exit code, duration, outputs, diagnostics, stream metadata, summary, and valid next actions.
The server also exposes:
- Resources: `nix://catalog`, `nix://manual/{tool}`, `nix://workflow/{name}`, `nix://workspace/{id}`, `nix://run/{runId}`, and `nix://run/{runId}/{stream}`.
- Prompts: `inspect-and-build`, `diagnose-failure`, `inspect-derivation`, `update-flake-input`, and `plan-deployment`.
- Optional MCP tasks for build, check, and deployment. Clients without task support get the same synchronous result and `runId`.
## Policy
`NIX_MCP_PROFILE` controls advertised mutation tools and execution authority:
| Profile | Added tools |
|---|---|
| `safe` (default) | Inspection, evaluation, builds, checks, apps, dev commands, diagnostics, and policy-classified read-only advanced commands. |
| `developer` | `format` and `flake_lock`. |
| `operator` | `format`, `flake_lock`, `deploy_plan`, and `deploy_apply`. |
| `v1-compat` | Deprecated v1 aliases for migration; removed in v3. |
Additional opt-ins remain independent:
| Variable | Authority |
|---|---|
| `NIX_MCP_ALLOW_PROFILE=1` | Advertise and permit `profile_manage`. |
| `NIX_MCP_ALLOW_STORE_MAINTENANCE=1` | Advertise store maintenance and permit destructive store operations. |
| `NIX_MCP_ALLOW_SIGNING=1` | Permit store signing without enabling deletion, GC, repair, or optimisation. |
| `NIX_MCP_DEPLOY_TARGETS=host1,root@host2` | Allow only these deployment targets. |
| `NIX_MCP_ALLOW_PROVISIONING=1` | Permit nixos-anywhere application under operator policy. |
`command` uses the same operation effect map and cannot bypass policy. Unsupported interactive commands such as `nix repl`, editors, daemons, and TTY-only operations are discoverable as unsupported rather than executed.
## Deployment
Deployments are always two-phase:
1. `deploy_plan` resolves deploy-rs, nixos-rebuild, or nixos-anywhere from an explicit configured path, `PATH`, or the project dev shell and performs a dry operation.
2. The plan records the adapter, target, exact command, lock hash, complete tracked/untracked source fingerprint, expiry, and warnings.
3. `deploy_apply` accepts only `planId` and rejects failed/expired/stale plans, denied targets, or a concurrent deployment to the same workspace and target.
No deployment tool is fetched implicitly. Set `NIX_MCP_DEPLOY_PATH`, `NIX_MCP_NIXOS_REBUILD_PATH`, or `NIX_MCP_NIXOS_ANYWHERE_PATH` to pin an executable explicitly.
## Version awareness and safety
At startup, nix-mcp detects `nix --version` and consumes `nix __dump-cli` only when its shape passes validation. A checked-in curated snapshot covers Nix 2.18–2.34. Unknown versions keep first-class tools available and surface catalog uncertainty explicitly.
Processes are spawned from fixed executable names with argument arrays and `shell: false`. Working directories must remain inside MCP roots. Output is retained in a private temporary run directory for up to 50 runs, 256 MiB, or 24 hours, with oldest-first eviction. Cancellation sends SIGTERM to the process group and escalates to SIGKILL after a bounded grace period.
## Verification
```console
npm run verify
nix flake check
```
The test suite covers schemas, exact argument handling, root policy, redaction, cancellation/timeouts, retention, MCP initialization/resources/prompts/tasks, real Nix evaluation, deployment plan/apply/staleness/target/concurrency/provisioning policy, and discovery routing. The checked-in discovery corpus currently gates top-5 recall ≥ 95%, MRR ≥ 0.85, and distractor rejection ≥ 95%.
See [MIGRATION.md](MIGRATION.md) and [CHANGELOG.md](CHANGELOG.md).
## License
MIT
TDQS
Scored across 14 tools
Descriptions actively draw boundaries between the easy-to-confuse pairs (run vs develop vs build, check vs build, eval vs flake_inspect), which is unusually careful. The only real overlap is the inspect family, where workspace_inspect and flake_inspect both surface flake/output information, and run_inspect's name collides conceptually with 'run'.
Two clear conventions coexist: bare verbs (build, check, run, eval, search, develop, discover) and noun+inspect (closure_inspect, workspace_inspect, flake_inspect, derivation_inspect, run_inspect), plus syntax_check which follows neither exactly. Each family is internally consistent and readable, but the set as a whole mixes patterns, and 'command' is a bare noun.
14 tools sits squarely in the well-scoped range and every tool maps to a recognizable Nix workflow (inspect, build, eval, run, develop, validate, search, diagnose). Nothing looks redundant or padded for the scope of a Nix repository assistant.
Coverage is broad: evaluation, building, checks, dev shells, package search, flake/derivation/closure inspection, syntax parsing, and post-run diagnostics. Gaps exist around flake lock/update and store management (gc, copy, delete), but the discover + command escape hatch provides a validated path to those operations.