···3737 </para>
3838 <para>
3939 In Nixpkgs, these three platforms are defined as attribute sets under the names <literal>buildPlatform</literal>, <literal>hostPlatform</literal>, and <literal>targetPlatform</literal>.
4040- All three are always defined at the top level, so one can get at them just like a dependency in a function that is imported with <literal>callPackage</literal>:
4141- <programlisting>{ stdenv, buildPlatform, hostPlatform, fooDep, barDep, .. }: ...</programlisting>
4040+ All three are always defined as attributes in the standard environment, and at the top level. That means one can get at them just like a dependency in a function that is imported with <literal>callPackage</literal>:
4141+ <programlisting>{ stdenv, buildPlatform, hostPlatform, fooDep, barDep, .. }: ...buildPlatform...</programlisting>, or just off <varname>stdenv</varname>:
4242+ <programlisting>{ stdenv, fooDep, barDep, .. }: ...stdenv.buildPlatform...</programlisting>.
4243 </para>
4344 <variablelist>
4445 <varlistentry>
+3-2
pkgs/build-support/cc-wrapper/default.nix
···1010, zlib ? null, extraPackages ? [], extraBuildCommands ? ""
1111, dyld ? null # TODO: should this be a setup-hook on dyld?
1212, isGNU ? false, isClang ? cc.isClang or false, gnugrep ? null
1313-, hostPlatform, targetPlatform
1413, runCommand ? null
1514}:
1615···2221assert !(nativeLibc && noLibc);
2322assert (noLibc || nativeLibc) == (libc == null);
24232525-assert targetPlatform != hostPlatform -> runCommand != null;
2424+assert stdenv.targetPlatform != stdenv.hostPlatform -> runCommand != null;
26252726# For ghdl (the vhdl language provider to gcc) we need zlib in the wrapper.
2827assert cc.langVhdl or false -> zlib != null;
29283029let
3030+ inherit (stdenv) hostPlatform targetPlatform;
3131+3132 # Prefix for binaries. Customarily ends with a dash separator.
3233 #
3334 # TODO(@Ericson2314) Make unconditional, or optional but always true by
+3-5
pkgs/stdenv/adapters.nix
···6161 , buildPlatform, hostPlatform, targetPlatform
6262 } @ overrideArgs: let
6363 stdenv = overrideArgs.stdenv.override {
6464- # TODO(@Ericson2314): Cannot do this for now because then Nix thinks the
6565- # resulting derivation should be built on the host platform.
6666- #hostPlatform = buildPlatform;
6767- #targetPlatform = hostPlatform;
6868- inherit cc;
6464+ inherit
6565+ buildPlatform hostPlatform targetPlatform
6666+ cc;
69677068 allowedRequisites = null;
7169
···1515, stdenvSandboxProfile ? ""
1616, extraSandboxProfile ? ""
17171818-, # The platforms here do *not* correspond to the stage the stdenv is
1919- # used in, but rather the previous one, in which it was built. We
2020- # use the latter two platforms, like a cross compiler, because the
2121- # stand environment is a build tool if you squint at it, and because
2222- # neither of these are used when building stdenv so we know the
2323- # build platform is irrelevant.
2424- hostPlatform, targetPlatform
1818+ ## Platform parameters
1919+ ##
2020+ ## The "build" "host" "target" terminology below comes from GNU Autotools. See
2121+ ## its documentation for more information on what those words mean. Note that
2222+ ## each should always be defined, even when not cross compiling.
2323+ ##
2424+ ## For purposes of bootstrapping, think of each stage as a "sliding window"
2525+ ## over a list of platforms. Specifically, the host platform of the previous
2626+ ## stage becomes the build platform of the current one, and likewise the
2727+ ## target platform of the previous stage becomes the host platform of the
2828+ ## current one.
2929+ ##
3030+3131+, # The platform on which packages are built. Consists of `system`, a
3232+ # string (e.g.,`i686-linux') identifying the most import attributes of the
3333+ # build platform, and `platform` a set of other details.
3434+ buildPlatform
3535+3636+, # The platform on which packages run.
3737+ hostPlatform
3838+3939+, # The platform which build tools (especially compilers) build for in this stage,
4040+ targetPlatform
2541}:
26422743let
2828- inherit (targetPlatform) system;
2929-3030- ifDarwin = attrs: if system == "x86_64-darwin" then attrs else {};
3131-3244 defaultNativeBuildInputs = extraBuildInputs ++
3345 [ ../../build-support/setup-hooks/move-docs.sh
3446 ../../build-support/setup-hooks/compress-man-pages.sh
···3749 ]
3850 # FIXME this on Darwin; see
3951 # https://github.com/NixOS/nixpkgs/commit/94d164dd7#commitcomment-22030369
4040- ++ lib.optional result.isLinux ../../build-support/setup-hooks/audit-tmpdir.sh
5252+ ++ lib.optional hostPlatform.isLinux ../../build-support/setup-hooks/audit-tmpdir.sh
4153 ++ [
4254 ../../build-support/setup-hooks/multiple-outputs.sh
4355 ../../build-support/setup-hooks/move-sbin.sh
···4658 cc
4759 ];
48604949- # `mkDerivation` wraps the builtin `derivation` function to
5050- # produce derivations that use this stdenv and its shell.
5151- #
5252- # See also:
5353- #
5454- # * https://nixos.org/nixpkgs/manual/#sec-using-stdenv
5555- # Details on how to use this mkDerivation function
5656- #
5757- # * https://nixos.org/nix/manual/#ssec-derivation
5858- # Explanation about derivations in general
5959- mkDerivation =
6060- { nativeBuildInputs ? []
6161- , buildInputs ? []
6262-6363- , propagatedNativeBuildInputs ? []
6464- , propagatedBuildInputs ? []
6565-6666- , crossConfig ? null
6767- , meta ? {}
6868- , passthru ? {}
6969- , pos ? # position used in error messages and for meta.position
7070- (if attrs.meta.description or null != null
7171- then builtins.unsafeGetAttrPos "description" attrs.meta
7272- else builtins.unsafeGetAttrPos "name" attrs)
7373- , separateDebugInfo ? false
7474- , outputs ? [ "out" ]
7575- , __impureHostDeps ? []
7676- , __propagatedImpureHostDeps ? []
7777- , sandboxProfile ? ""
7878- , propagatedSandboxProfile ? ""
7979- , ... } @ attrs:
8080- let
8181- dependencies = [
8282- (map (drv: drv.nativeDrv or drv) nativeBuildInputs)
8383- (map (drv: drv.crossDrv or drv) buildInputs)
8484- ];
8585- propagatedDependencies = [
8686- (map (drv: drv.nativeDrv or drv) propagatedNativeBuildInputs)
8787- (map (drv: drv.crossDrv or drv) propagatedBuildInputs)
8888- ];
8989- in let
9090-9191- outputs' =
9292- outputs ++
9393- (if separateDebugInfo then assert targetPlatform.isLinux; [ "debug" ] else []);
9494-9595- dependencies' = let
9696- justMap = map lib.chooseDevOutputs dependencies;
9797- nativeBuildInputs = lib.elemAt justMap 0
9898- ++ lib.optional targetPlatform.isWindows ../../build-support/setup-hooks/win-dll-link.sh;
9999- buildInputs = lib.elemAt justMap 1
100100- # TODO(@Ericson2314): Should instead also be appended to `nativeBuildInputs`.
101101- ++ lib.optional separateDebugInfo ../../build-support/setup-hooks/separate-debug-info.sh;
102102- in [ nativeBuildInputs buildInputs ];
103103-104104- propagatedDependencies' = map lib.chooseDevOutputs propagatedDependencies;
105105-106106- derivationArg =
107107- (removeAttrs attrs
108108- ["meta" "passthru" "crossAttrs" "pos"
109109- "__impureHostDeps" "__propagatedImpureHostDeps"
110110- "sandboxProfile" "propagatedSandboxProfile"])
111111- // (let
112112- # TODO(@Ericson2314): Reversing of dep lists is just temporary to avoid Darwin mass rebuild.
113113- computedSandboxProfile =
114114- lib.concatMap (input: input.__propagatedSandboxProfile or []) (extraBuildInputs ++ lib.concatLists (lib.reverseList dependencies'));
115115- computedPropagatedSandboxProfile =
116116- lib.concatMap (input: input.__propagatedSandboxProfile or []) (lib.concatLists (lib.reverseList propagatedDependencies'));
117117- computedImpureHostDeps =
118118- lib.unique (lib.concatMap (input: input.__propagatedImpureHostDeps or []) (extraBuildInputs ++ lib.concatLists (lib.reverseList dependencies')));
119119- computedPropagatedImpureHostDeps =
120120- lib.unique (lib.concatMap (input: input.__propagatedImpureHostDeps or []) (lib.concatLists (lib.reverseList propagatedDependencies')));
121121- in
122122- {
123123- builder = attrs.realBuilder or shell;
124124- args = attrs.args or ["-e" (attrs.builder or ./default-builder.sh)];
125125- stdenv = result;
126126- system = result.system;
127127- userHook = config.stdenv.userHook or null;
128128- __ignoreNulls = true;
129129-130130- nativeBuildInputs = lib.elemAt dependencies' 0;
131131- buildInputs = lib.elemAt dependencies' 1;
132132-133133- propagatedNativeBuildInputs = lib.elemAt propagatedDependencies' 0;
134134- propagatedBuildInputs = lib.elemAt propagatedDependencies' 1;
135135- } // ifDarwin {
136136- # TODO: remove lib.unique once nix has a list canonicalization primitive
137137- __sandboxProfile =
138138- let profiles = [ extraSandboxProfile ] ++ computedSandboxProfile ++ computedPropagatedSandboxProfile ++ [ propagatedSandboxProfile sandboxProfile ];
139139- final = lib.concatStringsSep "\n" (lib.filter (x: x != "") (lib.unique profiles));
140140- in final;
141141- __propagatedSandboxProfile = lib.unique (computedPropagatedSandboxProfile ++ [ propagatedSandboxProfile ]);
142142- __impureHostDeps = computedImpureHostDeps ++ computedPropagatedImpureHostDeps ++ __propagatedImpureHostDeps ++ __impureHostDeps ++ __extraImpureHostDeps ++ [
143143- "/dev/zero"
144144- "/dev/random"
145145- "/dev/urandom"
146146- "/bin/sh"
147147- ];
148148- __propagatedImpureHostDeps = computedPropagatedImpureHostDeps ++ __propagatedImpureHostDeps;
149149- } // (if outputs' != [ "out" ] then {
150150- outputs = outputs';
151151- } else { }));
152152-153153- # The meta attribute is passed in the resulting attribute set,
154154- # but it's not part of the actual derivation, i.e., it's not
155155- # passed to the builder and is not a dependency. But since we
156156- # include it in the result, it *is* available to nix-env for queries.
157157- meta = { }
158158- # If the packager hasn't specified `outputsToInstall`, choose a default,
159159- # which is the name of `p.bin or p.out or p`;
160160- # if he has specified it, it will be overridden below in `// meta`.
161161- # Note: This default probably shouldn't be globally configurable.
162162- # Services and users should specify outputs explicitly,
163163- # unless they are comfortable with this default.
164164- // { outputsToInstall =
165165- let
166166- outs = outputs'; # the value passed to derivation primitive
167167- hasOutput = out: builtins.elem out outs;
168168- in [( lib.findFirst hasOutput null (["bin" "out"] ++ outs) )];
169169- }
170170- // attrs.meta or {}
171171- # Fill `meta.position` to identify the source location of the package.
172172- // lib.optionalAttrs (pos != null)
173173- { position = pos.file + ":" + toString pos.line; }
174174- ;
175175-176176- in
177177-178178- lib.addPassthru
179179- (derivation (import ./check-meta.nix
180180- {
181181- inherit lib config meta derivationArg;
182182- mkDerivationArg = attrs;
183183- inherit system; # TODO: cross-compilation?
184184- }))
185185- ( {
186186- overrideAttrs = f: mkDerivation (attrs // (f attrs));
187187- inherit meta passthru;
188188- } //
189189- # Pass through extra attributes that are not inputs, but
190190- # should be made available to Nix expressions using the
191191- # derivation (e.g., in assertions).
192192- passthru);
193193-19461 # The stdenv that we are producing.
195195- result =
6262+ stdenv =
19663 derivation (
19764 (if isNull allowedRequisites then {} else { allowedRequisites = allowedRequisites ++ defaultNativeBuildInputs; }) //
19865 {
199199- inherit system name;
6666+ inherit name;
6767+6868+ # Nix itself uses the `system` field of a derivation to decide where to
6969+ # build it. This is a bit confusing for cross compilation.
7070+ inherit (buildPlatform) system;
2007120172 builder = shell;
20273···2067720778 inherit preHook initialPath shell defaultNativeBuildInputs;
20879 }
209209- // ifDarwin {
8080+ // lib.optionalAttrs buildPlatform.isDarwin {
21081 __sandboxProfile = stdenvSandboxProfile;
21182 __impureHostDeps = __stdenvImpureHostDeps;
21283 })
···21788 description = "The default build environment for Unix packages in Nixpkgs";
21889 platforms = lib.platforms.all;
21990 };
9191+9292+ inherit buildPlatform hostPlatform targetPlatform;
9393+9494+ inherit extraBuildInputs __extraImpureHostDeps extraSandboxProfile;
2209522196 # Utility flags to test the type of platform.
22297 inherit (hostPlatform)
···228103 # Whether we should run paxctl to pax-mark binaries.
229104 needsPax = isLinux;
230105231231- inherit mkDerivation;
106106+ inherit (import ./make-derivation.nix {
107107+ inherit lib config stdenv;
108108+ }) mkDerivation;
232109233110 # For convenience, bring in the library functions in lib/ so
234111 # packages don't have to do that themselves.
···247124 # like curl = if stdenv ? curl then stdenv.curl else ...).
248125 // extraAttrs;
249126250250-in result)
127127+in stdenv)
+150
pkgs/stdenv/generic/make-derivation.nix
···11+{ lib, config, stdenv }:
22+33+rec {
44+ # `mkDerivation` wraps the builtin `derivation` function to
55+ # produce derivations that use this stdenv and its shell.
66+ #
77+ # See also:
88+ #
99+ # * https://nixos.org/nixpkgs/manual/#sec-using-stdenv
1010+ # Details on how to use this mkDerivation function
1111+ #
1212+ # * https://nixos.org/nix/manual/#ssec-derivation
1313+ # Explanation about derivations in general
1414+ mkDerivation =
1515+ { nativeBuildInputs ? []
1616+ , buildInputs ? []
1717+1818+ , propagatedNativeBuildInputs ? []
1919+ , propagatedBuildInputs ? []
2020+2121+ , crossConfig ? null
2222+ , meta ? {}
2323+ , passthru ? {}
2424+ , pos ? # position used in error messages and for meta.position
2525+ (if attrs.meta.description or null != null
2626+ then builtins.unsafeGetAttrPos "description" attrs.meta
2727+ else builtins.unsafeGetAttrPos "name" attrs)
2828+ , separateDebugInfo ? false
2929+ , outputs ? [ "out" ]
3030+ , __impureHostDeps ? []
3131+ , __propagatedImpureHostDeps ? []
3232+ , sandboxProfile ? ""
3333+ , propagatedSandboxProfile ? ""
3434+ , ... } @ attrs:
3535+ let
3636+ dependencies = [
3737+ (map (drv: drv.nativeDrv or drv) nativeBuildInputs)
3838+ (map (drv: drv.crossDrv or drv) buildInputs)
3939+ ];
4040+ propagatedDependencies = [
4141+ (map (drv: drv.nativeDrv or drv) propagatedNativeBuildInputs)
4242+ (map (drv: drv.crossDrv or drv) propagatedBuildInputs)
4343+ ];
4444+ in let
4545+4646+ outputs' =
4747+ outputs ++
4848+ (if separateDebugInfo then assert stdenv.hostPlatform.isLinux; [ "debug" ] else []);
4949+5050+ dependencies' = let
5151+ justMap = map lib.chooseDevOutputs dependencies;
5252+ nativeBuildInputs = lib.elemAt justMap 0
5353+ ++ lib.optional stdenv.hostPlatform.isWindows ../../build-support/setup-hooks/win-dll-link.sh;
5454+ buildInputs = lib.elemAt justMap 1
5555+ # TODO(@Ericson2314): Should instead also be appended to `nativeBuildInputs`.
5656+ ++ lib.optional separateDebugInfo ../../build-support/setup-hooks/separate-debug-info.sh;
5757+ in [ nativeBuildInputs buildInputs ];
5858+5959+ propagatedDependencies' = map lib.chooseDevOutputs propagatedDependencies;
6060+6161+ derivationArg =
6262+ (removeAttrs attrs
6363+ ["meta" "passthru" "crossAttrs" "pos"
6464+ "__impureHostDeps" "__propagatedImpureHostDeps"
6565+ "sandboxProfile" "propagatedSandboxProfile"])
6666+ // (let
6767+ # TODO(@Ericson2314): Reversing of dep lists is just temporary to avoid Darwin mass rebuild.
6868+ computedSandboxProfile =
6969+ lib.concatMap (input: input.__propagatedSandboxProfile or []) (stdenv.extraBuildInputs ++ lib.concatLists (lib.reverseList dependencies'));
7070+ computedPropagatedSandboxProfile =
7171+ lib.concatMap (input: input.__propagatedSandboxProfile or []) (lib.concatLists (lib.reverseList propagatedDependencies'));
7272+ computedImpureHostDeps =
7373+ lib.unique (lib.concatMap (input: input.__propagatedImpureHostDeps or []) (stdenv.extraBuildInputs ++ lib.concatLists (lib.reverseList dependencies')));
7474+ computedPropagatedImpureHostDeps =
7575+ lib.unique (lib.concatMap (input: input.__propagatedImpureHostDeps or []) (lib.concatLists (lib.reverseList propagatedDependencies')));
7676+ in
7777+ {
7878+ builder = attrs.realBuilder or stdenv.shell;
7979+ args = attrs.args or ["-e" (attrs.builder or ./default-builder.sh)];
8080+ inherit stdenv;
8181+ inherit (stdenv) system;
8282+ userHook = config.stdenv.userHook or null;
8383+ __ignoreNulls = true;
8484+8585+ nativeBuildInputs = lib.elemAt dependencies' 0;
8686+ buildInputs = lib.elemAt dependencies' 1;
8787+8888+ propagatedNativeBuildInputs = lib.elemAt propagatedDependencies' 0;
8989+ propagatedBuildInputs = lib.elemAt propagatedDependencies' 1;
9090+ } // lib.optionalAttrs (stdenv.buildPlatform.isDarwin) {
9191+ # TODO: remove lib.unique once nix has a list canonicalization primitive
9292+ __sandboxProfile =
9393+ let profiles = [ stdenv.extraSandboxProfile ] ++ computedSandboxProfile ++ computedPropagatedSandboxProfile ++ [ propagatedSandboxProfile sandboxProfile ];
9494+ final = lib.concatStringsSep "\n" (lib.filter (x: x != "") (lib.unique profiles));
9595+ in final;
9696+ __propagatedSandboxProfile = lib.unique (computedPropagatedSandboxProfile ++ [ propagatedSandboxProfile ]);
9797+ __impureHostDeps = computedImpureHostDeps ++ computedPropagatedImpureHostDeps ++ __propagatedImpureHostDeps ++ __impureHostDeps ++ stdenv.__extraImpureHostDeps ++ [
9898+ "/dev/zero"
9999+ "/dev/random"
100100+ "/dev/urandom"
101101+ "/bin/sh"
102102+ ];
103103+ __propagatedImpureHostDeps = computedPropagatedImpureHostDeps ++ __propagatedImpureHostDeps;
104104+ } // (if outputs' != [ "out" ] then {
105105+ outputs = outputs';
106106+ } else { }));
107107+108108+ # The meta attribute is passed in the resulting attribute set,
109109+ # but it's not part of the actual derivation, i.e., it's not
110110+ # passed to the builder and is not a dependency. But since we
111111+ # include it in the result, it *is* available to nix-env for queries.
112112+ meta = { }
113113+ # If the packager hasn't specified `outputsToInstall`, choose a default,
114114+ # which is the name of `p.bin or p.out or p`;
115115+ # if he has specified it, it will be overridden below in `// meta`.
116116+ # Note: This default probably shouldn't be globally configurable.
117117+ # Services and users should specify outputs explicitly,
118118+ # unless they are comfortable with this default.
119119+ // { outputsToInstall =
120120+ let
121121+ outs = outputs'; # the value passed to derivation primitive
122122+ hasOutput = out: builtins.elem out outs;
123123+ in [( lib.findFirst hasOutput null (["bin" "out"] ++ outs) )];
124124+ }
125125+ // attrs.meta or {}
126126+ # Fill `meta.position` to identify the source location of the package.
127127+ // lib.optionalAttrs (pos != null)
128128+ { position = pos.file + ":" + toString pos.line; }
129129+ ;
130130+131131+ in
132132+133133+ lib.addPassthru
134134+ (derivation (import ./check-meta.nix
135135+ {
136136+ inherit lib config meta derivationArg;
137137+ mkDerivationArg = attrs;
138138+ # Nix itself uses the `system` field of a derivation to decide where
139139+ # to build it. This is a bit confusing for cross compilation.
140140+ inherit (stdenv) system;
141141+ }))
142142+ ( {
143143+ overrideAttrs = f: mkDerivation (attrs // (f attrs));
144144+ inherit meta passthru;
145145+ } //
146146+ # Pass through extra attributes that are not inputs, but
147147+ # should be made available to Nix expressions using the
148148+ # derivation (e.g., in assertions).
149149+ passthru);
150150+}
···1818, # Use to reevaluate Nixpkgs; a dirty hack that should be removed
1919 nixpkgsFun
20202121- ## Platform parameters
2222- ##
2323- ## The "build" "host" "target" terminology below comes from GNU Autotools. See
2424- ## its documentation for more information on what those words mean. Note that
2525- ## each should always be defined, even when not cross compiling.
2626- ##
2727- ## For purposes of bootstrapping, think of each stage as a "sliding window"
2828- ## over a list of platforms. Specifically, the host platform of the previous
2929- ## stage becomes the build platform of the current one, and likewise the
3030- ## target platform of the previous stage becomes the host platform of the
3131- ## current one.
3232- ##
3333-3434-, # The platform on which packages are built. Consists of `system`, a
3535- # string (e.g.,`i686-linux') identifying the most import attributes of the
3636- # build platform, and `platform` a set of other details.
3737- buildPlatform
3838-3939-, # The platform on which packages run.
4040- hostPlatform
4141-4242-, # The platform which build tools (especially compilers) build for in this stage,
4343- targetPlatform
4444-4521 ## Other parameters
4622 ##
4723···6945, # Non-GNU/Linux OSes are currently "impure" platforms, with their libc
7046 # outside of the store. Thus, GCC, GFortran, & co. must always look for files
7147 # in standard system directories (/usr/include, etc.)
7272- noSysDirs ? buildPlatform.system != "x86_64-freebsd"
7373- && buildPlatform.system != "i686-freebsd"
7474- && buildPlatform.system != "x86_64-solaris"
7575- && buildPlatform.system != "x86_64-kfreebsd-gnu"
4848+ noSysDirs ? stdenv.buildPlatform.system != "x86_64-freebsd"
4949+ && stdenv.buildPlatform.system != "i686-freebsd"
5050+ && stdenv.buildPlatform.system != "x86_64-solaris"
5151+ && stdenv.buildPlatform.system != "x86_64-kfreebsd-gnu"
76527753, # The configuration attribute set
7854 config
···9874 // { recurseForDerivations = false; };
9975 __targetPackages = (if __targetPackages == null then self else __targetPackages)
10076 // { recurseForDerivations = false; };
101101- inherit stdenv
102102- buildPlatform hostPlatform targetPlatform;
7777+ inherit stdenv;
10378 };
1047910580 # The old identifiers for cross-compiling. These should eventually be removed,
10681 # and the packages that rely on them refactored accordingly.
10782 platformCompat = self: super: let
108108- # TODO(@Ericson2314) this causes infinite recursion
109109- #inherit (self) buildPlatform hostPlatform targetPlatform;
8383+ inherit (super.stdenv) buildPlatform hostPlatform targetPlatform;
11084 in {
11185 stdenv = super.stdenv // {
112112- inherit (buildPlatform) platform;
8686+ inherit (super.stdenv.buildPlatform) platform;
11387 };
8888+ inherit buildPlatform hostPlatform targetPlatform;
11489 inherit (buildPlatform) system platform;
11590 };
11691