Nix 软件包打包
create-nix-package
根据包名或源码 URL 创建、更新并校验 Nix 软件包派生(derivation)。当用户要求为 Nix/Nixpkgs 打包软件、生成派生、获取上游源码、选择构建输入,或让打包好的可执行程序运行时使用。
- License 许可
- MIT
- 最近更新
- 2026-11-14
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- create-nix-package
- description
- Create, update, and validate Nix package derivations from a package name or source URL. Use when asked to package software for Nix/Nixpkgs, generate a derivation, fetch upstream sources, choose build inputs, or make the packaged executable run.
- compatibility
- Requires an environment with Nix available. Prefer nix-init in headless/non-interactive mode; use nurl when nix-init is unavailable or for source metadata.
03 SKILL.md 正文
Nix Packaging Derivations
Use this skill to package software as a Nix derivation, starting from a package name, repository URL, release URL, archive URL, or language package identifier.
First response / inputs
The task must have at least one of:
- package name
- upstream URL
- language ecosystem identifier, such as a PyPI/npm/Go/Rust package name
If missing, ask for it. Also ask for any missing required context that changes the derivation shape:
- target location/file to write, unless obvious from the repository
- whether this is for Nixpkgs or a local flake/package set
- expected executable/library name and a simple smoke-test command, if not obvious
- target platform constraints, if relevant
If the user only gives a package name, discover the canonical upstream source before writing the derivation.
Required workflow
- Inspect the existing repository structure and packaging conventions.
- Look for
pkgs/by-name,pkgs/top-level/all-packages.nix,packages,flake.nix, overlays, existing similar packages, and style conventions. - Do not manually install tools. If a needed tool is absent, use
nix run nixpkgs#<tool> -- ...or ask the user.
- Look for
- Fetch/identify upstream source metadata.
- Prefer
nix-initin headless/non-interactive mode to generate an initial derivation. - If
nix-initis unavailable or unsuitable, usenurlto produce fetcher expressions and hashes. - Always verify tags/releases/checksums against upstream, not only generated output.
- Prefer
- Determine the build system by inspecting source files.
- Examples:
Cargo.toml,go.mod,package.json,pyproject.toml,setup.py,meson.build,CMakeLists.txt,Makefile, Autotools files, static assets/scripts. - Read upstream docs for install/build/test instructions when present.
- Examples:
- Write or update the derivation in the project’s preferred location.
- Build it with Nix and fix failures.
- Treat failed build attempts as experiments: once the package works, remove every workaround, dependency, override, patch, phase, or environment variable that was tried but is not required by the final solution. Leave the smallest clear changeset.
- Run the packaged program or otherwise perform a meaningful smoke test.
- Leave a concise summary of files changed, build/test commands run, and any caveats.
Tooling guidance
nix-init
Use nix-init non-interactively/headlessly. Check the installed CLI with nix-init --help because flags may vary. Typical usage is along these lines:
nix-init --headless --url <upstream-url>
You can specify the builder (buildGoModule etc) with --builder
Then treat the generated file as a draft. Review and simplify it; do not blindly accept generated dependencies, metadata, or phases.
If nix-init is not on PATH, try:
nix run self#nix-init -- --help
nurl
Use nurl when you need a fetcher and hash, especially for GitHub/GitLab/source archives:
nurl <url>
If nurl is not on PATH, try:
nix run self#nurl -- <url>
Derivation style requirements
Prefer this idiom:
{
lib,
stdenvNoCC,
fetchFromGitHub,
}:
stdenvNoCC.mkDerivation (finalAttrs: {
pname = "example";
version = "1.0.0";
src = fetchFromGitHub {
owner = "upstream";
repo = "example";
tag = "v${finalAttrs.version}";
hash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
};
__structuredAttrs = true;
strictDeps = true;
installPhase = ''
runHook preInstall
install -Dm755 example $out/bin/example
runHook postInstall
'';
meta = {
description = "Short upstream-accurate description";
homepage = "https://example.org";
license = lib.licenses.mit;
maintainers = with lib.maintainers; [ ];
mainProgram = "example";
};
})
Requirements:
- Use
mkDerivation (finalAttrs: { ... })rather than a surroundingletblock when values can referencefinalAttrs. - Set
__structuredAttrs = true;andstrictDeps = true;unless the surrounding project has an explicit reason not to. - Prefer
stdenvNoCC.mkDerivationoverstdenv.mkDerivationwhen no C/C++/Objective-C compiler toolchain is needed. - Use
stdenv.mkDerivationonly when compilation or compiler-provided setup hooks are actually required. - Keep dependencies in the correct attributes:
nativeBuildInputs: tools that run while building, including build systems, code generators, wrappers, pkg-config, and install helpers. Do not put host/runtime libraries here merely because their commands were used during the build.buildInputs: host-platform libraries and other dependencies linked into or needed by the built package at runtime. Do not put build-only tools here: doing so can unnecessarily enlarge the runtime closure.- When cross-compilation is relevant, be especially precise: build-machine executables belong in
nativeBuildInputs; host-package libraries belong inbuildInputs. propagatedBuildInputs: only for dependencies consumers must see.nativeCheckInputs: test-only tools.
- Preserve
runHook pre*andrunHook post*in custom phases. - Keep metadata accurate. Do not invent licenses, maintainers, homepage, or descriptions.
- Use
meta.mainProgramfor executable packages.
Choosing stdenvNoCC
Use stdenvNoCC for packages that only install or wrap existing files, for example:
- shell/Python/Perl/Ruby scripts that are not compiled by this derivation
- static data, themes, icons, fonts, documentation, completions
- prebuilt single-file scripts or assets
- packages whose build is only copying, patching shebangs, substituting paths, or wrapping scripts
Use regular stdenv when building native code, when a compiler is needed during configure/build/check, or when a language builder internally requires it.
Source and hash handling
- Prefer stable release tags/tarballs over moving branches.
- Use
fetchFromGitHub,fetchFromGitLab,fetchgit,fetchurl, or language-specific fetchers according to project convention. - Do not use
lib.fakeHashin final code unless intentionally leaving a failing placeholder and telling the user. - If a hash mismatch occurs, update the hash from Nix’s reported
got:value after verifying the source is expected. - Prefer
hash = "sha256-...";over legacy hash attributes unless local style differs.
Update scripts
For independently versioned upstream packages, declare passthru.updateScript so the update policy lives in the derivation, not only in documentation or remembered CLI flags. Add nix-update-script to the package's function arguments and prefer the standard helper over a handwritten wrapper:
passthru.updateScript = nix-update-script {
attrPath = "example";
extraArgs = [ "--flake" ];
};
- Set
attrPathto the actual exported attribute (for example,emacs-reader, not its internalpnameofreader). - Use
--flakefor this repository's flake packages. For Nixpkgs packages, omit it and normally usenix-update-script { }for release updates. - For commit snapshots, add
"--version=branch"toextraArgs(or"--version=branch=<branch>"for a specific branch). Use a conventional<release-or-0>-unstable-YYYY-MM-DDversion and a pinned commitrev. Do not leave branch-update policy only in the README. - For release packages, link the source
tagor download URL tofinalAttrs.version; usetagfor release tags andrevfor commit revisions where the fetcher supports that distinction. - Use a custom update script only when the standard helper cannot coordinate the required work, such as generating an asset index or regenerating locally maintained dependency manifests. Expose a runnable command through
passthru.updateScript, and declare its runtime tools. Inner calls tonix-updatemust not use-u, which would recursively invoke the custom script. - Keep existing custom updaters intact unless a change is necessary. Integration should be the smallest required changeset: do not rewrite variables into temporary files, add unrelated hardening, or otherwise refactor the updater just to expose it through
passthru.updateScript. Set generated output paths explicitly relative to the repository root. - Local sources and packages that inherit their source/version from another package do not need independent update scripts. Intentionally frozen sources need an explicit documented policy;
nix-updatehas no universal opt-out, so an intentional no-op script is only respected when callers use-u. - Evaluate the resulting
updateScriptcommand and verify its attribute path and flags. Avoid advancing package versions merely to test wiring. Document thatnix-update --flake -u <attr>runs the declared script; nix-update 1.16 also requires<nixpkgs>inNIX_PATHfor this mode.
Build-system notes
Follow existing project conventions first. Common patterns:
- Rust: prefer
rustPlatform.buildRustPackage; updatecargoHashvia build output ornix-prefetch; run the binary. - Go: prefer
buildGoModule; setvendorHashorvendorHash = nullonly when appropriate. - Python: prefer
buildPythonApplication/buildPythonPackagewithpyproject = truefor PEP 517 projects; usepythonImportsCheckand a CLI smoke test. - Node: use the project’s established Node builder; avoid ad-hoc network access during build.
- CMake/Meson/Autotools: use the standard setup hooks in
nativeBuildInputs. - Plain scripts/assets: use
stdenvNoCC.mkDerivation, install files explicitly, patch shebangs, and wrap with runtime dependencies as needed.
Validation
Always attempt to build the package:
nix build .#<attr>
or, for a standalone derivation:
nix-build -E 'with import <nixpkgs> {}; callPackage ./package.nix {}'
Then test the installed output. For executables, run for example:
./result/bin/<mainProgram> --help
If --help is unsupported, use --version, a no-op invocation, or another harmless smoke test. For libraries, use imports/check phases or a minimal consumer/import test.
If tests are expensive or require network/secrets/GUI, do not run them blindly. Explain the limitation and run the best safe substitute.
Quality checklist before final response
- Derivation builds reproducibly without network access during build.
- Packaged executable/library runs or passes a meaningful smoke test.
__structuredAttrsandstrictDepsare enabled.stdenvNoCCis used when a compiler is unnecessary.finalAttrsis used for version/tag/hash references where helpful.- Dependencies are minimized and correctly split between native build tools and host/runtime inputs, without unnecessarily enlarging the runtime closure.
- Failed-experiment workarounds and other no-longer-needed code or dependencies have been removed.
- Metadata is accurate, including
licenseandmainProgramwhen applicable. - Independently updateable upstream packages declare an appropriate
passthru.updateScript, including branch flags for commit snapshots and the correct flake attribute path. - The final response lists changed files and exact validation commands run.
04 同仓库更多 Skills
方案决策压力质询
grilling
对用户的计划、决策或想法进行不留情面的连续追问。当用户想要对自己的思路进行压力测试,或使用任何 'grill' 触发短语时使用。
方案质询与文档生成
grill-with-docs
通过不留情面的连续追问来打磨方案或设计,并在此过程中同步创建文档(ADR 架构决策记录和术语表)。
方案设计质询访谈
grill-me
通过不留情面的追问式访谈,打磨和优化计划或设计。
领域模型构建与梳理
domain-modeling
构建并完善项目的领域模型。在讨论代码库术语、编写或编辑 GLOSSARY.md,或记录或编辑 ADR(架构决策记录)时使用。