From 115e3413e1ffb822d490ed11fa0d36d5f01cc268 Mon Sep 17 00:00:00 2001 From: Winter Date: Thu, 9 Feb 2023 22:12:57 -0500 Subject: [PATCH] rustPlatform.importCargoLock: add support for v1 lock files v1 lock files (generated by default by Cargo versions 1.40 and below) use a single table, `metadata`, to store the checksums of packages. The primary motivation for doing this now is that we're considering vendoring all Cargo lock files in Nixpkgs, some packages still use it (e.g. cargo-asm), and adding support for it doesn't increase the complexity of the function. No matter the outcome of the vendoring discussion, this is a nice thing to have because Cargo still supports v1 lock files. --- pkgs/build-support/rust/import-cargo-lock.nix | 17 ++-- .../rust/test/import-cargo-lock/default.nix | 1 + .../rust/test/import-cargo-lock/v1/Cargo.lock | 85 +++++++++++++++++++ .../rust/test/import-cargo-lock/v1/Cargo.toml | 8 ++ .../test/import-cargo-lock/v1/default.nix | 18 ++++ .../test/import-cargo-lock/v1/src/main.rs | 9 ++ 6 files changed, 130 insertions(+), 8 deletions(-) create mode 100644 pkgs/build-support/rust/test/import-cargo-lock/v1/Cargo.lock create mode 100644 pkgs/build-support/rust/test/import-cargo-lock/v1/Cargo.toml create mode 100644 pkgs/build-support/rust/test/import-cargo-lock/v1/default.nix create mode 100644 pkgs/build-support/rust/test/import-cargo-lock/v1/src/main.rs diff --git a/pkgs/build-support/rust/import-cargo-lock.nix b/pkgs/build-support/rust/import-cargo-lock.nix index 42e448b8246f..4ce552debe82 100644 --- a/pkgs/build-support/rust/import-cargo-lock.nix +++ b/pkgs/build-support/rust/import-cargo-lock.nix @@ -36,7 +36,9 @@ let then builtins.readFile lockFile else args.lockFileContents; - packages = (builtins.fromTOML lockFileContents).package; + parsedLockFile = builtins.fromTOML lockFileContents; + + packages = parsedLockFile.package; # There is no source attribute for the source package itself. But # since we do not want to vendor the source package anyway, we can @@ -79,17 +81,16 @@ let # We can't use the existing fetchCrate function, since it uses a # recursive hash of the unpacked crate. fetchCrate = pkg: - assert lib.assertMsg (pkg ? checksum) '' + let + checksum = pkg.checksum or parsedLockFile.metadata."checksum ${pkg.name} ${pkg.version} (${pkg.source})"; + in + assert lib.assertMsg (checksum != null) '' Package ${pkg.name} does not have a checksum. - Please note that the Cargo.lock format where checksums used to be listed - under [metadata] is not supported. - If that is the case, running `cargo update` with a recent toolchain will - automatically update the format along with the crate's depenendencies. ''; fetchurl { name = "crate-${pkg.name}-${pkg.version}.tar.gz"; url = "https://crates.io/api/v1/crates/${pkg.name}/${pkg.version}/download"; - sha256 = pkg.checksum; + sha256 = checksum; }; # Fetch and unpack a crate. @@ -105,7 +106,7 @@ let tar xf "${crateTarball}" -C $out --strip-components=1 # Cargo is happy with largely empty metadata. - printf '{"files":{},"package":"${pkg.checksum}"}' > "$out/.cargo-checksum.json" + printf '{"files":{},"package":"${crateTarball.outputHash}"}' > "$out/.cargo-checksum.json" '' else if gitParts != null then let diff --git a/pkgs/build-support/rust/test/import-cargo-lock/default.nix b/pkgs/build-support/rust/test/import-cargo-lock/default.nix index c5c7b2f1c931..74be68b2c356 100644 --- a/pkgs/build-support/rust/test/import-cargo-lock/default.nix +++ b/pkgs/build-support/rust/test/import-cargo-lock/default.nix @@ -11,4 +11,5 @@ gitDependencyTag = callPackage ./git-dependency-tag { }; gitDependencyBranch = callPackage ./git-dependency-branch { }; maturin = callPackage ./maturin { }; + v1 = callPackage ./v1 { }; } diff --git a/pkgs/build-support/rust/test/import-cargo-lock/v1/Cargo.lock b/pkgs/build-support/rust/test/import-cargo-lock/v1/Cargo.lock new file mode 100644 index 000000000000..fe976f090aac --- /dev/null +++ b/pkgs/build-support/rust/test/import-cargo-lock/v1/Cargo.lock @@ -0,0 +1,85 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "getrandom" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.94 (registry+https://github.com/rust-lang/crates.io-index)", + "wasi 0.10.2+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libc" +version = "0.2.94" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "rand" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.94 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_chacha 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_hc 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_chacha" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ppv-lite86 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "getrandom 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_hc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "v1" +version = "0.1.0" +dependencies = [ + "rand 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum cfg-if 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +"checksum getrandom 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" +"checksum libc 0.2.94 (registry+https://github.com/rust-lang/crates.io-index)" = "18794a8ad5b29321f790b55d93dfba91e125cb1a9edbd4f8e3150acc771c1a5e" +"checksum ppv-lite86 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" +"checksum rand 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +"checksum rand_chacha 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +"checksum rand_core 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" +"checksum rand_hc 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +"checksum wasi 0.10.2+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)" = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" diff --git a/pkgs/build-support/rust/test/import-cargo-lock/v1/Cargo.toml b/pkgs/build-support/rust/test/import-cargo-lock/v1/Cargo.toml new file mode 100644 index 000000000000..4b825c45cadc --- /dev/null +++ b/pkgs/build-support/rust/test/import-cargo-lock/v1/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "v1" +version = "0.1.0" +authors = ["Daniƫl de Kok "] +edition = "2018" + +[dependencies] +rand = "0.8" diff --git a/pkgs/build-support/rust/test/import-cargo-lock/v1/default.nix b/pkgs/build-support/rust/test/import-cargo-lock/v1/default.nix new file mode 100644 index 000000000000..d13d468ae7f9 --- /dev/null +++ b/pkgs/build-support/rust/test/import-cargo-lock/v1/default.nix @@ -0,0 +1,18 @@ +{ rustPlatform }: + +rustPlatform.buildRustPackage { + pname = "v1"; + version = "0.1.0"; + + src = ./.; + + cargoLock = { + lockFile = ./Cargo.lock; + }; + + doInstallCheck = true; + + installCheckPhase = '' + $out/bin/v1 + ''; +} diff --git a/pkgs/build-support/rust/test/import-cargo-lock/v1/src/main.rs b/pkgs/build-support/rust/test/import-cargo-lock/v1/src/main.rs new file mode 100644 index 000000000000..50b4ed799e43 --- /dev/null +++ b/pkgs/build-support/rust/test/import-cargo-lock/v1/src/main.rs @@ -0,0 +1,9 @@ +use rand::Rng; + +fn main() { + let mut rng = rand::thread_rng(); + + // Always draw zero :). + let roll: u8 = rng.gen_range(0..1); + assert_eq!(roll, 0); +}