From 6f3445b55ec849406dfb2c3a789ff8195611f753 Mon Sep 17 00:00:00 2001 From: Ivan Petkov Date: Fri, 29 Nov 2024 10:48:09 -0800 Subject: [PATCH] buildDepsOnly: prefer `dummySrc` when specified for fallback values (#750) --- CHANGELOG.md | 3 +++ checks/default.nix | 14 ++++++++++++++ docs/API.md | 11 ++++++++--- lib/buildDepsOnly.nix | 15 +++++++++++++-- 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 318bab79..f9058653 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Fixed * `removeReferencesToVendoredSources` now deduplicates any found references to avoid pathological memory usage before removing them. +* `buildDepsOnly` will calculate fallback `pname`/`version`/`cargoVendorDir` + attributes using `dummySrc` if it was specified (rather than attempting to use + `src`) ## [0.19.3] - 2024-11-18 A republish of 0.19.2 which was incorrectly tagged. diff --git a/checks/default.nix b/checks/default.nix index 6d4e2fa9..b7a92bb0 100644 --- a/checks/default.nix +++ b/checks/default.nix @@ -260,6 +260,20 @@ in touch $out ''; + customDummy = myLib.buildDepsOnly { + dummySrc = pkgs.stdenv.mkDerivation { + pname = "custom-dummy"; + version = "0.0.0"; + src = myLib.cleanCargoSource ./simple; + postInstall = '' + mkdir -p $out + cp -r . $out + echo 'fn main() {}' > $out/src/main.rs + find $out + ''; + }; + }; + # https://github.com/ipetkov/crane/pull/234 nonJsonCargoBuildLog = let diff --git a/docs/API.md b/docs/API.md index 761a2d67..2adbf1fa 100644 --- a/docs/API.md +++ b/docs/API.md @@ -130,7 +130,8 @@ to influence its behavior. be consumed without network access. Directory structure should basically follow the output of `cargo vendor`. - Default value: the result of `vendorCargoDeps` after applying the arguments - set (with the respective default values) + set (with the respective default values). Note if `dummySrc` is specified, + it will be used as the `src` passed into `vendorCargoDeps` * `checkPhaseCargoCommand`: A command to run during the derivation's check phase. Pre and post check hooks will automatically be run. - Default value: `"${cargoTestCommand} ${cargoExtraArgs}"` @@ -140,9 +141,13 @@ to influence its behavior. Automatically derived if not passed in - Default value: `mkDummySrc args.src` * `pname`: package name of the derivation - - Default value: inherited from calling `crateNameFromCargoToml` + - Default value: inherited from calling `crateNameFromCargoToml`. Note if + `dummySrc` is specified, it will be used as the `src` passed into + `crateNameFromCargoToml` * `version`: version of the derivation - - Default value: inherited from calling `crateNameFromCargoToml` + - Default value: inherited from calling `crateNameFromCargoToml`. Note if + `dummySrc` is specified, it will be used as the `src` passed into + `crateNameFromCargoToml` #### Remove attributes The following attributes will be removed before being lowered to diff --git a/lib/buildDepsOnly.nix b/lib/buildDepsOnly.nix index fecc7935..33813d1e 100644 --- a/lib/buildDepsOnly.nix +++ b/lib/buildDepsOnly.nix @@ -13,7 +13,6 @@ , ... }@args: let - crateName = crateNameFromCargoToml args; cleanedArgs = builtins.removeAttrs args [ "cargoBuildCommand" "cargoCheckCommand" @@ -39,6 +38,18 @@ let args.dummySrc else mkDummySrc args; + + # If dummySrc is define *in args*, use it as the `src` for fallback calculations, + # but DO NOT use the computed `dummySrc` above as that's likely to trigger IFD + # (in case anyone is trying to avoid that). Dummifiying the sources should preserve + # the name/version of the Cargo.toml, as well as the entirety of Cargo.lock, + # so it shouldn't matter anyway + argsMaybeDummySrcOverride = + if args ? dummySrc + then args // { src = args.dummySrc; } + else args; + + crateName = crateNameFromCargoToml argsMaybeDummySrcOverride; in mkCargoDerivation (cleanedArgs // { inherit doCheck; @@ -49,7 +60,7 @@ mkCargoDerivation (cleanedArgs // { version = args.version or crateName.version; cargoArtifacts = null; - cargoVendorDir = args.cargoVendorDir or (vendorCargoDeps args); + cargoVendorDir = args.cargoVendorDir or (vendorCargoDeps argsMaybeDummySrcOverride); env = (args.env or { }) // { # Export a marker variable in case any scripts or hooks want to customize