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: support fixed-point arguments via lib.extendMkDerivation #382550

Merged
merged 4 commits into from
Feb 22, 2025
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
354 changes: 177 additions & 177 deletions pkgs/build-support/rust/build-rust-package/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,185 +18,185 @@
windows,
}:

{
name ? "${args.pname}-${args.version}",

# Name for the vendored dependencies tarball
cargoDepsName ? name,

src ? null,
srcs ? null,
preUnpack ? null,
unpackPhase ? null,
postUnpack ? null,
cargoPatches ? [ ],
patches ? [ ],
sourceRoot ? null,
cargoRoot ? null,
logLevel ? "",
buildInputs ? [ ],
nativeBuildInputs ? [ ],
cargoUpdateHook ? "",
cargoDepsHook ? "",
buildType ? "release",
meta ? { },
useFetchCargoVendor ? false,
cargoDeps ? null,
cargoLock ? null,
cargoVendorDir ? null,
checkType ? buildType,
buildNoDefaultFeatures ? false,
checkNoDefaultFeatures ? buildNoDefaultFeatures,
buildFeatures ? [ ],
checkFeatures ? buildFeatures,
useNextest ? false,
auditable ? !cargo-auditable.meta.broken,

depsExtraArgs ? { },

# Needed to `pushd`/`popd` into a subdir of a tarball if this subdir
# contains a Cargo.toml, but isn't part of a workspace (which is e.g. the
# case for `rustfmt`/etc from the `rust-sources).
# Otherwise, everything from the tarball would've been built/tested.
buildAndTestSubdir ? null,
...
}@args:

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

let

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
cargoRoot
preUnpack
unpackPhase
postUnpack
;
name = cargoDepsName;
patches = cargoPatches;
hash = args.cargoHash;
}
// depsExtraArgs
)
else
fetchCargoTarball (
{
inherit
src
srcs
sourceRoot
cargoRoot
preUnpack
unpackPhase
postUnpack
cargoUpdateHook
;
name = cargoDepsName;
patches = cargoPatches;
}
// lib.optionalAttrs (args ? cargoHash) {
hash = args.cargoHash;
}
// lib.optionalAttrs (args ? cargoSha256) {
sha256 = lib.warn "cargoSha256 is deprecated. Please use cargoHash with SRI hash instead" args.cargoSha256;
}
// depsExtraArgs
);

target = stdenv.hostPlatform.rust.rustcTargetSpec;
targetIsJSON = lib.hasSuffix ".json" target;
in

stdenv.mkDerivation (
(removeAttrs args [
lib.extendMkDerivation {
constructDrv = stdenv.mkDerivation;

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

cargoBuildType = buildType;

cargoCheckType = checkType;

cargoBuildNoDefaultFeatures = buildNoDefaultFeatures;

cargoCheckNoDefaultFeatures = checkNoDefaultFeatures;

cargoBuildFeatures = buildFeatures;

cargoCheckFeatures = checkFeatures;

nativeBuildInputs =
nativeBuildInputs
++ lib.optionals auditable [
(buildPackages.cargo-auditable-cargo-wrapper.override {
inherit cargo cargo-auditable;
})
]
++ [
cargoBuildHook
(if useNextest then cargoNextestHook else cargoCheckHook)
cargoInstallHook
cargoSetupHook
rustc
cargo
];

buildInputs =
buildInputs
++ lib.optionals stdenv.hostPlatform.isDarwin [ libiconv ]
++ lib.optionals stdenv.hostPlatform.isMinGW [ windows.pthreads ];

patches = cargoPatches ++ patches;

PKG_CONFIG_ALLOW_CROSS = if stdenv.buildPlatform != stdenv.hostPlatform then 1 else 0;

postUnpack =
''
eval "$cargoDepsHook"

export RUST_LOG=${logLevel}
''
+ (args.postUnpack or "");

configurePhase =
args.configurePhase or ''
runHook preConfigure
runHook postConfigure
'';

doCheck = args.doCheck or true;

strictDeps = true;

meta = meta // {
badPlatforms = meta.badPlatforms or [ ] ++ rustc.badTargetPlatforms;
# default to Rust's platforms
platforms = lib.intersectLists meta.platforms or lib.platforms.all rustc.targetPlatforms;
];

extendDrvArgs =
finalAttrs:
{
name ? "${args.pname}-${args.version}",

# Name for the vendored dependencies tarball
cargoDepsName ? name,

src ? null,
srcs ? null,
preUnpack ? null,
unpackPhase ? null,
postUnpack ? null,
cargoPatches ? [ ],
patches ? [ ],
sourceRoot ? null,
cargoRoot ? null,
logLevel ? "",
buildInputs ? [ ],
nativeBuildInputs ? [ ],
cargoUpdateHook ? "",
cargoDepsHook ? "",
buildType ? "release",
meta ? { },
useFetchCargoVendor ? false,
cargoDeps ? null,
cargoLock ? null,
cargoVendorDir ? null,
checkType ? buildType,
buildNoDefaultFeatures ? false,
checkNoDefaultFeatures ? buildNoDefaultFeatures,
buildFeatures ? [ ],
checkFeatures ? buildFeatures,
useNextest ? false,
auditable ? !cargo-auditable.meta.broken,

depsExtraArgs ? { },

# Needed to `pushd`/`popd` into a subdir of a tarball if this subdir
# contains a Cargo.toml, but isn't part of a workspace (which is e.g. the
# case for `rustfmt`/etc from the `rust-sources).
# Otherwise, everything from the tarball would've been built/tested.
buildAndTestSubdir ? null,
...
}@args:

let

cargoDeps' =
if cargoVendorDir != null then
null
else if cargoDeps != null then
cargoDeps
else if cargoLock != null then
importCargoLock cargoLock
else if (args.cargoHash or null == null) && (args.cargoSha256 or null == null) then
throw "cargoHash, cargoVendorDir, cargoDeps, or cargoLock must be set"
else if useFetchCargoVendor then
fetchCargoVendor (
{
inherit
src
srcs
sourceRoot
cargoRoot
preUnpack
unpackPhase
postUnpack
;
name = cargoDepsName;
patches = cargoPatches;
hash = args.cargoHash;
}
// depsExtraArgs
)
else
fetchCargoTarball (
{
inherit
src
srcs
sourceRoot
cargoRoot
preUnpack
unpackPhase
postUnpack
cargoUpdateHook
;
name = cargoDepsName;
patches = cargoPatches;
}
// lib.optionalAttrs (args ? cargoHash) {
hash = args.cargoHash;
}
// lib.optionalAttrs (args ? cargoSha256) {
sha256 = lib.warn "cargoSha256 is deprecated. Please use cargoHash with SRI hash instead" args.cargoSha256;
}
// depsExtraArgs
);

target = stdenv.hostPlatform.rust.rustcTargetSpec;
targetIsJSON = lib.hasSuffix ".json" target;
in
lib.optionalAttrs (stdenv.hostPlatform.isDarwin && buildType == "debug") {
RUSTFLAGS = "-C split-debuginfo=packed " + (args.RUSTFLAGS or "");
}
// {
cargoDeps = cargoDeps';
inherit buildAndTestSubdir;

cargoBuildType = buildType;

cargoCheckType = checkType;

cargoBuildNoDefaultFeatures = buildNoDefaultFeatures;

cargoCheckNoDefaultFeatures = checkNoDefaultFeatures;

cargoBuildFeatures = buildFeatures;

cargoCheckFeatures = checkFeatures;

nativeBuildInputs =
nativeBuildInputs
++ lib.optionals auditable [
(buildPackages.cargo-auditable-cargo-wrapper.override {
inherit cargo cargo-auditable;
})
]
++ [
cargoBuildHook
(if useNextest then cargoNextestHook else cargoCheckHook)
cargoInstallHook
cargoSetupHook
rustc
cargo
];

buildInputs =
buildInputs
++ lib.optionals stdenv.hostPlatform.isDarwin [ libiconv ]
++ lib.optionals stdenv.hostPlatform.isMinGW [ windows.pthreads ];

patches = cargoPatches ++ patches;

PKG_CONFIG_ALLOW_CROSS = if stdenv.buildPlatform != stdenv.hostPlatform then 1 else 0;

postUnpack =
''
eval "$cargoDepsHook"

export RUST_LOG=${logLevel}
''
+ (args.postUnpack or "");

configurePhase =
args.configurePhase or ''
runHook preConfigure
runHook postConfigure
'';

doCheck = args.doCheck or true;

strictDeps = true;

meta = meta // {
badPlatforms = meta.badPlatforms or [ ] ++ rustc.badTargetPlatforms;
# default to Rust's platforms
platforms = lib.intersectLists meta.platforms or lib.platforms.all rustc.targetPlatforms;
};
};
}
)
}
Loading