Unverified Commit 3bdc867a authored by Aliaksandr's avatar Aliaksandr
Browse files

stdenv, trivial-builders: replace `// optionalAttrs` with nullable attr names

Follow-up to #430969. Replace remaining `// optionalAttrs` patterns with
`${if cond then "name" else null} = value;` to avoid per-derivation
closure allocations and `//` merge operations.

Changes:
- make-derivation.nix: inline Darwin, Windows, outputChecks blocks and
  the `env'`/`__structuredAttrs` patterns in mkDerivationSimple
- trivial-builders: inline optionalAttrs in runCommandWith and symlinkJoin
parent 99dd1abc
Loading
Loading
Loading
Loading
+6 −8
Original line number Diff line number Diff line
@@ -80,8 +80,8 @@ rec {
        inherit buildCommand name;
        passAsFile = [ "buildCommand" ] ++ (derivationArgs.passAsFile or [ ]);
      }
      // lib.optionalAttrs (!derivationArgs ? meta) {
        pos =
      // {
        ${if !derivationArgs ? meta then "pos" else null} =
          let
            args = builtins.attrNames derivationArgs;
          in
@@ -89,11 +89,9 @@ rec {
            builtins.unsafeGetAttrPos (builtins.head args) derivationArgs
          else
            null;
        ${if runLocal then "preferLocalBuild" else null} = true;
        ${if runLocal then "allowSubstitutes" else null} = false;
      }
      // (lib.optionalAttrs runLocal {
        preferLocalBuild = true;
        allowSubstitutes = false;
      })
      // removeAttrs derivationArgs [ "passAsFile" ]
    );

@@ -567,8 +565,8 @@ rec {
          ${postBuild}
        '';
      }
      // lib.optionalAttrs (!args ? meta) {
        pos =
      // {
        ${if !args ? meta then "pos" else null} =
          if args ? pname then
            builtins.unsafeGetAttrPos "pname" args
          else
+204 −187
Original line number Diff line number Diff line
# PERF: This file is evaluated for every derivation in the build closure.
# Avoid `// optionalAttrs` — each call allocates a closure, an intermediate
# attrset, and a `//` merge.  Use nullable attribute names instead:
#
#   ${if cond then "name" else null} = value;
#
# See https://github.com/NixOS/nixpkgs/pull/430969 for measurements.

{ lib, config }:

stdenv:
@@ -28,7 +36,6 @@ let
    mapAttrs
    mapNullable
    optional
    optionalAttrs
    optionalString
    optionals
    pipe
@@ -530,9 +537,7 @@ let
          ]
        ];

        derivationArg =
          removeAttrs attrs removedOrReplacedAttrNames
          // {
        derivationArg = removeAttrs attrs removedOrReplacedAttrNames // {
          ${if (attrs ? name || (attrs ? pname && attrs ? version)) then "name" else null} =
            let
              # Indicate the host platform of the derivation if cross compiling.
@@ -641,35 +646,19 @@ let
          # NixOS module already sets gccarch, unsure of nix installers and other distributions
          ${if requiredSystemFeaturesShouldBeSet then "requiredSystemFeatures" else null} =
            attrs.requiredSystemFeatures or [ ] ++ gccArchFeature;
          }
          // optionalAttrs buildIsDarwin (

          # -- Darwin-specific attrs --
          ${if buildIsDarwin then "__darwinAllowLocalNetworking" else null} = __darwinAllowLocalNetworking;
          ${if buildIsDarwin then "__sandboxProfile" else null} =
            let
              allDependencies = concatLists (concatLists dependencies);
              allPropagatedDependencies = concatLists (concatLists propagatedDependencies);

              computedSandboxProfile = concatMap (input: input.__propagatedSandboxProfile or [ ]) (
                extraNativeBuildInputs ++ extraBuildInputs ++ allDependencies
              );

              computedPropagatedSandboxProfile = concatMap (
                input: input.__propagatedSandboxProfile or [ ]
              ) allPropagatedDependencies;

              computedImpureHostDeps = unique (
                concatMap (input: input.__propagatedImpureHostDeps or [ ]) (
                  extraNativeBuildInputs ++ extraBuildInputs ++ allDependencies
                )
              );

              computedPropagatedImpureHostDeps = unique (
                concatMap (input: input.__propagatedImpureHostDeps or [ ]) allPropagatedDependencies
              );
            in
            {
              inherit __darwinAllowLocalNetworking;
              # TODO: remove `unique` once nix has a list canonicalization primitive
              __sandboxProfile =
                let
              profiles = [
                extraSandboxProfile
              ]
@@ -679,13 +668,30 @@ let
                propagatedSandboxProfile
                sandboxProfile
              ];
                  final = concatStringsSep "\n" (filter (x: x != "") (unique profiles));
            in
                final;
              __propagatedSandboxProfile = unique (
                computedPropagatedSandboxProfile ++ [ propagatedSandboxProfile ]
            # TODO: remove `unique` once nix has a list canonicalization primitive
            concatStringsSep "\n" (filter (x: x != "") (unique profiles));
          ${if buildIsDarwin then "__propagatedSandboxProfile" else null} =
            let
              allPropagatedDependencies = concatLists (concatLists propagatedDependencies);
              computedPropagatedSandboxProfile = concatMap (
                input: input.__propagatedSandboxProfile or [ ]
              ) allPropagatedDependencies;
            in
            unique (computedPropagatedSandboxProfile ++ [ propagatedSandboxProfile ]);
          ${if buildIsDarwin then "__impureHostDeps" else null} =
            let
              allDependencies = concatLists (concatLists dependencies);
              allPropagatedDependencies = concatLists (concatLists propagatedDependencies);
              computedImpureHostDeps = unique (
                concatMap (input: input.__propagatedImpureHostDeps or [ ]) (
                  extraNativeBuildInputs ++ extraBuildInputs ++ allDependencies
                )
              );
              computedPropagatedImpureHostDeps = unique (
                concatMap (input: input.__propagatedImpureHostDeps or [ ]) allPropagatedDependencies
              );
              __impureHostDeps =
            in
            computedImpureHostDeps
            ++ computedPropagatedImpureHostDeps
            ++ __propagatedImpureHostDeps
@@ -697,26 +703,37 @@ let
              "/dev/urandom"
              "/bin/sh"
            ];
              __propagatedImpureHostDeps = computedPropagatedImpureHostDeps ++ __propagatedImpureHostDeps;
            }
          )
          // optionalAttrs (isWindows || isCygwin) {
            allowedImpureDLLs =
          ${if buildIsDarwin then "__propagatedImpureHostDeps" else null} =
            let
              allPropagatedDependencies = concatLists (concatLists propagatedDependencies);
              computedPropagatedImpureHostDeps = unique (
                concatMap (input: input.__propagatedImpureHostDeps or [ ]) allPropagatedDependencies
              );
            in
            computedPropagatedImpureHostDeps ++ __propagatedImpureHostDeps;

          # -- Windows/Cygwin-specific attrs --
          ${if isWindows || isCygwin then "allowedImpureDLLs" else null} =
            allowedImpureDLLs
            ++ lib.optionals isCygwin [
              "KERNEL32.dll"
            ];
          }
          // (

          # -- Output reference checks --
          ${if !__structuredAttrs && attrs ? disallowedReferences then "disallowedReferences" else null} =
            map unsafeDerivationToUntrackedOutpath attrs.disallowedReferences;
          ${if !__structuredAttrs && attrs ? disallowedRequisites then "disallowedRequisites" else null} =
            map unsafeDerivationToUntrackedOutpath attrs.disallowedRequisites;
          ${if !__structuredAttrs && attrs ? allowedReferences then "allowedReferences" else null} =
            mapNullable unsafeDerivationToUntrackedOutpath attrs.allowedReferences;
          ${if !__structuredAttrs && attrs ? allowedRequisites then "allowedRequisites" else null} =
            mapNullable unsafeDerivationToUntrackedOutpath attrs.allowedRequisites;
          ${if __structuredAttrs then "outputChecks" else null} =
            let
              attrsOutputChecks = makeOutputChecks attrs;
              attrsOutputChecksFiltered = filterAttrs (_: v: v != null) attrsOutputChecks;
            in
            if !__structuredAttrs then
              attrsOutputChecks
            else
              {
                outputChecks = builtins.listToAttrs (
            builtins.listToAttrs (
              map (name: {
                inherit name;
                value =
@@ -741,9 +758,7 @@ let
                    raw;
              }) outputs
            );
              }
          );

        };
      in
      derivationArg;

@@ -806,7 +821,9 @@ let

    let
      mainProgram = meta.mainProgram or null;
      env' = env // lib.optionalAttrs (mainProgram != null) { NIX_MAIN_PROGRAM = mainProgram; };
      env' = env // {
        ${if mainProgram != null then "NIX_MAIN_PROGRAM" else null} = mainProgram;
      };

      derivationArg = makeDerivationArgument (
        removeAttrs attrs [
@@ -815,8 +832,8 @@ let
          "pos"
          "env"
        ]
        // lib.optionalAttrs __structuredAttrs { env = checkedEnv; }
        // {
          ${if __structuredAttrs then "env" else null} = checkedEnv;
          cmakeFlags = makeCMakeFlags attrs;
          mesonFlags = makeMesonFlags attrs;
        }