diff --git a/pkgs/build-support/rust/import-cargo-lock.nix b/pkgs/build-support/rust/import-cargo-lock.nix index 3973aff398ab..fe070e9638d4 100644 --- a/pkgs/build-support/rust/import-cargo-lock.nix +++ b/pkgs/build-support/rust/import-cargo-lock.nix @@ -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 <"] +edition = "2018" + +[dependencies] +rand = "0.8" diff --git a/pkgs/build-support/rust/test/import-cargo-lock/basic-dynamic/default.nix b/pkgs/build-support/rust/test/import-cargo-lock/basic-dynamic/default.nix new file mode 100644 index 000000000000..eea2c3760599 --- /dev/null +++ b/pkgs/build-support/rust/test/import-cargo-lock/basic-dynamic/default.nix @@ -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 + ''; +} diff --git a/pkgs/build-support/rust/test/import-cargo-lock/basic-dynamic/src/main.rs b/pkgs/build-support/rust/test/import-cargo-lock/basic-dynamic/src/main.rs new file mode 100644 index 000000000000..50b4ed799e43 --- /dev/null +++ b/pkgs/build-support/rust/test/import-cargo-lock/basic-dynamic/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); +} 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 95f1711d2da7..24e07099c058 100644 --- a/pkgs/build-support/rust/test/import-cargo-lock/default.nix +++ b/pkgs/build-support/rust/test/import-cargo-lock/default.nix @@ -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 { };