stdenv: Replace "// optionalAttrs" with nullable attr name

As in https://github.com/NixOS/nixpkgs/pull/430132, it saves a lot of
set allocations and merge operations, but makes code harder to read.

I've tried introducing a function like this to make code cleaner, but it
increases number of envs and space taken by them significantly:

  optionalAttr = cond: name: if cond then name else null;

I've also tried applying this logic to the section with isDarwin, but
it makes things measurably worse for x86_64-linux eval.
This commit is contained in:
Yuriy Taraday 2025-08-04 17:00:57 +02:00
parent ca22674fa3
commit 1d4489dc32

View File

@ -202,17 +202,15 @@ let
# to be built eventually, we would still like to get the error early and without # to be built eventually, we would still like to get the error early and without
# having to wait while nix builds a derivation that might not be used. # having to wait while nix builds a derivation that might not be used.
# See also https://github.com/NixOS/nix/issues/4629 # See also https://github.com/NixOS/nix/issues/4629
optionalAttrs (attrs ? disallowedReferences) { {
disallowedReferences = map unsafeDerivationToUntrackedOutpath attrs.disallowedReferences; ${if (attrs ? disallowedReferences) then "disallowedReferences" else null} =
} map unsafeDerivationToUntrackedOutpath attrs.disallowedReferences;
// optionalAttrs (attrs ? disallowedRequisites) { ${if (attrs ? disallowedRequisites) then "disallowedRequisites" else null} =
disallowedRequisites = map unsafeDerivationToUntrackedOutpath attrs.disallowedRequisites; map unsafeDerivationToUntrackedOutpath attrs.disallowedRequisites;
} ${if (attrs ? allowedReferences) then "allowedReferences" else null} =
// optionalAttrs (attrs ? allowedReferences) { mapNullable unsafeDerivationToUntrackedOutpath attrs.allowedReferences;
allowedReferences = mapNullable unsafeDerivationToUntrackedOutpath attrs.allowedReferences; ${if (attrs ? allowedRequisites) then "allowedRequisites" else null} =
} mapNullable unsafeDerivationToUntrackedOutpath attrs.allowedRequisites;
// optionalAttrs (attrs ? allowedRequisites) {
allowedRequisites = mapNullable unsafeDerivationToUntrackedOutpath attrs.allowedRequisites;
}; };
makeDerivationArgument = makeDerivationArgument =
@ -478,8 +476,8 @@ let
derivationArg = derivationArg =
removeAttrs attrs removedOrReplacedAttrNames removeAttrs attrs removedOrReplacedAttrNames
// (optionalAttrs (attrs ? name || (attrs ? pname && attrs ? version)) { // {
name = ${if (attrs ? name || (attrs ? pname && attrs ? version)) then "name" else null} =
let let
# Indicate the host platform of the derivation if cross compiling. # Indicate the host platform of the derivation if cross compiling.
# Fixed-output derivations like source tarballs shouldn't get a host # Fixed-output derivations like source tarballs shouldn't get a host
@ -507,8 +505,7 @@ let
) "The `version` attribute cannot be null."; ) "The `version` attribute cannot be null.";
"${attrs.pname}${staticMarker}${hostSuffix}-${attrs.version}" "${attrs.pname}${staticMarker}${hostSuffix}-${attrs.version}"
); );
})
// {
builder = attrs.realBuilder or stdenv.shell; builder = attrs.realBuilder or stdenv.shell;
args = args =
attrs.args or [ attrs.args or [
@ -556,28 +553,33 @@ let
inherit doCheck doInstallCheck; inherit doCheck doInstallCheck;
inherit outputs; inherit outputs;
}
// optionalAttrs (__contentAddressed) { # When the derivations is content addressed provide default values
inherit __contentAddressed; # for outputHashMode and outputHashAlgo because most people won't
# Provide default values for outputHashMode and outputHashAlgo because # care about these anyways
# most people won't care about these anyways ${if __contentAddressed then "__contentAddressed" else null} = __contentAddressed;
outputHashAlgo = attrs.outputHashAlgo or "sha256"; ${if __contentAddressed then "outputHashAlgo" else null} = attrs.outputHashAlgo or "sha256";
outputHashMode = attrs.outputHashMode or "recursive"; ${if __contentAddressed then "outputHashMode" else null} = attrs.outputHashMode or "recursive";
}
// optionalAttrs (enableParallelBuilding) { ${if enableParallelBuilding then "enableParallelBuilding" else null} = enableParallelBuilding;
inherit enableParallelBuilding; ${if enableParallelBuilding then "enableParallelChecking" else null} =
enableParallelChecking = attrs.enableParallelChecking or true; attrs.enableParallelChecking or true;
enableParallelInstalling = attrs.enableParallelInstalling or true; ${if enableParallelBuilding then "enableParallelInstalling" else null} =
} attrs.enableParallelInstalling or true;
// optionalAttrs (hardeningDisable != [ ] || hardeningEnable != [ ] || stdenv.hostPlatform.isMusl) {
NIX_HARDENING_ENABLE = builtins.concatStringsSep " " enabledHardeningOptions; ${
} if (hardeningDisable != [ ] || hardeningEnable != [ ] || stdenv.hostPlatform.isMusl) then
// "NIX_HARDENING_ENABLE"
else
null
} =
builtins.concatStringsSep " " enabledHardeningOptions;
# TODO: remove platform condition # TODO: remove platform condition
# Enabling this check could be a breaking change as it requires to edit nix.conf # Enabling this check could be a breaking change as it requires to edit nix.conf
# NixOS module already sets gccarch, unsure of nix installers and other distributions # NixOS module already sets gccarch, unsure of nix installers and other distributions
optionalAttrs ${
( if
stdenv.buildPlatform ? gcc.arch stdenv.buildPlatform ? gcc.arch
&& !( && !(
stdenv.buildPlatform.isAarch64 stdenv.buildPlatform.isAarch64
@ -589,12 +591,15 @@ let
stdenv.buildPlatform.gcc.arch == "armv8-a" stdenv.buildPlatform.gcc.arch == "armv8-a"
) )
) )
) then
{ "requiredSystemFeatures"
requiredSystemFeatures = attrs.requiredSystemFeatures or [ ] ++ [ else
"gccarch-${stdenv.buildPlatform.gcc.arch}" null
]; } =
} attrs.requiredSystemFeatures or [ ] ++ [
"gccarch-${stdenv.buildPlatform.gcc.arch}"
];
}
// optionalAttrs (stdenv.buildPlatform.isDarwin) ( // optionalAttrs (stdenv.buildPlatform.isDarwin) (
let let
allDependencies = concatLists (concatLists dependencies); allDependencies = concatLists (concatLists dependencies);