Create Rust Derivation
Create a new Rust derivation from: $ARGUMENTS
Steps
- •
Parse the GitHub URL: Extract the owner and repo name from the provided GitHub URL (e.g.,
https://github.com/owner/repo). - •
Fetch repo information: Use
gh repo view owner/repo --json description,licenseInfo,latestRelease,nameto get:- •Repository description
- •License information
- •Latest release version
- •Repo name (used as pname)
- •
Inspect Cargo.toml: Fetch the
Cargo.tomlfrom the repo's default branch to determine:- •The binary/package name (may differ from repo name)
- •Any notable features or workspace structure
bashgh api repos/<owner>/<repo>/contents/Cargo.toml --jq '.content' | base64 -d
- •
Create the derivation file: Create
derivations/<pname>.nixusing the standard template:nix{ lib , rustPlatform , fetchFromGitHub }: rustPlatform.buildRustPackage rec { pname = "<package-name>"; version = "<version>"; src = fetchFromGitHub { owner = "<owner>"; repo = "<repo>"; rev = "v${version}"; hash = "<source-hash>"; }; cargoHash = "<cargo-hash>"; doCheck = false; meta = with lib; { description = "<description>"; homepage = "https://github.com/<owner>/<repo>"; license = with licenses; [ <license> ]; maintainers = with maintainers; [ ]; platforms = platforms.unix; mainProgram = "<binary-name>"; }; }Notes on the template:
- •
revmay be"v${version}"orversiondepending on the repo's tag convention. Check the repo's tags to determine this. - •Add
nativeBuildInputs,buildInputs, orenvonly if needed (e.g., for openssl, pkg-config, darwin SDKs). - •Set
doCheck = false;by default (tests often fail in the nix sandbox).
- •
- •
Get the source hash:
bashnix-prefetch-url --unpack "https://github.com/<owner>/<repo>/archive/refs/tags/<rev>.tar.gz"
Then convert to SRI format:
bashnix hash to-sri --type sha256 <hash>
Update the
hashinfetchFromGitHubwith the SRI hash. - •
Set cargoHash to placeholder: Set
cargoHash = lib.fakeHash;to trigger nix to compute the correct hash. - •
Build to get cargoHash:
bashnix build --impure --expr 'let pkgs = import <nixpkgs> {}; in pkgs.callPackage ./derivations/<pname>.nix {}' 2>&1 | grep -A2 "got:"Extract the correct cargoHash from the error output.
- •
Update cargoHash: Replace
lib.fakeHashwith the actual cargoHash from the build output. - •
Verify the build:
bashnix build --impure --expr 'let pkgs = import <nixpkgs> {}; in pkgs.callPackage ./derivations/<pname>.nix {}'Ensure the build completes successfully. If it fails due to missing dependencies (e.g., openssl, pkg-config), add the appropriate
nativeBuildInputs/buildInputsand rebuild. - •
Add to flake.nix: Add the new package to the
my-packagesoverlay inflake.nix:nix<pname> = final.callPackage (self + "/derivations/<pname>.nix") { };Also expose it in the flake
packagesoutput if the other derivations are exposed there. - •
Summary: Report what was created (package name, version, file path) and remind to run
darwin-rebuild switch --flake .to apply changes.
Notes
- •The derivation must use
rustPlatform.buildRustPackagewithcargoHash - •All hashes must be in SRI format (e.g.,
sha256-...) - •Check the repo's release tags to determine if versions are prefixed with
v(e.g.,v1.0.0vs1.0.0) - •If the repo has no releases, use the latest commit hash for
revand setversionto a date or commit-based string - •Some packages may need additional inputs — inspect build errors and add dependencies as needed
- •License mapping: use nixpkgs license identifiers (e.g.,
mit,asl20for Apache-2.0,gpl3Only, etc.)