From b66ef28841319bf1a281bde4e97c82458839a483 Mon Sep 17 00:00:00 2001 From: Justin Humm Date: Mon, 20 Aug 2018 20:30:02 +0200 Subject: [PATCH] buildRustPackage, fetchcargo: optionally use vendor config from cargo-vendor By setting useRealVendorConfig explicitly to true, the actual (slightly modified) config generated by cargo-vendor is used. This solves a problem, where the static vendor config in pkgs/build-support/rust/default.nix would not sufficiently replace all crates Cargo is looking for. As useRealVendorConfig (and writeVendorConfig in fetchcargo) default to false, there should be no breakage in existing cargoSha256 hashes. Nethertheless, imho using this new feature should become standard. A possible deprecation path could be: - introduce this patch - set useRealVendorConfig explicitly to false whereever cargoSha256 is set but migration is not wanted yet. - after some time, let writeVendorConfig default to true - when useRealVendorConfig is true everywhere cargoSha256 is set and enough time is passed, `assert cargoVendorDir == null -> useRealVendorConfig;`, remove old behaviour - after some time, remove all appearences of useRealVendorConfig and the parameter itself --- doc/languages-frameworks/rust.section.md | 11 ++++++++- nixos/doc/manual/release-notes/rl-1809.xml | 21 +++++++++++++++++ pkgs/build-support/rust/default.nix | 26 ++++++++++++++++------ pkgs/build-support/rust/fetchcargo.nix | 10 +++++---- 4 files changed, 56 insertions(+), 12 deletions(-) diff --git a/doc/languages-frameworks/rust.section.md b/doc/languages-frameworks/rust.section.md index 6588281878a0..737759fd8bd8 100644 --- a/doc/languages-frameworks/rust.section.md +++ b/doc/languages-frameworks/rust.section.md @@ -42,7 +42,8 @@ rustPlatform.buildRustPackage rec { sha256 = "0y5d1n6hkw85jb3rblcxqas2fp82h3nghssa4xqrhqnz25l799pj"; }; - cargoSha256 = "0q68qyl2h6i0qsz82z840myxlnjay8p1w5z7hfyr8fqp7wgwa9cx"; + cargoSha256 = "194lzn9xjkc041lmqnm676ydgbhn45xx9jhrhz6lzlg99yr6b880"; + useRealVendorConfig = true; meta = with stdenv.lib; { description = "A fast line-oriented regex search tool, similar to ag and ack"; @@ -64,6 +65,14 @@ 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. +The `useRealVendorConfig` parameter tells cargo-vendor to include a Cargo +configuration file in the fetched dependencies. This will fix problems with +projects, where Crates are downloaded from non-crates.io sources. Please note, +that currently this parameter defaults to `false` only due to compatibility +reasons, as setting this to `true` requires a change in `cargoSha256`. +Eventually this distinction will be deprecated, so please always set +`useRealVendorConfig` to `true` and make sure to recompute the `cargoSha256`. + To install crates with nix there is also an experimental project called [nixcrates](https://github.com/fractalide/nixcrates). diff --git a/nixos/doc/manual/release-notes/rl-1809.xml b/nixos/doc/manual/release-notes/rl-1809.xml index 01421fc5dda7..dd04996925bb 100644 --- a/nixos/doc/manual/release-notes/rl-1809.xml +++ b/nixos/doc/manual/release-notes/rl-1809.xml @@ -551,6 +551,27 @@ inherit (pkgs.nixos { stdenv.buildPlatform, stdenv.hostPlatform, and stdenv.targetPlatform. + + + The buildRustPackage function got the new + argument useRealVendorConfig, currently + defaulting to false. Setting it to + true necessates the recomputation of the + cargoSha256 and fixes a problem with + dependencies, that were fetched from a non-crates.io source. + Eventually only the true behaviour will be kept, + so please set it explicitly to true and + recompute your cargoSha256, so that we can + migrate to the new behaviour and deprecate the option. + + + While recomputing cargoSha256, it is important + to first invalidate the hash (e.g. by changing a digit), so that + Nix actually rebuilds the fixed-output derivation. Otherwise this + could lead to hard to detect errors, where a package seemingly + builds on your computer, but breaks on others! + + diff --git a/pkgs/build-support/rust/default.nix b/pkgs/build-support/rust/default.nix index 820989a76206..a2dc5df4d926 100644 --- a/pkgs/build-support/rust/default.nix +++ b/pkgs/build-support/rust/default.nix @@ -17,9 +17,15 @@ in , cargoBuildFlags ? [] , cargoVendorDir ? null +# This tells cargo-vendor to include a Cargo config file in the fixed-output +# derivation. This is desirable in every case, so please set it to true. +# Eventually this will default to true and even later this option and the old +# behaviour will be removed. +, useRealVendorConfig ? false , ... } @ args: assert cargoVendorDir == null -> cargoSha256 != "unset"; +assert cargoVendorDir != null -> !useRealVendorConfig; let cargoDeps = if cargoVendorDir == null @@ -27,6 +33,7 @@ let inherit name src srcs sourceRoot cargoUpdateHook; patches = cargoPatches; sha256 = cargoSha256; + writeVendorConfig = useRealVendorConfig; } else null; @@ -61,14 +68,19 @@ in stdenv.mkDerivation (args // { ${setupVendorDir} mkdir .cargo - cat >.cargo/config <<-EOF - [source.crates-io] - registry = 'https://github.com/rust-lang/crates.io-index' - replace-with = 'vendored-sources' + '' + (if useRealVendorConfig then '' + sed "s|directory = \".*\"|directory = \"$(pwd)/$cargoDepsCopy\"|g" \ + "$(pwd)/$cargoDepsCopy/.cargo/config" > .cargo/config + '' else '' + cat >.cargo/config <<-EOF + [source.crates-io] + registry = 'https://github.com/rust-lang/crates.io-index' + replace-with = 'vendored-sources' - [source.vendored-sources] - directory = '$(pwd)/$cargoDepsCopy' - EOF + [source.vendored-sources] + directory = '$(pwd)/$cargoDepsCopy' + EOF + '') + '' unset cargoDepsCopy diff --git a/pkgs/build-support/rust/fetchcargo.nix b/pkgs/build-support/rust/fetchcargo.nix index 2670ed528640..2d8a36a30ace 100644 --- a/pkgs/build-support/rust/fetchcargo.nix +++ b/pkgs/build-support/rust/fetchcargo.nix @@ -1,5 +1,5 @@ { stdenv, cacert, git, rust, cargo-vendor }: -{ name ? "cargo-deps", src, srcs, patches, sourceRoot, sha256, cargoUpdateHook ? "" }: +{ name ? "cargo-deps", src, srcs, patches, sourceRoot, sha256, cargoUpdateHook ? "", writeVendorConfig ? false }: stdenv.mkDerivation { name = "${name}-vendor"; nativeBuildInputs = [ cacert cargo-vendor git rust.cargo ]; @@ -23,9 +23,11 @@ stdenv.mkDerivation { ${cargoUpdateHook} - cargo vendor - - cp -ar vendor $out + mkdir -p $out + cargo vendor $out > config + '' + stdenv.lib.optionalString writeVendorConfig '' + mkdir $out/.cargo + sed "s|directory = \".*\"|directory = \"./vendor\"|g" config > $out/.cargo/config ''; outputHashAlgo = "sha256";