From ab56d3ac71433e7a2cbe4101e17cff9c9acefc43 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 11 Mar 2024 16:59:16 +0100 Subject: [PATCH 01/11] make-derivation.nix: Move into let binding --- pkgs/stdenv/generic/make-derivation.nix | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pkgs/stdenv/generic/make-derivation.nix b/pkgs/stdenv/generic/make-derivation.nix index 6a53d519045c..3effa8fce49f 100644 --- a/pkgs/stdenv/generic/make-derivation.nix +++ b/pkgs/stdenv/generic/make-derivation.nix @@ -40,6 +40,12 @@ let unique ; + mkDerivation = + fnOrAttrs: + if builtins.isFunction fnOrAttrs + then makeDerivationExtensible fnOrAttrs + else makeDerivationExtensibleConst fnOrAttrs; + checkMeta = import ./check-meta.nix { inherit lib config; # Nix itself uses the `system` field of a derivation to decide where @@ -603,7 +609,4 @@ extendDerivation (derivation (derivationArg // optionalAttrs envIsExportable checkedEnv)); in - fnOrAttrs: - if builtins.isFunction fnOrAttrs - then makeDerivationExtensible fnOrAttrs - else makeDerivationExtensibleConst fnOrAttrs + mkDerivation From de516f6f130ff744c14dc1c58fbb428fa4bb3794 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 11 Mar 2024 17:06:02 +0100 Subject: [PATCH 02/11] make-derivation.nix: Return mkDerivation as an attribute --- pkgs/stdenv/adapters.nix | 2 +- pkgs/stdenv/generic/default.nix | 2 +- pkgs/stdenv/generic/make-derivation.nix | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/pkgs/stdenv/adapters.nix b/pkgs/stdenv/adapters.nix index 84d3258949eb..10ed96acc209 100644 --- a/pkgs/stdenv/adapters.nix +++ b/pkgs/stdenv/adapters.nix @@ -6,7 +6,7 @@ let # N.B. Keep in sync with default arg for stdenv/generic. - defaultMkDerivationFromStdenv = import ./generic/make-derivation.nix { inherit lib config; }; + defaultMkDerivationFromStdenv = stdenv: (import ./generic/make-derivation.nix { inherit lib config; } stdenv).mkDerivation; # Low level function to help with overriding `mkDerivationFromStdenv`. One # gives it the old stdenv arguments and a "continuation" function, and diff --git a/pkgs/stdenv/generic/default.nix b/pkgs/stdenv/generic/default.nix index e764571869db..2cda43d5632f 100644 --- a/pkgs/stdenv/generic/default.nix +++ b/pkgs/stdenv/generic/default.nix @@ -52,7 +52,7 @@ argsStdenv@{ name ? "stdenv", preHook ? "", initialPath , # The implementation of `mkDerivation`, parameterized with the final stdenv so we can tie the knot. # This is convient to have as a parameter so the stdenv "adapters" work better - mkDerivationFromStdenv ? import ./make-derivation.nix { inherit lib config; } + mkDerivationFromStdenv ? stdenv: (import ./make-derivation.nix { inherit lib config; } stdenv).mkDerivation }: let diff --git a/pkgs/stdenv/generic/make-derivation.nix b/pkgs/stdenv/generic/make-derivation.nix index 3effa8fce49f..8d1dc6bdeab7 100644 --- a/pkgs/stdenv/generic/make-derivation.nix +++ b/pkgs/stdenv/generic/make-derivation.nix @@ -609,4 +609,6 @@ extendDerivation (derivation (derivationArg // optionalAttrs envIsExportable checkedEnv)); in - mkDerivation +{ + inherit mkDerivation; +} From 8dad51a2e27d49db65d51277e57af595792e6613 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Tue, 12 Mar 2024 18:16:56 +0100 Subject: [PATCH 03/11] make-derivation.nix: Split makeDerivationArgument, mkDerivation with duplicate functionality --- pkgs/build-support/lib/cmake.nix | 30 +++++++++ pkgs/build-support/lib/meson.nix | 35 ++++++++++ pkgs/stdenv/generic/make-derivation.nix | 89 ++++++++++++++++++++++++- 3 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 pkgs/build-support/lib/cmake.nix create mode 100644 pkgs/build-support/lib/meson.nix diff --git a/pkgs/build-support/lib/cmake.nix b/pkgs/build-support/lib/cmake.nix new file mode 100644 index 000000000000..eff7bbca61a2 --- /dev/null +++ b/pkgs/build-support/lib/cmake.nix @@ -0,0 +1,30 @@ +{ stdenv, lib }: + +let + inherit (lib) findFirst isString optional optionals; + + makeCMakeFlags = { cmakeFlags ? [], ... }: + cmakeFlags + ++ optionals (stdenv.hostPlatform != stdenv.buildPlatform) ([ + "-DCMAKE_SYSTEM_NAME=${findFirst isString "Generic" (optional (!stdenv.hostPlatform.isRedox) stdenv.hostPlatform.uname.system)}" + ] ++ optionals (stdenv.hostPlatform.uname.processor != null) [ + "-DCMAKE_SYSTEM_PROCESSOR=${stdenv.hostPlatform.uname.processor}" + ] ++ optionals (stdenv.hostPlatform.uname.release != null) [ + "-DCMAKE_SYSTEM_VERSION=${stdenv.hostPlatform.uname.release}" + ] ++ optionals (stdenv.hostPlatform.isDarwin) [ + "-DCMAKE_OSX_ARCHITECTURES=${stdenv.hostPlatform.darwinArch}" + ] ++ optionals (stdenv.buildPlatform.uname.system != null) [ + "-DCMAKE_HOST_SYSTEM_NAME=${stdenv.buildPlatform.uname.system}" + ] ++ optionals (stdenv.buildPlatform.uname.processor != null) [ + "-DCMAKE_HOST_SYSTEM_PROCESSOR=${stdenv.buildPlatform.uname.processor}" + ] ++ optionals (stdenv.buildPlatform.uname.release != null) [ + "-DCMAKE_HOST_SYSTEM_VERSION=${stdenv.buildPlatform.uname.release}" + ] ++ optionals (stdenv.buildPlatform.canExecute stdenv.hostPlatform) [ + "-DCMAKE_CROSSCOMPILING_EMULATOR=env" + ] ++ optionals stdenv.hostPlatform.isStatic [ + "-DCMAKE_LINK_SEARCH_START_STATIC=ON" + ]); +in +{ + inherit makeCMakeFlags; +} diff --git a/pkgs/build-support/lib/meson.nix b/pkgs/build-support/lib/meson.nix new file mode 100644 index 000000000000..395b573f8587 --- /dev/null +++ b/pkgs/build-support/lib/meson.nix @@ -0,0 +1,35 @@ +{ stdenv, lib }: + +let + inherit (lib) boolToString optionals; + + # See https://mesonbuild.com/Reference-tables.html#cpu-families + cpuFamily = platform: with platform; + /**/ if isAarch32 then "arm" + else if isx86_32 then "x86" + else platform.uname.processor; + + makeMesonFlags = { mesonFlags ? [], ... }: + let + crossFile = builtins.toFile "cross-file.conf" '' + [properties] + bindgen_clang_arguments = ['-target', '${stdenv.targetPlatform.config}'] + needs_exe_wrapper = ${boolToString (!stdenv.buildPlatform.canExecute stdenv.hostPlatform)} + + [host_machine] + system = '${stdenv.targetPlatform.parsed.kernel.name}' + cpu_family = '${cpuFamily stdenv.targetPlatform}' + cpu = '${stdenv.targetPlatform.parsed.cpu.name}' + endian = ${if stdenv.targetPlatform.isLittleEndian then "'little'" else "'big'"} + + [binaries] + llvm-config = 'llvm-config-native' + rust = ['rustc', '--target', '${stdenv.targetPlatform.rust.rustcTargetSpec}'] + ''; + crossFlags = optionals (stdenv.hostPlatform != stdenv.buildPlatform) [ "--cross-file=${crossFile}" ]; + in crossFlags ++ mesonFlags; + +in +{ + inherit makeMesonFlags; +} diff --git a/pkgs/stdenv/generic/make-derivation.nix b/pkgs/stdenv/generic/make-derivation.nix index 8d1dc6bdeab7..7a3521c5153c 100644 --- a/pkgs/stdenv/generic/make-derivation.nix +++ b/pkgs/stdenv/generic/make-derivation.nix @@ -40,6 +40,9 @@ let unique ; + inherit (import ../../build-support/lib/cmake.nix { inherit lib stdenv; }) makeCMakeFlags; + inherit (import ../../build-support/lib/meson.nix { inherit lib stdenv; }) makeMesonFlags; + mkDerivation = fnOrAttrs: if builtins.isFunction fnOrAttrs @@ -108,7 +111,7 @@ let makeDerivationExtensible (self: attrs // (if builtins.isFunction f0 || f0?__functor then f self attrs else f0))) attrs; - mkDerivationSimple = overrideAttrs: + makeDerivationArgument = # `mkDerivation` wraps the builtin `derivation` function to @@ -555,6 +558,90 @@ else let "The ‘env’ attribute set can only contain derivation, string, boolean or integer attributes. The ‘${n}’ attribute is of type ${builtins.typeOf v}."; v) env; +in + derivationArg; + +mkDerivationSimple = overrideAttrs: + +# `mkDerivation` wraps the builtin `derivation` function to +# produce derivations that use this stdenv and its shell. +# +# See also: +# +# * https://nixos.org/nixpkgs/manual/#sec-using-stdenv +# Details on how to use this mkDerivation function +# +# * https://nixos.org/manual/nix/stable/expressions/derivations.html#derivations +# Explanation about derivations in general +{ + +# Configure Phase + cmakeFlags ? [] +, mesonFlags ? [] + +, meta ? {} +, passthru ? {} +, pos ? # position used in error messages and for meta.position + (if attrs.meta.description or null != null + then builtins.unsafeGetAttrPos "description" attrs.meta + else if attrs.version or null != null + then builtins.unsafeGetAttrPos "version" attrs + else builtins.unsafeGetAttrPos "name" attrs) + +# Experimental. For simple packages mostly just works, +# but for anything complex, be prepared to debug if enabling. +, __structuredAttrs ? config.structuredAttrsByDefault or false + +, env ? { } + +, ... } @ attrs: + +# Policy on acceptable hash types in nixpkgs +assert attrs ? outputHash -> ( + let algo = + attrs.outputHashAlgo or (head (splitString "-" attrs.outputHash)); + in + if algo == "md5" then + throw "Rejected insecure ${algo} hash '${attrs.outputHash}'" + else + true +); + +let + envIsExportable = isAttrs env && !isDerivation env; + + derivationArg = makeDerivationArgument + (removeAttrs + attrs + (["meta" "passthru" "pos"] + ++ optional (__structuredAttrs || envIsExportable) "env" + ) + // optionalAttrs __structuredAttrs { env = checkedEnv; } + // { + cmakeFlags = makeCMakeFlags attrs; + mesonFlags = makeMesonFlags attrs; + }); + + meta = checkMeta.commonMeta { + inherit validity attrs pos; + references = attrs.nativeBuildInputs ++ attrs.buildInputs + ++ attrs.propagatedNativeBuildInputs ++ attrs.propagatedBuildInputs; + }; + validity = checkMeta.assertValidity { inherit meta attrs; }; + + checkedEnv = + let + overlappingNames = attrNames (builtins.intersectAttrs env derivationArg); + in + assert assertMsg envIsExportable + "When using structured attributes, `env` must be an attribute set of environment variables."; + assert assertMsg (overlappingNames == [ ]) + "The ‘env’ attribute set cannot contain any attributes passed to derivation. The following attributes are overlapping: ${concatStringsSep ", " overlappingNames}"; + mapAttrs + (n: v: assert assertMsg (isString v || isBool v || isInt v || isDerivation v) + "The ‘env’ attribute set can only contain derivation, string, boolean or integer attributes. The ‘${n}’ attribute is of type ${builtins.typeOf v}."; v) + env; + in extendDerivation From 954d9ce64bc515746949b2bf37e58c9934be86db Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Tue, 12 Mar 2024 18:28:50 +0100 Subject: [PATCH 04/11] make-derivation.nix: Drop duplicate functionality from previous commit These two commits make for a cleaner commit history and git blame than https://github.com/NixOS/nixpkgs/pull/295105, where this refactor was developed. See its commit messages for details and design choices, esp. up to and including 37f76fd4c3b23b324ab7ea06ef3b747338d2080f. --- pkgs/stdenv/generic/make-derivation.nix | 87 +------------------------ 1 file changed, 2 insertions(+), 85 deletions(-) diff --git a/pkgs/stdenv/generic/make-derivation.nix b/pkgs/stdenv/generic/make-derivation.nix index 7a3521c5153c..785d8c36f869 100644 --- a/pkgs/stdenv/generic/make-derivation.nix +++ b/pkgs/stdenv/generic/make-derivation.nix @@ -155,8 +155,6 @@ let # Configure Phase , configureFlags ? [] -, cmakeFlags ? [] -, mesonFlags ? [] , # Target is not included by default because most programs don't care. # Including it then would cause needless mass rebuilds. # @@ -178,14 +176,6 @@ let , enableParallelBuilding ? config.enableParallelBuildingByDefault -, meta ? {} -, passthru ? {} -, pos ? # position used in error messages and for meta.position - (if attrs.meta.description or null != null - then builtins.unsafeGetAttrPos "description" attrs.meta - else if attrs.version or null != null - then builtins.unsafeGetAttrPos "version" attrs - else builtins.unsafeGetAttrPos "name" attrs) , separateDebugInfo ? false , outputs ? [ "out" ] , __darwinAllowLocalNetworking ? false @@ -207,8 +197,6 @@ let # but for anything complex, be prepared to debug if enabling. , __structuredAttrs ? config.structuredAttrsByDefault or false -, env ? { } - , ... } @ attrs: # Policy on acceptable hash types in nixpkgs @@ -295,9 +283,6 @@ else let outputs = outputs'; - references = nativeBuildInputs ++ buildInputs - ++ propagatedNativeBuildInputs ++ propagatedBuildInputs; - dependencies = map (map chooseDevOutputs) [ [ (map (drv: drv.__spliced.buildBuild or drv) (checkDependencyList "depsBuildBuild" depsBuildBuild)) @@ -347,18 +332,14 @@ else let unique (concatMap (input: input.__propagatedImpureHostDeps or []) (concatLists propagatedDependencies)); - envIsExportable = isAttrs env && !isDerivation env; - derivationArg = - (removeAttrs attrs - (["meta" "passthru" "pos" + removeAttrs attrs [ "checkInputs" "installCheckInputs" "nativeCheckInputs" "nativeInstallCheckInputs" "__contentAddressed" "__darwinAllowLocalNetworking" "__impureHostDeps" "__propagatedImpureHostDeps" "sandboxProfile" "propagatedSandboxProfile"] - ++ optional (__structuredAttrs || envIsExportable) "env")) // (optionalAttrs (attrs ? name || (attrs ? pname && attrs ? version)) { name = let @@ -386,7 +367,7 @@ else let assert assertMsg (attrs ? version && attrs.version != null) "The ‘version’ attribute cannot be null."; "${attrs.pname}${staticMarker}${hostSuffix}-${attrs.version}" ); - }) // optionalAttrs __structuredAttrs { env = checkedEnv; } // { + }) // { builder = attrs.realBuilder or stdenv.shell; args = attrs.args or ["-e" (attrs.builder or ./default-builder.sh)]; inherit stdenv; @@ -424,54 +405,6 @@ else let ++ optional (elem "host" configurePlatforms) "--host=${stdenv.hostPlatform.config}" ++ optional (elem "target" configurePlatforms) "--target=${stdenv.targetPlatform.config}"; - cmakeFlags = - cmakeFlags - ++ optionals (stdenv.hostPlatform != stdenv.buildPlatform) ([ - "-DCMAKE_SYSTEM_NAME=${findFirst isString "Generic" (optional (!stdenv.hostPlatform.isRedox) stdenv.hostPlatform.uname.system)}" - ] ++ optionals (stdenv.hostPlatform.uname.processor != null) [ - "-DCMAKE_SYSTEM_PROCESSOR=${stdenv.hostPlatform.uname.processor}" - ] ++ optionals (stdenv.hostPlatform.uname.release != null) [ - "-DCMAKE_SYSTEM_VERSION=${stdenv.hostPlatform.uname.release}" - ] ++ optionals (stdenv.hostPlatform.isDarwin) [ - "-DCMAKE_OSX_ARCHITECTURES=${stdenv.hostPlatform.darwinArch}" - ] ++ optionals (stdenv.buildPlatform.uname.system != null) [ - "-DCMAKE_HOST_SYSTEM_NAME=${stdenv.buildPlatform.uname.system}" - ] ++ optionals (stdenv.buildPlatform.uname.processor != null) [ - "-DCMAKE_HOST_SYSTEM_PROCESSOR=${stdenv.buildPlatform.uname.processor}" - ] ++ optionals (stdenv.buildPlatform.uname.release != null) [ - "-DCMAKE_HOST_SYSTEM_VERSION=${stdenv.buildPlatform.uname.release}" - ] ++ optionals (stdenv.buildPlatform.canExecute stdenv.hostPlatform) [ - "-DCMAKE_CROSSCOMPILING_EMULATOR=env" - ] ++ lib.optionals stdenv.hostPlatform.isStatic [ - "-DCMAKE_LINK_SEARCH_START_STATIC=ON" - ]); - - mesonFlags = - let - # See https://mesonbuild.com/Reference-tables.html#cpu-families - cpuFamily = platform: with platform; - /**/ if isAarch32 then "arm" - else if isx86_32 then "x86" - else platform.uname.processor; - - crossFile = builtins.toFile "cross-file.conf" '' - [properties] - bindgen_clang_arguments = ['-target', '${stdenv.targetPlatform.config}'] - needs_exe_wrapper = ${boolToString (!stdenv.buildPlatform.canExecute stdenv.hostPlatform)} - - [host_machine] - system = '${stdenv.targetPlatform.parsed.kernel.name}' - cpu_family = '${cpuFamily stdenv.targetPlatform}' - cpu = '${stdenv.targetPlatform.parsed.cpu.name}' - endian = ${if stdenv.targetPlatform.isLittleEndian then "'little'" else "'big'"} - - [binaries] - llvm-config = 'llvm-config-native' - rust = ['rustc', '--target', '${stdenv.targetPlatform.rust.rustcTargetSpec}'] - ''; - crossFlags = optionals (stdenv.hostPlatform != stdenv.buildPlatform) [ "--cross-file=${crossFile}" ]; - in crossFlags ++ mesonFlags; - inherit patches; inherit doCheck doInstallCheck; @@ -542,22 +475,6 @@ else let mapNullable unsafeDerivationToUntrackedOutpath attrs.allowedRequisites; }; - meta = checkMeta.commonMeta { inherit validity attrs pos references; }; - validity = checkMeta.assertValidity { inherit meta attrs; }; - - checkedEnv = - let - overlappingNames = attrNames (builtins.intersectAttrs env derivationArg); - in - assert assertMsg envIsExportable - "When using structured attributes, `env` must be an attribute set of environment variables."; - assert assertMsg (overlappingNames == [ ]) - "The ‘env’ attribute set cannot contain any attributes passed to derivation. The following attributes are overlapping: ${concatStringsSep ", " overlappingNames}"; - mapAttrs - (n: v: assert assertMsg (isString v || isBool v || isInt v || isDerivation v) - "The ‘env’ attribute set can only contain derivation, string, boolean or integer attributes. The ‘${n}’ attribute is of type ${builtins.typeOf v}."; v) - env; - in derivationArg; From 90197b68338ae7bbda084aaec59c74ec130ccf4e Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Tue, 12 Mar 2024 18:37:17 +0100 Subject: [PATCH 05/11] make-derivation.nix: Apply map composition law ... after inlining chooseDevOutputs. --- pkgs/stdenv/generic/make-derivation.nix | 30 ++++++++++++------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/pkgs/stdenv/generic/make-derivation.nix b/pkgs/stdenv/generic/make-derivation.nix index 785d8c36f869..659c191262e2 100644 --- a/pkgs/stdenv/generic/make-derivation.nix +++ b/pkgs/stdenv/generic/make-derivation.nix @@ -9,7 +9,6 @@ let assertMsg attrNames boolToString - chooseDevOutputs concatLists concatMap concatMapStrings @@ -20,6 +19,7 @@ let filter findFirst flip + getDev head imap1 isAttrs @@ -283,32 +283,32 @@ else let outputs = outputs'; - dependencies = map (map chooseDevOutputs) [ + dependencies = [ [ - (map (drv: drv.__spliced.buildBuild or drv) (checkDependencyList "depsBuildBuild" depsBuildBuild)) - (map (drv: drv.__spliced.buildHost or drv) (checkDependencyList "nativeBuildInputs" nativeBuildInputs')) - (map (drv: drv.__spliced.buildTarget or drv) (checkDependencyList "depsBuildTarget" depsBuildTarget)) + (map (drv: getDev drv.__spliced.buildBuild or drv) (checkDependencyList "depsBuildBuild" depsBuildBuild)) + (map (drv: getDev drv.__spliced.buildHost or drv) (checkDependencyList "nativeBuildInputs" nativeBuildInputs')) + (map (drv: getDev drv.__spliced.buildTarget or drv) (checkDependencyList "depsBuildTarget" depsBuildTarget)) ] [ - (map (drv: drv.__spliced.hostHost or drv) (checkDependencyList "depsHostHost" depsHostHost)) - (map (drv: drv.__spliced.hostTarget or drv) (checkDependencyList "buildInputs" buildInputs')) + (map (drv: getDev drv.__spliced.hostHost or drv) (checkDependencyList "depsHostHost" depsHostHost)) + (map (drv: getDev drv.__spliced.hostTarget or drv) (checkDependencyList "buildInputs" buildInputs')) ] [ - (map (drv: drv.__spliced.targetTarget or drv) (checkDependencyList "depsTargetTarget" depsTargetTarget)) + (map (drv: getDev drv.__spliced.targetTarget or drv) (checkDependencyList "depsTargetTarget" depsTargetTarget)) ] ]; - propagatedDependencies = map (map chooseDevOutputs) [ + propagatedDependencies = [ [ - (map (drv: drv.__spliced.buildBuild or drv) (checkDependencyList "depsBuildBuildPropagated" depsBuildBuildPropagated)) - (map (drv: drv.__spliced.buildHost or drv) (checkDependencyList "propagatedNativeBuildInputs" propagatedNativeBuildInputs)) - (map (drv: drv.__spliced.buildTarget or drv) (checkDependencyList "depsBuildTargetPropagated" depsBuildTargetPropagated)) + (map (drv: getDev drv.__spliced.buildBuild or drv) (checkDependencyList "depsBuildBuildPropagated" depsBuildBuildPropagated)) + (map (drv: getDev drv.__spliced.buildHost or drv) (checkDependencyList "propagatedNativeBuildInputs" propagatedNativeBuildInputs)) + (map (drv: getDev drv.__spliced.buildTarget or drv) (checkDependencyList "depsBuildTargetPropagated" depsBuildTargetPropagated)) ] [ - (map (drv: drv.__spliced.hostHost or drv) (checkDependencyList "depsHostHostPropagated" depsHostHostPropagated)) - (map (drv: drv.__spliced.hostTarget or drv) (checkDependencyList "propagatedBuildInputs" propagatedBuildInputs)) + (map (drv: getDev drv.__spliced.hostHost or drv) (checkDependencyList "depsHostHostPropagated" depsHostHostPropagated)) + (map (drv: getDev drv.__spliced.hostTarget or drv) (checkDependencyList "propagatedBuildInputs" propagatedBuildInputs)) ] [ - (map (drv: drv.__spliced.targetTarget or drv) (checkDependencyList "depsTargetTargetPropagated" depsTargetTargetPropagated)) + (map (drv: getDev drv.__spliced.targetTarget or drv) (checkDependencyList "depsTargetTargetPropagated" depsTargetTargetPropagated)) ] ]; From 2fb7255cc03e83eb9c860e03218fdf3644208e98 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Tue, 12 Mar 2024 18:39:19 +0100 Subject: [PATCH 06/11] make-derivation.nix: Evaluate flip --- pkgs/stdenv/generic/make-derivation.nix | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pkgs/stdenv/generic/make-derivation.nix b/pkgs/stdenv/generic/make-derivation.nix index 659c191262e2..c3230a27b42c 100644 --- a/pkgs/stdenv/generic/make-derivation.nix +++ b/pkgs/stdenv/generic/make-derivation.nix @@ -18,7 +18,6 @@ let extendDerivation filter findFirst - flip getDev head imap1 @@ -261,10 +260,13 @@ let erroneousHardeningFlags = subtractLists knownHardeningFlags (hardeningEnable ++ remove "all" hardeningDisable); checkDependencyList = checkDependencyList' []; - checkDependencyList' = positions: name: deps: flip imap1 deps (index: dep: - if isDerivation dep || dep == null || builtins.isString dep || builtins.isPath dep then dep - else if isList dep then checkDependencyList' ([index] ++ positions) name dep - else throw "Dependency is not of a valid type: ${concatMapStrings (ix: "element ${toString ix} of ") ([index] ++ positions)}${name} for ${attrs.name or attrs.pname}"); + checkDependencyList' = positions: name: deps: + imap1 + (index: dep: + if isDerivation dep || dep == null || builtins.isString dep || builtins.isPath dep then dep + else if isList dep then checkDependencyList' ([index] ++ positions) name dep + else throw "Dependency is not of a valid type: ${concatMapStrings (ix: "element ${toString ix} of ") ([index] ++ positions)}${name} for ${attrs.name or attrs.pname}") + deps; in if builtins.length erroneousHardeningFlags != 0 then abort ("mkDerivation was called with unsupported hardening flags: " + lib.generators.toPretty {} { inherit erroneousHardeningFlags hardeningDisable hardeningEnable knownHardeningFlags; From 464d8c4a6c6282ea51e80b695f4d0eb39dd24b96 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Tue, 12 Mar 2024 18:41:18 +0100 Subject: [PATCH 07/11] make-derivation.nix: Float inward darwin-specific derivation attributes --- pkgs/stdenv/generic/make-derivation.nix | 45 +++++++++++++------------ 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/pkgs/stdenv/generic/make-derivation.nix b/pkgs/stdenv/generic/make-derivation.nix index c3230a27b42c..36d34b796f90 100644 --- a/pkgs/stdenv/generic/make-derivation.nix +++ b/pkgs/stdenv/generic/make-derivation.nix @@ -314,26 +314,6 @@ else let ] ]; - computedSandboxProfile = - concatMap (input: input.__propagatedSandboxProfile or []) - (stdenv.extraNativeBuildInputs - ++ stdenv.extraBuildInputs - ++ concatLists dependencies); - - computedPropagatedSandboxProfile = - concatMap (input: input.__propagatedSandboxProfile or []) - (concatLists propagatedDependencies); - - computedImpureHostDeps = - unique (concatMap (input: input.__propagatedImpureHostDeps or []) - (stdenv.extraNativeBuildInputs - ++ stdenv.extraBuildInputs - ++ concatLists dependencies)); - - computedPropagatedImpureHostDeps = - unique (concatMap (input: input.__propagatedImpureHostDeps or []) - (concatLists propagatedDependencies)); - derivationArg = removeAttrs attrs [ "checkInputs" "installCheckInputs" @@ -426,7 +406,28 @@ else let NIX_HARDENING_ENABLE = enabledHardeningOptions; } // optionalAttrs (stdenv.hostPlatform.isx86_64 && stdenv.hostPlatform ? gcc.arch) { requiredSystemFeatures = attrs.requiredSystemFeatures or [] ++ [ "gccarch-${stdenv.hostPlatform.gcc.arch}" ]; - } // optionalAttrs (stdenv.buildPlatform.isDarwin) { + } // optionalAttrs (stdenv.buildPlatform.isDarwin) ( + let + computedSandboxProfile = + concatMap (input: input.__propagatedSandboxProfile or []) + (stdenv.extraNativeBuildInputs + ++ stdenv.extraBuildInputs + ++ concatLists dependencies); + + computedPropagatedSandboxProfile = + concatMap (input: input.__propagatedSandboxProfile or []) + (concatLists propagatedDependencies); + + computedImpureHostDeps = + unique (concatMap (input: input.__propagatedImpureHostDeps or []) + (stdenv.extraNativeBuildInputs + ++ stdenv.extraBuildInputs + ++ concatLists dependencies)); + + computedPropagatedImpureHostDeps = + unique (concatMap (input: input.__propagatedImpureHostDeps or []) + (concatLists propagatedDependencies)); + in { inherit __darwinAllowLocalNetworking; # TODO: remove `unique` once nix has a list canonicalization primitive __sandboxProfile = @@ -441,7 +442,7 @@ else let "/bin/sh" ]; __propagatedImpureHostDeps = computedPropagatedImpureHostDeps ++ __propagatedImpureHostDeps; - } // + }) // # If we use derivations directly here, they end up as build-time dependencies. # This is especially problematic in the case of disallowed*, since the disallowed # derivations will be built by nix as build-time dependencies, while those From cac23248b1f873d10f4604b0f0facea81987367b Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Tue, 12 Mar 2024 18:41:57 +0100 Subject: [PATCH 08/11] make-derivation.nix: Float out knownHardeningFlags --- pkgs/stdenv/generic/make-derivation.nix | 27 +++++++++++++------------ 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/pkgs/stdenv/generic/make-derivation.nix b/pkgs/stdenv/generic/make-derivation.nix index 36d34b796f90..b86cfd1f5927 100644 --- a/pkgs/stdenv/generic/make-derivation.nix +++ b/pkgs/stdenv/generic/make-derivation.nix @@ -110,6 +110,20 @@ let makeDerivationExtensible (self: attrs // (if builtins.isFunction f0 || f0?__functor then f self attrs else f0))) attrs; + knownHardeningFlags = [ + "bindnow" + "format" + "fortify" + "fortify3" + "pic" + "pie" + "relro" + "stackprotector" + "strictoverflow" + "trivialautovarinit" + "zerocallusedregs" + ]; + makeDerivationArgument = @@ -235,19 +249,6 @@ let # disabling fortify implies fortify3 should also be disabled then unique (hardeningDisable ++ [ "fortify3" ]) else hardeningDisable; - knownHardeningFlags = [ - "bindnow" - "format" - "fortify" - "fortify3" - "pic" - "pie" - "relro" - "stackprotector" - "strictoverflow" - "trivialautovarinit" - "zerocallusedregs" - ]; defaultHardeningFlags = (if stdenv.hasCC then stdenv.cc else {}).defaultHardeningFlags or # fallback safe-ish set of flags From 797ad5ae87786ba0911d0d2f9ce4cc9ec0cdb7ac Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Tue, 12 Mar 2024 18:43:11 +0100 Subject: [PATCH 09/11] make-derivation.nix: Float out new constant removedOrReplacedAttrNames --- pkgs/stdenv/generic/make-derivation.nix | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/pkgs/stdenv/generic/make-derivation.nix b/pkgs/stdenv/generic/make-derivation.nix index b86cfd1f5927..3ada1f6ec973 100644 --- a/pkgs/stdenv/generic/make-derivation.nix +++ b/pkgs/stdenv/generic/make-derivation.nix @@ -124,6 +124,15 @@ let "zerocallusedregs" ]; + removedOrReplacedAttrNames = [ + "checkInputs" "installCheckInputs" + "nativeCheckInputs" "nativeInstallCheckInputs" + "__contentAddressed" + "__darwinAllowLocalNetworking" + "__impureHostDeps" "__propagatedImpureHostDeps" + "sandboxProfile" "propagatedSandboxProfile" + ]; + makeDerivationArgument = @@ -316,13 +325,7 @@ else let ]; derivationArg = - removeAttrs attrs [ - "checkInputs" "installCheckInputs" - "nativeCheckInputs" "nativeInstallCheckInputs" - "__contentAddressed" - "__darwinAllowLocalNetworking" - "__impureHostDeps" "__propagatedImpureHostDeps" - "sandboxProfile" "propagatedSandboxProfile"] + removeAttrs attrs removedOrReplacedAttrNames // (optionalAttrs (attrs ? name || (attrs ? pname && attrs ? version)) { name = let From 0ab7b23637df66aec41bf34fd261f43d2e95309d Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Tue, 12 Mar 2024 19:06:07 +0100 Subject: [PATCH 10/11] make-derivation.nix: Update inline docs --- pkgs/stdenv/generic/make-derivation.nix | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pkgs/stdenv/generic/make-derivation.nix b/pkgs/stdenv/generic/make-derivation.nix index 3ada1f6ec973..b469904c6a33 100644 --- a/pkgs/stdenv/generic/make-derivation.nix +++ b/pkgs/stdenv/generic/make-derivation.nix @@ -136,8 +136,9 @@ let makeDerivationArgument = -# `mkDerivation` wraps the builtin `derivation` function to -# produce derivations that use this stdenv and its shell. +# `makeDerivationArgument` is responsible for the `mkDerivation` arguments that +# affect the actual derivation, excluding a few behaviors that are not +# essential, and specific to `mkDerivation`: `env`, `cmakeFlags`, `mesonFlags`. # # See also: # @@ -490,6 +491,11 @@ mkDerivationSimple = overrideAttrs: # `mkDerivation` wraps the builtin `derivation` function to # produce derivations that use this stdenv and its shell. # +# Internally, it delegates most of its behavior to `makeDerivationArgument`, +# except for the `env`, `cmakeFlags`, and `mesonFlags` attributes, as well +# as the attributes `meta` and `passthru` that affect [package attributes], +# and not the derivation itself. +# # See also: # # * https://nixos.org/nixpkgs/manual/#sec-using-stdenv @@ -497,6 +503,8 @@ mkDerivationSimple = overrideAttrs: # # * https://nixos.org/manual/nix/stable/expressions/derivations.html#derivations # Explanation about derivations in general +# +# * [package attributes]: https://nixos.org/manual/nix/stable/glossary#package-attribute-set { # Configure Phase From ba463e70e3d8c86d16b38ddddfe4f30aeede26b4 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Tue, 12 Mar 2024 20:15:47 +0100 Subject: [PATCH 11/11] make-derivation.nix: Float out unsafeDerivationToUntrackedOutpath --- pkgs/stdenv/generic/make-derivation.nix | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pkgs/stdenv/generic/make-derivation.nix b/pkgs/stdenv/generic/make-derivation.nix index b469904c6a33..c40eaee5e4d6 100644 --- a/pkgs/stdenv/generic/make-derivation.nix +++ b/pkgs/stdenv/generic/make-derivation.nix @@ -133,6 +133,13 @@ let "sandboxProfile" "propagatedSandboxProfile" ]; + # Turn a derivation into its outPath without a string context attached. + # See the comment at the usage site. + unsafeDerivationToUntrackedOutpath = drv: + if isDerivation drv + then builtins.unsafeDiscardStringContext drv.outPath + else drv; + makeDerivationArgument = @@ -242,13 +249,6 @@ let separateDebugInfo' = separateDebugInfo && stdenv.hostPlatform.isLinux; outputs' = outputs ++ optional separateDebugInfo' "debug"; - # Turn a derivation into its outPath without a string context attached. - # See the comment at the usage site. - unsafeDerivationToUntrackedOutpath = drv: - if isDerivation drv - then builtins.unsafeDiscardStringContext drv.outPath - else drv; - noNonNativeDeps = builtins.length (depsBuildTarget ++ depsBuildTargetPropagated ++ depsHostHost ++ depsHostHostPropagated ++ buildInputs ++ propagatedBuildInputs