Merge pull request #137395 from dermetfan/cargo-lock-restricted

importCargoLock: introduce alternative parameter `lockFileContents`
This commit is contained in:
Daniël de Kok 2021-09-20 18:05:08 +02:00 committed by GitHub
commit 24b5074348
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 140 additions and 5 deletions

View File

@ -2,11 +2,16 @@
{
# Cargo lock file
lockFile
lockFile ? null
# Cargo lock file contents as string
, lockFileContents ? null
# Hashes for git dependencies.
, outputHashes ? {}
}:
} @ args:
assert (lockFile == null) != (lockFileContents == null);
let
# Parse a git source into different components.
@ -22,7 +27,13 @@ let
sha = builtins.elemAt parts 4;
} // lib.optionalAttrs (type != null) { inherit type value; };
packages = (builtins.fromTOML (builtins.readFile lockFile)).package;
# shadows args.lockFileContents
lockFileContents =
if lockFile != null
then builtins.readFile lockFile
else args.lockFileContents;
packages = (builtins.fromTOML lockFileContents).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
@ -144,10 +155,17 @@ let
''
else throw "Cannot handle crate source: ${pkg.source}";
vendorDir = runCommand "cargo-vendor-dir" {} ''
vendorDir = runCommand "cargo-vendor-dir" (lib.optionalAttrs (lockFile == null) {
inherit lockFileContents;
passAsFile = [ "lockFileContents" ];
}) ''
mkdir -p $out/.cargo
ln -s ${lockFile} $out/Cargo.lock
${
if lockFile != null
then "ln -s ${lockFile} $out/Cargo.lock"
else "cp $lockFileContentsPath $out/Cargo.lock"
}
cat > $out/.cargo/config <<EOF
[source.crates-io]

View File

@ -0,0 +1,83 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "basic-dynamic"
version = "0.1.0"
dependencies = [
"rand",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "getrandom"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "libc"
version = "0.2.102"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2a5ac8f984bfcf3a823267e5fde638acc3325f6496633a5da6bb6eb2171e103"
[[package]]
name = "ppv-lite86"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
[[package]]
name = "rand"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
dependencies = [
"getrandom",
]
[[package]]
name = "rand_hc"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7"
dependencies = [
"rand_core",
]
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"

View File

@ -0,0 +1,8 @@
[package]
name = "basic-dynamic"
version = "0.1.0"
authors = ["Daniël de Kok <me@danieldk.eu>"]
edition = "2018"
[dependencies]
rand = "0.8"

View File

@ -0,0 +1,16 @@
{ rustPlatform }:
rustPlatform.buildRustPackage {
pname = "basic-dynamic";
version = "0.1.0";
src = ./.;
cargoLock.lockFileContents = builtins.readFile ./Cargo.lock;
doInstallCheck = true;
installCheckPhase = ''
$out/bin/basic-dynamic
'';
}

View File

@ -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);
}

View File

@ -4,6 +4,7 @@
# $ nix-build -A tests.importCargoLock
{
basic = callPackage ./basic { };
basicDynamic = callPackage ./basic-dynamic { };
gitDependency = callPackage ./git-dependency { };
gitDependencyRev = callPackage ./git-dependency-rev { };
gitDependencyTag = callPackage ./git-dependency-tag { };