0
0
Fork 0
mirror of https://github.com/NixOS/nixpkgs.git synced 2025-07-13 21:50:33 +03:00

rustPlatform.fetchCargoTarball: support pname+version (#332975)

This commit is contained in:
Philip Taron 2024-09-06 23:00:21 -07:00 committed by GitHub
commit 29cca09041
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 150 additions and 115 deletions

View file

@ -567,8 +567,7 @@ buildPythonPackage rec {
}; };
cargoDeps = rustPlatform.fetchCargoTarball { cargoDeps = rustPlatform.fetchCargoTarball {
inherit src sourceRoot; inherit pname version src sourceRoot;
name = "${pname}-${version}";
hash = "sha256-miW//pnOmww2i6SOGbkrAIdc/JMDT4FJLqdMFojZeoY="; hash = "sha256-miW//pnOmww2i6SOGbkrAIdc/JMDT4FJLqdMFojZeoY=";
}; };
@ -611,9 +610,8 @@ buildPythonPackage rec {
}; };
cargoDeps = rustPlatform.fetchCargoTarball { cargoDeps = rustPlatform.fetchCargoTarball {
inherit src; inherit pname version src;
sourceRoot = "${pname}-${version}/${cargoRoot}"; sourceRoot = "${pname}-${version}/${cargoRoot}";
name = "${pname}-${version}";
hash = "sha256-PS562W4L1NimqDV2H0jl5vYhL08H9est/pbIxSdYVfo="; hash = "sha256-PS562W4L1NimqDV2H0jl5vYhL08H9est/pbIxSdYVfo=";
}; };
@ -652,8 +650,7 @@ buildPythonPackage rec {
}; };
cargoDeps = rustPlatform.fetchCargoTarball { cargoDeps = rustPlatform.fetchCargoTarball {
inherit src; inherit pname version src;
name = "${pname}-${version}";
hash = "sha256-heOBK8qi2nuc/Ib+I/vLzZ1fUUD/G/KTw9d7M4Hz5O0="; hash = "sha256-heOBK8qi2nuc/Ib+I/vLzZ1fUUD/G/KTw9d7M4Hz5O0=";
}; };
@ -697,8 +694,7 @@ stdenv.mkDerivation rec {
}; };
cargoDeps = rustPlatform.fetchCargoTarball { cargoDeps = rustPlatform.fetchCargoTarball {
inherit src; inherit pname version src;
name = "${pname}-${version}";
hash = "sha256-8fa3fa+sFi5H+49B5sr2vYPkp9C9s6CcE0zv4xB8gww="; hash = "sha256-8fa3fa+sFi5H+49B5sr2vYPkp9C9s6CcE0zv4xB8gww=";
}; };

View file

@ -34,7 +34,7 @@ stdenv.mkDerivation rec {
}; };
cargoDeps = rustPlatform.fetchCargoTarball { cargoDeps = rustPlatform.fetchCargoTarball {
inherit pname version src; inherit src;
hash = "sha256-XTfKqKs7874ak7Lzscxw8E2qcnJOWMZaaol8TpIB6Vw="; hash = "sha256-XTfKqKs7874ak7Lzscxw8E2qcnJOWMZaaol8TpIB6Vw=";
}; };

View file

@ -1,116 +1,156 @@
{ lib, stdenv, cacert, git, cargo, python3 }: {
let cargo-vendor-normalise = stdenv.mkDerivation { lib,
name = "cargo-vendor-normalise"; stdenv,
src = ./cargo-vendor-normalise.py; cacert,
nativeBuildInputs = [ python3.pkgs.wrapPython ]; git,
dontUnpack = true; cargo,
installPhase = "install -D $src $out/bin/cargo-vendor-normalise"; python3,
pythonPath = [ python3.pkgs.toml ]; }:
postFixup = "wrapPythonPrograms"; let
doInstallCheck = true; cargo-vendor-normalise = stdenv.mkDerivation {
installCheckPhase = '' name = "cargo-vendor-normalise";
# check that ../fetchcargo-default-config.toml is a fix point src = ./cargo-vendor-normalise.py;
reference=${../fetchcargo-default-config.toml} nativeBuildInputs = [ python3.pkgs.wrapPython ];
< $reference $out/bin/cargo-vendor-normalise > test; dontUnpack = true;
cmp test $reference installPhase = "install -D $src $out/bin/cargo-vendor-normalise";
''; pythonPath = [ python3.pkgs.toml ];
preferLocalBuild = true; postFixup = "wrapPythonPrograms";
}; doInstallCheck = true;
installCheckPhase = ''
# check that ../fetchcargo-default-config.toml is a fix point
reference=${../fetchcargo-default-config.toml}
< $reference $out/bin/cargo-vendor-normalise > test;
cmp test $reference
'';
preferLocalBuild = true;
};
in in
{ name ? "cargo-deps" {
, src ? null pname ? null,
, srcs ? [] version ? null,
, patches ? [] name ? if args ? pname && args ? version then "${pname}-${version}" else "cargo-deps",
, sourceRoot ? "" src ? null,
, cargoUpdateHook ? "" srcs ? [ ],
, nativeBuildInputs ? [] patches ? [ ],
, ... sourceRoot ? "",
} @ args: cargoUpdateHook ? "",
nativeBuildInputs ? [ ],
...
}@args:
let hash_ = assert lib.assertMsg (
if args ? hash then (args ? pname || args ? version) -> !(args ? name)
{ ) "Either specify `pname` with `version`, or specify `name` only, not a mix of both.";
outputHashAlgo = if args.hash == "" then "sha256" else null; assert lib.assertMsg (
outputHash = args.hash; args ? pname == args ? version
} ) "If `pname` is specified, `version` must be also, and vice versa.";
else if args ? sha256 then { outputHashAlgo = "sha256"; outputHash = args.sha256; } let
else throw "fetchCargoTarball requires a hash for ${name}"; # args to remove from the final call to stdenv.mkDerivation, as we've already handled them
in stdenv.mkDerivation ({ removedArgs = [
name = "${name}-vendor.tar.gz"; "name"
nativeBuildInputs = [ cacert git cargo-vendor-normalise cargo ] ++ nativeBuildInputs; "pname"
"version"
"sha256"
"cargoUpdateHook"
"nativeBuildInputs"
];
buildPhase = '' hash_ =
runHook preBuild if args ? hash then
{
outputHashAlgo = if args.hash == "" then "sha256" else null;
outputHash = args.hash;
}
else if args ? sha256 then
{
outputHashAlgo = "sha256";
outputHash = args.sha256;
}
else
throw "fetchCargoTarball requires a hash for ${name}";
in
stdenv.mkDerivation (
{
name = "${name}-vendor.tar.gz";
nativeBuildInputs = [
cacert
git
cargo-vendor-normalise
cargo
] ++ nativeBuildInputs;
# Ensure deterministic Cargo vendor builds buildPhase = ''
export SOURCE_DATE_EPOCH=1 runHook preBuild
if [[ ! -f Cargo.lock ]]; then # Ensure deterministic Cargo vendor builds
echo export SOURCE_DATE_EPOCH=1
echo "ERROR: The Cargo.lock file doesn't exist"
echo
echo "Cargo.lock is needed to make sure that cargoHash/cargoSha256 doesn't change"
echo "when the registry is updated."
echo
exit 1 if [[ ! -f Cargo.lock ]]; then
fi echo
echo "ERROR: The Cargo.lock file doesn't exist"
echo
echo "Cargo.lock is needed to make sure that cargoHash/cargoSha256 doesn't change"
echo "when the registry is updated."
echo
# Keep the original around for copyLockfile exit 1
cp Cargo.lock Cargo.lock.orig fi
export CARGO_HOME=$(mktemp -d cargo-home.XXX) # Keep the original around for copyLockfile
CARGO_CONFIG=$(mktemp cargo-config.XXXX) cp Cargo.lock Cargo.lock.orig
if [[ -n "$NIX_CRATES_INDEX" ]]; then export CARGO_HOME=$(mktemp -d cargo-home.XXX)
cat >$CARGO_HOME/config.toml <<EOF CARGO_CONFIG=$(mktemp cargo-config.XXXX)
[source.crates-io]
replace-with = 'mirror'
[source.mirror]
registry = "$NIX_CRATES_INDEX"
EOF
fi
${cargoUpdateHook} if [[ -n "$NIX_CRATES_INDEX" ]]; then
cat >$CARGO_HOME/config.toml <<EOF
[source.crates-io]
replace-with = 'mirror'
[source.mirror]
registry = "$NIX_CRATES_INDEX"
EOF
fi
# Override the `http.cainfo` option usually specified in `.cargo/config`. ${cargoUpdateHook}
export CARGO_HTTP_CAINFO=${cacert}/etc/ssl/certs/ca-bundle.crt
if grep '^source = "git' Cargo.lock; then # Override the `http.cainfo` option usually specified in `.cargo/config`.
echo export CARGO_HTTP_CAINFO=${cacert}/etc/ssl/certs/ca-bundle.crt
echo "ERROR: The Cargo.lock contains git dependencies"
echo
echo "This is currently not supported in the fixed-output derivation fetcher."
echo "Use cargoLock.lockFile / importCargoLock instead."
echo
exit 1 if grep '^source = "git' Cargo.lock; then
fi echo
echo "ERROR: The Cargo.lock contains git dependencies"
echo
echo "This is currently not supported in the fixed-output derivation fetcher."
echo "Use cargoLock.lockFile / importCargoLock instead."
echo
cargo vendor $name --respect-source-config | cargo-vendor-normalise > $CARGO_CONFIG exit 1
fi
# Create an empty vendor directory when there is no dependency to vendor cargo vendor $name --respect-source-config | cargo-vendor-normalise > $CARGO_CONFIG
mkdir -p $name
# Add the Cargo.lock to allow hash invalidation
cp Cargo.lock.orig $name/Cargo.lock
# Packages with git dependencies generate non-default cargo configs, so # Create an empty vendor directory when there is no dependency to vendor
# always install it rather than trying to write a standard default template. mkdir -p $name
install -D $CARGO_CONFIG $name/.cargo/config; # Add the Cargo.lock to allow hash invalidation
cp Cargo.lock.orig $name/Cargo.lock
runHook postBuild # Packages with git dependencies generate non-default cargo configs, so
''; # always install it rather than trying to write a standard default template.
install -D $CARGO_CONFIG $name/.cargo/config;
# Build a reproducible tar, per instructions at https://reproducible-builds.org/docs/archives/ runHook postBuild
installPhase = '' '';
tar --owner=0 --group=0 --numeric-owner --format=gnu \
--sort=name --mtime="@$SOURCE_DATE_EPOCH" \
-czf $out $name
'';
inherit (hash_) outputHashAlgo outputHash; # Build a reproducible tar, per instructions at https://reproducible-builds.org/docs/archives/
installPhase = ''
tar --owner=0 --group=0 --numeric-owner --format=gnu \
--sort=name --mtime="@$SOURCE_DATE_EPOCH" \
-czf $out $name
'';
impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [ "NIX_CRATES_INDEX" ]; inherit (hash_) outputHashAlgo outputHash;
} // (builtins.removeAttrs args [
"name" "sha256" "cargoUpdateHook" "nativeBuildInputs" impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [ "NIX_CRATES_INDEX" ];
])) }
// (removeAttrs args removedArgs)
)

View file

@ -26,7 +26,7 @@ stdenv.mkDerivation (finalAttrs: {
}; };
cargoDeps = rustPlatform.fetchCargoTarball { cargoDeps = rustPlatform.fetchCargoTarball {
inherit (finalAttrs) pname version src; inherit (finalAttrs) src;
hash = "sha256-Q4CfDQxlhspjg7Et+0zHwZ/iSnp0CnwwpW/gT7htlL8="; hash = "sha256-Q4CfDQxlhspjg7Et+0zHwZ/iSnp0CnwwpW/gT7htlL8=";
}; };

View file

@ -26,7 +26,7 @@ stdenv.mkDerivation (finalAttrs: {
}; };
cargoDeps = rustPlatform.fetchCargoTarball { cargoDeps = rustPlatform.fetchCargoTarball {
inherit (finalAttrs) pname version src; inherit (finalAttrs) src;
hash = "sha256-YVbaXGGwQaqjUkA47ryW1VgJpZTx5ApRGdCcB5aA71M="; hash = "sha256-YVbaXGGwQaqjUkA47ryW1VgJpZTx5ApRGdCcB5aA71M=";
}; };

View file

@ -32,7 +32,7 @@ stdenv.mkDerivation (finalAttrs: {
''; '';
cargoDeps = rustPlatform.fetchCargoTarball { cargoDeps = rustPlatform.fetchCargoTarball {
inherit (finalAttrs) pname version src; inherit (finalAttrs) src;
hash = "sha256-wCJTm0W+g3+O1t1fR4maqJoxpPM0NeJG7d54MMAH33c="; hash = "sha256-wCJTm0W+g3+O1t1fR4maqJoxpPM0NeJG7d54MMAH33c=";
}; };

View file

@ -32,8 +32,7 @@ stdenv.mkDerivation rec {
}; };
cargoDeps = rustPlatform.fetchCargoTarball { cargoDeps = rustPlatform.fetchCargoTarball {
inherit src; inherit pname version src;
name = "${pname}-${version}";
hash = "sha256-FjnRI1vHA9YF/Uw2+hDtMJmeJVa5RcxaYoG4XgXa9Ds="; hash = "sha256-FjnRI1vHA9YF/Uw2+hDtMJmeJVa5RcxaYoG4XgXa9Ds=";
}; };

View file

@ -111,7 +111,7 @@ in stdenv.mkDerivation (finalAttrs: {
}; };
cargoDeps = rustPlatform.fetchCargoTarball { cargoDeps = rustPlatform.fetchCargoTarball {
inherit (finalAttrs) patches src sourceRoot version; inherit (finalAttrs) patches src sourceRoot;
name = "${finalAttrs.pname}-${finalAttrs.version}"; name = "${finalAttrs.pname}-${finalAttrs.version}";
hash = "sha256-LtQS0kH+2P4odV7BJYiH6T51+iZHAM9W9mV96rNfNWs="; hash = "sha256-LtQS0kH+2P4odV7BJYiH6T51+iZHAM9W9mV96rNfNWs=";
}; };

View file

@ -33,7 +33,7 @@ stdenv.mkDerivation (finalAttrs: {
outputs = [ "out" "dev" ]; outputs = [ "out" "dev" ];
cargoDeps = rustPlatform.fetchCargoTarball { cargoDeps = rustPlatform.fetchCargoTarball {
inherit (finalAttrs) pname version src; inherit (finalAttrs) src;
hash = "sha256-33s62iOWYh1a8ETY/fbPRxvnj8dR4/UfG8mjFyWwz5k="; hash = "sha256-33s62iOWYh1a8ETY/fbPRxvnj8dR4/UfG8mjFyWwz5k=";
}; };

View file

@ -46,7 +46,7 @@ buildPythonPackage rec {
cargoDeps = rustPlatform.fetchCargoTarball { cargoDeps = rustPlatform.fetchCargoTarball {
name = "datafusion-cargo-deps"; name = "datafusion-cargo-deps";
inherit src pname version; inherit src;
hash = "sha256-M2ZNAFWdsnN9C4+YbqFxZVH9fHR10Bimf1Xzrd9oy9E="; hash = "sha256-M2ZNAFWdsnN9C4+YbqFxZVH9fHR10Bimf1Xzrd9oy9E=";
}; };

View file

@ -21,7 +21,7 @@ buildPythonPackage rec {
}; };
cargoDeps = rustPlatform.fetchCargoTarball { cargoDeps = rustPlatform.fetchCargoTarball {
inherit pname version src; inherit src;
hash = "sha256-KrEBr998AV/bKcIoq0tX72/QwPD9bQplrS0Zw+JiSMQ="; hash = "sha256-KrEBr998AV/bKcIoq0tX72/QwPD9bQplrS0Zw+JiSMQ=";
}; };

View file

@ -37,7 +37,7 @@ stdenv.mkDerivation rec {
}; };
cargoDeps = rustPlatform.fetchCargoTarball { cargoDeps = rustPlatform.fetchCargoTarball {
inherit pname version src; inherit src;
hash = "sha256-1KzOKo5Q1uBqO3aCBYUJJxla4873AzrwoFPaNpKKFJU="; hash = "sha256-1KzOKo5Q1uBqO3aCBYUJJxla4873AzrwoFPaNpKKFJU=";
}; };