From b3969f3ad793ee98af14cf0c649f626520943a53 Mon Sep 17 00:00:00 2001 From: Daniƫl de Kok Date: Sat, 8 May 2021 07:44:31 +0200 Subject: rustPlatform.buildRustPackage: support direct use of Cargo.lock This change introduces the cargoLock argument to buildRustPackage, which can be used in place of cargo{Sha256,Hash} or cargoVendorDir. It uses the importCargoLock function to build the vendor directory. Differences compared to cargo{Sha256,Hash}: - Requires a Cargo.lock file. - Does not require a Cargo hash. - Retrieves all dependencies as fixed-output derivations. This makes buildRustPackage much easier to use as part of a Rust project, since it does not require updating cargo{Sha256,Hash} for every change to the lock file. --- pkgs/build-support/rust/default.nix | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'pkgs/build-support/rust') diff --git a/pkgs/build-support/rust/default.nix b/pkgs/build-support/rust/default.nix index ff9ca642daa..711276116a6 100644 --- a/pkgs/build-support/rust/default.nix +++ b/pkgs/build-support/rust/default.nix @@ -7,6 +7,7 @@ , cargoInstallHook , cargoSetupHook , fetchCargoTarball +, importCargoLock , runCommandNoCC , rustPlatform , callPackage @@ -41,6 +42,7 @@ , cargoDepsHook ? "" , buildType ? "release" , meta ? {} +, cargoLock ? null , cargoVendorDir ? null , checkType ? buildType , depsExtraArgs ? {} @@ -55,19 +57,22 @@ , buildAndTestSubdir ? null , ... } @ args: -assert cargoVendorDir == null -> !(cargoSha256 == "" && cargoHash == ""); +assert cargoVendorDir == null && cargoLock == null -> cargoSha256 == "" && cargoHash == "" + -> throw "cargoSha256, cargoHash, cargoVendorDir, or cargoLock must be set"; assert buildType == "release" || buildType == "debug"; let - cargoDeps = if cargoVendorDir == null - then fetchCargoTarball ({ - inherit src srcs sourceRoot unpackPhase cargoUpdateHook; - name = cargoDepsName; - hash = cargoHash; - patches = cargoPatches; - sha256 = cargoSha256; - } // depsExtraArgs) + cargoDeps = + if cargoVendorDir == null + then if cargoLock != null then importCargoLock cargoLock + else fetchCargoTarball ({ + inherit src srcs sourceRoot unpackPhase cargoUpdateHook; + name = cargoDepsName; + hash = cargoHash; + patches = cargoPatches; + sha256 = cargoSha256; + } // depsExtraArgs) else null; # If we have a cargoSha256 fixed-output derivation, validate it at build time @@ -96,7 +101,7 @@ in # See https://os.phil-opp.com/testing/ for more information. assert useSysroot -> !(args.doCheck or true); -stdenv.mkDerivation ((removeAttrs args ["depsExtraArgs"]) // lib.optionalAttrs useSysroot { +stdenv.mkDerivation ((removeAttrs args [ "depsExtraArgs" "cargoLock" ]) // lib.optionalAttrs useSysroot { RUSTFLAGS = "--sysroot ${sysroot} " + (args.RUSTFLAGS or ""); } // { inherit buildAndTestSubdir cargoDeps; -- cgit 1.4.1