Try to avoid IFD in vendorCargoDeps and crateNameFromCargoToml; also avoid recommending nesting cleanCargoSource and path (#641)

We don't need to nest `cleanCargoSource` and `path` just to populate a
default value for `name`. As they both ultimately delegate to
`builtins.path`, the nesting can lead to IFD in situations which are
otherwise avoidable
This commit is contained in:
Ivan Petkov 2024-06-11 03:53:46 +00:00 committed by GitHub
parent 17d9e9dedd
commit a3f0c63eed
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
31 changed files with 103 additions and 55 deletions

View File

@ -9,10 +9,18 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Changed
* **Breaking**: dropped compatibility for Nix versions below 2.18.2
* **Breaking**: dropped compatibility for nixpkgs-23.11.
* The guidance around using (both) `cleanCargoSource` and `path` has been
updated. Namely, it is no longer necessary to call both (e.g.
`craneLib.cleanCargoSource (craneLib.path ./.)`): it is recommended to either
use `craneLib.cleanCargoSource ./.` directly (if the default source cleaning
is desired) or `craneLib.path ./.` (if not).
### Fixed
* The cross compilation example also hows how to set the `TARGET_CC` environment
variable which may be required by some build scripts to function properly
* `vendorCargoDeps` and `crateNameFromCargoToml` do their best to avoid IFD when
`src` is the result of `lib.cleanSourceWith` (and by extension
`cleanCargoSource`)
## [0.17.3] - 2024-06-02

View File

@ -741,7 +741,7 @@ written (which may want to also call `craneLib.filterCargoSources`) to achieve t
desired behavior.
```nix
craneLib.cleanCargoSource (craneLib.path ./.)
craneLib.cleanCargoSource ./.
```
### `craneLib.cleanCargoToml`
@ -983,8 +983,9 @@ will retain the following files from a given source:
```nix
cleanSourceWith {
src = craneLib.path ./.;
src = ./.;
filter = craneLib.filterCargoSources;
name = "source"; # Be reproducible, regardless of the directory name
}
```
@ -999,8 +1000,9 @@ let
(markdownFilter path type) || (craneLib.filterCargoSources path type);
in
cleanSourceWith {
src = craneLib.path ./.;
src = ./.;
filter = markdownOrCargo;
name = "source"; # Be reproducible, regardless of the directory name
}
```

View File

@ -31,11 +31,11 @@ in
# Build two different workspaces with the modified behavior above
foo = craneLib.buildPackage {
src = craneLib.cleanCargoSource (craneLib.path ./foo);
src = craneLib.cleanCargoSource ./foo;
};
bar = craneLib.buildPackage {
src = craneLib.cleanCargoSource (craneLib.path ./bar);
src = craneLib.cleanCargoSource ./bar;
};
}
```

View File

@ -46,6 +46,6 @@ let
});
in
cargoAwesome {
src = craneLib.cleanCargoSource (craneLib.path ./.);
src = craneLib.cleanCargoSource ./.;
}
```

View File

@ -34,7 +34,7 @@ and hooks to customize a particular build:
```nix
craneLib.buildPackage {
src = craneLib.cleanCargoSource (craneLib.path ./.);
src = craneLib.cleanCargoSource ./.;
# Define a list of function names to execute before the `configurePhase` runs
preConfigurePhases = [

View File

@ -29,7 +29,7 @@ craneLib.buildPackage {
src = patchedCargoLock;
};
src = craneLib.cleanCargoSource (craneLib.path ./.);
src = craneLib.cleanCargoSource ./.;
patches = [
./update-cargo-lock.patch

View File

@ -13,7 +13,7 @@ deeper directory:
# ./nested/Cargo.lock
# ./nested/src/*.rs
craneLib.buildPackage {
src = myLib.cleanCargoSource (craneLib.path ./.);
src = myLib.cleanCargoSource ./.;
cargoLock = ./nested/Cargo.lock;
cargoToml = ./nested/Cargo.toml;
# Use a postUnpack hook to jump into our nested directory. This will work

View File

@ -51,7 +51,7 @@ following contents at the root of your cargo workspace:
in
{
packages.default = craneLib.buildPackage {
src = craneLib.cleanCargoSource (craneLib.path ./.);
src = craneLib.cleanCargoSource ./.;
# Add extra inputs here or any other derivation settings
# doCheck = true;

View File

@ -32,7 +32,7 @@ Here's how we can set up our flake to achieve our goals:
# Common derivation arguments used for all builds
commonArgs = {
src = craneLib.cleanCargoSource (craneLib.path ./.);
src = craneLib.cleanCargoSource ./.;
strictDeps = true;
buildInputs = with pkgs; [

View File

@ -24,7 +24,7 @@ build.
# Common derivation arguments used for all builds
commonArgs = {
src = craneLib.cleanCargoSource (craneLib.path ./.);
src = craneLib.cleanCargoSource ./.;
strictDeps = true;
buildInputs = with pkgs; [

View File

@ -34,7 +34,7 @@ Sample `flake.nix`:
craneLib = crane.mkLib pkgs;
my-crate = craneLib.buildPackage {
src = craneLib.cleanCargoSource (craneLib.path ./.);
src = craneLib.cleanCargoSource ./.;
buildInputs = [
# Add additional build inputs here

View File

@ -25,7 +25,7 @@ does not alter the derivation's attributes_ directly:
}:
craneLib.buildPackage {
src = craneLib.cleanCargoSource (craneLib.path ./..);
src = craneLib.cleanCargoSource ./..;
strictDeps = true;
cargoExtraArgs =
(lib.optionalString withFoo "--features foo") +
@ -123,7 +123,7 @@ If you need to change behavior that way, consider using a combination of
craneLib = crane.mkLib pkgs;
my-crate = craneLib.buildPackage {
src = craneLib.cleanCargoSource (craneLib.path ./.);
src = craneLib.cleanCargoSource ./.;
strictDeps = true;
};
in

View File

@ -17,7 +17,7 @@ non-Rust/non-cargo related files. It can be used like so:
```nix
craneLib.buildPackage {
# other attributes omitted
src = craneLib.cleanCargoSource (craneLib.path ./.);
src = craneLib.cleanCargoSource ./.;
}
```
@ -37,8 +37,9 @@ in
craneLib.buildPackage {
# other attributes omitted
src = lib.cleanSourceWith {
src = craneLib.path ./.; # The original, unfiltered source
src = ./.; # The original, unfiltered source
filter = markdownOrCargo;
name = "source"; # Be reproducible, regardless of the directory name
};
}
```

View File

@ -60,7 +60,7 @@
];
my-crate = craneLib.buildPackage {
src = craneLib.cleanCargoSource (craneLib.path ./.);
src = craneLib.cleanCargoSource ./.;
strictDeps = true;
buildInputs = [

View File

@ -39,7 +39,7 @@
# our specific toolchain there.
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
src = craneLib.cleanCargoSource (craneLib.path ./.);
src = craneLib.cleanCargoSource ./.;
my-crate = craneLib.buildPackage {
inherit src;

View File

@ -35,7 +35,7 @@
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
my-crate = craneLib.buildPackage {
src = craneLib.cleanCargoSource (craneLib.path ./.);
src = craneLib.cleanCargoSource ./.;
strictDeps = true;
CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl";

View File

@ -55,7 +55,7 @@
, stdenv
}:
craneLib.buildPackage {
src = craneLib.cleanCargoSource (craneLib.path ./.);
src = craneLib.cleanCargoSource ./.;
strictDeps = true;
# Build-time tools which are target agnostic. build = host = target = your-machine.

View File

@ -32,7 +32,7 @@
craneLib = (crane.mkLib pkgs).overrideToolchain toolchain;
my-crate = craneLib.buildPackage {
src = craneLib.cleanCargoSource (craneLib.path ./.);
src = craneLib.cleanCargoSource ./.;
strictDeps = true;
doCheck = false;

View File

@ -39,7 +39,7 @@
craneLib = (crane.mkLib pkgs).overrideToolchain rustWithWasiTarget;
my-crate = craneLib.buildPackage {
src = craneLib.cleanCargoSource (craneLib.path ./.);
src = craneLib.cleanCargoSource ./.;
strictDeps = true;
cargoExtraArgs = "--target wasm32-wasi";

View File

@ -29,7 +29,7 @@
rustToolchain = pkgs.rust-bin.stable.latest.default;
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
src = craneLib.cleanCargoSource (craneLib.path ./.);
src = craneLib.cleanCargoSource ./.;
workspace = craneLib.buildPackage {
inherit src;

View File

@ -22,7 +22,7 @@
# Common arguments can be set here to avoid repeating them later
# Note: changes here will rebuild all dependency crates
commonArgs = {
src = craneLib.cleanCargoSource (craneLib.path ./.);
src = craneLib.cleanCargoSource ./.;
strictDeps = true;
buildInputs = [

View File

@ -31,7 +31,7 @@
inherit (pkgs) lib;
craneLib = crane.mkLib pkgs;
src = craneLib.cleanCargoSource (craneLib.path ./.);
src = craneLib.cleanCargoSource ./.;
# Common arguments can be set here to avoid repeating them later
commonArgs = {

View File

@ -31,7 +31,7 @@
inherit (pkgs) lib;
craneLib = crane.mkLib pkgs;
src = craneLib.cleanCargoSource (craneLib.path ./.);
src = craneLib.cleanCargoSource ./.;
# Common arguments can be set here to avoid repeating them later
commonArgs = {

View File

@ -25,8 +25,9 @@
sqlOrCargo = path: type: (sqlFilter path type) || (craneLib.filterCargoSources path type);
src = lib.cleanSourceWith {
src = craneLib.path ./.; # The original, unfiltered source
src = ./.; # The original, unfiltered source
filter = sqlOrCargo;
name = "source"; # Be reproducible, regardless of the directory name
};
# Common arguments can be set here to avoid repeating them later

View File

@ -13,7 +13,7 @@
{
# https://github.com/ipetkov/crane/issues/446
packages.default = craneLib.buildPackage {
src = craneLib.cleanCargoSource (craneLib.path ../../checks/simple);
src = craneLib.cleanCargoSource ../../checks/simple;
};
});
}

View File

@ -1,4 +1,5 @@
{ filterCargoSources
, internalCrateNameForCleanSource
, lib
}:
@ -8,4 +9,6 @@ src: lib.cleanSourceWith {
# Then add our own filter on top
filter = filterCargoSources;
name = internalCrateNameForCleanSource src;
}

View File

@ -12,7 +12,12 @@ let
- `pname` and `version` are explicitly set
'';
src = args.src or throwMsg;
origSrc = src:
if src ? _isLibCleanSourceWith
then src.origSrc
else src;
src = origSrc (args.src or throwMsg);
cargoToml = args.cargoToml or (src + "/Cargo.toml");
cargoTomlContents = args.cargoTomlContents or (
if builtins.pathExists cargoToml

View File

@ -18,6 +18,9 @@ let
inherit (self) callPackage;
internalCrateNameFromCargoToml = callPackage ./internalCrateNameFromCargoToml.nix { };
internalCrateNameForCleanSource = callPackage ./internalCrateNameForCleanSource.nix {
inherit internalCrateNameFromCargoToml;
};
in
{
appendCrateRegistries = input: self.overrideScope (_final: prev: {
@ -38,7 +41,9 @@ in
cargoNextest = callPackage ./cargoNextest.nix { };
cargoTarpaulin = callPackage ./cargoTarpaulin.nix { };
cargoTest = callPackage ./cargoTest.nix { };
cleanCargoSource = callPackage ./cleanCargoSource.nix { };
cleanCargoSource = callPackage ./cleanCargoSource.nix {
inherit internalCrateNameForCleanSource;
};
cleanCargoToml = callPackage ./cleanCargoToml.nix { };
configureCargoCommonVarsHook = callPackage ./setupHooks/configureCargoCommonVars.nix { };
configureCargoVendoredDepsHook = callPackage ./setupHooks/configureCargoVendoredDeps.nix { };
@ -71,7 +76,7 @@ in
});
path = callPackage ./path.nix {
inherit internalCrateNameFromCargoToml;
inherit internalCrateNameForCleanSource;
};
registryFromDownloadUrl = callPackage ./registryFromDownloadUrl.nix { };

View File

@ -0,0 +1,26 @@
{ internalCrateNameFromCargoToml
, lib
}:
src:
let
origSrc =
if src ? _isLibCleanSourceWith
then src.origSrc
else src;
cargoTomlContents =
let
emptyToml = { };
cargoToml = origSrc + "/Cargo.toml";
cargoTomlContents = builtins.readFile cargoToml;
toml = builtins.tryEval (builtins.fromTOML cargoTomlContents);
in
if builtins.pathExists cargoToml
then
if toml.success then toml.value else emptyToml
else
emptyToml;
in
(internalCrateNameFromCargoToml cargoTomlContents).pname or "source"

View File

@ -1,24 +1,14 @@
{ internalCrateNameFromCargoToml
{ internalCrateNameForCleanSource
, lib
}:
input:
let
pathArgs = if lib.isAttrs input then input else { path = input; };
inputIsAttrs = lib.isAttrs input;
name = input.name or (internalCrateNameForCleanSource (
if inputIsAttrs then input.path else input
));
cargoTomlContents =
let
emptyToml = { };
cargoToml = pathArgs.path + "/Cargo.toml";
cargoTomlContents = builtins.readFile cargoToml;
toml = builtins.tryEval (builtins.fromTOML cargoTomlContents);
in
if builtins.pathExists cargoToml
then
if toml.success then toml.value else emptyToml
else
emptyToml;
name = (internalCrateNameFromCargoToml cargoTomlContents).pname or "source";
pathArgs = if inputIsAttrs then input else { path = input; };
in
builtins.path ({ inherit name; } // pathArgs)

View File

@ -11,14 +11,21 @@ let
inherit (lib.attrsets) optionalAttrs;
cargoConfigs = if args ? src then (findCargoFiles args.src).cargoConfigs else [ ];
origSrc = src:
if src ? _isLibCleanSourceWith
then src.origSrc
else src;
src = args.src or (throw ''
unable to find `src` attribute. consider one of the following:
- set `cargoVendorDir = vendorCargoDeps { cargoLock = ./some/path/to/Cargo.lock; }`
- set `cargoVendorDir = vendorCargoDeps { src = ./src/containing/cargo/lock/file; }`
- set `cargoVendorDir = null` to skip vendoring altogether
'');
cargoConfigs = if args ? src then (findCargoFiles (origSrc args.src)).cargoConfigs else [ ];
src = origSrc (
args.src or (throw ''
unable to find `src` attribute. consider one of the following:
- set `cargoVendorDir = vendorCargoDeps { cargoLock = ./some/path/to/Cargo.lock; }`
- set `cargoVendorDir = vendorCargoDeps { src = ./src/containing/cargo/lock/file; }`
- set `cargoVendorDir = null` to skip vendoring altogether
'')
);
cargoLock = args.cargoLock or (src + "/Cargo.lock");
cargoLockContents = args.cargoLockContents or (