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

buildRustPackage: add overrideRust attribute #288430

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
62 changes: 55 additions & 7 deletions pkgs/development/compilers/rust/make-rust-platform.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,59 @@
, ...
}:

rec {
rust = {
rustc = lib.warn "rustPlatform.rust.rustc is deprecated. Use rustc instead." rustc;
cargo = lib.warn "rustPlatform.rust.cargo is deprecated. Use cargo instead." cargo;
};

let
fetchCargoTarball = buildPackages.callPackage ../../../build-support/rust/fetch-cargo-tarball {
git = buildPackages.gitMinimal;
inherit cargo;
};

buildRustPackage = callPackage ../../../build-support/rust/build-rust-package {
# Derivations built with `buildRustPackage` can already be overridden with
# `override`, `overrideAttrs`, and `overrideDerivation`.
# This function introduces `overrideRust` and it overrides the call to
# `buildRustPackage`.
Copy link
Member

Choose a reason for hiding this comment

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

Some motivation would be nice.

Suggested change
# `buildRustPackage`.
# `buildRustPackage`. This is useful for ...

makeOverridableRustPackage = f:
let
# Creates a functor with the same arguments as f
mirrorArgs = lib.mirrorFunctionArgs f;
transform = origArgs:
let
result = f origArgs;

overrideWith =
newArgs: origArgs // (
if lib.isFunction newArgs
then newArgs origArgs
else newArgs);

overrideRust = mirrorArgs (newArgs: makeOverridableRustPackage f (overrideWith newArgs));

# Change the result of the function call by applying g to it
overrideResult = g: makeOverridableRustPackage (mirrorArgs (newArgs: g (f newArgs))) origArgs;
in
if builtins.isAttrs result
then result // {
inherit overrideRust;
} // (lib.optionalAttrs (result ? override) {
override = newArgs: overrideResult (x: x.override newArgs);
})
else if builtins.isFunction result
# Note: `lib.mirrorFunctionArgs` transforms `result` into a functor
# while propagating its arguments.
then lib.mirrorFunctionArgs result result // {
inherit overrideRust;
}
else result;
in if builtins.isAttrs f
then f // (mirrorArgs transform)
else mirrorArgs transform;

buildRustPackageImpl = callPackage ../../../build-support/rust/build-rust-package {
inherit stdenv cargoBuildHook cargoCheckHook cargoInstallHook cargoNextestHook cargoSetupHook
fetchCargoTarball importCargoLock rustc cargo cargo-auditable;
};

buildRustPackage = makeOverridableRustPackage buildRustPackageImpl;

importCargoLock = buildPackages.callPackage ../../../build-support/rust/import-cargo-lock.nix { inherit cargo; };

rustcSrc = callPackage ./rust-src.nix {
Expand All @@ -37,4 +74,15 @@ rec {
inherit (callPackage ../../../build-support/rust/hooks {
inherit stdenv cargo rustc;
}) cargoBuildHook cargoCheckHook cargoInstallHook cargoNextestHook cargoSetupHook maturinBuildHook bindgenHook;

in {
rust = {
rustc = lib.warn "rustPlatform.rust.rustc is deprecated. Use rustc instead." rustc;
cargo = lib.warn "rustPlatform.rust.cargo is deprecated. Use cargo instead." cargo;
};

inherit fetchCargoTarball buildRustPackage importCargoLock rustcSrc rustLibSrc;

# Hooks
inherit cargoBuildHook cargoCheckHook cargoInstallHook cargoNextestHook cargoSetupHook maturinBuildHook bindgenHook;
}
Loading