feat: add Nix package for zhtw-mcp#88
Closed
pan93412 wants to merge 3 commits into
Closed
Conversation
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds a Nix flake to build and run zhtw-mcp reproducibly, enabling nix run usage and a Nix-based dev shell, plus documentation for running/registering the MCP server via Nix.
Changes:
- Introduce
flake.nixwithzhtw-mcppackage, pinned OpenCC dictionaries, and Rust toolchain via fenix. - Add Nix usage instructions to README for running and registering MCP servers without global installs.
- Add
.envrcto enable direnv/Nix flake development environment.
Reviewed changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| flake.nix | Defines Nix flake outputs: overlay, package build, devShell, formatter; pins OpenCC inputs for reproducibility. |
| README.md | Documents nix run / nix profile install workflows and MCP client registration examples. |
| .envrc | Enables use flake for direnv-based local development. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- flake.nix: use final.stdenv instead of prev.stdenv for fenix toolchain selection (fixes cross-compilation and system consistency) - flake.nix: use self instead of inputs.self (redundant indirection) - flake.nix: read version from Cargo.toml via builtins.fromTOML to avoid manual sync - flake.nix: add rustToolchain to nativeBuildInputs and use rustfmt from PATH in preBuild - flake.nix: rewrite meta with `with final.lib`, add platforms = platforms.unix, drop empty maintainers list - README.md: remove hallucinated opencode mcp add example - .gitignore: add /result (nix build output symlink) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
增加 Nix 套件,在有安裝 Nix 的情況下,可以使用
nix run sysprog21/zhtw-mcp來執行zhtw-mcp(而且不會影響到現有$PATH裡面的內容):$ claude mcp add zhtw-mcp -- nix run github:pan93412/zhtw-mcp/add-nix-package -- Added stdio MCP server zhtw-mcp with command: nix run github:pan93412/zhtw-mcp/add-nix-package -- to local config File modified: /Users/yjpan/.claude.json [project: /Volumes/Dev/tmp/clawd-on-desk] ❯ /mcp Manage MCP servers 9 servers Local MCPs (/Users/yjpan/.claude.json [project: /Volumes/Dev/tmp/clawd-on-desk]) ❯ zhtw-mcp · ✔ connected · 1 toolNote
AI Disclosure:
buildRustPackage的啟發式探索(包括doCheck、OpenCC 等等的版本固定)均由 Codex 完成。Nix 更新教學和 README 也是由 Codex 更新,但人類有確認可以編譯。Nix 套件的維護略顯複雜。它為了達到可復現編譯 (reproducible build),要求把
Cargo.lock以及 OpenCC 的 hash 定義上去。因此,如果Cargo.lock有更新,或者是想要更新flake.nix中 OpenCC 的修訂版本,就需要另外開一個 PR 來更新 Nix 上面的 Hash。之後可以另外寫一個 GitHub Actions 來自動執行下面的任務(類似於 cc-statusline 的做法),在此之前應該是需要「如果壞了,再讓有興趣的人上去修 PR」的形式,而使用者則需要做好 Git 版本的鎖定(而不能盲目跟著主幹更新 Nix 套件)。
Nix 更新教學
1. Enter the Nix development shell
If the repository has
flake.nix, run all project commands throughnix develop.For one-off commands, prefer:
2. Update package metadata
The package definition lives in
flake.nixunder:When the Rust crate version changes, update both:
Cargo.toml:package.versionflake.nix:zhtw-mcp.versionKeep package metadata in sync with
Cargo.toml, especially:descriptionhomepagelicensemainProgram3. Refresh
cargoHashIf
Cargo.lockchanges, the vendored Cargo dependency hash must be refreshed.Temporarily replace the existing
cargoHashinflake.nixwith:Then run:
nix develop -c nix build .#zhtw-mcpThe build should fail with a fixed-output hash mismatch and print a
got:hash. Copy that value back into
cargoHash.Example:
Re-run the package build after updating the hash.
4. Refresh OpenCC dictionary inputs
zhtw-mcpgeneratessrc/engine/s2t_data.rsfrom three OpenCC dictionaryfiles during the Nix build:
STPhrases.txtSTCharacters.txtTWVariants.txtThe Nix package pins these files through:
To update the OpenCC source revision:
Copy the commit hash into
openccRev.Then prefetch each dictionary at the new revision:
Copy each returned
hashinto the matchingfetchurlentry inflake.nix.5. Format the flake
After editing
flake.nix, run:6. Build and verify
Build the named package:
nix develop -c nix build .#zhtw-mcpBuild the default package:
nix develop -c nix build .Run the built binary:
Remove the local
resultsymlink before committing:7. Optional flake input updates
If you need newer
nixpkgsorfenixinputs, updateflake.lock:Then rebuild:
Review
flake.lockcarefully before committing. Input updates can change theRust toolchain, dependency build hooks, or platform behavior.
Troubleshooting
Missing
src/engine/s2t_data.rsThe generated S2T table is intentionally ignored by Git. The Nix package
creates it in
preBuildfrom the fixed OpenCC dictionary inputs. If the buildfails with a missing
s2t_data.rs, check thatpreBuildstill runs:Cargo vendor hash mismatch
This is expected after changing Rust dependencies. Use the fake-hash workflow
in "Refresh
cargoHash".CLI integration tests fail in the Nix check phase
The package check phase is limited to:
Some CLI integration tests derive a binary path from Cargo's local
targetlayout and are not reliable inside the Nix build sandbox. Run the full test
suite from the development shell when changing application behavior:
Summary by cubic
Add a Nix flake for
zhtw-mcpso it can be run withnix runor installed withnix profile, plus a dev shell for contributors. Also fixes toolchain selection and build metadata to improve cross-compilation, reproducibility, and docs.New Features
zhtw-mcpas the default package; run withnix run github:sysprog21/zhtw-mcp --or install vianix profile.fenixRust toolchain and common Cargo tools;.envrcenablesuse flake.cargoHashand OpenCC dictionaries;s2t_data.rsgenerated inpreBuild; pinsnixpkgs/fenixinflake.lock.Bug Fixes
fenixviafinal.stdenvfor correct system/cross builds; read version fromCargo.tomlusingbuiltins.fromTOML; includerustToolchaininnativeBuildInputsand use PATHrustfmt.meta(adds Unix platforms), remove incorrectopencode mcp addexample, and ignore/result.Written for commit 54de53d. Summary will update on new commits.