···6677let
88 # N.B. Keep in sync with default arg for stdenv/generic.
99- defaultMkDerivationFromStdenv = import ./generic/make-derivation.nix { inherit lib config; };
99+ defaultMkDerivationFromStdenv = stdenv: (import ./generic/make-derivation.nix { inherit lib config; } stdenv).mkDerivation;
10101111 # Low level function to help with overriding `mkDerivationFromStdenv`. One
1212 # gives it the old stdenv arguments and a "continuation" function, and
+1-1
pkgs/stdenv/generic/default.nix
···52525353, # The implementation of `mkDerivation`, parameterized with the final stdenv so we can tie the knot.
5454 # This is convient to have as a parameter so the stdenv "adapters" work better
5555- mkDerivationFromStdenv ? import ./make-derivation.nix { inherit lib config; }
5555+ mkDerivationFromStdenv ? stdenv: (import ./make-derivation.nix { inherit lib config; } stdenv).mkDerivation
5656}:
57575858let
+169-145
pkgs/stdenv/generic/make-derivation.nix
···99 assertMsg
1010 attrNames
1111 boolToString
1212- chooseDevOutputs
1312 concatLists
1413 concatMap
1514 concatMapStrings
···1918 extendDerivation
2019 filter
2120 findFirst
2222- flip
2121+ getDev
2322 head
2423 imap1
2524 isAttrs
···3938 subtractLists
4039 unique
4140 ;
4141+4242+ inherit (import ../../build-support/lib/cmake.nix { inherit lib stdenv; }) makeCMakeFlags;
4343+ inherit (import ../../build-support/lib/meson.nix { inherit lib stdenv; }) makeMesonFlags;
4444+4545+ mkDerivation =
4646+ fnOrAttrs:
4747+ if builtins.isFunction fnOrAttrs
4848+ then makeDerivationExtensible fnOrAttrs
4949+ else makeDerivationExtensibleConst fnOrAttrs;
42504351 checkMeta = import ./check-meta.nix {
4452 inherit lib config;
···102110 makeDerivationExtensible (self: attrs // (if builtins.isFunction f0 || f0?__functor then f self attrs else f0)))
103111 attrs;
104112105105- mkDerivationSimple = overrideAttrs:
113113+ knownHardeningFlags = [
114114+ "bindnow"
115115+ "format"
116116+ "fortify"
117117+ "fortify3"
118118+ "pic"
119119+ "pie"
120120+ "relro"
121121+ "stackprotector"
122122+ "strictoverflow"
123123+ "trivialautovarinit"
124124+ "zerocallusedregs"
125125+ ];
126126+127127+ removedOrReplacedAttrNames = [
128128+ "checkInputs" "installCheckInputs"
129129+ "nativeCheckInputs" "nativeInstallCheckInputs"
130130+ "__contentAddressed"
131131+ "__darwinAllowLocalNetworking"
132132+ "__impureHostDeps" "__propagatedImpureHostDeps"
133133+ "sandboxProfile" "propagatedSandboxProfile"
134134+ ];
135135+136136+ # Turn a derivation into its outPath without a string context attached.
137137+ # See the comment at the usage site.
138138+ unsafeDerivationToUntrackedOutpath = drv:
139139+ if isDerivation drv
140140+ then builtins.unsafeDiscardStringContext drv.outPath
141141+ else drv;
142142+143143+ makeDerivationArgument =
106144107145108108-# `mkDerivation` wraps the builtin `derivation` function to
109109-# produce derivations that use this stdenv and its shell.
146146+# `makeDerivationArgument` is responsible for the `mkDerivation` arguments that
147147+# affect the actual derivation, excluding a few behaviors that are not
148148+# essential, and specific to `mkDerivation`: `env`, `cmakeFlags`, `mesonFlags`.
110149#
111150# See also:
112151#
···146185147186# Configure Phase
148187, configureFlags ? []
149149-, cmakeFlags ? []
150150-, mesonFlags ? []
151188, # Target is not included by default because most programs don't care.
152189 # Including it then would cause needless mass rebuilds.
153190 #
···169206170207, enableParallelBuilding ? config.enableParallelBuildingByDefault
171208172172-, meta ? {}
173173-, passthru ? {}
174174-, pos ? # position used in error messages and for meta.position
175175- (if attrs.meta.description or null != null
176176- then builtins.unsafeGetAttrPos "description" attrs.meta
177177- else if attrs.version or null != null
178178- then builtins.unsafeGetAttrPos "version" attrs
179179- else builtins.unsafeGetAttrPos "name" attrs)
180209, separateDebugInfo ? false
181210, outputs ? [ "out" ]
182211, __darwinAllowLocalNetworking ? false
···198227# but for anything complex, be prepared to debug if enabling.
199228, __structuredAttrs ? config.structuredAttrsByDefault or false
200229201201-, env ? { }
202202-203230, ... } @ attrs:
204231205232# Policy on acceptable hash types in nixpkgs
···222249 separateDebugInfo' = separateDebugInfo && stdenv.hostPlatform.isLinux;
223250 outputs' = outputs ++ optional separateDebugInfo' "debug";
224251225225- # Turn a derivation into its outPath without a string context attached.
226226- # See the comment at the usage site.
227227- unsafeDerivationToUntrackedOutpath = drv:
228228- if isDerivation drv
229229- then builtins.unsafeDiscardStringContext drv.outPath
230230- else drv;
231231-232252 noNonNativeDeps = builtins.length (depsBuildTarget ++ depsBuildTargetPropagated
233253 ++ depsHostHost ++ depsHostHostPropagated
234254 ++ buildInputs ++ propagatedBuildInputs
···239259 # disabling fortify implies fortify3 should also be disabled
240260 then unique (hardeningDisable ++ [ "fortify3" ])
241261 else hardeningDisable;
242242- knownHardeningFlags = [
243243- "bindnow"
244244- "format"
245245- "fortify"
246246- "fortify3"
247247- "pic"
248248- "pie"
249249- "relro"
250250- "stackprotector"
251251- "strictoverflow"
252252- "trivialautovarinit"
253253- "zerocallusedregs"
254254- ];
255262 defaultHardeningFlags =
256263 (if stdenv.hasCC then stdenv.cc else {}).defaultHardeningFlags or
257264 # fallback safe-ish set of flags
···264271 erroneousHardeningFlags = subtractLists knownHardeningFlags (hardeningEnable ++ remove "all" hardeningDisable);
265272266273 checkDependencyList = checkDependencyList' [];
267267- checkDependencyList' = positions: name: deps: flip imap1 deps (index: dep:
268268- if isDerivation dep || dep == null || builtins.isString dep || builtins.isPath dep then dep
269269- else if isList dep then checkDependencyList' ([index] ++ positions) name dep
270270- else throw "Dependency is not of a valid type: ${concatMapStrings (ix: "element ${toString ix} of ") ([index] ++ positions)}${name} for ${attrs.name or attrs.pname}");
274274+ checkDependencyList' = positions: name: deps:
275275+ imap1
276276+ (index: dep:
277277+ if isDerivation dep || dep == null || builtins.isString dep || builtins.isPath dep then dep
278278+ else if isList dep then checkDependencyList' ([index] ++ positions) name dep
279279+ else throw "Dependency is not of a valid type: ${concatMapStrings (ix: "element ${toString ix} of ") ([index] ++ positions)}${name} for ${attrs.name or attrs.pname}")
280280+ deps;
271281in if builtins.length erroneousHardeningFlags != 0
272282then abort ("mkDerivation was called with unsupported hardening flags: " + lib.generators.toPretty {} {
273283 inherit erroneousHardeningFlags hardeningDisable hardeningEnable knownHardeningFlags;
···286296287297 outputs = outputs';
288298289289- references = nativeBuildInputs ++ buildInputs
290290- ++ propagatedNativeBuildInputs ++ propagatedBuildInputs;
291291-292292- dependencies = map (map chooseDevOutputs) [
299299+ dependencies = [
293300 [
294294- (map (drv: drv.__spliced.buildBuild or drv) (checkDependencyList "depsBuildBuild" depsBuildBuild))
295295- (map (drv: drv.__spliced.buildHost or drv) (checkDependencyList "nativeBuildInputs" nativeBuildInputs'))
296296- (map (drv: drv.__spliced.buildTarget or drv) (checkDependencyList "depsBuildTarget" depsBuildTarget))
301301+ (map (drv: getDev drv.__spliced.buildBuild or drv) (checkDependencyList "depsBuildBuild" depsBuildBuild))
302302+ (map (drv: getDev drv.__spliced.buildHost or drv) (checkDependencyList "nativeBuildInputs" nativeBuildInputs'))
303303+ (map (drv: getDev drv.__spliced.buildTarget or drv) (checkDependencyList "depsBuildTarget" depsBuildTarget))
297304 ]
298305 [
299299- (map (drv: drv.__spliced.hostHost or drv) (checkDependencyList "depsHostHost" depsHostHost))
300300- (map (drv: drv.__spliced.hostTarget or drv) (checkDependencyList "buildInputs" buildInputs'))
306306+ (map (drv: getDev drv.__spliced.hostHost or drv) (checkDependencyList "depsHostHost" depsHostHost))
307307+ (map (drv: getDev drv.__spliced.hostTarget or drv) (checkDependencyList "buildInputs" buildInputs'))
301308 ]
302309 [
303303- (map (drv: drv.__spliced.targetTarget or drv) (checkDependencyList "depsTargetTarget" depsTargetTarget))
310310+ (map (drv: getDev drv.__spliced.targetTarget or drv) (checkDependencyList "depsTargetTarget" depsTargetTarget))
304311 ]
305312 ];
306306- propagatedDependencies = map (map chooseDevOutputs) [
313313+ propagatedDependencies = [
307314 [
308308- (map (drv: drv.__spliced.buildBuild or drv) (checkDependencyList "depsBuildBuildPropagated" depsBuildBuildPropagated))
309309- (map (drv: drv.__spliced.buildHost or drv) (checkDependencyList "propagatedNativeBuildInputs" propagatedNativeBuildInputs))
310310- (map (drv: drv.__spliced.buildTarget or drv) (checkDependencyList "depsBuildTargetPropagated" depsBuildTargetPropagated))
315315+ (map (drv: getDev drv.__spliced.buildBuild or drv) (checkDependencyList "depsBuildBuildPropagated" depsBuildBuildPropagated))
316316+ (map (drv: getDev drv.__spliced.buildHost or drv) (checkDependencyList "propagatedNativeBuildInputs" propagatedNativeBuildInputs))
317317+ (map (drv: getDev drv.__spliced.buildTarget or drv) (checkDependencyList "depsBuildTargetPropagated" depsBuildTargetPropagated))
311318 ]
312319 [
313313- (map (drv: drv.__spliced.hostHost or drv) (checkDependencyList "depsHostHostPropagated" depsHostHostPropagated))
314314- (map (drv: drv.__spliced.hostTarget or drv) (checkDependencyList "propagatedBuildInputs" propagatedBuildInputs))
320320+ (map (drv: getDev drv.__spliced.hostHost or drv) (checkDependencyList "depsHostHostPropagated" depsHostHostPropagated))
321321+ (map (drv: getDev drv.__spliced.hostTarget or drv) (checkDependencyList "propagatedBuildInputs" propagatedBuildInputs))
315322 ]
316323 [
317317- (map (drv: drv.__spliced.targetTarget or drv) (checkDependencyList "depsTargetTargetPropagated" depsTargetTargetPropagated))
324324+ (map (drv: getDev drv.__spliced.targetTarget or drv) (checkDependencyList "depsTargetTargetPropagated" depsTargetTargetPropagated))
318325 ]
319326 ];
320327321321- computedSandboxProfile =
322322- concatMap (input: input.__propagatedSandboxProfile or [])
323323- (stdenv.extraNativeBuildInputs
324324- ++ stdenv.extraBuildInputs
325325- ++ concatLists dependencies);
326326-327327- computedPropagatedSandboxProfile =
328328- concatMap (input: input.__propagatedSandboxProfile or [])
329329- (concatLists propagatedDependencies);
330330-331331- computedImpureHostDeps =
332332- unique (concatMap (input: input.__propagatedImpureHostDeps or [])
333333- (stdenv.extraNativeBuildInputs
334334- ++ stdenv.extraBuildInputs
335335- ++ concatLists dependencies));
336336-337337- computedPropagatedImpureHostDeps =
338338- unique (concatMap (input: input.__propagatedImpureHostDeps or [])
339339- (concatLists propagatedDependencies));
340340-341341- envIsExportable = isAttrs env && !isDerivation env;
342342-343328 derivationArg =
344344- (removeAttrs attrs
345345- (["meta" "passthru" "pos"
346346- "checkInputs" "installCheckInputs"
347347- "nativeCheckInputs" "nativeInstallCheckInputs"
348348- "__contentAddressed"
349349- "__darwinAllowLocalNetworking"
350350- "__impureHostDeps" "__propagatedImpureHostDeps"
351351- "sandboxProfile" "propagatedSandboxProfile"]
352352- ++ optional (__structuredAttrs || envIsExportable) "env"))
329329+ removeAttrs attrs removedOrReplacedAttrNames
353330 // (optionalAttrs (attrs ? name || (attrs ? pname && attrs ? version)) {
354331 name =
355332 let
···377354 assert assertMsg (attrs ? version && attrs.version != null) "The ‘version’ attribute cannot be null.";
378355 "${attrs.pname}${staticMarker}${hostSuffix}-${attrs.version}"
379356 );
380380- }) // optionalAttrs __structuredAttrs { env = checkedEnv; } // {
357357+ }) // {
381358 builder = attrs.realBuilder or stdenv.shell;
382359 args = attrs.args or ["-e" (attrs.builder or ./default-builder.sh)];
383360 inherit stdenv;
···415392 ++ optional (elem "host" configurePlatforms) "--host=${stdenv.hostPlatform.config}"
416393 ++ optional (elem "target" configurePlatforms) "--target=${stdenv.targetPlatform.config}";
417394418418- cmakeFlags =
419419- cmakeFlags
420420- ++ optionals (stdenv.hostPlatform != stdenv.buildPlatform) ([
421421- "-DCMAKE_SYSTEM_NAME=${findFirst isString "Generic" (optional (!stdenv.hostPlatform.isRedox) stdenv.hostPlatform.uname.system)}"
422422- ] ++ optionals (stdenv.hostPlatform.uname.processor != null) [
423423- "-DCMAKE_SYSTEM_PROCESSOR=${stdenv.hostPlatform.uname.processor}"
424424- ] ++ optionals (stdenv.hostPlatform.uname.release != null) [
425425- "-DCMAKE_SYSTEM_VERSION=${stdenv.hostPlatform.uname.release}"
426426- ] ++ optionals (stdenv.hostPlatform.isDarwin) [
427427- "-DCMAKE_OSX_ARCHITECTURES=${stdenv.hostPlatform.darwinArch}"
428428- ] ++ optionals (stdenv.buildPlatform.uname.system != null) [
429429- "-DCMAKE_HOST_SYSTEM_NAME=${stdenv.buildPlatform.uname.system}"
430430- ] ++ optionals (stdenv.buildPlatform.uname.processor != null) [
431431- "-DCMAKE_HOST_SYSTEM_PROCESSOR=${stdenv.buildPlatform.uname.processor}"
432432- ] ++ optionals (stdenv.buildPlatform.uname.release != null) [
433433- "-DCMAKE_HOST_SYSTEM_VERSION=${stdenv.buildPlatform.uname.release}"
434434- ] ++ optionals (stdenv.buildPlatform.canExecute stdenv.hostPlatform) [
435435- "-DCMAKE_CROSSCOMPILING_EMULATOR=env"
436436- ] ++ lib.optionals stdenv.hostPlatform.isStatic [
437437- "-DCMAKE_LINK_SEARCH_START_STATIC=ON"
438438- ]);
439439-440440- mesonFlags =
441441- let
442442- # See https://mesonbuild.com/Reference-tables.html#cpu-families
443443- cpuFamily = platform: with platform;
444444- /**/ if isAarch32 then "arm"
445445- else if isx86_32 then "x86"
446446- else platform.uname.processor;
447447-448448- crossFile = builtins.toFile "cross-file.conf" ''
449449- [properties]
450450- bindgen_clang_arguments = ['-target', '${stdenv.targetPlatform.config}']
451451- needs_exe_wrapper = ${boolToString (!stdenv.buildPlatform.canExecute stdenv.hostPlatform)}
452452-453453- [host_machine]
454454- system = '${stdenv.targetPlatform.parsed.kernel.name}'
455455- cpu_family = '${cpuFamily stdenv.targetPlatform}'
456456- cpu = '${stdenv.targetPlatform.parsed.cpu.name}'
457457- endian = ${if stdenv.targetPlatform.isLittleEndian then "'little'" else "'big'"}
458458-459459- [binaries]
460460- llvm-config = 'llvm-config-native'
461461- rust = ['rustc', '--target', '${stdenv.targetPlatform.rust.rustcTargetSpec}']
462462- '';
463463- crossFlags = optionals (stdenv.hostPlatform != stdenv.buildPlatform) [ "--cross-file=${crossFile}" ];
464464- in crossFlags ++ mesonFlags;
465465-466395 inherit patches;
467396468397 inherit doCheck doInstallCheck;
···482411 NIX_HARDENING_ENABLE = enabledHardeningOptions;
483412 } // optionalAttrs (stdenv.hostPlatform.isx86_64 && stdenv.hostPlatform ? gcc.arch) {
484413 requiredSystemFeatures = attrs.requiredSystemFeatures or [] ++ [ "gccarch-${stdenv.hostPlatform.gcc.arch}" ];
485485- } // optionalAttrs (stdenv.buildPlatform.isDarwin) {
414414+ } // optionalAttrs (stdenv.buildPlatform.isDarwin) (
415415+ let
416416+ computedSandboxProfile =
417417+ concatMap (input: input.__propagatedSandboxProfile or [])
418418+ (stdenv.extraNativeBuildInputs
419419+ ++ stdenv.extraBuildInputs
420420+ ++ concatLists dependencies);
421421+422422+ computedPropagatedSandboxProfile =
423423+ concatMap (input: input.__propagatedSandboxProfile or [])
424424+ (concatLists propagatedDependencies);
425425+426426+ computedImpureHostDeps =
427427+ unique (concatMap (input: input.__propagatedImpureHostDeps or [])
428428+ (stdenv.extraNativeBuildInputs
429429+ ++ stdenv.extraBuildInputs
430430+ ++ concatLists dependencies));
431431+432432+ computedPropagatedImpureHostDeps =
433433+ unique (concatMap (input: input.__propagatedImpureHostDeps or [])
434434+ (concatLists propagatedDependencies));
435435+ in {
486436 inherit __darwinAllowLocalNetworking;
487437 # TODO: remove `unique` once nix has a list canonicalization primitive
488438 __sandboxProfile =
···497447 "/bin/sh"
498448 ];
499449 __propagatedImpureHostDeps = computedPropagatedImpureHostDeps ++ __propagatedImpureHostDeps;
500500- } //
450450+ }) //
501451 # If we use derivations directly here, they end up as build-time dependencies.
502452 # This is especially problematic in the case of disallowed*, since the disallowed
503453 # derivations will be built by nix as build-time dependencies, while those
···533483 mapNullable unsafeDerivationToUntrackedOutpath attrs.allowedRequisites;
534484 };
535485536536- meta = checkMeta.commonMeta { inherit validity attrs pos references; };
486486+in
487487+ derivationArg;
488488+489489+mkDerivationSimple = overrideAttrs:
490490+491491+# `mkDerivation` wraps the builtin `derivation` function to
492492+# produce derivations that use this stdenv and its shell.
493493+#
494494+# Internally, it delegates most of its behavior to `makeDerivationArgument`,
495495+# except for the `env`, `cmakeFlags`, and `mesonFlags` attributes, as well
496496+# as the attributes `meta` and `passthru` that affect [package attributes],
497497+# and not the derivation itself.
498498+#
499499+# See also:
500500+#
501501+# * https://nixos.org/nixpkgs/manual/#sec-using-stdenv
502502+# Details on how to use this mkDerivation function
503503+#
504504+# * https://nixos.org/manual/nix/stable/expressions/derivations.html#derivations
505505+# Explanation about derivations in general
506506+#
507507+# * [package attributes]: https://nixos.org/manual/nix/stable/glossary#package-attribute-set
508508+{
509509+510510+# Configure Phase
511511+ cmakeFlags ? []
512512+, mesonFlags ? []
513513+514514+, meta ? {}
515515+, passthru ? {}
516516+, pos ? # position used in error messages and for meta.position
517517+ (if attrs.meta.description or null != null
518518+ then builtins.unsafeGetAttrPos "description" attrs.meta
519519+ else if attrs.version or null != null
520520+ then builtins.unsafeGetAttrPos "version" attrs
521521+ else builtins.unsafeGetAttrPos "name" attrs)
522522+523523+# Experimental. For simple packages mostly just works,
524524+# but for anything complex, be prepared to debug if enabling.
525525+, __structuredAttrs ? config.structuredAttrsByDefault or false
526526+527527+, env ? { }
528528+529529+, ... } @ attrs:
530530+531531+# Policy on acceptable hash types in nixpkgs
532532+assert attrs ? outputHash -> (
533533+ let algo =
534534+ attrs.outputHashAlgo or (head (splitString "-" attrs.outputHash));
535535+ in
536536+ if algo == "md5" then
537537+ throw "Rejected insecure ${algo} hash '${attrs.outputHash}'"
538538+ else
539539+ true
540540+);
541541+542542+let
543543+ envIsExportable = isAttrs env && !isDerivation env;
544544+545545+ derivationArg = makeDerivationArgument
546546+ (removeAttrs
547547+ attrs
548548+ (["meta" "passthru" "pos"]
549549+ ++ optional (__structuredAttrs || envIsExportable) "env"
550550+ )
551551+ // optionalAttrs __structuredAttrs { env = checkedEnv; }
552552+ // {
553553+ cmakeFlags = makeCMakeFlags attrs;
554554+ mesonFlags = makeMesonFlags attrs;
555555+ });
556556+557557+ meta = checkMeta.commonMeta {
558558+ inherit validity attrs pos;
559559+ references = attrs.nativeBuildInputs ++ attrs.buildInputs
560560+ ++ attrs.propagatedNativeBuildInputs ++ attrs.propagatedBuildInputs;
561561+ };
537562 validity = checkMeta.assertValidity { inherit meta attrs; };
538563539564 checkedEnv =
···603628 (derivation (derivationArg // optionalAttrs envIsExportable checkedEnv));
604629605630in
606606- fnOrAttrs:
607607- if builtins.isFunction fnOrAttrs
608608- then makeDerivationExtensible fnOrAttrs
609609- else makeDerivationExtensibleConst fnOrAttrs
631631+{
632632+ inherit mkDerivation;
633633+}