Skip to content

Commit 29cca09

Browse files
authored
rustPlatform.fetchCargoTarball: support pname+version (#332975)
2 parents 71f01ce + e273bc8 commit 29cca09

File tree

12 files changed

+169
-134
lines changed

12 files changed

+169
-134
lines changed

doc/languages-frameworks/rust.section.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -567,8 +567,7 @@ buildPythonPackage rec {
567567
};
568568
569569
cargoDeps = rustPlatform.fetchCargoTarball {
570-
inherit src sourceRoot;
571-
name = "${pname}-${version}";
570+
inherit pname version src sourceRoot;
572571
hash = "sha256-miW//pnOmww2i6SOGbkrAIdc/JMDT4FJLqdMFojZeoY=";
573572
};
574573
@@ -611,9 +610,8 @@ buildPythonPackage rec {
611610
};
612611
613612
cargoDeps = rustPlatform.fetchCargoTarball {
614-
inherit src;
613+
inherit pname version src;
615614
sourceRoot = "${pname}-${version}/${cargoRoot}";
616-
name = "${pname}-${version}";
617615
hash = "sha256-PS562W4L1NimqDV2H0jl5vYhL08H9est/pbIxSdYVfo=";
618616
};
619617
@@ -652,8 +650,7 @@ buildPythonPackage rec {
652650
};
653651
654652
cargoDeps = rustPlatform.fetchCargoTarball {
655-
inherit src;
656-
name = "${pname}-${version}";
653+
inherit pname version src;
657654
hash = "sha256-heOBK8qi2nuc/Ib+I/vLzZ1fUUD/G/KTw9d7M4Hz5O0=";
658655
};
659656
@@ -697,8 +694,7 @@ stdenv.mkDerivation rec {
697694
};
698695
699696
cargoDeps = rustPlatform.fetchCargoTarball {
700-
inherit src;
701-
name = "${pname}-${version}";
697+
inherit pname version src;
702698
hash = "sha256-8fa3fa+sFi5H+49B5sr2vYPkp9C9s6CcE0zv4xB8gww=";
703699
};
704700

pkgs/applications/audio/gnome-podcasts/default.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ stdenv.mkDerivation rec {
3434
};
3535

3636
cargoDeps = rustPlatform.fetchCargoTarball {
37-
inherit pname version src;
37+
inherit src;
3838
hash = "sha256-XTfKqKs7874ak7Lzscxw8E2qcnJOWMZaaol8TpIB6Vw=";
3939
};
4040

Lines changed: 155 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,156 @@
1-
{ lib, stdenv, cacert, git, cargo, python3 }:
2-
let cargo-vendor-normalise = stdenv.mkDerivation {
3-
name = "cargo-vendor-normalise";
4-
src = ./cargo-vendor-normalise.py;
5-
nativeBuildInputs = [ python3.pkgs.wrapPython ];
6-
dontUnpack = true;
7-
installPhase = "install -D $src $out/bin/cargo-vendor-normalise";
8-
pythonPath = [ python3.pkgs.toml ];
9-
postFixup = "wrapPythonPrograms";
10-
doInstallCheck = true;
11-
installCheckPhase = ''
12-
# check that ../fetchcargo-default-config.toml is a fix point
13-
reference=${../fetchcargo-default-config.toml}
14-
< $reference $out/bin/cargo-vendor-normalise > test;
15-
cmp test $reference
16-
'';
17-
preferLocalBuild = true;
18-
};
1+
{
2+
lib,
3+
stdenv,
4+
cacert,
5+
git,
6+
cargo,
7+
python3,
8+
}:
9+
let
10+
cargo-vendor-normalise = stdenv.mkDerivation {
11+
name = "cargo-vendor-normalise";
12+
src = ./cargo-vendor-normalise.py;
13+
nativeBuildInputs = [ python3.pkgs.wrapPython ];
14+
dontUnpack = true;
15+
installPhase = "install -D $src $out/bin/cargo-vendor-normalise";
16+
pythonPath = [ python3.pkgs.toml ];
17+
postFixup = "wrapPythonPrograms";
18+
doInstallCheck = true;
19+
installCheckPhase = ''
20+
# check that ../fetchcargo-default-config.toml is a fix point
21+
reference=${../fetchcargo-default-config.toml}
22+
< $reference $out/bin/cargo-vendor-normalise > test;
23+
cmp test $reference
24+
'';
25+
preferLocalBuild = true;
26+
};
1927
in
20-
{ name ? "cargo-deps"
21-
, src ? null
22-
, srcs ? []
23-
, patches ? []
24-
, sourceRoot ? ""
25-
, cargoUpdateHook ? ""
26-
, nativeBuildInputs ? []
27-
, ...
28-
} @ args:
29-
30-
let hash_ =
31-
if args ? hash then
32-
{
33-
outputHashAlgo = if args.hash == "" then "sha256" else null;
34-
outputHash = args.hash;
35-
}
36-
else if args ? sha256 then { outputHashAlgo = "sha256"; outputHash = args.sha256; }
37-
else throw "fetchCargoTarball requires a hash for ${name}";
38-
in stdenv.mkDerivation ({
39-
name = "${name}-vendor.tar.gz";
40-
nativeBuildInputs = [ cacert git cargo-vendor-normalise cargo ] ++ nativeBuildInputs;
41-
42-
buildPhase = ''
43-
runHook preBuild
44-
45-
# Ensure deterministic Cargo vendor builds
46-
export SOURCE_DATE_EPOCH=1
47-
48-
if [[ ! -f Cargo.lock ]]; then
49-
echo
50-
echo "ERROR: The Cargo.lock file doesn't exist"
51-
echo
52-
echo "Cargo.lock is needed to make sure that cargoHash/cargoSha256 doesn't change"
53-
echo "when the registry is updated."
54-
echo
55-
56-
exit 1
57-
fi
58-
59-
# Keep the original around for copyLockfile
60-
cp Cargo.lock Cargo.lock.orig
61-
62-
export CARGO_HOME=$(mktemp -d cargo-home.XXX)
63-
CARGO_CONFIG=$(mktemp cargo-config.XXXX)
64-
65-
if [[ -n "$NIX_CRATES_INDEX" ]]; then
66-
cat >$CARGO_HOME/config.toml <<EOF
67-
[source.crates-io]
68-
replace-with = 'mirror'
69-
[source.mirror]
70-
registry = "$NIX_CRATES_INDEX"
71-
EOF
72-
fi
73-
74-
${cargoUpdateHook}
75-
76-
# Override the `http.cainfo` option usually specified in `.cargo/config`.
77-
export CARGO_HTTP_CAINFO=${cacert}/etc/ssl/certs/ca-bundle.crt
78-
79-
if grep '^source = "git' Cargo.lock; then
80-
echo
81-
echo "ERROR: The Cargo.lock contains git dependencies"
82-
echo
83-
echo "This is currently not supported in the fixed-output derivation fetcher."
84-
echo "Use cargoLock.lockFile / importCargoLock instead."
85-
echo
86-
87-
exit 1
88-
fi
89-
90-
cargo vendor $name --respect-source-config | cargo-vendor-normalise > $CARGO_CONFIG
91-
92-
# Create an empty vendor directory when there is no dependency to vendor
93-
mkdir -p $name
94-
# Add the Cargo.lock to allow hash invalidation
95-
cp Cargo.lock.orig $name/Cargo.lock
96-
97-
# Packages with git dependencies generate non-default cargo configs, so
98-
# always install it rather than trying to write a standard default template.
99-
install -D $CARGO_CONFIG $name/.cargo/config;
100-
101-
runHook postBuild
102-
'';
103-
104-
# Build a reproducible tar, per instructions at https://reproducible-builds.org/docs/archives/
105-
installPhase = ''
106-
tar --owner=0 --group=0 --numeric-owner --format=gnu \
107-
--sort=name --mtime="@$SOURCE_DATE_EPOCH" \
108-
-czf $out $name
109-
'';
110-
111-
inherit (hash_) outputHashAlgo outputHash;
112-
113-
impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [ "NIX_CRATES_INDEX" ];
114-
} // (builtins.removeAttrs args [
115-
"name" "sha256" "cargoUpdateHook" "nativeBuildInputs"
116-
]))
28+
{
29+
pname ? null,
30+
version ? null,
31+
name ? if args ? pname && args ? version then "${pname}-${version}" else "cargo-deps",
32+
src ? null,
33+
srcs ? [ ],
34+
patches ? [ ],
35+
sourceRoot ? "",
36+
cargoUpdateHook ? "",
37+
nativeBuildInputs ? [ ],
38+
...
39+
}@args:
40+
41+
assert lib.assertMsg (
42+
(args ? pname || args ? version) -> !(args ? name)
43+
) "Either specify `pname` with `version`, or specify `name` only, not a mix of both.";
44+
assert lib.assertMsg (
45+
args ? pname == args ? version
46+
) "If `pname` is specified, `version` must be also, and vice versa.";
47+
let
48+
# args to remove from the final call to stdenv.mkDerivation, as we've already handled them
49+
removedArgs = [
50+
"name"
51+
"pname"
52+
"version"
53+
"sha256"
54+
"cargoUpdateHook"
55+
"nativeBuildInputs"
56+
];
57+
58+
hash_ =
59+
if args ? hash then
60+
{
61+
outputHashAlgo = if args.hash == "" then "sha256" else null;
62+
outputHash = args.hash;
63+
}
64+
else if args ? sha256 then
65+
{
66+
outputHashAlgo = "sha256";
67+
outputHash = args.sha256;
68+
}
69+
else
70+
throw "fetchCargoTarball requires a hash for ${name}";
71+
in
72+
stdenv.mkDerivation (
73+
{
74+
name = "${name}-vendor.tar.gz";
75+
nativeBuildInputs = [
76+
cacert
77+
git
78+
cargo-vendor-normalise
79+
cargo
80+
] ++ nativeBuildInputs;
81+
82+
buildPhase = ''
83+
runHook preBuild
84+
85+
# Ensure deterministic Cargo vendor builds
86+
export SOURCE_DATE_EPOCH=1
87+
88+
if [[ ! -f Cargo.lock ]]; then
89+
echo
90+
echo "ERROR: The Cargo.lock file doesn't exist"
91+
echo
92+
echo "Cargo.lock is needed to make sure that cargoHash/cargoSha256 doesn't change"
93+
echo "when the registry is updated."
94+
echo
95+
96+
exit 1
97+
fi
98+
99+
# Keep the original around for copyLockfile
100+
cp Cargo.lock Cargo.lock.orig
101+
102+
export CARGO_HOME=$(mktemp -d cargo-home.XXX)
103+
CARGO_CONFIG=$(mktemp cargo-config.XXXX)
104+
105+
if [[ -n "$NIX_CRATES_INDEX" ]]; then
106+
cat >$CARGO_HOME/config.toml <<EOF
107+
[source.crates-io]
108+
replace-with = 'mirror'
109+
[source.mirror]
110+
registry = "$NIX_CRATES_INDEX"
111+
EOF
112+
fi
113+
114+
${cargoUpdateHook}
115+
116+
# Override the `http.cainfo` option usually specified in `.cargo/config`.
117+
export CARGO_HTTP_CAINFO=${cacert}/etc/ssl/certs/ca-bundle.crt
118+
119+
if grep '^source = "git' Cargo.lock; then
120+
echo
121+
echo "ERROR: The Cargo.lock contains git dependencies"
122+
echo
123+
echo "This is currently not supported in the fixed-output derivation fetcher."
124+
echo "Use cargoLock.lockFile / importCargoLock instead."
125+
echo
126+
127+
exit 1
128+
fi
129+
130+
cargo vendor $name --respect-source-config | cargo-vendor-normalise > $CARGO_CONFIG
131+
132+
# Create an empty vendor directory when there is no dependency to vendor
133+
mkdir -p $name
134+
# Add the Cargo.lock to allow hash invalidation
135+
cp Cargo.lock.orig $name/Cargo.lock
136+
137+
# Packages with git dependencies generate non-default cargo configs, so
138+
# always install it rather than trying to write a standard default template.
139+
install -D $CARGO_CONFIG $name/.cargo/config;
140+
141+
runHook postBuild
142+
'';
143+
144+
# Build a reproducible tar, per instructions at https://reproducible-builds.org/docs/archives/
145+
installPhase = ''
146+
tar --owner=0 --group=0 --numeric-owner --format=gnu \
147+
--sort=name --mtime="@$SOURCE_DATE_EPOCH" \
148+
-czf $out $name
149+
'';
150+
151+
inherit (hash_) outputHashAlgo outputHash;
152+
153+
impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [ "NIX_CRATES_INDEX" ];
154+
}
155+
// (removeAttrs args removedArgs)
156+
)

pkgs/by-name/ch/chance/package.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ stdenv.mkDerivation (finalAttrs: {
2626
};
2727

2828
cargoDeps = rustPlatform.fetchCargoTarball {
29-
inherit (finalAttrs) pname version src;
29+
inherit (finalAttrs) src;
3030
hash = "sha256-Q4CfDQxlhspjg7Et+0zHwZ/iSnp0CnwwpW/gT7htlL8=";
3131
};
3232

pkgs/by-name/ge/geopard/package.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ stdenv.mkDerivation (finalAttrs: {
2626
};
2727

2828
cargoDeps = rustPlatform.fetchCargoTarball {
29-
inherit (finalAttrs) pname version src;
29+
inherit (finalAttrs) src;
3030
hash = "sha256-YVbaXGGwQaqjUkA47ryW1VgJpZTx5ApRGdCcB5aA71M=";
3131
};
3232

pkgs/by-name/ke/key-rack/package.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ stdenv.mkDerivation (finalAttrs: {
3232
'';
3333

3434
cargoDeps = rustPlatform.fetchCargoTarball {
35-
inherit (finalAttrs) pname version src;
35+
inherit (finalAttrs) src;
3636
hash = "sha256-wCJTm0W+g3+O1t1fR4maqJoxpPM0NeJG7d54MMAH33c=";
3737
};
3838

pkgs/by-name/mo/mousai/package.nix

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ stdenv.mkDerivation rec {
3232
};
3333

3434
cargoDeps = rustPlatform.fetchCargoTarball {
35-
inherit src;
36-
name = "${pname}-${version}";
35+
inherit pname version src;
3736
hash = "sha256-FjnRI1vHA9YF/Uw2+hDtMJmeJVa5RcxaYoG4XgXa9Ds=";
3837
};
3938

pkgs/by-name/su/surrealist/package.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ in stdenv.mkDerivation (finalAttrs: {
111111
};
112112

113113
cargoDeps = rustPlatform.fetchCargoTarball {
114-
inherit (finalAttrs) patches src sourceRoot version;
114+
inherit (finalAttrs) patches src sourceRoot;
115115
name = "${finalAttrs.pname}-${finalAttrs.version}";
116116
hash = "sha256-LtQS0kH+2P4odV7BJYiH6T51+iZHAM9W9mV96rNfNWs=";
117117
};

pkgs/development/libraries/libkrun/default.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ stdenv.mkDerivation (finalAttrs: {
3333
outputs = [ "out" "dev" ];
3434

3535
cargoDeps = rustPlatform.fetchCargoTarball {
36-
inherit (finalAttrs) pname version src;
36+
inherit (finalAttrs) src;
3737
hash = "sha256-33s62iOWYh1a8ETY/fbPRxvnj8dR4/UfG8mjFyWwz5k=";
3838
};
3939

pkgs/development/python-modules/datafusion/default.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ buildPythonPackage rec {
4646

4747
cargoDeps = rustPlatform.fetchCargoTarball {
4848
name = "datafusion-cargo-deps";
49-
inherit src pname version;
49+
inherit src;
5050
hash = "sha256-M2ZNAFWdsnN9C4+YbqFxZVH9fHR10Bimf1Xzrd9oy9E=";
5151
};
5252

0 commit comments

Comments
 (0)