Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustPlatform.importCargoLock: add support for v1 lock files #215629

Merged
merged 1 commit into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions pkgs/build-support/rust/import-cargo-lock.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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})";
figsoda marked this conversation as resolved.
Show resolved Hide resolved
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.
'';
Comment on lines +87 to 89
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I should adjust the message to say something like "the lock file may be corrupted, you can generate one with ...", instead of now just leaving it vague?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also does beg the question of why v1 support was intentionally omitted from the initial implementation -- is there some issue we may run into? I haven't run into anything in my limited testing, at least, so I hope that's not it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving it as it is lgtm. I think suggesting running cargo update could be helpful, but I don't think we should say it might be corrupted, since the issue is probably more likely to be impoCargoLock instead of Cargo.lock

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.
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkgs/build-support/rust/test/import-cargo-lock/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
gitDependencyTag = callPackage ./git-dependency-tag { };
gitDependencyBranch = callPackage ./git-dependency-branch { };
maturin = callPackage ./maturin { };
v1 = callPackage ./v1 { };
}
85 changes: 85 additions & 0 deletions pkgs/build-support/rust/test/import-cargo-lock/v1/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions pkgs/build-support/rust/test/import-cargo-lock/v1/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "v1"
version = "0.1.0"
authors = ["Daniël de Kok <[email protected]>"]
edition = "2018"

[dependencies]
rand = "0.8"
18 changes: 18 additions & 0 deletions pkgs/build-support/rust/test/import-cargo-lock/v1/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{ rustPlatform }:

rustPlatform.buildRustPackage {
pname = "v1";
version = "0.1.0";

src = ./.;

cargoLock = {
lockFile = ./Cargo.lock;
};

doInstallCheck = true;

installCheckPhase = ''
$out/bin/v1
'';
}
9 changes: 9 additions & 0 deletions pkgs/build-support/rust/test/import-cargo-lock/v1/src/main.rs
Original file line number Diff line number Diff line change
@@ -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);
}