summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--pkgs/development/compilers/llvm/3.6/default.nix2
-rw-r--r--pkgs/development/interpreters/perl/5.16/default.nix11
-rw-r--r--pkgs/development/interpreters/perl/5.20/default.nix11
-rw-r--r--pkgs/development/interpreters/python/2.7/default.nix4
-rw-r--r--pkgs/development/libraries/gettext/default.nix7
-rw-r--r--pkgs/development/libraries/icu/default.nix5
-rw-r--r--pkgs/development/libraries/libedit/default.nix2
-rw-r--r--pkgs/development/libraries/zlib/default.nix7
-rw-r--r--pkgs/development/tools/misc/binutils/default.nix6
-rw-r--r--pkgs/os-specific/darwin/adv_cmds/locale.nix29
-rw-r--r--pkgs/os-specific/darwin/binutils/default.nix39
-rw-r--r--pkgs/stdenv/darwin/default.nix364
-rw-r--r--pkgs/stdenv/darwin/make-bootstrap-tools.nix10
-rw-r--r--pkgs/stdenv/default.nix2
-rw-r--r--pkgs/stdenv/generic/default.nix27
-rw-r--r--pkgs/tools/archivers/gnutar/default.nix6
-rw-r--r--pkgs/tools/archivers/sharutils/default.nix2
-rw-r--r--pkgs/tools/text/gawk/default.nix11
-rw-r--r--pkgs/top-level/all-packages.nix24
19 files changed, 436 insertions, 133 deletions
diff --git a/pkgs/development/compilers/llvm/3.6/default.nix b/pkgs/development/compilers/llvm/3.6/default.nix
index 3321e38949f..d95ffe47326 100644
--- a/pkgs/development/compilers/llvm/3.6/default.nix
+++ b/pkgs/development/compilers/llvm/3.6/default.nix
@@ -1,6 +1,6 @@
 { pkgs, newScope, stdenv, isl, fetchurl, overrideCC, wrapCC }:
 let
-  callPackage = newScope (self // { inherit isl version fetch; });
+  callPackage = newScope (self // { inherit stdenv isl version fetch; });
 
   version = "3.6.1";
 
diff --git a/pkgs/development/interpreters/perl/5.16/default.nix b/pkgs/development/interpreters/perl/5.16/default.nix
index 9ed3aa54f51..568025edfd4 100644
--- a/pkgs/development/interpreters/perl/5.16/default.nix
+++ b/pkgs/development/interpreters/perl/5.16/default.nix
@@ -24,6 +24,17 @@ stdenv.mkDerivation rec {
     ++ lib.optional stdenv.isSunOS  ./ld-shared.patch
     ++ lib.optional stdenv.isDarwin [ ./cpp-precomp.patch ./no-libutil.patch ] ;
 
+  # There's an annoying bug on sandboxed Darwin in Perl's Cwd.pm where it looks for pwd
+  # in /bin/pwd and /usr/bin/pwd and then falls back on just "pwd" if it can't get them
+  # while at the same time erasing the PATH environment variable so it unconditionally
+  # fails. The code in question is guarded by a check for Mac OS, but the patch below
+  # doesn't have any runtime effect on other platforms.
+  postPatch = ''
+    pwd="$(type -P pwd)"
+    substituteInPlace dist/Cwd/Cwd.pm \
+      --replace "pwd_cmd = 'pwd'" "pwd_cmd = '$pwd'"
+  '';
+
   # Build a thread-safe Perl with a dynamic libperls.o.  We need the
   # "installstyle" option to ensure that modules are put under
   # $out/lib/perl5 - this is the general default, but because $out
diff --git a/pkgs/development/interpreters/perl/5.20/default.nix b/pkgs/development/interpreters/perl/5.20/default.nix
index 0ad955d0363..bfdb09e779a 100644
--- a/pkgs/development/interpreters/perl/5.20/default.nix
+++ b/pkgs/development/interpreters/perl/5.20/default.nix
@@ -35,6 +35,17 @@ stdenv.mkDerivation rec {
     ++ optional stdenv.isSunOS ./ld-shared.patch
     ++ stdenv.lib.optional stdenv.isDarwin [ ./cpp-precomp.patch ./no-libutil.patch ] ;
 
+  # There's an annoying bug on sandboxed Darwin in Perl's Cwd.pm where it looks for pwd
+  # in /bin/pwd and /usr/bin/pwd and then falls back on just "pwd" if it can't get them
+  # while at the same time erasing the PATH environment variable so it unconditionally
+  # fails. The code in question is guarded by a check for Mac OS, but the patch below
+  # doesn't have any runtime effect on other platforms.
+  postPatch = ''
+    pwd="$(type -P pwd)"
+    substituteInPlace dist/PathTools/Cwd.pm \
+      --replace "pwd_cmd = 'pwd'" "pwd_cmd = '$pwd'"
+  '';
+
   # Build a thread-safe Perl with a dynamic libperls.o.  We need the
   # "installstyle" option to ensure that modules are put under
   # $out/lib/perl5 - this is the general default, but because $out
diff --git a/pkgs/development/interpreters/python/2.7/default.nix b/pkgs/development/interpreters/python/2.7/default.nix
index 4ad4679bd6e..9e1f0e61744 100644
--- a/pkgs/development/interpreters/python/2.7/default.nix
+++ b/pkgs/development/interpreters/python/2.7/default.nix
@@ -62,6 +62,8 @@ let
       for i in Lib/plat-*/regen; do
         substituteInPlace $i --replace /usr/include/ ${stdenv.cc.libc}/include/
       done
+    '' + optionalString stdenv.isDarwin ''
+      substituteInPlace configure --replace '`/usr/bin/arch`' '"i386"'
     '';
 
   configureFlags = [
@@ -72,6 +74,8 @@ let
     "--with-system-ffi"
     "--with-system-expat"
     "ac_cv_func_bind_textdomain_codeset=yes"
+  ] ++ optionals stdenv.isDarwin [
+    "--disable-toolbox-glue"
   ];
 
   postConfigure = if stdenv.isCygwin then ''
diff --git a/pkgs/development/libraries/gettext/default.nix b/pkgs/development/libraries/gettext/default.nix
index 15f11f8a133..f86a15cb0d6 100644
--- a/pkgs/development/libraries/gettext/default.nix
+++ b/pkgs/development/libraries/gettext/default.nix
@@ -19,7 +19,12 @@ stdenv.mkDerivation (rec {
             "--with-included-gettext"
             "--with-included-glib"
             "--with-included-libcroco"
-          ]);
+          ])
+     # avoid retaining reference to CF during stdenv bootstrap
+     ++ (stdenv.lib.optionals stdenv.isDarwin [
+        "gt_cv_func_CFPreferencesCopyAppValue=no"
+        "gt_cv_func_CFLocaleCopyCurrent=no"
+      ]);
 
   # On cross building, gettext supposes that the wchar.h from libc
   # does not fulfill gettext needs, so it tries to work with its
diff --git a/pkgs/development/libraries/icu/default.nix b/pkgs/development/libraries/icu/default.nix
index e95cb7ad097..909f4e936a9 100644
--- a/pkgs/development/libraries/icu/default.nix
+++ b/pkgs/development/libraries/icu/default.nix
@@ -32,6 +32,11 @@ stdenv.mkDerivation {
   configureFlags = "--disable-debug" +
     stdenv.lib.optionalString stdenv.isDarwin " --enable-rpath";
 
+  # remove dependency on bootstrap-tools in early stdenv build
+  postInstall = stdenv.lib.optionalString stdenv.isDarwin ''
+    sed -i 's/INSTALL_CMD=.*install/INSTALL_CMD=install/' $out/lib/icu/${version}/pkgdata.inc
+  '';
+
   enableParallelBuilding = true;
 
   meta = with stdenv.lib; {
diff --git a/pkgs/development/libraries/libedit/default.nix b/pkgs/development/libraries/libedit/default.nix
index dc2f5842b28..5adef716eb0 100644
--- a/pkgs/development/libraries/libedit/default.nix
+++ b/pkgs/development/libraries/libedit/default.nix
@@ -16,7 +16,7 @@ stdenv.mkDerivation rec {
   ] else null;
 
   postInstall = ''
-    sed -i ${stdenv.lib.optionalString (stdenv.isDarwin && stdenv.cc.nativeTools) "''"} s/-lncurses/-lncursesw/g $out/lib/pkgconfig/libedit.pc
+    sed -i s/-lncurses/-lncursesw/g $out/lib/pkgconfig/libedit.pc
   '';
 
   configureFlags = [ "--enable-widec" ];
diff --git a/pkgs/development/libraries/zlib/default.nix b/pkgs/development/libraries/zlib/default.nix
index 419a2584ca8..93474d14344 100644
--- a/pkgs/development/libraries/zlib/default.nix
+++ b/pkgs/development/libraries/zlib/default.nix
@@ -13,6 +13,13 @@ stdenv.mkDerivation (rec {
     sha256 = "039agw5rqvqny92cpkrfn243x2gd4xn13hs3xi6isk55d2vqqr9n";
   };
 
+  postPatch = stdenv.lib.optionalString stdenv.isDarwin ''
+    substituteInPlace configure \
+      --replace '/usr/bin/libtool' 'ar' \
+      --replace 'AR="libtool"' 'AR="ar"' \
+      --replace 'ARFLAGS="-o"' 'ARFLAGS="-r"'
+  '';
+
   configureFlags = if static then "" else "--shared";
 
   preConfigure = ''
diff --git a/pkgs/development/tools/misc/binutils/default.nix b/pkgs/development/tools/misc/binutils/default.nix
index f740c60c487..4b2a55c1d90 100644
--- a/pkgs/development/tools/misc/binutils/default.nix
+++ b/pkgs/development/tools/misc/binutils/default.nix
@@ -2,8 +2,6 @@
 , cross ? null, gold ? true, bison ? null
 }:
 
-assert !stdenv.isDarwin;
-
 let basename = "binutils-2.23.1"; in
 
 with { inherit (stdenv.lib) optional optionals optionalString; };
@@ -56,7 +54,9 @@ stdenv.mkDerivation rec {
 
   # As binutils takes part in the stdenv building, we don't want references
   # to the bootstrap-tools libgcc (as uses to happen on arm/mips)
-  NIX_CFLAGS_COMPILE = "-static-libgcc";
+  NIX_CFLAGS_COMPILE = if stdenv.isDarwin
+    then "-Wno-string-plus-int -Wno-deprecated-declarations"
+    else "-static-libgcc";
 
   configureFlags =
     [ "--enable-shared" "--enable-deterministic-archives" ]
diff --git a/pkgs/os-specific/darwin/adv_cmds/locale.nix b/pkgs/os-specific/darwin/adv_cmds/locale.nix
new file mode 100644
index 00000000000..92de8a242e2
--- /dev/null
+++ b/pkgs/os-specific/darwin/adv_cmds/locale.nix
@@ -0,0 +1,29 @@
+{ stdenv, fetchurl }:
+
+stdenv.mkDerivation rec {
+  name = "locale-${version}";
+  version = "153";
+
+  src = fetchurl {
+    url    = "http://opensource.apple.com/tarballs/adv_cmds/adv_cmds-${version}.tar.gz";
+    sha256 = "174v6a4zkcm2pafzgdm6kvs48z5f911zl7k49hv7kjq6gm58w99v";
+  };
+
+  buildPhase = ''
+    cd locale
+    c++ -Os -Wall -o locale locale.cc
+  '';
+
+  installPhase = ''
+    mkdir -p $out/bin $out/share/man/man1
+
+    cp locale   $out/bin/locale
+    cp locale.1 $out/share/man/man1
+  '';
+
+
+  meta = {
+    platforms = stdenv.lib.platforms.darwin;
+    maintainers = with stdenv.lib.maintainers; [ gridaphobe ];
+  };
+}
diff --git a/pkgs/os-specific/darwin/binutils/default.nix b/pkgs/os-specific/darwin/binutils/default.nix
new file mode 100644
index 00000000000..abe4aa67b14
--- /dev/null
+++ b/pkgs/os-specific/darwin/binutils/default.nix
@@ -0,0 +1,39 @@
+{ stdenv, binutils-raw, cctools }:
+
+stdenv.mkDerivation {
+  name = "cctools-binutils-darwin";
+  buildCommand = ''
+    mkdir -p $out/bin $out/include
+
+    ln -s ${binutils-raw}/bin/c++filt $out/bin/c++filt
+
+    # We specifically need:
+    # - ld: binutils doesn't provide it on darwin
+    # - as: as above
+    # - ar: the binutils one prodices .a files that the cctools ld doesn't like
+    # - ranlib: for compatibility with ar
+    # - dsymutil: soon going away once it goes into LLVM (this one is fake anyway)
+    # - otool: we use it for some of our name mangling
+    # - install_name_tool: we use it to rewrite stuff in our bootstrap tools
+    # - strip: the binutils one seems to break mach-o files
+    # - lipo: gcc build assumes it exists
+    # - nm: the gnu one doesn't understand many new load commands
+    for i in ar ranlib as dsymutil install_name_tool ld strip otool lipo nm strings size; do
+      ln -sf "${cctools}/bin/$i" "$out/bin/$i"
+    done
+
+    for i in ${binutils-raw}/include/*.h; do
+      ln -s "$i" "$out/include/$(basename $i)"
+    done
+
+    for i in ${cctools}/include/*; do
+      ln -s "$i" "$out/include/$(basename $i)"
+    done
+
+    # FIXME: this will give us incorrect man pages for bits of cctools
+    ln -s ${binutils-raw}/share $out/share
+    ln -s ${binutils-raw}/lib $out/lib
+
+    ln -s ${cctools}/libexec $out/libexec
+  '';
+}
diff --git a/pkgs/stdenv/darwin/default.nix b/pkgs/stdenv/darwin/default.nix
index ac396ecdf12..d4b17a7909c 100644
--- a/pkgs/stdenv/darwin/default.nix
+++ b/pkgs/stdenv/darwin/default.nix
@@ -4,144 +4,294 @@
 , config      ? {}
 }:
 
-rec {
+let
+  fetch = { file, sha256 }: import <nix/fetchurl.nix> {
+    url = "https://dl.dropboxusercontent.com/u/2857322/${file}";
+    inherit sha256;
+    executable = true;
+  };
+
+  bootstrapFiles = {
+    sh    = fetch { file = "sh";    sha256 = "1qakpg37vl61jnkplz13m3g1csqr85cg8ybp6jwiv6apmg26isnm"; };
+    bzip2 = fetch { file = "bzip2"; sha256 = "1gxa67255q9v00j1vn1mzyrnbwys2g1102cx02vpcyvvrl4vqxr0"; };
+    mkdir = fetch { file = "mkdir"; sha256 = "1yfl8w65ksji7fggrbvqxw8lp0gm02qilk11n9axj2jxay53ngvg"; };
+    cpio  = fetch { file = "cpio";  sha256 = "0nssyg19smgcblwq1mfcw4djbd85md84d2f093qcqkbigdjg484b"; };
+  };
+  tarball = fetch { file = "bootstrap-tools.9.cpio.bz2"; sha256 = "0fd79k7gy3z3sba5w4f4lnrcpiwff31vw02480x1pdry8bbgbf2j"; };
+in rec {
   allPackages = import ../../top-level/all-packages.nix;
 
-  bootstrapTools = derivation {
-    inherit system;
+  commonPreHook = ''
+    export NIX_ENFORCE_PURITY=1
+    export NIX_IGNORE_LD_THROUGH_GCC=1
+    stripAllFlags=" " # the Darwin "strip" command doesn't know "-s"
+    export MACOSX_DEPLOYMENT_TARGET=10.7
+    export SDKROOT=
+    export CMAKE_OSX_ARCHITECTURES=x86_64
+  '';
+
+  # libSystem and its transitive dependencies. Get used to this; it's a recurring theme in darwin land
+  libSystemClosure = [
+    "/usr/lib/libSystem.dylib"
+    "/usr/lib/libSystem.B.dylib"
+    "/usr/lib/libobjc.A.dylib"
+    "/usr/lib/libobjc.dylib"
+    "/usr/lib/libauto.dylib"
+    "/usr/lib/libc++abi.dylib"
+    "/usr/lib/libc++.1.dylib"
+    "/usr/lib/libDiagnosticMessagesClient.dylib"
+    "/usr/lib/system"
+  ];
 
-    name    = "trivial-bootstrap-tools";
-    builder = "/bin/sh";
-    args    = [ ./trivial-bootstrap.sh ];
+  # The one dependency of /bin/sh :(
+  binShClosure = [ "/usr/lib/libncurses.5.4.dylib" ];
 
-    mkdir   = "/bin/mkdir";
-    ln      = "/bin/ln";
+  bootstrapTools = derivation rec {
+    inherit system tarball;
+
+    name    = "bootstrap-tools";
+    builder = bootstrapFiles.sh; # Not a filename! Attribute 'sh' on bootstrapFiles
+    args    = [ ./unpack-bootstrap-tools.sh ];
+
+    inherit (bootstrapFiles) mkdir bzip2 cpio;
+
+    __impureHostDeps  = binShClosure ++ libSystemClosure;
   };
 
-  # The simplest stdenv possible to run fetchadc and get the Apple command-line tools
-  stage0 = rec {
-    fetchurl = import ../../build-support/fetchurl {
-      inherit stdenv;
-      curl = bootstrapTools;
-    };
+  stageFun = step: last: {shell             ? "${bootstrapTools}/bin/sh",
+                          overrides         ? (pkgs: {}),
+                          extraPreHook      ? "",
+                          extraBuildInputs  ? with last.pkgs; [ xz darwin.CF libcxx ],
+                          extraInitialPath  ? [],
+                          allowedRequisites ? null}:
+    let
+      thisStdenv = import ../generic {
+        inherit system config shell extraBuildInputs allowedRequisites;
+
+        name = "stdenv-darwin-boot-${toString step}";
+
+        cc = if isNull last then "/no-such-path" else import ../../build-support/cc-wrapper {
+          inherit shell;
+          inherit (last) stdenv;
+          inherit (last.pkgs.darwin) dyld;
+
+          nativeTools  = true;
+          nativePrefix = bootstrapTools;
+          nativeLibc   = false;
+          libc         = last.pkgs.darwin.Libsystem;
+          cc           = { name = "clang-9.9.9"; outPath = bootstrapTools; };
+        };
+
+        preHook = stage0.stdenv.lib.optionalString (shell == "${bootstrapTools}/bin/sh") ''
+          # Don't patch #!/interpreter because it leads to retained
+          # dependencies on the bootstrapTools in the final stdenv.
+          dontPatchShebangs=1
+        '' + ''
+          ${commonPreHook}
+          ${extraPreHook}
+        '';
+        initialPath  = extraInitialPath ++ [ bootstrapTools ];
+        fetchurlBoot = import ../../build-support/fetchurl {
+          stdenv = stage0.stdenv;
+          curl   = bootstrapTools;
+        };
+
+        # The stdenvs themselves don't use mkDerivation, so I need to specify this here
+        __stdenvImpureHostDeps = binShClosure ++ libSystemClosure;
+        __extraImpureHostDeps  = binShClosure ++ libSystemClosure;
+
+        extraAttrs = { inherit platform; };
+        overrides  = pkgs: (overrides pkgs) // { fetchurl = thisStdenv.fetchurlBoot; };
+      };
+
+      thisPkgs = allPackages {
+        inherit system platform;
+        bootStdenv = thisStdenv;
+      };
+    in { stdenv = thisStdenv; pkgs = thisPkgs; };
+
+  stage0 = stageFun 0 null {
+    overrides = orig: with stage0; rec {
+      darwin = orig.darwin // {
+        Libsystem = stdenv.mkDerivation {
+          name = "bootstrap-Libsystem";
+          buildCommand = ''
+            mkdir -p $out
+            ln -s ${bootstrapTools}/lib $out/lib
+            ln -s ${bootstrapTools}/include-Libsystem $out/include
+          '';
+        };
+        dyld = bootstrapTools;
+      };
+
+      libcxx = stdenv.mkDerivation {
+        name = "bootstrap-libcxx";
+        phases = [ "installPhase" "fixupPhase" ];
+        installPhase = ''
+          mkdir -p $out/lib $out/include
+          ln -s ${bootstrapTools}/lib/libc++.dylib $out/lib/libc++.dylib
+          ln -s ${bootstrapTools}/include/c++      $out/include/c++
+        '';
+        setupHook = ../../development/compilers/llvm/3.5/libc++/setup-hook.sh;
+      };
+
+      libcxxabi = stdenv.mkDerivation {
+        name = "bootstrap-libcxxabi";
+        buildCommand = ''
+          mkdir -p $out/lib
+          ln -s ${bootstrapTools}/lib/libc++abi.dylib $out/lib/libc++abi.dylib
+        '';
+      };
 
-    stdenv = import ../generic {
-      inherit system config;
-      name         = "stdenv-darwin-boot-0";
-      shell        = "/bin/bash";
-      initialPath  = [ bootstrapTools ];
-      fetchurlBoot = fetchurl;
-      cc           = "/no-such-path";
     };
+
+    extraBuildInputs = [];
   };
 
-  buildTools = import ../../os-specific/darwin/command-line-tools {
-    inherit (stage0) stdenv fetchurl;
-    xar  = bootstrapTools;
-    gzip = bootstrapTools;
-    cpio = bootstrapTools;
+  persistent0 = _: {};
+
+  stage1 = with stage0; stageFun 1 stage0 {
+    extraPreHook = "export NIX_CFLAGS_COMPILE+=\" -F${bootstrapTools}/Library/Frameworks\"";
+    extraBuildInputs = [ pkgs.libcxx ];
+
+    allowedRequisites =
+      [ bootstrapTools ] ++ (with pkgs; [ libcxx libcxxabi ]) ++ [ pkgs.darwin.Libsystem ];
+
+    overrides = persistent0;
   };
 
-  preHook = ''
-    export NIX_IGNORE_LD_THROUGH_GCC=1
-    export NIX_DONT_SET_RPATH=1
-    export NIX_NO_SELF_RPATH=1
-    dontFixLibtool=1
-    stripAllFlags=" " # the Darwin "strip" command doesn't know "-s"
-    xargsFlags=" "
-    export MACOSX_DEPLOYMENT_TARGET=10.7
-    # Use the 10.9 SDK if we're running on 10.9, and 10.10 if we're
-    # running on 10.10. We need to use the 10.10 headers for functions
-    # like readlinkat() that are dynamically detected by configure
-    # scripts. Very impure, obviously.
-    export SDKROOT=$(/usr/bin/xcrun --sdk macosx"$(/usr/bin/sw_vers -productVersion | /usr/bin/cut -d. -f1,2)" --show-sdk-path 2> /dev/null || echo /)
-    export NIX_CFLAGS_COMPILE+=" --sysroot=/var/empty -idirafter $SDKROOT/usr/include -F$SDKROOT/System/Library/Frameworks -Wno-multichar -Wno-deprecated-declarations"
-    export NIX_LDFLAGS_AFTER+=" -L$SDKROOT/usr/lib"
-    export CMAKE_OSX_ARCHITECTURES=x86_64
-  '';
+  persistent1 = orig: with stage1.pkgs; {
+    inherit
+      zlib patchutils m4 scons flex perl bison unifdef unzip openssl icu python
+      libxml2 gettext sharutils gmp libarchive ncurses pkg-config libedit groff
+      openssh sqlite sed serf openldap db cyrus-sasl expat apr-util subversion xz
+      findfreetype libssh curl cmake autoconf automake libtool ed cpio coreutils;
 
-  # A stdenv that wraps the Apple command-line tools and our other trivial symlinked bootstrap tools
-  stage1 = rec {
-    nativePrefix = "${buildTools.tools}/Library/Developer/CommandLineTools/usr";
-
-    stdenv = import ../generic {
-      name = "stdenv-darwin-boot-1";
-
-      inherit system config;
-      inherit (stage0.stdenv) shell fetchurlBoot;
-
-      initialPath = stage0.stdenv.initialPath ++ [ nativePrefix ];
-
-      preHook = preHook + "\n" + ''
-        export NIX_LDFLAGS_AFTER+=" -L/usr/lib"
-        export NIX_ENFORCE_PURITY=
-        export NIX_CFLAGS_COMPILE+=" -isystem ${nativePrefix}/include/c++/v1 -stdlib=libc++"
-        export NIX_CFLAGS_LINK+=" -stdlib=libc++ -Wl,-rpath,${nativePrefix}/lib"
-      '';
-
-      cc = import ../../build-support/cc-wrapper {
-        nativeTools  = true;
-        nativePrefix = nativePrefix;
-        nativeLibc   = true;
-        stdenv       = stage0.stdenv;
-        shell        = "/bin/bash";
-        cc           = {
-          name    = "clang-9.9.9";
-          cc      = "/usr";
-          outPath = nativePrefix;
-        };
-        isClang      = true;
-      };
+    darwin = orig.darwin // {
+      inherit (darwin)
+        dyld Libsystem xnu configd libdispatch libclosure launchd;
     };
-    pkgs = allPackages {
-      inherit system platform;
-      bootStdenv = stdenv;
+  };
+
+  stage2 = with stage1; stageFun 2 stage1 {
+    allowedRequisites =
+      [ bootstrapTools ] ++
+      (with pkgs; [ xz libcxx libcxxabi icu ]) ++
+      (with pkgs.darwin; [ dyld Libsystem CF ]);
+
+    overrides = persistent1;
+  };
+
+  persistent2 = orig: with stage2.pkgs; {
+    inherit
+      patchutils m4 scons flex perl bison unifdef unzip openssl python
+      gettext sharutils libarchive pkg-config groff bash subversion
+      openssh sqlite sed serf openldap db cyrus-sasl expat apr-util
+      findfreetype libssh curl cmake autoconf automake libtool cpio
+      libcxx libcxxabi;
+
+    darwin = orig.darwin // {
+      inherit (darwin)
+        dyld Libsystem xnu configd libdispatch libclosure launchd libiconv;
     };
   };
 
-  stage2 = rec {
-    stdenv = import ../generic {
-      name = "stdenv-darwin-boot-2";
+  stage3 = with stage2; stageFun 3 stage2 {
+    shell = "${pkgs.bash}/bin/bash";
+
+    # We have a valid shell here (this one has no bootstrap-tools runtime deps) so stageFun
+    # enables patchShebangs above. Unfortunately, patchShebangs ignores our $SHELL setting
+    # and instead goes by $PATH, which happens to contain bootstrapTools. So it goes and
+    # patches our shebangs back to point at bootstrapTools. This makes sure bash comes first.
+    extraInitialPath = [ pkgs.bash ];
 
-      inherit system config;
-      inherit (stage1.stdenv) shell fetchurlBoot preHook cc;
+    allowedRequisites =
+      [ bootstrapTools ] ++
+      (with pkgs; [ icu bash libcxx libcxxabi ]) ++
+      (with pkgs.darwin; [ dyld Libsystem ]);
 
-      initialPath = [ stage1.pkgs.xz ] ++ stage1.stdenv.initialPath;
+    overrides = persistent2;
+  };
+
+  persistent3 = orig: with stage3.pkgs; {
+    inherit
+      gnumake gzip gnused bzip2 gawk ed xz patch bash
+      libcxxabi libcxx ncurses libffi zlib llvm gmp pcre gnugrep
+      coreutils findutils diffutils patchutils;
+
+    llvmPackages = orig.llvmPackages // {
+      inherit (llvmPackages) llvm clang-unwrapped;
     };
-    pkgs = allPackages {
-      inherit system platform;
-      bootStdenv = stdenv;
+
+    darwin = orig.darwin // {
+      inherit (darwin) dyld Libsystem libiconv;
     };
   };
 
-  # Use stage1 to build a whole set of actual tools so we don't have to rely on the Apple prebuilt ones or
-  # the ugly symlinked bootstrap tools anymore.
-  stage3 = with stage2; import ../generic {
-    name = "stdenv-darwin-boot-3";
+  stage4 = with stage3; stageFun 4 stage3 {
+    shell = "${pkgs.bash}/bin/bash";
+    extraInitialPath = [ pkgs.bash ];
+    overrides = persistent3;
+  };
+
+  persistent4 = orig: with stage4.pkgs; {
+    inherit
+      gnumake gzip gnused bzip2 gawk ed xz patch bash
+      libcxxabi libcxx ncurses libffi zlib icu llvm gmp pcre gnugrep
+      coreutils findutils diffutils patchutils binutils binutils-raw;
+
+    llvmPackages = orig.llvmPackages // {
+      inherit (llvmPackages) llvm clang-unwrapped;
+    };
 
+    darwin = orig.darwin // {
+      inherit (darwin) dyld Libsystem cctools CF libiconv;
+    };
+  };
+
+  stage5 = with stage4; import ../generic rec {
     inherit system config;
     inherit (stdenv) fetchurlBoot;
 
-    initialPath = (import ../common-path.nix) { inherit pkgs; };
+    name = "stdenv-darwin";
+
+    preHook = commonPreHook;
 
-    preHook = preHook + "\n" + ''
-      export NIX_ENFORCE_PURITY=1
-    '';
+    __stdenvImpureHostDeps = binShClosure ++ libSystemClosure;
+    __extraImpureHostDeps  = binShClosure ++ libSystemClosure;
+
+    initialPath = import ../common-path.nix { inherit pkgs; };
+    shell       = "${pkgs.bash}/bin/bash";
 
     cc = import ../../build-support/cc-wrapper {
-      inherit stdenv;
-      nativeTools   = false;
-      nativeLibc    = true;
-      binutils      = pkgs.darwin.cctools;
-      cc            = pkgs.llvmPackages.clang-unwrapped;
-      coreutils     = pkgs.coreutils;
-      shell         = "${pkgs.bash}/bin/bash";
-      extraPackages = [ pkgs.libcxx ];
-      isClang       = true;
+      inherit stdenv shell;
+      nativeTools = false;
+      nativeLibc  = false;
+      inherit (pkgs) coreutils binutils;
+      inherit (pkgs.darwin) dyld;
+      cc   = pkgs.llvmPackages.clang-unwrapped;
+      libc = pkgs.darwin.Libsystem;
     };
 
-    shell = "${pkgs.bash}/bin/bash";
-  };
+    extraBuildInputs = with pkgs; [ darwin.CF libcxx ];
+
+    extraAttrs = {
+      inherit platform bootstrapTools;
+      libc         = pkgs.darwin.Libsystem;
+      shellPackage = pkgs.bash;
+    };
+
+    allowedRequisites = (with pkgs; [
+      xz libcxx libcxxabi icu gmp gnumake findutils bzip2 llvm zlib libffi
+      coreutils ed diffutils gnutar gzip ncurses gnused bash gawk
+      gnugrep llvmPackages.clang-unwrapped patch pcre binutils-raw binutils gettext
+    ]) ++ (with pkgs.darwin; [
+      dyld Libsystem CF cctools libiconv
+    ]);
 
-  stdenvDarwin = stage3;
+    overrides = orig: persistent4 orig // {
+      clang = cc;
+      inherit cc;
+    };
+  };
 }
diff --git a/pkgs/stdenv/darwin/make-bootstrap-tools.nix b/pkgs/stdenv/darwin/make-bootstrap-tools.nix
index 636410fdd78..433638dd1bd 100644
--- a/pkgs/stdenv/darwin/make-bootstrap-tools.nix
+++ b/pkgs/stdenv/darwin/make-bootstrap-tools.nix
@@ -63,13 +63,15 @@ rec {
 
       cp -d ${gnugrep.pcre}/lib/libpcre*.dylib $out/lib
       cp -d ${libiconv}/lib/libiconv*.dylib $out/lib
+      cp -d ${gettext}/lib/libintl*.dylib $out/lib
+      chmod +x $out/lib/libintl*.dylib
 
       # Copy what we need of clang
-      cp -d ${llvmPackages.clang}/bin/clang $out/bin
-      cp -d ${llvmPackages.clang}/bin/clang++ $out/bin
-      cp -d ${llvmPackages.clang}/bin/clang-3.5 $out/bin
+      cp -d ${llvmPackages.clang-unwrapped}/bin/clang $out/bin
+      cp -d ${llvmPackages.clang-unwrapped}/bin/clang++ $out/bin
+      cp -d ${llvmPackages.clang-unwrapped}/bin/clang-3.6 $out/bin
 
-      cp -rL ${llvmPackages.clang}/lib/clang $out/lib
+      cp -rL ${llvmPackages.clang-unwrapped}/lib/clang $out/lib
 
       cp -d ${libcxx}/lib/libc++*.dylib $out/lib
       cp -d ${libcxxabi}/lib/libc++abi*.dylib $out/lib
diff --git a/pkgs/stdenv/default.nix b/pkgs/stdenv/default.nix
index 545a3d748eb..28ba8001471 100644
--- a/pkgs/stdenv/default.nix
+++ b/pkgs/stdenv/default.nix
@@ -37,7 +37,7 @@ rec {
   stdenvLinux = (import ./linux { inherit system allPackages platform config lib; }).stdenvLinux;
 
   # Darwin standard environment.
-  stdenvDarwin = (import ./darwin { inherit system allPackages platform config;}).stdenvDarwin;
+  stdenvDarwin = (import ./darwin { inherit system allPackages platform config;}).stage5;
 
   # Select the appropriate stdenv for the platform `system'.
   stdenv =
diff --git a/pkgs/stdenv/generic/default.nix b/pkgs/stdenv/generic/default.nix
index 863acc31a61..67adaf42a8d 100644
--- a/pkgs/stdenv/generic/default.nix
+++ b/pkgs/stdenv/generic/default.nix
@@ -10,6 +10,8 @@ let lib = import ../../../lib; in lib.makeOverridable (
 , setupScript ? ./setup.sh
 
 , extraBuildInputs ? []
+, __stdenvImpureHostDeps ? []
+, __extraImpureHostDeps ? []
 }:
 
 let
@@ -131,7 +133,19 @@ let
 
       lib.addPassthru (derivation (
         (removeAttrs attrs ["meta" "passthru" "crossAttrs" "pos"])
-        //
+        // (let
+          buildInputs = attrs.buildInputs or [];
+          nativeBuildInputs = attrs.nativeBuildInputs or [];
+          propagatedBuildInputs = attrs.propagatedBuildInputs or [];
+          propagatedNativeBuildInputs = attrs.propagatedNativeBuildInputs or [];
+          crossConfig = attrs.crossConfig or null;
+
+          __impureHostDeps = attrs.__impureHostDeps or [];
+          __propagatedImpureHostDeps = attrs.__propagatedImpureHostDeps or [];
+
+          computedImpureHostDeps           = lib.concatMap (input: input.__propagatedImpureHostDeps or []) (extraBuildInputs ++ buildInputs ++ nativeBuildInputs);
+          computedPropagatedImpureHostDeps = lib.concatMap (input: input.__propagatedImpureHostDeps or []) (propagatedBuildInputs ++ propagatedNativeBuildInputs);
+        in
         {
           builder = attrs.realBuilder or shell;
           args = attrs.args or ["-e" (attrs.builder or ./default-builder.sh)];
@@ -147,7 +161,15 @@ let
           nativeBuildInputs = nativeBuildInputs ++ (if crossConfig == null then buildInputs else []);
           propagatedNativeBuildInputs = propagatedNativeBuildInputs ++
             (if crossConfig == null then propagatedBuildInputs else []);
-        })) (
+
+          __impureHostDeps = lib.unique (lib.sort (x: y: x < y) (computedImpureHostDeps ++ computedPropagatedImpureHostDeps ++ __propagatedImpureHostDeps ++ __impureHostDeps ++ __extraImpureHostDeps ++ [
+            "/dev/zero"
+            "/dev/random"
+            "/dev/urandom"
+            "/bin/sh"
+          ]));
+          __propagatedImpureHostDeps = lib.unique (lib.sort (x: y: x < y) (computedPropagatedImpureHostDeps ++ __propagatedImpureHostDeps));
+        }))) (
       {
         # The meta attribute is passed in the resulting attribute set,
         # but it's not part of the actual derivation, i.e., it's not
@@ -171,6 +193,7 @@ let
     (if isNull allowedRequisites then {} else { allowedRequisites = allowedRequisites ++ defaultNativeBuildInputs; }) //
     {
       inherit system name;
+      __impureHostDeps = __stdenvImpureHostDeps;
 
       builder = shell;
 
diff --git a/pkgs/tools/archivers/gnutar/default.nix b/pkgs/tools/archivers/gnutar/default.nix
index 00be3967768..71b43bf2de9 100644
--- a/pkgs/tools/archivers/gnutar/default.nix
+++ b/pkgs/tools/archivers/gnutar/default.nix
@@ -11,6 +11,12 @@ stdenv.mkDerivation rec {
 
   patches = stdenv.lib.optional stdenv.isDarwin ./gnutar-1.28-darwin.patch;
 
+  # avoid retaining reference to CF during stdenv bootstrap
+  configureFlags = stdenv.lib.optionals stdenv.isDarwin [
+    "gt_cv_func_CFPreferencesCopyAppValue=no"
+    "gt_cv_func_CFLocaleCopyCurrent=no"
+  ];
+
   # gnutar tries to call into gettext between `fork` and `exec`,
   # which is not safe on darwin.
   # see http://article.gmane.org/gmane.os.macosx.fink.devel/21882
diff --git a/pkgs/tools/archivers/sharutils/default.nix b/pkgs/tools/archivers/sharutils/default.nix
index 146f9bea87a..bfeda5085b6 100644
--- a/pkgs/tools/archivers/sharutils/default.nix
+++ b/pkgs/tools/archivers/sharutils/default.nix
@@ -12,7 +12,7 @@ stdenv.mkDerivation rec {
     ''
        # Fix for building on Glibc 2.16.  Won't be needed once the
        # gnulib in sharutils is updated.
-       sed -i ${stdenv.lib.optionalString ((stdenv.isFreeBSD || stdenv.isOpenBSD || stdenv.isDarwin) && stdenv.cc.nativeTools) "''"} '/gets is a security hole/d' lib/stdio.in.h
+       sed -i ${stdenv.lib.optionalString ((stdenv.isFreeBSD || stdenv.isOpenBSD) && stdenv.cc.nativeTools) "''"} '/gets is a security hole/d' lib/stdio.in.h
     '';
 
   # GNU Gettext is needed on non-GNU platforms.
diff --git a/pkgs/tools/text/gawk/default.nix b/pkgs/tools/text/gawk/default.nix
index c6eb7ba1101..001a50458e2 100644
--- a/pkgs/tools/text/gawk/default.nix
+++ b/pkgs/tools/text/gawk/default.nix
@@ -1,4 +1,5 @@
-{ stdenv, fetchurl, libsigsegv, readline, readlineSupport ? false }:
+{ stdenv, fetchurl, libsigsegv, readline, readlineSupport ? false
+, locale ? null }:
 
 stdenv.mkDerivation rec {
   name = "gawk-4.1.3";
@@ -8,10 +9,14 @@ stdenv.mkDerivation rec {
     sha256 = "09d6pmx6h3i2glafm0jd1v1iyrs03vcyv2rkz12jisii3vlmbkz3";
   };
 
-  doCheck = !stdenv.isCygwin; # XXX: `test-dup2' segfaults on Cygwin 6.1
+  doCheck = !(
+       stdenv.isCygwin # XXX: `test-dup2' segfaults on Cygwin 6.1
+    || stdenv.isDarwin # XXX: `locale' segfaults
+  );
 
   buildInputs = stdenv.lib.optional (stdenv.system != "x86_64-cygwin") libsigsegv
-    ++ stdenv.lib.optional readlineSupport readline;
+    ++ stdenv.lib.optional readlineSupport readline
+    ++ stdenv.lib.optional stdenv.isDarwin locale;
 
   configureFlags = stdenv.lib.optional (stdenv.system != "x86_64-cygwin") "--with-libsigsegv-prefix=${libsigsegv}"
     ++ stdenv.lib.optional readlineSupport "--with-readline=${readline}"
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index 1e2dd6ad220..e903f6395b9 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -1512,7 +1512,7 @@ let
 
   garmintools = callPackage ../development/libraries/garmintools {};
 
-  gawk = callPackage ../tools/text/gawk { };
+  gawk = callPackage ../tools/text/gawk { inherit (darwin) locale; };
 
   gawkInteractive = appendToName "interactive"
     (gawk.override { readlineSupport = true; });
@@ -4670,12 +4670,13 @@ let
     nativePrefix = stdenv.cc.nativePrefix or "";
     cc = baseCC;
     libc = libc;
+    dyld = if stdenv.isDarwin then darwin.dyld else null;
     isGNU = baseCC.isGNU or false;
     isClang = baseCC.isClang or false;
     inherit stdenv binutils coreutils zlib;
   };
 
-  wrapCC = wrapCCWith (makeOverridable (import ../build-support/cc-wrapper)) glibc;
+  wrapCC = wrapCCWith (makeOverridable (import ../build-support/cc-wrapper)) stdenv.cc.libc;
   # legacy version, used for gnat bootstrapping
   wrapGCC-old = baseGCC: (makeOverridable (import ../build-support/gcc-wrapper-old)) {
     nativeTools = stdenv.cc.nativeTools or false;
@@ -5182,11 +5183,9 @@ let
 
   bin_replace_string = callPackage ../development/tools/misc/bin_replace_string { };
 
-  binutils = if stdenv.isDarwin
-    then import ../build-support/native-darwin-cctools-wrapper {inherit stdenv;}
-    else callPackage ../development/tools/misc/binutils {
-      inherit noSysDirs;
-    };
+  binutils = if stdenv.isDarwin then darwin.binutils else binutils-raw;
+
+  binutils-raw = callPackage ../development/tools/misc/binutils { inherit noSysDirs; };
 
   binutils_nogold = lowPrio (callPackage ../development/tools/misc/binutils {
     inherit noSysDirs;
@@ -6942,7 +6941,10 @@ let
   # glibc provides libiconv so systems with glibc don't need to build libiconv
   # separately, but we also provide libiconvReal, which will always be a
   # standalone libiconv, just in case you want it
-  libiconv = if stdenv.isGlibc then stdenv.cc.libc else libiconvReal;
+  libiconv =
+    if stdenv.isGlibc then stdenv.cc.libc
+    else if stdenv.isDarwin then darwin.libiconv
+    else libiconvReal;
 
   libiconvReal = callPackage ../development/libraries/libiconv {
     fetchurl = fetchurlBoot;
@@ -9279,7 +9281,7 @@ let
       xctoolchain = xcode.toolchain;
     };
 
-    cctools = (callPackage ../os-specific/darwin/cctools/port.nix {}).native;
+    cctools = (callPackage ../os-specific/darwin/cctools/port.nix { inherit libobjc; }).native;
 
     maloader = callPackage ../os-specific/darwin/maloader {
       inherit opencflite;
@@ -9294,8 +9296,12 @@ let
 
     ps = callPackage ../os-specific/darwin/adv_cmds/ps.nix {};
 
+    locale = callPackage ../os-specific/darwin/adv_cmds/locale.nix {};
+
     security_tool = callPackage ../os-specific/darwin/security-tool { inherit osx_private_sdk; };
 
+    binutils = callPackage ../os-specific/darwin/binutils { inherit cctools; };
+
     cmdline_sdk   = cmdline.sdk;
     cmdline_tools = cmdline.tools;