From d3018c4522053afbb58cc067d4b9dec17bc034c1 Mon Sep 17 00:00:00 2001 From: Robin Stumm Date: Fri, 10 Sep 2021 19:09:29 +0200 Subject: [PATCH] importCargoLock: introduce alternative parameter `lockFileContents` In restricted mode (and therefore with flakes) `builtins.readFile` may not be the result of `builtins.toFile`, making it impossible to use a generated lockFile (with or without IFD), and thereby causing evaluation to fail if `system != builtins.currentSystem` on Hydra so the jobs are not delegated to eligible build machines that support that system. This is done in a way that avoids rebuilds. --- pkgs/build-support/rust/import-cargo-lock.nix | 28 +++++-- .../basic-dynamic/Cargo.lock | 83 +++++++++++++++++++ .../basic-dynamic/Cargo.toml | 8 ++ .../basic-dynamic/default.nix | 16 ++++ .../basic-dynamic/src/main.rs | 9 ++ .../rust/test/import-cargo-lock/default.nix | 1 + 6 files changed, 140 insertions(+), 5 deletions(-) create mode 100644 pkgs/build-support/rust/test/import-cargo-lock/basic-dynamic/Cargo.lock create mode 100644 pkgs/build-support/rust/test/import-cargo-lock/basic-dynamic/Cargo.toml create mode 100644 pkgs/build-support/rust/test/import-cargo-lock/basic-dynamic/default.nix create mode 100644 pkgs/build-support/rust/test/import-cargo-lock/basic-dynamic/src/main.rs diff --git a/pkgs/build-support/rust/import-cargo-lock.nix b/pkgs/build-support/rust/import-cargo-lock.nix index 83f4e0df4f2d..860e6fb99709 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. @@ -21,7 +26,13 @@ let sha = builtins.elemAt parts 3; } // lib.optionalAttrs (rev != null) { inherit rev; }; - 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 @@ -143,10 +154,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 2dd525a8ac3f..e3a94a758c48 100644 --- a/pkgs/build-support/rust/test/import-cargo-lock/default.nix +++ b/pkgs/build-support/rust/test/import-cargo-lock/default.nix @@ -2,6 +2,7 @@ { basic = callPackage ./basic { }; + basicDynamic = callPackage ./basic-dynamic { }; gitDependency = callPackage ./git-dependency { }; gitDependencyNoRev = callPackage ./git-dependency-no-rev { }; maturin = callPackage ./maturin { };