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
4 changes: 4 additions & 0 deletions doc/languages-frameworks/rust.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ rustPlatform.buildRustPackage rec {
};

cargoSha256 = "0q68qyl2h6i0qsz82z840myxlnjay8p1w5z7hfyr8fqp7wgwa9cx";
verifyCargoDeps = true;

meta = with stdenv.lib; {
description = "A fast line-oriented regex search tool, similar to ag and ack";
Expand All @@ -64,6 +65,9 @@ When the `Cargo.lock`, provided by upstream, is not in sync with the
added in `cargoPatches` will also be prepended to the patches in `patches` at
build-time.

When `verifyCargoDeps` is set to `true`, the build will also verify that the
`cargoSha256` is not out of date by comparing the `Cargo.lock` file in both the `cargoDeps` and `src`. Note that this option changes the value of `cargoSha256` since it also copies the `Cargo.lock` in it. To avoid breaking backward-compatibility this option is not enabled by default but hopefully will be in the future.

## Compiling Rust crates using Nix instead of Cargo

### Simple operation
Expand Down
19 changes: 19 additions & 0 deletions pkgs/build-support/rust/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
, cargoUpdateHook ? ""
, cargoDepsHook ? ""
, cargoBuildFlags ? []
, # Set to true to verify if the cargo dependencies are up to date.
# This will change the value of cargoSha256.
verifyCargoDeps ? false
, buildType ? "release"
, meta ? {}

Expand All @@ -26,6 +29,7 @@ let
cargoDeps = if cargoVendorDir == null
then fetchcargo {
inherit name src srcs sourceRoot cargoUpdateHook;
copyLockfile = verifyCargoDeps;
patches = cargoPatches;
sha256 = cargoSha256;
}
Expand Down Expand Up @@ -95,6 +99,21 @@ stdenv.mkDerivation (args // {

unset cargoDepsCopy
export RUST_LOG=${logLevel}
'' + stdenv.lib.optionalString verifyCargoDeps ''
if ! diff source/Cargo.lock $cargoDeps/Cargo.lock ; then
echo
echo "ERROR: cargoSha256 is out of date."
echo
echo "Cargo.lock is not the same in $cargoDeps."
echo
echo "To fix the issue:"
echo '1. Use "1111111111111111111111111111111111111111111111111111" as the cargoSha256 value'
echo "2. Build the derivation and wait it to fail with a hash mismatch"
echo "3. Copy the 'got: sha256:' value back into the cargoSha256 field"
echo

exit 1
fi
'' + (args.postUnpack or "");

configurePhase = args.configurePhase or ''
Expand Down
18 changes: 17 additions & 1 deletion pkgs/build-support/rust/fetchcargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ let cargo-vendor-normalise = stdenv.mkDerivation {
preferLocalBuild = true;
};
in
{ name ? "cargo-deps", src, srcs, patches, sourceRoot, sha256, cargoUpdateHook ? "" }:
{ name ? "cargo-deps"
, src
, srcs
, patches
, sourceRoot
, sha256
, cargoUpdateHook ? ""
, # whenever to also include the Cargo.lock in the output
copyLockfile ? false
}:
stdenv.mkDerivation {
name = "${name}-vendor";
nativeBuildInputs = [ cacert git cargo-vendor-normalise cargo ];
Expand All @@ -37,6 +46,9 @@ stdenv.mkDerivation {
exit 1
fi

# Keep the original around for copyLockfile
cp Cargo.lock Cargo.lock.orig

export CARGO_HOME=$(mktemp -d cargo-home.XXX)
CARGO_CONFIG=$(mktemp cargo-config.XXXX)

Expand All @@ -52,6 +64,10 @@ stdenv.mkDerivation {
if ! cmp $CARGO_CONFIG ${./fetchcargo-default-config.toml} > /dev/null; then
install -D $CARGO_CONFIG $out/.cargo/config;
fi;

'' + stdenv.lib.optionalString copyLockfile ''
# add the Cargo.lock to allow hash invalidation
cp Cargo.lock.orig $out/Cargo.lock
'';

outputHashAlgo = "sha256";
Expand Down
3 changes: 2 additions & 1 deletion pkgs/development/tools/documentation/mdsh/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ rustPlatform.buildRustPackage rec {
sha256 = "0m3f5mrdmnmkfsy7mc6x3jf4ainmq0z42mv935ikcdbjwwjbd5gq";
};

cargoSha256 = "11kzl0ns84xhdacn0k7nilgzgpwazmaaqdjf2kcarxf2h01b0rjv";
cargoSha256 = "0adc525hbs7j7zasvcdxy7k6pf145hs711hjbzgwf72k514ypyrl";
verifyCargoDeps = true;

meta = with stdenv.lib; {
description = "Markdown shell pre-processor";
Expand Down