1 /* This file contains various functions that take a stdenv and return
2 a new stdenv with different behaviour, e.g. using a different C
8 # N.B. Keep in sync with default arg for stdenv/generic.
9 defaultMkDerivationFromStdenv = import ./generic/make-derivation.nix { inherit lib config; };
11 # Low level function to help with overriding `mkDerivationFromStdenv`. One
12 # gives it the old stdenv arguments and a "continuation" function, and
13 # underneath the final stdenv argument it yields to the continuation to do
14 # whatever it wants with old `mkDerivation` (old `mkDerivationFromStdenv`
15 # applied to the *new, final* stdenv) provided for convenience.
16 withOldMkDerivation = stdenvSuperArgs: k: stdenvSelf: let
17 mkDerivationFromStdenv-super = stdenvSuperArgs.mkDerivationFromStdenv or defaultMkDerivationFromStdenv;
18 mkDerivationSuper = mkDerivationFromStdenv-super stdenvSelf;
20 k stdenvSelf mkDerivationSuper;
22 # Wrap the original `mkDerivation` providing extra args to it.
23 extendMkDerivationArgs = old: f: withOldMkDerivation old (_: mkDerivationSuper: args:
24 (mkDerivationSuper args).overrideAttrs f);
26 # Wrap the original `mkDerivation` transforming the result.
27 overrideMkDerivationResult = old: f: withOldMkDerivation old (_: mkDerivationSuper: args:
28 f (mkDerivationSuper args));
34 # Override the compiler in stdenv for specific packages.
35 overrideCC = stdenv: cc: stdenv.override { allowedRequisites = null; cc = cc; };
38 # Add some arbitrary packages to buildInputs for specific packages.
39 # Used to override packages in stdenv like Make. Should not be used
40 # for other dependencies.
41 overrideInStdenv = stdenv: pkgs:
42 stdenv.override (prev: { allowedRequisites = null; extraBuildInputs = (prev.extraBuildInputs or []) ++ pkgs; });
45 # Override the setup script of stdenv. Useful for testing new
46 # versions of the setup script without causing a rebuild of
50 # randomPkg = import ../bla { ...
51 # stdenv = overrideSetup stdenv ../stdenv/generic/setup-latest.sh;
53 overrideSetup = stdenv: setupScript: stdenv.override { inherit setupScript; };
56 # Return a modified stdenv that tries to build statically linked
58 makeStaticBinaries = stdenv0:
59 stdenv0.override (old: {
60 mkDerivationFromStdenv = withOldMkDerivation old (stdenv: mkDerivationSuper: args:
61 if stdenv.hostPlatform.isDarwin
62 then throw "Cannot build fully static binaries on Darwin/macOS"
63 else (mkDerivationSuper args).overrideAttrs(finalAttrs: {
64 NIX_CFLAGS_LINK = toString (finalAttrs.NIX_CFLAGS_LINK or "") + " -static";
65 } // lib.optionalAttrs (!(finalAttrs.dontAddStaticConfigureFlags or false)) {
66 configureFlags = (finalAttrs.configureFlags or []) ++ [
67 "--disable-shared" # brrr...
70 } // lib.optionalAttrs (stdenv0.hostPlatform.libc == "libc") {
71 extraBuildInputs = (old.extraBuildInputs or []) ++ [
77 # Return a modified stdenv that builds static libraries instead of
79 makeStaticLibraries = stdenv:
80 stdenv.override (old: {
81 mkDerivationFromStdenv = extendMkDerivationArgs old (args: {
82 dontDisableStatic = true;
83 } // lib.optionalAttrs (!(args.dontAddStaticConfigureFlags or false)) {
84 configureFlags = (args.configureFlags or []) ++ [
88 cmakeFlags = (args.cmakeFlags or []) ++ [ "-DBUILD_SHARED_LIBS:BOOL=OFF" ];
89 mesonFlags = (args.mesonFlags or []) ++ [ "-Ddefault_library=static" ];
93 # Best effort static binaries. Will still be linked to libSystem,
94 # but more portable than Nix store binaries.
95 makeStaticDarwin = stdenv: stdenv.override (old: {
96 # extraBuildInputs are dropped in cross.nix, but darwin still needs them
97 extraBuildInputs = [ pkgs.buildPackages.darwin.CF ];
98 mkDerivationFromStdenv = extendMkDerivationArgs old (args: {
99 NIX_CFLAGS_LINK = toString (args.NIX_CFLAGS_LINK or "")
100 + lib.optionalString (stdenv.cc.isGNU or false) " -static-libgcc";
101 nativeBuildInputs = (args.nativeBuildInputs or []) ++ [
102 (pkgs.buildPackages.makeSetupHook {
104 libsystem = "${stdenv.cc.libc}/lib/libSystem.B.dylib";
106 } ./darwin/portable-libsystem.sh)
111 # Puts all the other ones together
112 makeStatic = stdenv: lib.foldl (lib.flip lib.id) stdenv (
113 lib.optional stdenv.hostPlatform.isDarwin makeStaticDarwin
115 ++ [ makeStaticLibraries propagateBuildInputs ]
117 # Apple does not provide a static version of libSystem or crt0.o
118 # So we can’t build static binaries without extensive hacks.
119 ++ lib.optional (!stdenv.hostPlatform.isDarwin) makeStaticBinaries
121 # Glibc doesn’t come with static runtimes by default.
122 # ++ lib.optional (stdenv.hostPlatform.libc == "glibc") ((lib.flip overrideInStdenv) [ self.glibc.static ])
126 /* Modify a stdenv so that all buildInputs are implicitly propagated to
127 consuming derivations
129 propagateBuildInputs = stdenv:
130 stdenv.override (old: {
131 mkDerivationFromStdenv = extendMkDerivationArgs old (args: {
132 propagatedBuildInputs = (args.propagatedBuildInputs or []) ++ (args.buildInputs or []);
138 /* Modify a stdenv so that the specified attributes are added to
139 every derivation returned by its mkDerivation function.
144 { NIX_CFLAGS_COMPILE = "-O0"; }
147 addAttrsToDerivation = extraAttrs: stdenv: stdenv.override (old: {
148 mkDerivationFromStdenv = extendMkDerivationArgs old (_: extraAttrs);
152 /* Use the trace output to report all processed derivations with their
155 traceDrvLicenses = stdenv:
156 stdenv.override (old: {
157 mkDerivationFromStdenv = overrideMkDerivationResult (pkg:
159 printDrvPath = val: let
160 drvPath = builtins.unsafeDiscardStringContext pkg.drvPath;
161 license = pkg.meta.license or null;
163 builtins.trace "@:drv:${toString drvPath}:${builtins.toString license}:@" val;
165 outPath = printDrvPath pkg.outPath;
166 drvPath = printDrvPath pkg.drvPath;
171 /* Modify a stdenv so that it produces debug builds; that is,
172 binaries have debug info, and compiler optimisations are
174 keepDebugInfo = stdenv:
175 stdenv.override (old: {
176 mkDerivationFromStdenv = extendMkDerivationArgs old (args: {
178 NIX_CFLAGS_COMPILE = toString (args.NIX_CFLAGS_COMPILE or "") + " -ggdb -Og";
183 /* Modify a stdenv so that it uses the Gold linker. */
184 useGoldLinker = stdenv:
185 stdenv.override (old: {
186 mkDerivationFromStdenv = extendMkDerivationArgs old (args: {
187 NIX_CFLAGS_LINK = toString (args.NIX_CFLAGS_LINK or "") + " -fuse-ld=gold";
192 /* Modify a stdenv so that it builds binaries optimized specifically
193 for the machine they are built on.
195 WARNING: this breaks purity! */
196 impureUseNativeOptimizations = stdenv:
197 stdenv.override (old: {
198 mkDerivationFromStdenv = extendMkDerivationArgs old (args: {
199 NIX_CFLAGS_COMPILE = toString (args.NIX_CFLAGS_COMPILE or "") + " -march=native";
200 NIX_ENFORCE_NO_NATIVE = false;
202 preferLocalBuild = true;
203 allowSubstitutes = false;
208 /* Modify a stdenv so that it builds binaries with the specified list of
209 compilerFlags appended and passed to the compiler.
211 This example would recompile every derivation on the system with
212 -funroll-loops and -O3 passed to each gcc invocation.
217 stdenv = super.withCFlags [ "-funroll-loops" "-O3" ] super.stdenv;
221 withCFlags = compilerFlags: stdenv:
222 stdenv.override (old: {
223 mkDerivationFromStdenv = extendMkDerivationArgs old (args: {
224 NIX_CFLAGS_COMPILE = toString (args.NIX_CFLAGS_COMPILE or "") + " ${toString compilerFlags}";