Skip to content
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
25 changes: 22 additions & 3 deletions doc/languages-frameworks/rust.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,18 @@ hash using `nix-hash --to-sri --type sha256 "<original sha256>"`.
```

Exception: If the application has cargo `git` dependencies, the `cargoHash`
approach will not work, and you will need to copy the `Cargo.lock` file of the application
to nixpkgs and continue with the next section for specifying the options of the `cargoLock`
section.
approach will not work by default. In this case, you can set `useFetchCargoVendor = true`
to use an improved fetcher that supports handling `git` dependencies.

```nix
{
useFetchCargoVendor = true;
cargoHash = "sha256-RqPVFovDaD2rW31HyETJfQ0qVwFxoGEvqkIgag3H6KU=";
}
```

If this method still does not work, you can resort to copying the `Cargo.lock` file into nixpkgs
and importing it as described in the [next section](#importing-a-cargo.lock-file).

Both types of hashes are permitted when contributing to nixpkgs. The
Cargo hash is obtained by inserting a fake checksum into the
Expand Down Expand Up @@ -462,6 +470,17 @@ also be used:
the `Cargo.lock`/`Cargo.toml` files need to be patched before
vendoring.

In case the lockfile contains cargo `git` dependencies, you can use
`fetchCargoVendor` instead.
```nix
{
cargoDeps = rustPlatform.fetchCargoVendor {
inherit src;
hash = "sha256-RqPVFovDaD2rW31HyETJfQ0qVwFxoGEvqkIgag3H6KU=";
};
}
```

If a `Cargo.lock` file is available, you can alternatively use the
`importCargoLock` function. In contrast to `fetchCargoTarball`, this
function does not require a hash (unless git dependencies are used)
Expand Down
21 changes: 16 additions & 5 deletions pkgs/build-support/rust/build-rust-package/default.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{ lib
, importCargoLock
, fetchCargoTarball
, fetchCargoVendor
, stdenv
, callPackage
, cargoBuildHook
Expand Down Expand Up @@ -36,6 +37,8 @@
, cargoDepsHook ? ""
, buildType ? "release"
, meta ? {}
, useFetchCargoVendor ? false
, cargoDeps ? null
, cargoLock ? null
, cargoVendorDir ? null
, checkType ? buildType
Expand All @@ -58,15 +61,22 @@
, buildAndTestSubdir ? null
, ... } @ args:

assert cargoVendorDir == null && cargoLock == null
assert cargoVendorDir == null && cargoDeps == null && cargoLock == null
-> !(args ? cargoSha256 && args.cargoSha256 != null) && !(args ? cargoHash && args.cargoHash != null)
-> throw "cargoHash, cargoVendorDir, or cargoLock must be set";
-> throw "cargoHash, cargoVendorDir, cargoDeps, or cargoLock must be set";

let

cargoDeps =
cargoDeps' =
if cargoVendorDir != null then null
else if cargoDeps != null then cargoDeps
else if cargoLock != null then importCargoLock cargoLock
else if useFetchCargoVendor then fetchCargoVendor ({
inherit src srcs sourceRoot preUnpack unpackPhase postUnpack;
name = cargoDepsName;
patches = cargoPatches;
hash = args.cargoHash;
} // depsExtraArgs)
else fetchCargoTarball ({
inherit src srcs sourceRoot preUnpack unpackPhase postUnpack cargoUpdateHook;
name = cargoDepsName;
Expand Down Expand Up @@ -94,15 +104,16 @@ in
# See https://os.phil-opp.com/testing/ for more information.
assert useSysroot -> !(args.doCheck or true);

stdenv.mkDerivation ((removeAttrs args [ "depsExtraArgs" "cargoUpdateHook" "cargoLock" ]) // lib.optionalAttrs useSysroot {
stdenv.mkDerivation ((removeAttrs args [ "depsExtraArgs" "cargoUpdateHook" "cargoDeps" "cargoLock" ]) // lib.optionalAttrs useSysroot {
RUSTFLAGS = "--sysroot ${sysroot} " + (args.RUSTFLAGS or "");
} // lib.optionalAttrs (stdenv.isDarwin && buildType == "debug") {
RUSTFLAGS =
"-C split-debuginfo=packed "
+ lib.optionalString useSysroot "--sysroot ${sysroot} "
+ (args.RUSTFLAGS or "");
} // {
inherit buildAndTestSubdir cargoDeps;
cargoDeps = cargoDeps';
inherit buildAndTestSubdir;

cargoBuildType = buildType;

Expand Down
5 changes: 3 additions & 2 deletions pkgs/build-support/rust/fetch-cargo-tarball/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ stdenv.mkDerivation (
echo
echo "ERROR: The Cargo.lock contains git dependencies"
echo
echo "This is currently not supported in the fixed-output derivation fetcher."
echo "Use cargoLock.lockFile / importCargoLock instead."
echo "This is not supported in the default fixed-output derivation fetcher."
echo "Set \`useFetchCargoVendor = true\` / use fetchCargoVendor"
echo "or use cargoLock.lockFile / importCargoLock instead."
echo

exit 1
Expand Down
Loading