@ -2,7 +2,31 @@
a new stdenv with different behaviour , e . g . using a different C
compiler . * /
pkgs :
{ lib , pkgs , config }:
let
# N.B. Keep in sync with default arg for stdenv/generic.
defaultMkDerivationFromStdenv = import ./generic/make-derivation.nix { inherit lib config ; } ;
# Low level function to help with overriding `mkDerivationFromStdenv`. One
# gives it the old stdenv arguments and a "continuation" function, and
# underneath the final stdenv argument it yields to the continuation to do
# whatever it wants with old `mkDerivation` (old `mkDerivationFromStdenv`
# applied to the *new, final* stdenv) provided for convenience.
withOldMkDerivation = stdenvSuperArgs : k : stdenvSelf : let
mkDerivationFromStdenv-super = stdenvSuperArgs . mkDerivationFromStdenv or defaultMkDerivationFromStdenv ;
mkDerivationSuper = mkDerivationFromStdenv-super stdenvSelf ;
in
k stdenvSelf mkDerivationSuper ;
# Wrap the original `mkDerivation` providing extra args to it.
extendMkDerivationArgs = old : f : withOldMkDerivation old ( _ : mkDerivationSuper : args :
mkDerivationSuper ( args // f args ) ) ;
# Wrap the original `mkDerivation` transforming the result.
overrideMkDerivationResult = old : f : withOldMkDerivation old ( _ : mkDerivationSuper : args :
f ( mkDerivationSuper args ) ) ;
in
rec {
@ -31,33 +55,32 @@ rec {
# Return a modified stdenv that tries to build statically linked
# binaries.
makeStaticBinaries = stdenv :
let stdenv' = if stdenv . hostPlatform . libc != " g l i b c " then stdenv else
stdenv . override ( prev : {
extraBuildInputs = ( prev . extraBuildInputs or [ ] ) ++ [
stdenv . glibc . static
] ;
} ) ;
in stdenv' //
{ mkDerivation = args :
if stdenv' . hostPlatform . isDarwin
makeStaticBinaries = stdenv0 :
stdenv0 . override ( old : {
mkDerivationFromStdenv = withOldMkDerivation old ( stdenv : mkDerivationSuper : args :
if stdenv . hostPlatform . isDarwin
then throw " C a n n o t b u i l d f u l l y s t a t i c b i n a r i e s o n D a r w i n / m a c O S "
else stdenv' . mkDerivation ( args // {
else mkDerivationSuper ( args // {
NIX_CFLAGS_LINK = toString ( args . NIX_CFLAGS_LINK or " " ) + " - s t a t i c " ;
} // pkgs . lib . optionalAttrs ( ! ( args . dontAddStaticConfigureFlags or false ) ) {
} // lib . optionalAttrs ( ! ( args . dontAddStaticConfigureFlags or false ) ) {
configureFlags = ( args . configureFlags or [ ] ) ++ [
" - - d i s a b l e - s h a r e d " # brrr...
] ;
} ) ;
} ;
} ) ) ;
} // lib . optionalAttrs ( stdenv0 . hostPlatform . libc == " l i b c " ) {
extraBuildInputs = ( old . extraBuildInputs or [ ] ) ++ [
stdenv0 . glibc . static
] ;
} ) ;
# Return a modified stdenv that builds static libraries instead of
# shared libraries.
makeStaticLibraries = stdenv : stdenv //
{ mkDerivation = args : stdenv . mkDerivation ( args // {
makeStaticLibraries = stdenv :
stdenv . override ( old : {
mkDerivationFromStdenv = extendMkDerivationArgs old ( args : {
dontDisableStatic = true ;
} // pkgs . lib . optionalAttrs ( ! ( args . dontAddStaticConfigureFlags or false ) ) {
} // lib . optionalAttrs ( ! ( args . dontAddStaticConfigureFlags or false ) ) {
configureFlags = ( args . configureFlags or [ ] ) ++ [
" - - e n a b l e - s t a t i c "
" - - d i s a b l e - s h a r e d "
@ -65,18 +88,19 @@ rec {
cmakeFlags = ( args . cmakeFlags or [ ] ) ++ [ " - D B U I L D _ S H A R E D _ L I B S : B O O L = O F F " ] ;
mesonFlags = ( args . mesonFlags or [ ] ) ++ [ " - D d e f a u l t _ l i b r a r y = s t a t i c " ] ;
} ) ;
} ;
} ) ;
/* M o d i f y a s t d e n v s o t h a t a l l b u i l d I n p u t s a r e i m p l i c i t l y p r o p a g a t e d t o
consuming derivations
* /
propagateBuildInputs = stdenv : stdenv //
{ mkDerivation = args : stdenv . mkDerivation ( args // {
propagateBuildInputs = stdenv :
stdenv . override ( old : {
mkDerivationFromStdenv = extendMkDerivationArgs old ( args : {
propagatedBuildInputs = ( args . propagatedBuildInputs or [ ] ) ++ ( args . buildInputs or [ ] ) ;
buildInputs = [ ] ;
} ) ;
} ;
} ) ;
/* M o d i f y a s t d e n v s o t h a t t h e s p e c i f i e d a t t r i b u t e s a r e a d d e d t o
@ -88,8 +112,9 @@ rec {
{ NIX_CFLAGS_COMPILE = " - O 0 " ; }
stdenv ;
* /
addAttrsToDerivation = extraAttrs : stdenv : stdenv //
{ mkDerivation = args : stdenv . mkDerivation ( args // extraAttrs ) ; } ;
addAttrsToDerivation = extraAttrs : stdenv : stdenv . override ( old : {
mkDerivationFromStdenv = extendMkDerivationArgs old ( _ : extraAttrs ) ;
} ) ;
/* R e t u r n a m o d i f i e d s t d e n v t h a t b u i l d s p a c k a g e s w i t h G C C ' s c o v e r a g e
@ -110,21 +135,20 @@ rec {
# remove all maintainers.
defaultStdenv = replaceMaintainersField allStdenvs . stdenv pkgs [ ] ;
* /
replaceMaintainersField = stdenv : pkgs : maintainers : stdenv //
{ mkDerivation = args :
pkgs . lib . recursiveUpdate
( stdenv . mkDerivation args )
{ meta . maintainers = maintainers ; } ;
} ;
replaceMaintainersField = stdenv : pkgs : maintainers :
stdenv . override ( old : {
mkDerivationFromStdenv = overrideMkDerivationResult ( pkg :
lib . recursiveUpdate pkg { meta . maintainers = maintainers ; } ) ;
} ) ;
/* U s e t h e t r a c e o u t p u t t o r e p o r t a l l p r o c e s s e d d e r i v a t i o n s w i t h t h e i r
license name .
* /
traceDrvLicenses = stdenv : stdenv //
{ mkDerivation = args :
traceDrvLicenses = stdenv :
stdenv . override ( old : {
mkDerivationFromStdenv = overrideMkDerivationResult ( pkg :
let
pkg = stdenv . mkDerivation args ;
printDrvPath = val : let
drvPath = builtins . unsafeDiscardStringContext pkg . drvPath ;
license = pkg . meta . license or null ;
@ -133,8 +157,8 @@ rec {
in pkg // {
outPath = printDrvPath pkg . outPath ;
drvPath = printDrvPath pkg . drvPath ;
} ;
} ;
} ) ;
} ) ;
/* A b o r t i f t h e l i c e n s e p r e d i c a t e i s n o t v e r i f i e d f o r a d e r i v a t i o n
@ -152,10 +176,10 @@ rec {
use it by patching the all-packages . nix file or by using the override
feature of ~/.config/nixpkgs/config.nix .
* /
validateLicenses = licensePred : stdenv : stdenv //
{ mkDerivation = args :
validateLicenses = licensePred : stdenv :
stdenv . override ( old : {
mkDerivationFromStdenv = overrideMkDerivationResult ( pkg :
let
pkg = stdenv . mkDerivation args ;
drv = builtins . unsafeDiscardStringContext pkg . drvPath ;
license =
pkg . meta . license or
@ -175,40 +199,43 @@ rec {
in pkg // {
outPath = validate pkg . outPath ;
drvPath = validate pkg . drvPath ;
} ;
} ;
} ) ;
} ) ;
/* M o d i f y a s t d e n v s o t h a t i t p r o d u c e s d e b u g b u i l d s ; t h a t i s ,
binaries have debug info , and compiler optimisations are
disabled . * /
keepDebugInfo = stdenv : stdenv //
{ mkDerivation = args : stdenv . mkDerivation ( args // {
keepDebugInfo = stdenv :
stdenv . override ( old : {
mkDerivationFromStdenv = extendMkDerivationArgs old ( args : {
dontStrip = true ;
NIX_CFLAGS_COMPILE = toString ( args . NIX_CFLAGS_COMPILE or " " ) + " - g g d b - O g " ;
} ) ;
} ;
} ) ;
/* M o d i f y a s t d e n v s o t h a t i t u s e s t h e G o l d l i n k e r . */
useGoldLinker = stdenv : stdenv //
{ mkDerivation = args : stdenv . mkDerivation ( args // {
useGoldLinker = stdenv :
stdenv . override ( old : {
mkDerivationFromStdenv = extendMkDerivationArgs old ( args : {
NIX_CFLAGS_LINK = toString ( args . NIX_CFLAGS_LINK or " " ) + " - f u s e - l d = g o l d " ;
} ) ;
} ;
} ) ;
/* M o d i f y a s t d e n v s o t h a t i t b u i l d s b i n a r i e s o p t i m i z e d s p e c i f i c a l l y
for the machine they are built on .
WARNING : this breaks purity ! * /
impureUseNativeOptimizations = stdenv : stdenv //
{ mkDerivation = args : stdenv . mkDerivation ( args // {
impureUseNativeOptimizations = stdenv :
stdenv . override ( old : {
mkDerivationFromStdenv = extendMkDerivationArgs old ( args : {
NIX_CFLAGS_COMPILE = toString ( args . NIX_CFLAGS_COMPILE or " " ) + " - m a r c h = n a t i v e " ;
NIX_ENFORCE_NO_NATIVE = false ;
preferLocalBuild = true ;
allowSubstitutes = false ;
} ) ;
} ;
} ) ;
}