Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build-support/rust: add cargoPurityFlag parameter #187838

Closed
wants to merge 3 commits into from
Closed
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
7 changes: 6 additions & 1 deletion pkgs/build-support/rust/build-rust-package/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
, useNextest ? false
, depsExtraArgs ? {}

# You must use "--offline" instead of "--frozen" if `Cargo.lock` is
# valid-but-outdated, for example when adding a `[patch]` stanza to
# `Cargo.toml` without regenerating `Cargo.lock`.
Copy link
Contributor

@zowoq zowoq Dec 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're adding a patch stanza why can't you regenerate the lock at the same time?

For that matter why use a patch stanza? Patch toml and lock, add to cargoPatches.

Copy link
Author

@ghost ghost Dec 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because you're adding the patch stanza from inside a derivation!

This lets us do for crate dependencies of a buildRustPackage what we can already do for ordinary packages in nixpkgs -- override a specific dependency throughout all of nixpkgs.

My own motivating example case is the ring crate, which is basically "openssl/boringssl for Rust". It's a deep, deep dependency of a huge chunk of the ecosystem. Ring does not build on PowerPC, and the Ring maintainer has ignored a patch submitted by IBM for over two full years. There needs to be a way for people to route around this sort of nonsense.

But this is generally useful on all platforms, for the same reasons why .override is useful. At least until crate2nix conquers the universe.

Copy link
Author

@ghost ghost Dec 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is what the example usage looks like:

78b4b5b

This shows what "replace this crate with that crate in this one package" looks like:

https://github.com/NixOS/nixpkgs/blob/78b4b5bf68d41f412944a2ee846715dfb090d045/pkgs/applications/networking/irc/tiny/default.nix#L26-L29

I'm working on a more general "replace this crate with that crate in every use of buildRustPackage".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This lets us do for crate dependencies of a buildRustPackage what we can already do for ordinary packages in nixpkgs -- override a specific dependency throughout all of nixpkgs.

How does that work?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the example you posted you can do this:

Patch toml and lock, add to cargoPatches.

, cargoPurityFlag ? "--frozen"

# Toggles whether a custom sysroot is created when the target is a .json file.
, __internal_dontAddSysroot ? false

Expand Down Expand Up @@ -118,7 +123,7 @@ stdenv.mkDerivation ((removeAttrs args [ "depsExtraArgs" "cargoUpdateHook" "carg
nativeBuildInputs = nativeBuildInputs ++ [
cacert
git
cargoBuildHook
(cargoBuildHook.override { inherit cargoPurityFlag; })
(if useNextest then cargoNextestHook else cargoCheckHook)
cargoInstallHook
cargoSetupHook
Expand Down
2 changes: 1 addition & 1 deletion pkgs/build-support/rust/hooks/cargo-build-hook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ cargoBuildHook() {
"CXX_@rustTargetPlatform@=@cxxForHost@" \
cargo build -j $NIX_BUILD_CORES \
--target @rustTargetPlatformSpec@ \
--frozen \
@cargoPurityFlag@ \
${cargoBuildProfileFlag} \
${cargoBuildNoDefaultFeaturesFlag} \
${cargoBuildFeaturesFlag} \
Expand Down
4 changes: 2 additions & 2 deletions pkgs/build-support/rust/hooks/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ let
rustTargetPlatform = rust.toRustTarget stdenv.hostPlatform;
rustTargetPlatformSpec = rust.toRustTargetSpec stdenv.hostPlatform;
in {
cargoBuildHook = callPackage ({ }:
cargoBuildHook = callPackage ({ cargoPurityFlag ? "--frozen" }:
makeSetupHook {
name = "cargo-build-hook.sh";
deps = [ cargo ];
substitutions = {
inherit ccForBuild ccForHost cxxForBuild cxxForHost
inherit ccForBuild ccForHost cxxForBuild cxxForHost cargoPurityFlag
rustBuildPlatform rustTargetPlatform rustTargetPlatformSpec;
};
} ./cargo-build-hook.sh) {};
Expand Down