mirror of
https://github.com/ipetkov/crane.git
synced 2024-10-03 17:28:35 +03:00
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:
parent
17d9e9dedd
commit
a3f0c63eed
@ -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
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -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;
|
||||
};
|
||||
}
|
||||
```
|
||||
|
@ -46,6 +46,6 @@ let
|
||||
});
|
||||
in
|
||||
cargoAwesome {
|
||||
src = craneLib.cleanCargoSource (craneLib.path ./.);
|
||||
src = craneLib.cleanCargoSource ./.;
|
||||
}
|
||||
```
|
||||
|
@ -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 = [
|
||||
|
@ -29,7 +29,7 @@ craneLib.buildPackage {
|
||||
src = patchedCargoLock;
|
||||
};
|
||||
|
||||
src = craneLib.cleanCargoSource (craneLib.path ./.);
|
||||
src = craneLib.cleanCargoSource ./.;
|
||||
|
||||
patches = [
|
||||
./update-cargo-lock.patch
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
@ -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; [
|
||||
|
@ -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; [
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
};
|
||||
}
|
||||
```
|
||||
|
@ -60,7 +60,7 @@
|
||||
];
|
||||
|
||||
my-crate = craneLib.buildPackage {
|
||||
src = craneLib.cleanCargoSource (craneLib.path ./.);
|
||||
src = craneLib.cleanCargoSource ./.;
|
||||
strictDeps = true;
|
||||
|
||||
buildInputs = [
|
||||
|
@ -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;
|
||||
|
@ -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";
|
||||
|
@ -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.
|
||||
|
@ -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;
|
||||
|
@ -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";
|
||||
|
@ -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;
|
||||
|
@ -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 = [
|
||||
|
@ -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 = {
|
||||
|
@ -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 = {
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
};
|
||||
});
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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 { };
|
||||
|
26
lib/internalCrateNameForCleanSource.nix
Normal file
26
lib/internalCrateNameForCleanSource.nix
Normal 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"
|
||||
|
22
lib/path.nix
22
lib/path.nix
@ -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)
|
||||
|
@ -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 (
|
||||
|
Loading…
Reference in New Issue
Block a user