diff options
| author | tv <tv@krebsco.de> | 2026-11-10 05:48:43 +0100 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-10 05:48:43 +0100 |
| commit | baec651a12204daccd76cacb3a59f13bda8ba6a6 (patch) | |
| tree | 026e307bce106a66d25421b01faa1f8a3b68ffb8 /pkgs/rust/lib/crate2nix-package.nix | |
| parent | e732f21699db07b0777f43ef2a64d503274171a6 (diff) | |
pkgs: add rust overlay
Diffstat (limited to 'pkgs/rust/lib/crate2nix-package.nix')
| -rw-r--r-- | pkgs/rust/lib/crate2nix-package.nix | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/pkgs/rust/lib/crate2nix-package.nix b/pkgs/rust/lib/crate2nix-package.nix new file mode 100644 index 0000000..d97a97e --- /dev/null +++ b/pkgs/rust/lib/crate2nix-package.nix @@ -0,0 +1,77 @@ +# Per-crate Rust builds with crate2nix. +# +# resolvedJson selects how the build plan is obtained: +# null (default) <src>/Cargo.json if present, else IFD from <src>/Cargo.lock +# a path that file (crate2nix generate --format json) +# false always IFD from <src>/Cargo.lock +# +# Overrides are attrs, or functions from the crate's buildRustCrate arguments +# to attrs, and are merged into those arguments before buildRustCrate sees +# them, so they can also set release. For a workspace member, +# workspaceOverride applies first and crateOverrides.<name> sees its result. +# nixpkgs' defaultCrateOverrides still apply inside buildRustCrate. + +{ pkgs +, applyOverride + +, name +, src + +# crate name -> attrs or (args -> attrs) +, crateOverrides ? {} + +# attrs or (args -> attrs), for every workspace member +, workspaceOverride ? {} + +# IFD only; Cargo.json has its features baked in +, features ? [ "default" ] + +, resolvedJson ? null +}: + +let + resolvedJsonFromSrc = src + "/Cargo.json"; + + useJson = + if resolvedJson == null then builtins.pathExists resolvedJsonFromSrc + else resolvedJson != false; + + resolvedJson' = + if resolvedJson != null then resolvedJson else resolvedJsonFromSrc; + + workspaceMembers = builtins.attrNames cargoNix.workspaceMembers; + + buildRustCrateForPkgs = pkgs: args: + let + withWorkspace = + if builtins.elem args.crateName workspaceMembers + then applyOverride workspaceOverride args + else args; + in + pkgs.buildRustCrate + (applyOverride (crateOverrides.${args.crateName} or {}) withWorkspace); + + fromJson = pkgs.crate2nix.build-from-json { + inherit buildRustCrateForPkgs pkgs src; + resolvedJson = resolvedJson'; + }; + + fromLock = + pkgs.callPackage + (pkgs.crate2nix.tools.generatedCargoNix { + inherit name src; + additionalCargoNixArgs = [ "--all-features" ]; + }) + { + rootFeatures = features; + inherit buildRustCrateForPkgs; + }; + + cargoNix = + if useJson then fromJson else fromLock; +in +cargoNix // { + build = + if cargoNix.rootCrate or null != null then cargoNix.rootCrate.build + else cargoNix.allWorkspaceMembers; +} |
