From f5e96d3e1b701691d213fcd17db3831a64bdedc3 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Mon, 12 Feb 2018 19:42:57 -0600
Subject: [PATCH 001/173] xz: set CONFIG_SHELL to /bin/sh, fix retained
 reference to bootstrap

---
 pkgs/tools/compression/xz/default.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/tools/compression/xz/default.nix b/pkgs/tools/compression/xz/default.nix
index 05cc672ab15..8d02e926e57 100644
--- a/pkgs/tools/compression/xz/default.nix
+++ b/pkgs/tools/compression/xz/default.nix
@@ -13,7 +13,7 @@ stdenv.mkDerivation rec {
   doCheck = true;
 
   # In stdenv-linux, prevent a dependency on bootstrap-tools.
-  preConfigure = "unset CONFIG_SHELL";
+  preConfigure = "CONFIG_SHELL=/bin/sh";
 
   postInstall = "rm -rf $out/share/doc";
 

From 2870c99cef39d8eecd17d01acb00479cf579b2c9 Mon Sep 17 00:00:00 2001
From: Ben Gamari <ben@smart-cactus.org>
Date: Wed, 25 Oct 2017 15:44:19 -0400
Subject: [PATCH 002/173] autogen: Enable cross-compilation

---
 .../tools/misc/autogen/default.nix            | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/pkgs/development/tools/misc/autogen/default.nix b/pkgs/development/tools/misc/autogen/default.nix
index 28034f9d549..77944297a97 100644
--- a/pkgs/development/tools/misc/autogen/default.nix
+++ b/pkgs/development/tools/misc/autogen/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, which, pkgconfig, perl, guile, libxml2 }:
+{ stdenv, buildPackages, fetchurl, which, pkgconfig, texinfo, perl, guile, libxml2 }:
 
 stdenv.mkDerivation rec {
   name = "autogen-${version}";
@@ -11,8 +11,21 @@ stdenv.mkDerivation rec {
 
   outputs = [ "bin" "dev" "lib" "out" "man" "info" ];
 
-  nativeBuildInputs = [ which pkgconfig perl ];
-  buildInputs = [ guile libxml2 ];
+  nativeBuildInputs = [ which pkgconfig perl ]
+    # autogen needs a build autogen when cross-compiling
+    ++ stdenv.lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
+      buildPackages.autogen buildPackages.texinfo ];
+  buildInputs = [
+    guile libxml2
+  ];
+
+  configureFlags = stdenv.lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
+    "--with-libxml2=${libxml2.dev}"
+    "--with-libxml2-cflags=-I${libxml2.dev}/include/libxml2"
+    # the configure check for regcomp wants to run a host program
+    "libopts_cv_with_libregex=yes"
+    #"MAKEINFO=${buildPackages.texinfo}/bin/makeinfo"
+  ];
 
   postPatch = ''
     # Fix a broken sed expression used for detecting the minor

From df5b0ce589f369dbc10c23f5dadd57f8e9b669c2 Mon Sep 17 00:00:00 2001
From: Ben Gamari <ben@smart-cactus.org>
Date: Fri, 27 Oct 2017 19:08:28 -0400
Subject: [PATCH 003/173] xorg: Use build dependencies where appropriate

---
 pkgs/top-level/all-packages.nix | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index 7a9fb7bc1fb..9b7c1863553 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -12570,11 +12570,12 @@ with pkgs;
   };
 
   xorg = recurseIntoAttrs (lib.callPackagesWith pkgs ../servers/x11/xorg/default.nix {
-    inherit clangStdenv fetchurl fetchgit fetchpatch stdenv pkgconfig intltool freetype fontconfig
+    inherit clangStdenv fetchurl fetchgit fetchpatch stdenv intltool freetype fontconfig
       libxslt expat libpng zlib perl mesa_drivers spice_protocol libunwind
       dbus libuuid openssl gperf m4 libevdev tradcpp libinput mcpp makeWrapper autoreconfHook
-      autoconf automake libtool xmlto asciidoc flex bison mtdev pixman
+      autoconf automake libtool mtdev pixman
       cairo epoxy;
+    inherit (buildPackages) pkgconfig xmlto asciidoc flex bison;
     inherit (darwin) apple_sdk cf-private libobjc;
     bootstrap_cmds = if stdenv.isDarwin then darwin.bootstrap_cmds else null;
     mesa = mesa_noglu;

From d72c4c694ffac76d8cc20813c8b9dc2a11ad46a9 Mon Sep 17 00:00:00 2001
From: Ben Gamari <ben@smart-cactus.org>
Date: Sun, 10 Dec 2017 13:40:18 -0500
Subject: [PATCH 004/173] w3m: Enable cross-compilation

---
 pkgs/applications/networking/browsers/w3m/default.nix | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/pkgs/applications/networking/browsers/w3m/default.nix b/pkgs/applications/networking/browsers/w3m/default.nix
index 83819761e9b..c71ccdf8a0d 100644
--- a/pkgs/applications/networking/browsers/w3m/default.nix
+++ b/pkgs/applications/networking/browsers/w3m/default.nix
@@ -53,8 +53,12 @@ stdenv.mkDerivation rec {
 
   hardeningDisable = [ "format" ];
 
-  configureFlags = "--with-ssl=${openssl.dev} --with-gc=${boehmgc.dev}"
-    + optionalString graphicsSupport " --enable-image=${optionalString x11Support "x11,"}fb";
+  configureFlags =
+    [ "--with-ssl=${openssl.dev}" "--with-gc=${boehmgc.dev}" ]
+    ++ optionals (stdenv.buildPlatform != stdenv.hostPlatform) [
+      "ac_cv_func_setpgrp_void=yes"
+    ]
+    ++ optional graphicsSupport "--enable-image=${optionalString x11Support "x11,"}fb";
 
   preConfigure = ''
     substituteInPlace ./configure --replace "/lib /usr/lib /usr/local/lib /usr/ucblib /usr/ccslib /usr/ccs/lib /lib64 /usr/lib64" /no-such-path

From a868bf379727628e55eb30d872c5c6b5c91fba1b Mon Sep 17 00:00:00 2001
From: Ben Gamari <ben@smart-cactus.org>
Date: Fri, 15 Dec 2017 21:30:37 -0500
Subject: [PATCH 005/173] glibc: Fix cross-compilation of locales

---
 pkgs/development/libraries/glibc/common.nix  | 2 +-
 pkgs/development/libraries/glibc/locales.nix | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/pkgs/development/libraries/glibc/common.nix b/pkgs/development/libraries/glibc/common.nix
index b17d4effb1e..d40733adf87 100644
--- a/pkgs/development/libraries/glibc/common.nix
+++ b/pkgs/development/libraries/glibc/common.nix
@@ -222,6 +222,6 @@ stdenv.mkDerivation ({
 
   # To avoid a dependency on the build system 'bash'.
   preFixup = ''
-    rm $bin/bin/{ldd,tzselect,catchsegv,xtrace}
+    rm -f $bin/bin/{ldd,tzselect,catchsegv,xtrace}
   '';
 })
diff --git a/pkgs/development/libraries/glibc/locales.nix b/pkgs/development/libraries/glibc/locales.nix
index debd7b39c86..7090c5cecee 100644
--- a/pkgs/development/libraries/glibc/locales.nix
+++ b/pkgs/development/libraries/glibc/locales.nix
@@ -6,7 +6,7 @@
    https://sourceware.org/git/?p=glibc.git;a=blob;f=localedata/SUPPORTED
 */
 
-{ stdenv, callPackage, writeText
+{ stdenv, buildPackages, callPackage, writeText
 , allLocales ? true, locales ? [ "en_US.UTF-8/UTF-8" ]
 }:
 
@@ -26,7 +26,7 @@ callPackage ./common.nix { inherit stdenv; } {
   # $TMPDIR/nix/store/...-glibc-.../lib/locale/locale-archive.
   buildPhase =
     ''
-      mkdir -p $TMPDIR/"${stdenv.cc.libc.out}/lib/locale"
+      mkdir -p $TMPDIR/"${buildPackages.stdenv.cc.libc.out}/lib/locale"
 
       # Hack to allow building of the locales (needed since glibc-2.12)
       sed -i -e 's,^$(rtld-prefix) $(common-objpfx)locale/localedef,localedef --prefix='$TMPDIR',' ../glibc-2*/localedata/Makefile

From da310bdd6c9c91d3d89750e02eff3106d9344c37 Mon Sep 17 00:00:00 2001
From: Ben Gamari <ben@smart-cactus.org>
Date: Sat, 16 Dec 2017 16:59:34 -0500
Subject: [PATCH 006/173] sudo: Enable tmpfiles.d is disabled

---
 pkgs/tools/security/sudo/default.nix | 1 +
 1 file changed, 1 insertion(+)

diff --git a/pkgs/tools/security/sudo/default.nix b/pkgs/tools/security/sudo/default.nix
index 5192f57582c..1edd5b60926 100644
--- a/pkgs/tools/security/sudo/default.nix
+++ b/pkgs/tools/security/sudo/default.nix
@@ -28,6 +28,7 @@ stdenv.mkDerivation rec {
     "--with-logpath=/var/log/sudo.log"
     "--with-iologdir=/var/log/sudo-io"
     "--with-sendmail=${sendmailPath}"
+    "--enable-tmpfiles.d=no"
   ] ++ stdenv.lib.optional withInsults [
     "--with-insults"
     "--with-all-insults"

From e120c42ccf212a26bdea54c2c67f2419e364d88b Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sun, 14 Jan 2018 19:07:06 -0600
Subject: [PATCH 007/173] iproute: don't use host BASH for scripts, fix for
 cross

Doesn't eliminate glibc dep, somehow
a dependency on host's "db" leaks,
but it's a start.

Also don't attempt to build unused 'netem' tools,
avoiding need to use HOSTCC.
---
 pkgs/os-specific/linux/iproute/default.nix | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/pkgs/os-specific/linux/iproute/default.nix b/pkgs/os-specific/linux/iproute/default.nix
index 793c9db603b..a78107310c3 100644
--- a/pkgs/os-specific/linux/iproute/default.nix
+++ b/pkgs/os-specific/linux/iproute/default.nix
@@ -12,6 +12,8 @@ stdenv.mkDerivation rec {
   preConfigure = ''
     patchShebangs ./configure
     sed -e '/ARPDDIR/d' -i Makefile
+    # Don't build netem tools--they're not installed and require HOSTCC
+    substituteInPlace Makefile --replace " netem " " "
   '';
 
   makeFlags = [
@@ -37,6 +39,10 @@ stdenv.mkDerivation rec {
 
   enableParallelBuilding = true;
 
+  postInstall = ''
+    PATH=${stdenv.shell}/bin:$PATH patchShebangs $out/sbin
+  '';
+
   meta = with stdenv.lib; {
     homepage = https://wiki.linuxfoundation.org/networking/iproute2;
     description = "A collection of utilities for controlling TCP/IP networking and traffic control in Linux";

From 47540dab74224bb94ebb8c8a327391b5bd45e960 Mon Sep 17 00:00:00 2001
From: Ben Gamari <ben@smart-cactus.org>
Date: Mon, 25 Dec 2017 10:30:25 -0500
Subject: [PATCH 008/173] lua: Fix cross-compilation

---
 pkgs/development/interpreters/lua-5/5.1.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/development/interpreters/lua-5/5.1.nix b/pkgs/development/interpreters/lua-5/5.1.nix
index 1981c15c5f3..1c4fcd2811d 100644
--- a/pkgs/development/interpreters/lua-5/5.1.nix
+++ b/pkgs/development/interpreters/lua-5/5.1.nix
@@ -33,7 +33,7 @@ stdenv.mkDerivation rec {
     makeFlagsArray=( INSTALL_TOP=$out INSTALL_MAN=$out/share/man/man1 PLAT=macosx CFLAGS="-DLUA_USE_LINUX -fno-common -O2" LDFLAGS="" CC="$CC" )
     installFlagsArray=( TO_BIN="lua luac" TO_LIB="liblua.5.1.5.dylib" INSTALL_DATA='cp -d' )
   '' else ''
-    makeFlagsArray=( INSTALL_TOP=$out INSTALL_MAN=$out/share/man/man1 PLAT=linux CFLAGS="-DLUA_USE_LINUX -O2 -fPIC" LDFLAGS="-fPIC" )
+    makeFlagsArray=( INSTALL_TOP=$out INSTALL_MAN=$out/share/man/man1 PLAT=linux CFLAGS="-DLUA_USE_LINUX -O2 -fPIC" LDFLAGS="-fPIC" CC="$CC" AR="$AR q" RANLIB="$RANLIB" )
     installFlagsArray=( TO_BIN="lua luac" TO_LIB="liblua.a liblua.so liblua.so.5.1 liblua.so.5.1.5" INSTALL_DATA='cp -d' )
   '';
 

From 46c2e619e856ad679ae41005e8ba2a9dac39ff77 Mon Sep 17 00:00:00 2001
From: Ben Gamari <ben@smart-cactus.org>
Date: Sun, 15 Oct 2017 13:04:42 -0400
Subject: [PATCH 009/173] linux-pam: Add necessary build dependencies for
 cross-compilation

Ensure that we use the correct `ar` and add explicit dependency on the build CC.

(cherry picked from commit 5efb768011f6bce870f4ce814295a399d5a89c10)
---
 pkgs/os-specific/linux/pam/default.nix | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/pkgs/os-specific/linux/pam/default.nix b/pkgs/os-specific/linux/pam/default.nix
index 3de7916bff6..fc2a7ea61d2 100644
--- a/pkgs/os-specific/linux/pam/default.nix
+++ b/pkgs/os-specific/linux/pam/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, flex, cracklib }:
+{ stdenv, buildPackages, fetchurl, flex, cracklib }:
 
 stdenv.mkDerivation rec {
   name = "linux-pam-${version}";
@@ -11,7 +11,7 @@ stdenv.mkDerivation rec {
 
   outputs = [ "out" "doc" "man" /* "modules" */ ];
 
-  nativeBuildInputs = [ flex ];
+  nativeBuildInputs = [ buildPackages.stdenv.cc flex ];
 
   buildInputs = [ cracklib ];
 
@@ -20,7 +20,7 @@ stdenv.mkDerivation rec {
   crossAttrs = {
     propagatedBuildInputs = [ flex.crossDrv cracklib.crossDrv ];
     preConfigure = preConfigure + ''
-      ar x ${flex.crossDrv}/lib/libfl.a
+      $crossConfig-ar x ${flex.crossDrv}/lib/libfl.a
       mv libyywrap.o libyywrap-target.o
       ar x ${flex}/lib/libfl.a
       mv libyywrap.o libyywrap-host.o

From 4f3c8178b8e5eef920c31b030cd89591deb2417d Mon Sep 17 00:00:00 2001
From: Ben Gamari <ben@smart-cactus.org>
Date: Sun, 10 Dec 2017 00:17:26 -0500
Subject: [PATCH 010/173] groff: Enable cross-compilation

---
 pkgs/tools/text/groff/default.nix       |  6 ++++
 pkgs/tools/text/groff/look-for-ar.patch | 39 +++++++++++++++++++++++++
 2 files changed, 45 insertions(+)
 create mode 100644 pkgs/tools/text/groff/look-for-ar.patch

diff --git a/pkgs/tools/text/groff/default.nix b/pkgs/tools/text/groff/default.nix
index b11f3cb4b8c..6d01c42f93d 100644
--- a/pkgs/tools/text/groff/default.nix
+++ b/pkgs/tools/text/groff/default.nix
@@ -2,6 +2,7 @@
 , ghostscript #for postscript and html output
 , psutils, netpbm #for html output
 , buildPackages
+, autoreconfHook
 }:
 
 stdenv.mkDerivation rec {
@@ -17,6 +18,8 @@ stdenv.mkDerivation rec {
 
   enableParallelBuilding = false;
 
+  patches = [ ./look-for-ar.patch ];
+
   postPatch = stdenv.lib.optionalString (psutils != null) ''
     substituteInPlace src/preproc/html/pre-html.cpp \
       --replace "psselect" "${psutils}/bin/psselect"
@@ -32,6 +35,7 @@ stdenv.mkDerivation rec {
   '';
 
   buildInputs = [ ghostscript psutils netpbm perl ];
+  nativeBuildInputs = [ autoreconfHook ];
 
   # Builds running without a chroot environment may detect the presence
   # of /usr/X11 in the host system, leading to an impure build of the
@@ -42,6 +46,8 @@ stdenv.mkDerivation rec {
     "--without-x"
   ] ++ stdenv.lib.optionals (ghostscript != null) [
     "--with-gs=${ghostscript}/bin/gs"
+  ] ++ stdenv.lib.optionals (stdenv.buildPlatform != stdenv.hostPlatform) [
+    "ac_cv_path_PERL=${perl}/bin/perl"
   ];
 
   doCheck = true;
diff --git a/pkgs/tools/text/groff/look-for-ar.patch b/pkgs/tools/text/groff/look-for-ar.patch
new file mode 100644
index 00000000000..3b4d09c260f
--- /dev/null
+++ b/pkgs/tools/text/groff/look-for-ar.patch
@@ -0,0 +1,39 @@
+commit 988a001f969262089579958827652da00fda096f
+Author: Ben Gamari <ben@smart-cactus.org>
+Date:   Tue Oct 17 16:15:20 2017 -0400
+
+    Look for AR
+
+diff --git a/Makefile.in b/Makefile.in
+index bc156ce..cc02126 100644
+--- a/Makefile.in
++++ b/Makefile.in
+@@ -464,7 +464,7 @@ INSTALL_SCRIPT=@INSTALL_SCRIPT@
+ INSTALL_DATA=@INSTALL_DATA@
+ INSTALL_INFO=@INSTALL_INFO@
+ LN_S=@LN_S@
+-AR=ar
++AR=@AR@
+ ETAGS=etags
+ ETAGSFLAGS=
+ # Flag that tells etags to assume C++.
+diff --git a/configure.ac b/configure.ac
+index c779165..6d53f5e 100644
+--- a/configure.ac
++++ b/configure.ac
+@@ -45,6 +45,7 @@ AC_PREREQ([2.62])
+ 
+ AC_CONFIG_HEADERS([src/include/config.h:src/include/config.hin])
+ AC_CONFIG_SRCDIR([src/roff/groff/groff.cpp])
++AC_CONFIG_MACRO_DIR([m4])
+ 
+ AC_USE_SYSTEM_EXTENSIONS
+ 
+@@ -73,6 +74,7 @@ GROFF_PROG_YACC
+ GROFF_DOC_CHECK
+ GROFF_MAKEINFO
+ AC_PROG_RANLIB
++AC_CHECK_TOOL([AR], [ar], [ar])
+ GROFF_INSTALL_SH
+ GROFF_INSTALL_INFO
+ AC_PROG_INSTALL

From d625f181bd2dc21a7095066d50f778bd6be92332 Mon Sep 17 00:00:00 2001
From: Ben Gamari <ben@smart-cactus.org>
Date: Sun, 5 Nov 2017 18:16:11 -0500
Subject: [PATCH 011/173] autoconf: Enable cross-compilation

---
 .../compilers/gcc/6/fix-objdump-check.patch   | 43 +++++++++++++++++++
 .../tools/misc/autoconf/default.nix           |  3 +-
 2 files changed, 45 insertions(+), 1 deletion(-)
 create mode 100644 pkgs/development/compilers/gcc/6/fix-objdump-check.patch

diff --git a/pkgs/development/compilers/gcc/6/fix-objdump-check.patch b/pkgs/development/compilers/gcc/6/fix-objdump-check.patch
new file mode 100644
index 00000000000..f9adbe9eb1c
--- /dev/null
+++ b/pkgs/development/compilers/gcc/6/fix-objdump-check.patch
@@ -0,0 +1,43 @@
+commit 4c38abe0967bad78dd6baa61c86923e4d4b346d3
+Author: Ben Gamari <ben@smart-cactus.org>
+Date:   Sun Nov 5 13:14:19 2017 -0500
+
+    Fix it
+
+diff --git a/config/gcc-plugin.m4 b/config/gcc-plugin.m4
+index dd06a58..f4435b8 100644
+--- a/config/gcc-plugin.m4
++++ b/config/gcc-plugin.m4
+@@ -13,6 +13,32 @@ dnl the same distribution terms as the rest of that program.
+ # Sets the shell variables enable_plugin and pluginlibs.
+ AC_DEFUN([GCC_ENABLE_PLUGINS],
+   [# Check for plugin support
++
++   # Figure out what objdump we will be using.
++   AS_VAR_SET_IF(gcc_cv_objdump,, [
++   if test -f $gcc_cv_binutils_srcdir/configure.ac \
++        && test -f ../binutils/Makefile \
++        && test x$build = x$host; then
++   	# Single tree build which includes binutils.
++   	gcc_cv_objdump=../binutils/objdump$build_exeext
++   elif test -x objdump$build_exeext; then
++   	gcc_cv_objdump=./objdump$build_exeext
++   elif ( set dummy $OBJDUMP_FOR_TARGET; test -x $[2] ); then
++           gcc_cv_objdump="$OBJDUMP_FOR_TARGET"
++   else
++           AC_PATH_PROG(gcc_cv_objdump, $OBJDUMP_FOR_TARGET)
++   fi])
++   
++   AC_MSG_CHECKING(what objdump to use)
++   if test "$gcc_cv_objdump" = ../binutils/objdump$build_exeext; then
++   	# Single tree build which includes binutils.
++   	AC_MSG_RESULT(newly built objdump)
++   elif test x$gcc_cv_objdump = x; then
++   	AC_MSG_RESULT(not found)
++   else
++   	AC_MSG_RESULT($gcc_cv_objdump)
++   fi
++
+    AC_ARG_ENABLE(plugin,
+    [AS_HELP_STRING([--enable-plugin], [enable plugin support])],
+    enable_plugin=$enableval,
diff --git a/pkgs/development/tools/misc/autoconf/default.nix b/pkgs/development/tools/misc/autoconf/default.nix
index 472f437978b..579dea33df4 100644
--- a/pkgs/development/tools/misc/autoconf/default.nix
+++ b/pkgs/development/tools/misc/autoconf/default.nix
@@ -8,7 +8,8 @@ stdenv.mkDerivation rec {
     sha256 = "113nlmidxy9kjr45kg9x3ngar4951mvag1js2a3j8nxcz34wxsv4";
   };
 
-  buildInputs = [ m4 perl ];
+  nativeBuildInputs = [ m4 perl ];
+  buildInputs = [ m4 ];
 
   # Work around a known issue in Cygwin.  See
   # http://thread.gmane.org/gmane.comp.sysutils.autoconf.bugs/6822 for

From cb5453e13a564a356ae0913075529852d8cdf212 Mon Sep 17 00:00:00 2001
From: Ben Gamari <ben@smart-cactus.org>
Date: Tue, 17 Oct 2017 23:35:20 -0400
Subject: [PATCH 012/173] cpython: Enable cross-compilation

---
 .../python/cpython/2.7/cross-compile.patch    | 44 +++++++++++++++++++
 .../python/cpython/2.7/default.nix            | 36 +++++++++++++--
 .../python/cpython/3.6/default.nix            | 30 +++++++++++--
 3 files changed, 104 insertions(+), 6 deletions(-)
 create mode 100644 pkgs/development/interpreters/python/cpython/2.7/cross-compile.patch

diff --git a/pkgs/development/interpreters/python/cpython/2.7/cross-compile.patch b/pkgs/development/interpreters/python/cpython/2.7/cross-compile.patch
new file mode 100644
index 00000000000..4d0c72675cc
--- /dev/null
+++ b/pkgs/development/interpreters/python/cpython/2.7/cross-compile.patch
@@ -0,0 +1,44 @@
+commit 9ac1104b075ecea6175e2214b81c6538adbb9e84
+Author: Ben Gamari <ben@smart-cactus.org>
+Date:   Tue Oct 17 23:53:25 2017 -0400
+
+    Fix it
+
+diff --git a/Makefile.pre.in b/Makefile.pre.in
+index e288964..ddc7428 100644
+--- a/Makefile.pre.in
++++ b/Makefile.pre.in
+@@ -1120,27 +1120,27 @@ libinstall:	build_all $(srcdir)/Lib/$(PLATDIR) $(srcdir)/Modules/xxmodule.c
+ 		$(INSTALL_DATA) $(srcdir)/Modules/xxmodule.c \
+ 			$(DESTDIR)$(LIBDEST)/distutils/tests ; \
+ 	fi
+-	PYTHONPATH=$(DESTDIR)$(LIBDEST)  $(RUNSHARED) \
++	$(RUNSHARED) \
+ 		$(PYTHON_FOR_BUILD) -Wi -tt $(DESTDIR)$(LIBDEST)/compileall.py \
+ 		-d $(LIBDEST) -f \
+ 		-x 'bad_coding|badsyntax|site-packages|lib2to3/tests/data' \
+ 		$(DESTDIR)$(LIBDEST)
+-	PYTHONPATH=$(DESTDIR)$(LIBDEST) $(RUNSHARED) \
++	$(RUNSHARED) \
+ 		$(PYTHON_FOR_BUILD) -Wi -tt -O $(DESTDIR)$(LIBDEST)/compileall.py \
+ 		-d $(LIBDEST) -f \
+ 		-x 'bad_coding|badsyntax|site-packages|lib2to3/tests/data' \
+ 		$(DESTDIR)$(LIBDEST)
+-	-PYTHONPATH=$(DESTDIR)$(LIBDEST)  $(RUNSHARED) \
++	-$(RUNSHARED) \
+ 		$(PYTHON_FOR_BUILD) -Wi -t $(DESTDIR)$(LIBDEST)/compileall.py \
+ 		-d $(LIBDEST)/site-packages -f \
+ 		-x badsyntax $(DESTDIR)$(LIBDEST)/site-packages
+-	-PYTHONPATH=$(DESTDIR)$(LIBDEST) $(RUNSHARED) \
++	-$(RUNSHARED) \
+ 		$(PYTHON_FOR_BUILD) -Wi -t -O $(DESTDIR)$(LIBDEST)/compileall.py \
+ 		-d $(LIBDEST)/site-packages -f \
+ 		-x badsyntax $(DESTDIR)$(LIBDEST)/site-packages
+-	-PYTHONPATH=$(DESTDIR)$(LIBDEST) $(RUNSHARED) \
++	-$(RUNSHARED) \
+ 		$(PYTHON_FOR_BUILD) -m lib2to3.pgen2.driver $(DESTDIR)$(LIBDEST)/lib2to3/Grammar.txt
+-	-PYTHONPATH=$(DESTDIR)$(LIBDEST) $(RUNSHARED) \
++	-$(RUNSHARED) \
+ 		$(PYTHON_FOR_BUILD) -m lib2to3.pgen2.driver $(DESTDIR)$(LIBDEST)/lib2to3/PatternGrammar.txt
+ 
+ # Create the PLATDIR source directory, if one wasn't distributed..
diff --git a/pkgs/development/interpreters/python/cpython/2.7/default.nix b/pkgs/development/interpreters/python/cpython/2.7/default.nix
index 1cb739b4d29..28ad3aeca91 100644
--- a/pkgs/development/interpreters/python/cpython/2.7/default.nix
+++ b/pkgs/development/interpreters/python/cpython/2.7/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, hostPlatform, fetchurl
+{ stdenv, hostPlatform, buildPlatform, buildPackages, fetchurl
 , bzip2
 , gdbm
 , fetchpatch
@@ -89,6 +89,10 @@ let
       # compiler when needed.
       ./python-2.7-distutils-C++.patch
 
+    ] ++ [
+
+      # Don't use pass host PYTHONPATH to build python
+      #./cross-compile.patch
     ];
 
   preConfigure = ''
@@ -117,6 +121,28 @@ let
     "ac_cv_func_bind_textdomain_codeset=yes"
   ] ++ optionals stdenv.isDarwin [
     "--disable-toolbox-glue"
+  ] ++ optionals (hostPlatform != buildPlatform) [
+    "PYTHON_FOR_BUILD=${getBin buildPackages.python}/bin/python"
+    "ac_cv_buggy_getaddrinfo=no"
+    # Assume little-endian IEEE 754 floating point when cross compiling
+    "ac_cv_little_endian_double=yes"
+    "ac_cv_big_endian_double=no"
+    "ac_cv_mixed_endian_double=no"
+    "ac_cv_x87_double_rounding=yes"
+    "ac_cv_tanh_preserves_zero_sign=yes"
+    # Generally assume that things are present and work
+    "ac_cv_posix_semaphores_enabled=yes"
+    "ac_cv_broken_sem_getvalue=no"
+    "ac_cv_wchar_t_signed=yes"
+    "ac_cv_rshift_extends_sign=yes"
+    "ac_cv_broken_nice=no"
+    "ac_cv_broken_poll=no"
+    "ac_cv_working_tzset=yes"
+    "ac_cv_have_long_long_format=yes"
+    "ac_cv_have_size_t_format=yes"
+    "ac_cv_computed_gotos=yes"
+    "ac_cv_file__dev_ptmx=yes"
+    "ac_cv_file__dev_ptc=yes"
   ];
 
   postConfigure = if hostPlatform.isCygwin then ''
@@ -131,6 +157,9 @@ let
     ++ [ db gdbm ncurses sqlite readline ]
     ++ optionals x11Support [ tcl tk xlibsWrapper libX11 ]
     ++ optionals stdenv.isDarwin ([ CF ] ++ optional (configd != null) configd);
+  nativeBuildInputs =
+    optionals (hostPlatform != buildPlatform)
+    [ buildPackages.stdenv.cc buildPackages.python ];
 
   mkPaths = paths: {
     C_INCLUDE_PATH = makeSearchPathOutput "dev" "include" paths;
@@ -144,7 +173,7 @@ in stdenv.mkDerivation {
     name = "python-${version}";
     pythonVersion = majorVersion;
 
-    inherit majorVersion version src patches buildInputs
+    inherit majorVersion version src patches buildInputs nativeBuildInputs
             preConfigure configureFlags;
 
     LDFLAGS = stdenv.lib.optionalString (!stdenv.isDarwin) "-lgcc_s";
@@ -187,7 +216,8 @@ in stdenv.mkDerivation {
         # Determinism: Windows installers were not deterministic.
         # We're also not interested in building Windows installers.
         find "$out" -name 'wininst*.exe' | xargs -r rm -f
-
+      '' + optionalString (stdenv.hostPlatform == stdenv.buildPlatform)
+      ''
         # Determinism: rebuild all bytecode
         # We exclude lib2to3 because that's Python 2 code which fails
         # We rebuild three times, once for each optimization level
diff --git a/pkgs/development/interpreters/python/cpython/3.6/default.nix b/pkgs/development/interpreters/python/cpython/3.6/default.nix
index f48f2c19026..fb58d0871ec 100644
--- a/pkgs/development/interpreters/python/cpython/3.6/default.nix
+++ b/pkgs/development/interpreters/python/cpython/3.6/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, fetchpatch
+{ stdenv, fetchurl, fetchpatch, buildPackages
 , glibc
 , bzip2
 , expat
@@ -39,12 +39,15 @@ let
     ++ optionals x11Support [ tcl tk libX11 xproto ]
     ++ optionals stdenv.isDarwin [ CF configd ];
 
+  nativeBuildInputs =
+    optional (stdenv.hostPlatform != stdenv.buildPlatform) buildPackages.python3;
+
 in stdenv.mkDerivation {
   name = "python3-${version}";
   pythonVersion = majorVersion;
   inherit majorVersion version;
 
-  inherit buildInputs;
+  inherit buildInputs nativeBuildInputs;
 
   src = fetchurl {
     url = "https://www.python.org/ftp/python/${majorVersion}.${minorVersion}/Python-${version}.tar.xz";
@@ -87,6 +90,27 @@ in stdenv.mkDerivation {
     "--without-ensurepip"
     "--with-system-expat"
     "--with-system-ffi"
+  ] ++ optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
+    "ac_cv_buggy_getaddrinfo=no"
+    # Assume little-endian IEEE 754 floating point when cross compiling
+    "ac_cv_little_endian_double=yes"
+    "ac_cv_big_endian_double=no"
+    "ac_cv_mixed_endian_double=no"
+    "ac_cv_x87_double_rounding=yes"
+    "ac_cv_tanh_preserves_zero_sign=yes"
+    # Generally assume that things are present and work
+    "ac_cv_posix_semaphores_enabled=yes"
+    "ac_cv_broken_sem_getvalue=no"
+    "ac_cv_wchar_t_signed=yes"
+    "ac_cv_rshift_extends_sign=yes"
+    "ac_cv_broken_nice=no"
+    "ac_cv_broken_poll=no"
+    "ac_cv_working_tzset=yes"
+    "ac_cv_have_long_long_format=yes"
+    "ac_cv_have_size_t_format=yes"
+    "ac_cv_computed_gotos=yes"
+    "ac_cv_file__dev_ptmx=yes"
+    "ac_cv_file__dev_ptc=yes"
   ];
 
   preConfigure = ''
@@ -139,7 +163,7 @@ in stdenv.mkDerivation {
     for i in $out/lib/python${majorVersion}/_sysconfigdata*.py $out/lib/python${majorVersion}/config-${majorVersion}m*/Makefile; do
       sed -i $i -e "s|-I/nix/store/[^ ']*||g" -e "s|-L/nix/store/[^ ']*||g" -e "s|$TMPDIR|/no-such-path|g"
     done
-
+  '' + optionalString (stdenv.hostPlatform == stdenv.buildPlatform) ''
     # Determinism: rebuild all bytecode
     # We exclude lib2to3 because that's Python 2 code which fails
     # We rebuild three times, once for each optimization level

From b9b57dada7cb2ba36e5550a8e37918ccddd5eb8c Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 9 Jan 2018 17:39:54 -0600
Subject: [PATCH 013/173] shadow: disable nscd on non-glibc (musl)

---
 pkgs/os-specific/linux/shadow/default.nix | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/pkgs/os-specific/linux/shadow/default.nix b/pkgs/os-specific/linux/shadow/default.nix
index 64d7a694fc1..ab53dcca025 100644
--- a/pkgs/os-specific/linux/shadow/default.nix
+++ b/pkgs/os-specific/linux/shadow/default.nix
@@ -60,9 +60,10 @@ stdenv.mkDerivation rec {
     configureFlags="$configureFlags --with-xml-catalog=$PWD/xmlcatalog ";
   '';
 
-  configureFlags = " --enable-man ";
+  configureFlags = " --enable-man "
+    + stdenv.lib.optionalString (hostPlatform.libc != "glibc") " --disable-nscd ";
 
-  preBuild = assert glibc != null;
+  preBuild = stdenv.lib.optionalString (hostPlatform.libc == "glibc")
     ''
       substituteInPlace lib/nscd.c --replace /usr/sbin/nscd ${glibc.bin}/bin/nscd
     '';

From 37c2ac90e6819c43fa1a1fc87a9c1f3ffc19957b Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 9 Jan 2018 17:46:16 -0600
Subject: [PATCH 014/173] cmake: fix cross

TODO: toolchain file instead?
---
 .../tools/build-managers/cmake/default.nix       | 16 +++++++++++++++-
 .../tools/build-managers/cmake/setup-hook.sh     | 10 +++++++++-
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/pkgs/development/tools/build-managers/cmake/default.nix b/pkgs/development/tools/build-managers/cmake/default.nix
index fed88561cf9..7dd97b8a206 100644
--- a/pkgs/development/tools/build-managers/cmake/default.nix
+++ b/pkgs/development/tools/build-managers/cmake/default.nix
@@ -83,11 +83,25 @@ stdenv.mkDerivation rec {
   configureFlags = [ "--docdir=share/doc/${name}" ]
     ++ (if useSharedLibraries then [ "--no-system-jsoncpp" "--system-libs" ] else [ "--no-system-libs" ]) # FIXME: cleanup
     ++ optional (useQt4 || withQt5) "--qt-gui"
-    ++ optionals (!useNcurses) [ "--" "-DBUILD_CursesDialog=OFF" ];
+    ++ ["--"]
+    ++ optionals (!useNcurses) [ "-DBUILD_CursesDialog=OFF" ]
+    ++ optionals (stdenv.buildPlatform != stdenv.hostPlatform) [
+      "-DCMAKE_CXX_COMPILER=${stdenv.cc.targetPrefix}g++"
+      "-DCMAKE_C_COMPILER=${stdenv.cc.targetPrefix}gcc"
+      "-DCMAKE_AR=${getBin stdenv.cc.bintools.bintools}/bin/${stdenv.cc.targetPrefix}ar"
+      "-DCMAKE_RANLIB=${getBin stdenv.cc.bintools.bintools}/bin/${stdenv.cc.targetPrefix}ranlib"
+      "-DCMAKE_STRIP=${getBin stdenv.cc.bintools.bintools}/bin/${stdenv.cc.targetPrefix}strip"
+      # TODO: Why are ar and friends not provided by the bintools wrapper?
+    ];
 
   dontUseCmakeConfigure = true;
   enableParallelBuilding = true;
 
+  # This isn't an autoconf configure script; triples are passed via
+  # CMAKE_SYSTEM_NAME, etc.
+  configurePlatforms = [ ];
+
+
   meta = with stdenv.lib; {
     homepage = http://www.cmake.org/;
     description = "Cross-Platform Makefile Generator";
diff --git a/pkgs/development/tools/build-managers/cmake/setup-hook.sh b/pkgs/development/tools/build-managers/cmake/setup-hook.sh
index a0f1cf00814..50e9e93b015 100755
--- a/pkgs/development/tools/build-managers/cmake/setup-hook.sh
+++ b/pkgs/development/tools/build-managers/cmake/setup-hook.sh
@@ -33,7 +33,15 @@ cmakeConfigurePhase() {
         # By now it supports linux builds only. We should set the proper
         # CMAKE_SYSTEM_NAME otherwise.
         # http://www.cmake.org/Wiki/CMake_Cross_Compiling
-        cmakeFlags="-DCMAKE_CXX_COMPILER=$crossConfig-g++ -DCMAKE_C_COMPILER=$crossConfig-gcc $cmakeFlags"
+        #
+        # Unfortunately cmake seems to expect absolute paths for ar, ranlib, and
+        # strip. Otherwise they are taken to be relative to the source root of
+        # the package being built.
+        cmakeFlags="-DCMAKE_CXX_COMPILER=$crossConfig-g++ $cmakeFlags"
+        cmakeFlags="-DCMAKE_C_COMPILER=$crossConfig-gcc $cmakeFlags"
+        cmakeFlags="-DCMAKE_AR=$(command -v $crossConfig-ar) $cmakeFlags"
+        cmakeFlags="-DCMAKE_RANLIB=$(command -v $crossConfig-ranlib) $cmakeFlags"
+        cmakeFlags="-DCMAKE_STRIP=$(command -v $crossConfig-strip) $cmakeFlags"
     fi
 
     # This installs shared libraries with a fully-specified install

From e6da426a54cbc754faa67c79888bcac49ef89a73 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Wed, 10 Jan 2018 17:33:19 -0600
Subject: [PATCH 015/173] man-db: fix cross

---
 pkgs/tools/misc/man-db/default.nix | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/pkgs/tools/misc/man-db/default.nix b/pkgs/tools/misc/man-db/default.nix
index d0cb9a22336..eadb736aeeb 100644
--- a/pkgs/tools/misc/man-db/default.nix
+++ b/pkgs/tools/misc/man-db/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, pkgconfig, libpipeline, db, groff, makeWrapper }:
+{ stdenv, fetchurl, pkgconfig, libpipeline, db, groff, libiconv, makeWrapper, buildPackages }:
 
 stdenv.mkDerivation rec {
   name = "man-db-2.7.5";
@@ -11,8 +11,10 @@ stdenv.mkDerivation rec {
   outputs = [ "out" "doc" ];
   outputMan = "out"; # users will want `man man` to work
 
-  nativeBuildInputs = [ pkgconfig makeWrapper ];
-  buildInputs = [ libpipeline db groff ];
+  nativeBuildInputs = [ pkgconfig makeWrapper groff ]
+    ++ stdenv.lib.optionals doCheck checkInputs;
+  buildInputs = [ libpipeline db groff ]; # (Yes, 'groff' is both native and build input)
+  checkInputs = [ libiconv /* for 'iconv' binary */ ];
 
   postPatch = ''
     substituteInPlace src/man_db.conf.in \
@@ -41,6 +43,18 @@ stdenv.mkDerivation rec {
     done
   '';
 
+  postFixup = stdenv.lib.optionalString (buildPackages.groff != groff) ''
+    # Check to make sure none of the outputs depend on build-time-only groff:
+    for outName in $outputs; do
+      out=''${!outName}
+      echo "Checking $outName(=$out) for references to build-time groff..."
+      if grep -r '${buildPackages.groff}' $out; then
+        echo "Found an erroneous dependency on groff ^^^" >&2
+        exit 1
+      fi
+    done
+  '';
+
   enableParallelBuilding = true;
 
   doCheck = true;

From 2d772d52ae92d97ef1bd62e0f084d162bda0a62c Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Wed, 10 Jan 2018 18:07:20 -0600
Subject: [PATCH 016/173] libbfd: fix for cross (based on same from @bgamari)

---
 pkgs/development/libraries/libbfd/default.nix | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/pkgs/development/libraries/libbfd/default.nix b/pkgs/development/libraries/libbfd/default.nix
index 5bcf243155b..b41fe0ad47e 100644
--- a/pkgs/development/libraries/libbfd/default.nix
+++ b/pkgs/development/libraries/libbfd/default.nix
@@ -1,5 +1,5 @@
 { stdenv
-, fetchurl, autoreconfHook264, bison, binutils-raw
+, fetchurl, fetchpatch, autoreconfHook264, buildPackages, bison, binutils-raw
 , libiberty, zlib
 }:
 
@@ -11,24 +11,37 @@ stdenv.mkDerivation rec {
 
   patches = binutils-raw.bintools.patches ++ [
     ../../tools/misc/binutils/build-components-separately.patch
+    (fetchpatch {
+      url = "https://raw.githubusercontent.com/mxe/mxe/e1d4c144ee1994f70f86cf7fd8168fe69bd629c6/src/bfd-1-disable-subdir-doc.patch";
+      sha256 = "0pzb3i74d1r7lhjan376h59a7kirw15j7swwm8pz3zy9lkdqkj6q";
+    })
   ];
 
   # We just want to build libbfd
-  postPatch = ''
+  preConfigure = ''
     cd bfd
   '';
 
+  depsBuildBuilds = [ buildPackages.stdenv.cc ];
   nativeBuildInputs = [ autoreconfHook264 bison ];
   buildInputs = [ libiberty zlib ];
 
-  configurePlatforms = [ "build" "host" ];
+  configurePlatforms = [ "build" "host" "target" ];
   configureFlags = [
     "--enable-targets=all" "--enable-64-bit-bfd"
     "--enable-install-libbfd"
     "--enable-shared"
     "--with-system-zlib"
+    "CC_FOR_BUILD=$(CC)"
   ];
 
+  postInstall = stdenv.lib.optionalString (stdenv.hostPlatform != stdenv.targetPlatform) ''
+    # the build system likes to move things into atypical locations
+    mkdir -p $dev
+    mv $out/${stdenv.hostPlatform.config}/${stdenv.targetPlatform.config}/include $dev/include
+    mv $out/${stdenv.hostPlatform.config}/${stdenv.targetPlatform.config}/lib $out/lib
+  '';
+
   enableParallelBuilding = true;
 
   meta = with stdenv.lib; {

From 41026550e2afb0182fca1ca59c57437b554359c6 Mon Sep 17 00:00:00 2001
From: Ben Gamari <ben@smart-cactus.org>
Date: Sun, 29 Oct 2017 15:51:16 -0400
Subject: [PATCH 017/173] ruby: Enable cross-compilation

---
 .../development/interpreters/ruby/default.nix | 24 +++++++++++++++----
 1 file changed, 19 insertions(+), 5 deletions(-)

diff --git a/pkgs/development/interpreters/ruby/default.nix b/pkgs/development/interpreters/ruby/default.nix
index 9d82810900f..a0c563e3fb2 100644
--- a/pkgs/development/interpreters/ruby/default.nix
+++ b/pkgs/development/interpreters/ruby/default.nix
@@ -1,4 +1,5 @@
-{ stdenv, lib, fetchurl, fetchpatch, fetchFromSavannah, fetchFromGitHub
+{ stdenv, buildPackages, lib
+, fetchurl, fetchpatch, fetchFromSavannah, fetchFromGitHub
 , zlib, openssl, gdbm, ncurses, readline, groff, libyaml, libffi, autoreconfHook, bison
 , autoconf, darwin ? null
 , buildEnv, bundler, bundix, Foundation
@@ -22,6 +23,12 @@ let
   # Contains the ruby version heuristics
   rubyVersion = import ./ruby-version.nix { inherit lib; };
 
+  # Needed during postInstall
+  buildRuby =
+    if stdenv.hostPlatform == stdenv.buildPlatform
+    then "$out/bin/ruby"
+    else "${buildPackages.ruby}/bin/ruby";
+
   generic = { version, sha256 }: let
     ver = version;
     tag = ver.gitTag;
@@ -30,7 +37,8 @@ let
     isRuby25 = ver.majMin == "2.5";
     baseruby = self.override { useRailsExpress = false; };
     self = lib.makeOverridable (
-      { stdenv, lib, fetchurl, fetchpatch, fetchFromSavannah, fetchFromGitHub
+  { stdenv, buildPackages, lib
+      , fetchurl, fetchpatch, fetchFromSavannah, fetchFromGitHub
       , useRailsExpress ? true
       , zlib, zlibSupport ? true
       , openssl, opensslSupport ? true
@@ -67,7 +75,11 @@ let
         # Have `configure' avoid `/usr/bin/nroff' in non-chroot builds.
         NROFF = "${groff}/bin/nroff";
 
-        nativeBuildInputs = ops useRailsExpress [ autoreconfHook bison ];
+        nativeBuildInputs =
+             ops useRailsExpress [ autoreconfHook bison ]
+          ++ ops (stdenv.buildPlatform != stdenv.hostPlatform) [
+               buildPackages.ruby
+             ];
         buildInputs =
              (op fiddleSupport libffi)
           ++ (ops cursesSupport [ ncurses readline ])
@@ -129,14 +141,16 @@ let
             "--with-out-ext=tk"
             # on yosemite, "generating encdb.h" will hang for a very long time without this flag
             "--with-setjmp-type=setjmp"
-          ];
+          ]
+          ++ op (stdenv.hostPlatform != stdenv.buildPlatform)
+             "--with-baseruby=${buildRuby}";
 
         installFlags = stdenv.lib.optionalString docSupport "install-doc";
         # Bundler tries to create this directory
         postInstall = ''
           # Update rubygems
           pushd rubygems
-          $out/bin/ruby setup.rb
+          ${buildRuby} setup.rb
           popd
 
           # Remove unnecessary groff reference from runtime closure, since it's big

From bc2ad5a9683d772ace19cf28f79ef98f0722b29a Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Thu, 11 Jan 2018 14:27:52 -0600
Subject: [PATCH 018/173] libunwind: patch to fix build on non-glibc

---
 .../libunwind/backtrace-only-with-glibc.patch | 45 +++++++++++++++++++
 .../libraries/libunwind/default.nix           |  1 +
 2 files changed, 46 insertions(+)
 create mode 100644 pkgs/development/libraries/libunwind/backtrace-only-with-glibc.patch

diff --git a/pkgs/development/libraries/libunwind/backtrace-only-with-glibc.patch b/pkgs/development/libraries/libunwind/backtrace-only-with-glibc.patch
new file mode 100644
index 00000000000..5fcaa72c0c0
--- /dev/null
+++ b/pkgs/development/libraries/libunwind/backtrace-only-with-glibc.patch
@@ -0,0 +1,45 @@
+From 04437142399662b576bd55a85485c6dcc14d0812 Mon Sep 17 00:00:00 2001
+From: Khem Raj <raj.khem at gmail.com>
+Date: Thu, 31 Dec 2015 06:44:07 +0000
+Subject: [PATCH] backtrace: Use only with glibc and uclibc
+
+backtrace API is glibc specific not linux specific
+so make it behave so.
+
+Signed-off-by: Khem Raj <raj.khem at gmail.com>
+---
+Upstream-Status: Pending
+
+ tests/test-coredump-unwind.c | 4 +++-
+ 1 file changed, 3 insertions(+), 1 deletion(-)
+
+diff --git a/tests/test-coredump-unwind.c b/tests/test-coredump-unwind.c
+index 5254708..8767b42 100644
+--- a/tests/test-coredump-unwind.c
++++ b/tests/test-coredump-unwind.c
+@@ -57,7 +57,9 @@
+ #include <grp.h>
+ 
+ /* For SIGSEGV handler code */
++#ifdef __GLIBC__
+ #include <execinfo.h>
++#endif
+ #include <sys/ucontext.h>
+ 
+ #include <libunwind-coredump.h>
+@@ -238,11 +240,11 @@ void handle_sigsegv(int sig, siginfo_t *info, void *ucontext)
+ 			ip);
+ 
+   {
++#ifdef __GLIBC__
+     /* glibc extension */
+     void *array[50];
+     int size;
+     size = backtrace(array, 50);
+-#ifdef __linux__
+     backtrace_symbols_fd(array, size, 2);
+ #endif
+   }
+-- 
+2.6.4
+
diff --git a/pkgs/development/libraries/libunwind/default.nix b/pkgs/development/libraries/libunwind/default.nix
index 8565bc75ff9..6afdac3fdf5 100644
--- a/pkgs/development/libraries/libunwind/default.nix
+++ b/pkgs/development/libraries/libunwind/default.nix
@@ -11,6 +11,7 @@ stdenv.mkDerivation rec {
 
   patches = [
     ./version-1.2.1.patch
+    ./backtrace-only-with-glibc.patch
   ];
 
   nativeBuildInputs = [ autoreconfHook ];

From 9ba32c76f4b7b229c5cbd8c5c7b71626c5e58bb4 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Thu, 11 Jan 2018 14:51:53 -0600
Subject: [PATCH 019/173] libnfnetlink: patch to fix build w/non-glibc (namely,
 musl)

---
 .../Use-stdlib-uint-instead-of-u_int.patch    | 499 ++++++++++++++++++
 .../libraries/libnfnetlink/default.nix        |   4 +
 2 files changed, 503 insertions(+)
 create mode 100644 pkgs/development/libraries/libnfnetlink/Use-stdlib-uint-instead-of-u_int.patch

diff --git a/pkgs/development/libraries/libnfnetlink/Use-stdlib-uint-instead-of-u_int.patch b/pkgs/development/libraries/libnfnetlink/Use-stdlib-uint-instead-of-u_int.patch
new file mode 100644
index 00000000000..074bef42b97
--- /dev/null
+++ b/pkgs/development/libraries/libnfnetlink/Use-stdlib-uint-instead-of-u_int.patch
@@ -0,0 +1,499 @@
+From patchwork Fri Apr  3 22:04:46 2015
+Content-Type: text/plain; charset="utf-8"
+MIME-Version: 1.0
+Content-Transfer-Encoding: 7bit
+Subject: Use stdlib uint* instead of u_int*
+From: Nathan McSween <nwmcsween@gmail.com>
+X-Patchwork-Id: 458131
+X-Patchwork-Delegate: pablo@netfilter.org
+Message-Id: <1428098686-17843-1-git-send-email-nwmcsween@gmail.com>
+To: netfilter-devel@vger.kernel.org
+Cc: Nathan McSween <nwmcsween@gmail.com>
+Date: Fri,  3 Apr 2015 22:04:46 +0000
+
+Signed-off-by: Nathan McSween <nwmcsween@gmail.com>
+---
+ include/libnfnetlink/libnfnetlink.h           | 25 +++++-----
+ include/libnfnetlink/linux_nfnetlink.h        | 11 +++--
+ include/libnfnetlink/linux_nfnetlink_compat.h |  6 ++-
+ src/iftable.c                                 |  9 ++--
+ src/iftable.h                                 |  6 ++-
+ src/libnfnetlink.c                            | 71 ++++++++++++++-------------
+ src/rtnl.c                                    |  5 +-
+ src/rtnl.h                                    |  3 +-
+ 8 files changed, 73 insertions(+), 63 deletions(-)
+
+diff --git a/include/libnfnetlink/libnfnetlink.h b/include/libnfnetlink/libnfnetlink.h
+index 1d8c49d..cd0be3d 100644
+--- a/include/libnfnetlink/libnfnetlink.h
++++ b/include/libnfnetlink/libnfnetlink.h
+@@ -15,6 +15,7 @@
+ #define aligned_u64 unsigned long long __attribute__((aligned(8)))
+ #endif
+ 
++#include <stdint.h>
+ #include <sys/socket.h>	/* for sa_family_t */
+ #include <linux/netlink.h>
+ #include <libnfnetlink/linux_nfnetlink.h>
+@@ -55,7 +56,7 @@ struct nfnlhdr {
+ struct nfnl_callback {
+ 	int (*call)(struct nlmsghdr *nlh, struct nfattr *nfa[], void *data);
+ 	void *data;
+-	u_int16_t attr_count;
++	uint16_t attr_count;
+ };
+ 
+ struct nfnl_handle;
+@@ -69,7 +70,7 @@ extern struct nfnl_handle *nfnl_open(void);
+ extern int nfnl_close(struct nfnl_handle *);
+ 
+ extern struct nfnl_subsys_handle *nfnl_subsys_open(struct nfnl_handle *, 
+-						   u_int8_t, u_int8_t, 
++						   uint8_t, uint8_t,
+ 						   unsigned int);
+ extern void nfnl_subsys_close(struct nfnl_subsys_handle *);
+ 
+@@ -88,8 +89,8 @@ extern int nfnl_sendiov(const struct nfnl_handle *nfnlh,
+ 			const struct iovec *iov, unsigned int num,
+ 			unsigned int flags);
+ extern void nfnl_fill_hdr(struct nfnl_subsys_handle *, struct nlmsghdr *,
+-			  unsigned int, u_int8_t, u_int16_t, u_int16_t,
+-			  u_int16_t);
++			  unsigned int, uint8_t, uint16_t, uint16_t,
++			  uint16_t);
+ extern __attribute__((deprecated)) int
+ nfnl_talk(struct nfnl_handle *, struct nlmsghdr *, pid_t,
+           unsigned, struct nlmsghdr *,
+@@ -103,8 +104,8 @@ nfnl_listen(struct nfnl_handle *,
+ /* receiving */
+ extern ssize_t nfnl_recv(const struct nfnl_handle *h, unsigned char *buf, size_t len);
+ extern int nfnl_callback_register(struct nfnl_subsys_handle *,
+-				  u_int8_t type, struct nfnl_callback *cb);
+-extern int nfnl_callback_unregister(struct nfnl_subsys_handle *, u_int8_t type);
++				  uint8_t type, struct nfnl_callback *cb);
++extern int nfnl_callback_unregister(struct nfnl_subsys_handle *, uint8_t type);
+ extern int nfnl_handle_packet(struct nfnl_handle *, char *buf, int len);
+ 
+ /* parsing */
+@@ -180,12 +181,12 @@ extern int nfnl_query(struct nfnl_handle *h, struct nlmsghdr *nlh);
+ 
+ /* nfnl attribute handling functions */
+ extern int nfnl_addattr_l(struct nlmsghdr *, int, int, const void *, int);
+-extern int nfnl_addattr8(struct nlmsghdr *, int, int, u_int8_t);
+-extern int nfnl_addattr16(struct nlmsghdr *, int, int, u_int16_t);
+-extern int nfnl_addattr32(struct nlmsghdr *, int, int, u_int32_t);
++extern int nfnl_addattr8(struct nlmsghdr *, int, int, uint8_t);
++extern int nfnl_addattr16(struct nlmsghdr *, int, int, uint16_t);
++extern int nfnl_addattr32(struct nlmsghdr *, int, int, uint32_t);
+ extern int nfnl_nfa_addattr_l(struct nfattr *, int, int, const void *, int);
+-extern int nfnl_nfa_addattr16(struct nfattr *, int, int, u_int16_t);
+-extern int nfnl_nfa_addattr32(struct nfattr *, int, int, u_int32_t);
++extern int nfnl_nfa_addattr16(struct nfattr *, int, int, uint16_t);
++extern int nfnl_nfa_addattr32(struct nfattr *, int, int, uint32_t);
+ extern int nfnl_parse_attr(struct nfattr **, int, struct nfattr *, int);
+ #define nfnl_parse_nested(tb, max, nfa) \
+ 	nfnl_parse_attr((tb), (max), NFA_DATA((nfa)), NFA_PAYLOAD((nfa)))
+@@ -197,7 +198,7 @@ extern int nfnl_parse_attr(struct nfattr **, int, struct nfattr *, int);
+ ({	(tail)->nfa_len = (void *) NLMSG_TAIL(nlh) - (void *) tail; })
+ 
+ extern void nfnl_build_nfa_iovec(struct iovec *iov, struct nfattr *nfa, 
+-				 u_int16_t type, u_int32_t len,
++				 uint16_t type, uint32_t len,
+ 				 unsigned char *val);
+ extern unsigned int nfnl_rcvbufsiz(const struct nfnl_handle *h, 
+ 				   unsigned int size);
+diff --git a/include/libnfnetlink/linux_nfnetlink.h b/include/libnfnetlink/linux_nfnetlink.h
+index 76a8550..7b843c6 100644
+--- a/include/libnfnetlink/linux_nfnetlink.h
++++ b/include/libnfnetlink/linux_nfnetlink.h
+@@ -1,5 +1,6 @@
+ #ifndef _NFNETLINK_H
+ #define _NFNETLINK_H
++#include <stdint.h>
+ #include <linux/types.h>
+ #include <libnfnetlink/linux_nfnetlink_compat.h>
+ 
+@@ -25,9 +26,9 @@ enum nfnetlink_groups {
+ /* General form of address family dependent message.
+  */
+ struct nfgenmsg {
+-	u_int8_t  nfgen_family;		/* AF_xxx */
+-	u_int8_t  version;		/* nfnetlink version */
+-	u_int16_t    res_id;		/* resource id */
++	uint8_t  nfgen_family;		/* AF_xxx */
++	uint8_t  version;		/* nfnetlink version */
++	uint16_t    res_id;		/* resource id */
+ };
+ 
+ #define NFNETLINK_V0	0
+@@ -59,7 +60,7 @@ struct nfnl_callback
+ 	int (*call)(struct sock *nl, struct sk_buff *skb, 
+ 		struct nlmsghdr *nlh, struct nlattr *cda[]);
+ 	const struct nla_policy *policy;	/* netlink attribute policy */
+-	const u_int16_t attr_count;		/* number of nlattr's */
++	const uint16_t attr_count;		/* number of nlattr's */
+ };
+ 
+ struct nfnetlink_subsystem
+@@ -76,7 +77,7 @@ extern int nfnetlink_subsys_unregister(const struct nfnetlink_subsystem *n);
+ extern int nfnetlink_has_listeners(unsigned int group);
+ extern int nfnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, 
+ 			  int echo);
+-extern int nfnetlink_unicast(struct sk_buff *skb, u_int32_t pid, int flags);
++extern int nfnetlink_unicast(struct sk_buff *skb, uint32_t pid, int flags);
+ 
+ #define MODULE_ALIAS_NFNL_SUBSYS(subsys) \
+ 	MODULE_ALIAS("nfnetlink-subsys-" __stringify(subsys))
+diff --git a/include/libnfnetlink/linux_nfnetlink_compat.h b/include/libnfnetlink/linux_nfnetlink_compat.h
+index e145176..cd094fc 100644
+--- a/include/libnfnetlink/linux_nfnetlink_compat.h
++++ b/include/libnfnetlink/linux_nfnetlink_compat.h
+@@ -3,6 +3,8 @@
+ #ifndef __KERNEL__
+ /* Old nfnetlink macros for userspace */
+ 
++#include <stdint.h>
++
+ /* nfnetlink groups: Up to 32 maximum */
+ #define NF_NETLINK_CONNTRACK_NEW 		0x00000001
+ #define NF_NETLINK_CONNTRACK_UPDATE		0x00000002
+@@ -20,8 +22,8 @@
+ 
+ struct nfattr
+ {
+-	u_int16_t nfa_len;
+-	u_int16_t nfa_type;	/* we use 15 bits for the type, and the highest
++	uint16_t nfa_len;
++	uint16_t nfa_type;	/* we use 15 bits for the type, and the highest
+ 				 * bit to indicate whether the payload is nested */
+ };
+ 
+diff --git a/src/iftable.c b/src/iftable.c
+index 5976ed8..3411c4c 100644
+--- a/src/iftable.c
++++ b/src/iftable.c
+@@ -9,6 +9,7 @@
+ /* IFINDEX handling */
+ 
+ #include <unistd.h>
++#include <stdint.h>
+ #include <stdlib.h>
+ #include <stdio.h>
+ #include <string.h>
+@@ -27,10 +28,10 @@
+ struct ifindex_node {
+ 	struct list_head head;
+ 
+-	u_int32_t	index;
+-	u_int32_t	type;
+-	u_int32_t	alen;
+-	u_int32_t	flags;
++	uint32_t	index;
++	uint32_t	type;
++	uint32_t	alen;
++	uint32_t	flags;
+ 	char		addr[8];
+ 	char		name[16];
+ };
+diff --git a/src/iftable.h b/src/iftable.h
+index 8df7f24..0cc5335 100644
+--- a/src/iftable.h
++++ b/src/iftable.h
+@@ -1,8 +1,10 @@
+ #ifndef _IFTABLE_H
+ #define _IFTABLE_H
+ 
+-int iftable_delete(u_int32_t dst, u_int32_t mask, u_int32_t gw, u_int32_t oif);
+-int iftable_insert(u_int32_t dst, u_int32_t mask, u_int32_t gw, u_int32_t oif);
++#include <stdint.h>
++
++int iftable_delete(uint32_t dst, uint32_t mask, uint32_t gw, uint32_t oif);
++int iftable_insert(uint32_t dst, uint32_t mask, uint32_t gw, uint32_t oif);
+ 
+ int iftable_init(void);
+ void iftable_fini(void);
+diff --git a/src/libnfnetlink.c b/src/libnfnetlink.c
+index 398b7d7..b8958dc 100644
+--- a/src/libnfnetlink.c
++++ b/src/libnfnetlink.c
+@@ -36,6 +36,7 @@
+  * 	minor cleanups
+  */
+ 
++#include <stdint.h>
+ #include <stdlib.h>
+ #include <stdio.h>
+ #include <unistd.h>
+@@ -72,9 +73,9 @@
+ 
+ struct nfnl_subsys_handle {
+ 	struct nfnl_handle 	*nfnlh;
+-	u_int32_t		subscriptions;
+-	u_int8_t		subsys_id;
+-	u_int8_t		cb_count;
++	uint32_t		subscriptions;
++	uint8_t		subsys_id;
++	uint8_t		cb_count;
+ 	struct nfnl_callback 	*cb;	/* array of callbacks */
+ };
+ 
+@@ -86,11 +87,11 @@ struct nfnl_handle {
+ 	int			fd;
+ 	struct sockaddr_nl	local;
+ 	struct sockaddr_nl	peer;
+-	u_int32_t		subscriptions;
+-	u_int32_t		seq;
+-	u_int32_t		dump;
+-	u_int32_t		rcv_buffer_size;	/* for nfnl_catch */
+-	u_int32_t		flags;
++	uint32_t		subscriptions;
++	uint32_t		seq;
++	uint32_t		dump;
++	uint32_t		rcv_buffer_size;	/* for nfnl_catch */
++	uint32_t		flags;
+ 	struct nlmsghdr 	*last_nlhdr;
+ 	struct nfnl_subsys_handle subsys[NFNL_MAX_SUBSYS+1];
+ };
+@@ -145,7 +146,7 @@ unsigned int nfnl_portid(const struct nfnl_handle *h)
+ static int recalc_rebind_subscriptions(struct nfnl_handle *nfnlh)
+ {
+ 	int i, err;
+-	u_int32_t new_subscriptions = nfnlh->subscriptions;
++	uint32_t new_subscriptions = nfnlh->subscriptions;
+ 
+ 	for (i = 0; i < NFNL_MAX_SUBSYS; i++)
+ 		new_subscriptions |= nfnlh->subsys[i].subscriptions;
+@@ -273,8 +274,8 @@ void nfnl_set_rcv_buffer_size(struct nfnl_handle *h, unsigned int size)
+  * a valid address that points to a nfnl_subsys_handle structure is returned.
+  */
+ struct nfnl_subsys_handle *
+-nfnl_subsys_open(struct nfnl_handle *nfnlh, u_int8_t subsys_id,
+-		 u_int8_t cb_count, u_int32_t subscriptions)
++nfnl_subsys_open(struct nfnl_handle *nfnlh, uint8_t subsys_id,
++		 uint8_t cb_count, uint32_t subscriptions)
+ {
+ 	struct nfnl_subsys_handle *ssh;
+ 
+@@ -435,10 +436,10 @@ int nfnl_sendiov(const struct nfnl_handle *nfnlh, const struct iovec *iov,
+  */
+ void nfnl_fill_hdr(struct nfnl_subsys_handle *ssh,
+ 		    struct nlmsghdr *nlh, unsigned int len, 
+-		    u_int8_t family,
+-		    u_int16_t res_id,
+-		    u_int16_t msg_type,
+-		    u_int16_t msg_flags)
++		    uint8_t family,
++		    uint16_t res_id,
++		    uint16_t msg_type,
++		    uint16_t msg_flags)
+ {
+ 	assert(ssh);
+ 	assert(nlh);
+@@ -849,14 +850,14 @@ int nfnl_nfa_addattr_l(struct nfattr *nfa, int maxlen, int type,
+ }
+ 
+ /**
+- * nfnl_addattr8 - Add u_int8_t attribute to nlmsghdr
++ * nfnl_addattr8 - Add uint8_t attribute to nlmsghdr
+  *
+  * @n: netlink message header to which attribute is to be added
+  * @maxlen: maximum length of netlink message header
+  * @type: type of new attribute
+  * @data: content of new attribute
+  */
+-int nfnl_addattr8(struct nlmsghdr *n, int maxlen, int type, u_int8_t data)
++int nfnl_addattr8(struct nlmsghdr *n, int maxlen, int type, uint8_t data)
+ {
+ 	assert(n);
+ 	assert(maxlen > 0);
+@@ -866,7 +867,7 @@ int nfnl_addattr8(struct nlmsghdr *n, int maxlen, int type, u_int8_t data)
+ }
+ 
+ /**
+- * nfnl_nfa_addattr16 - Add u_int16_t attribute to struct nfattr 
++ * nfnl_nfa_addattr16 - Add uint16_t attribute to struct nfattr
+  *
+  * @nfa: struct nfattr
+  * @maxlen: maximal length of nfattr buffer
+@@ -875,7 +876,7 @@ int nfnl_addattr8(struct nlmsghdr *n, int maxlen, int type, u_int8_t data)
+  *
+  */
+ int nfnl_nfa_addattr16(struct nfattr *nfa, int maxlen, int type, 
+-		       u_int16_t data)
++		       uint16_t data)
+ {
+ 	assert(nfa);
+ 	assert(maxlen > 0);
+@@ -885,7 +886,7 @@ int nfnl_nfa_addattr16(struct nfattr *nfa, int maxlen, int type,
+ }
+ 
+ /**
+- * nfnl_addattr16 - Add u_int16_t attribute to nlmsghdr
++ * nfnl_addattr16 - Add uint16_t attribute to nlmsghdr
+  *
+  * @n: netlink message header to which attribute is to be added
+  * @maxlen: maximum length of netlink message header
+@@ -894,7 +895,7 @@ int nfnl_nfa_addattr16(struct nfattr *nfa, int maxlen, int type,
+  *
+  */
+ int nfnl_addattr16(struct nlmsghdr *n, int maxlen, int type,
+-		   u_int16_t data)
++		   uint16_t data)
+ {
+ 	assert(n);
+ 	assert(maxlen > 0);
+@@ -904,7 +905,7 @@ int nfnl_addattr16(struct nlmsghdr *n, int maxlen, int type,
+ }
+ 
+ /**
+- * nfnl_nfa_addattr32 - Add u_int32_t attribute to struct nfattr 
++ * nfnl_nfa_addattr32 - Add uint32_t attribute to struct nfattr
+  *
+  * @nfa: struct nfattr
+  * @maxlen: maximal length of nfattr buffer
+@@ -913,7 +914,7 @@ int nfnl_addattr16(struct nlmsghdr *n, int maxlen, int type,
+  *
+  */
+ int nfnl_nfa_addattr32(struct nfattr *nfa, int maxlen, int type, 
+-		       u_int32_t data)
++		       uint32_t data)
+ {
+ 	assert(nfa);
+ 	assert(maxlen > 0);
+@@ -923,7 +924,7 @@ int nfnl_nfa_addattr32(struct nfattr *nfa, int maxlen, int type,
+ }
+ 
+ /**
+- * nfnl_addattr32 - Add u_int32_t attribute to nlmsghdr
++ * nfnl_addattr32 - Add uint32_t attribute to nlmsghdr
+  *
+  * @n: netlink message header to which attribute is to be added
+  * @maxlen: maximum length of netlink message header
+@@ -932,7 +933,7 @@ int nfnl_nfa_addattr32(struct nfattr *nfa, int maxlen, int type,
+  *
+  */
+ int nfnl_addattr32(struct nlmsghdr *n, int maxlen, int type,
+-		   u_int32_t data)
++		   uint32_t data)
+ {
+ 	assert(n);
+ 	assert(maxlen > 0);
+@@ -980,7 +981,7 @@ int nfnl_parse_attr(struct nfattr *tb[], int max, struct nfattr *nfa, int len)
+  *
+  */ 
+ void nfnl_build_nfa_iovec(struct iovec *iov, struct nfattr *nfa, 
+-			  u_int16_t type, u_int32_t len, unsigned char *val)
++			  uint16_t type, uint32_t len, unsigned char *val)
+ {
+ 	assert(iov);
+ 	assert(nfa);
+@@ -1115,7 +1116,7 @@ struct nlmsghdr *nfnl_get_msg_next(struct nfnl_handle *h,
+  * appropiately.
+  */
+ int nfnl_callback_register(struct nfnl_subsys_handle *ssh,
+-			   u_int8_t type, struct nfnl_callback *cb)
++			   uint8_t type, struct nfnl_callback *cb)
+ {
+ 	assert(ssh);
+ 	assert(cb);
+@@ -1138,7 +1139,7 @@ int nfnl_callback_register(struct nfnl_subsys_handle *ssh,
+  * On sucess, 0 is returned. On error, -1 is returned and errno is
+  * set appropiately.
+  */
+-int nfnl_callback_unregister(struct nfnl_subsys_handle *ssh, u_int8_t type)
++int nfnl_callback_unregister(struct nfnl_subsys_handle *ssh, uint8_t type)
+ {
+ 	assert(ssh);
+ 
+@@ -1161,8 +1162,8 @@ int nfnl_check_attributes(const struct nfnl_handle *h,
+ 	assert(nfa);
+ 
+ 	int min_len;
+-	u_int8_t type = NFNL_MSG_TYPE(nlh->nlmsg_type);
+-	u_int8_t subsys_id = NFNL_SUBSYS_ID(nlh->nlmsg_type);
++	uint8_t type = NFNL_MSG_TYPE(nlh->nlmsg_type);
++	uint8_t subsys_id = NFNL_SUBSYS_ID(nlh->nlmsg_type);
+ 	const struct nfnl_subsys_handle *ssh;
+ 	struct nfnl_callback *cb;
+ 
+@@ -1212,8 +1213,8 @@ static int __nfnl_handle_msg(struct nfnl_handle *h, struct nlmsghdr *nlh,
+ 			     int len)
+ {
+ 	struct nfnl_subsys_handle *ssh;
+-	u_int8_t type = NFNL_MSG_TYPE(nlh->nlmsg_type);
+-	u_int8_t subsys_id = NFNL_SUBSYS_ID(nlh->nlmsg_type);
++	uint8_t type = NFNL_MSG_TYPE(nlh->nlmsg_type);
++	uint8_t subsys_id = NFNL_SUBSYS_ID(nlh->nlmsg_type);
+ 	int err = 0;
+ 
+ 	if (subsys_id > NFNL_MAX_SUBSYS)
+@@ -1243,7 +1244,7 @@ int nfnl_handle_packet(struct nfnl_handle *h, char *buf, int len)
+ {
+ 
+ 	while (len >= NLMSG_SPACE(0)) {
+-		u_int32_t rlen;
++		uint32_t rlen;
+ 		struct nlmsghdr *nlh = (struct nlmsghdr *)buf;
+ 
+ 		if (nlh->nlmsg_len < sizeof(struct nlmsghdr)
+@@ -1285,8 +1286,8 @@ static int nfnl_is_error(struct nfnl_handle *h, struct nlmsghdr *nlh)
+ static int nfnl_step(struct nfnl_handle *h, struct nlmsghdr *nlh)
+ {
+ 	struct nfnl_subsys_handle *ssh;
+-	u_int8_t type = NFNL_MSG_TYPE(nlh->nlmsg_type);
+-	u_int8_t subsys_id = NFNL_SUBSYS_ID(nlh->nlmsg_type);
++	uint8_t type = NFNL_MSG_TYPE(nlh->nlmsg_type);
++	uint8_t subsys_id = NFNL_SUBSYS_ID(nlh->nlmsg_type);
+ 
+ 	/* Is this an error message? */
+ 	if (nfnl_is_error(h, nlh)) {
+diff --git a/src/rtnl.c b/src/rtnl.c
+index 7b4ac7d..34802fe 100644
+--- a/src/rtnl.c
++++ b/src/rtnl.c
+@@ -11,6 +11,7 @@
+ /* rtnetlink - routing table netlink interface */
+ 
+ #include <unistd.h>
++#include <stdint.h>
+ #include <stdlib.h>
+ #include <string.h>
+ #include <errno.h>
+@@ -30,7 +31,7 @@
+ #define rtnl_log(x, ...)
+ 
+ static inline struct rtnl_handler *
+-find_handler(struct rtnl_handle *rtnl_handle, u_int16_t type)
++find_handler(struct rtnl_handle *rtnl_handle, uint16_t type)
+ {
+ 	struct rtnl_handler *h;
+ 	for (h = rtnl_handle->handlers; h; h = h->next) {
+@@ -41,7 +42,7 @@ find_handler(struct rtnl_handle *rtnl_handle, u_int16_t type)
+ }
+ 
+ static int call_handler(struct rtnl_handle *rtnl_handle,
+-			u_int16_t type,
++			uint16_t type,
+ 			struct nlmsghdr *hdr)
+ {
+ 	struct rtnl_handler *h = find_handler(rtnl_handle, type);
+diff --git a/src/rtnl.h b/src/rtnl.h
+index 0c403dc..9858ae5 100644
+--- a/src/rtnl.h
++++ b/src/rtnl.h
+@@ -1,13 +1,14 @@
+ #ifndef _RTNL_H
+ #define _RTNL_H
+ 
++#include <stdint.h>
+ #include <linux/types.h>
+ #include <linux/rtnetlink.h>
+ 
+ struct rtnl_handler {
+ 	struct rtnl_handler *next;
+ 
+-	u_int16_t	nlmsg_type;
++	uint16_t	nlmsg_type;
+ 	int		(*handlefn)(struct nlmsghdr *h, void *arg);
+ 	void		*arg;
+ };
diff --git a/pkgs/development/libraries/libnfnetlink/default.nix b/pkgs/development/libraries/libnfnetlink/default.nix
index be60612a4ff..5395b5b2e99 100644
--- a/pkgs/development/libraries/libnfnetlink/default.nix
+++ b/pkgs/development/libraries/libnfnetlink/default.nix
@@ -8,6 +8,10 @@ stdenv.mkDerivation rec {
     sha256 = "06mm2x4b01k3m7wnrxblk9j0mybyr4pfz28ml7944xhjx6fy2w7j";
   };
 
+  patches = [
+    ./Use-stdlib-uint-instead-of-u_int.patch
+  ];
+
   meta = {
     description = "Low-level library for netfilter related kernel/userspace communication";
     longDescription = ''

From afe517acd5bf023c1015b8c3467f2c3f95d78720 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 12 Jan 2018 20:52:54 -0600
Subject: [PATCH 020/173] nix: 1.11 needs 'curl' on PATH so provide as
 nativeBuildInput.

---
 pkgs/tools/package-management/nix/default.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/tools/package-management/nix/default.nix b/pkgs/tools/package-management/nix/default.nix
index 3f77e47868b..fc731c54f45 100644
--- a/pkgs/tools/package-management/nix/default.nix
+++ b/pkgs/tools/package-management/nix/default.nix
@@ -24,7 +24,7 @@ let
 
     nativeBuildInputs =
       [ pkgconfig ]
-      ++ lib.optionals (!is20) [ perl ]
+      ++ lib.optionals (!is20) [ curl perl ]
       ++ lib.optionals fromGit [ autoreconfHook autoconf-archive bison flex libxml2 libxslt docbook5 docbook5_xsl ];
 
     buildInputs = [ curl openssl sqlite xz ]

From 3aa61e33bedc13f1db6c7b62db76ce2b412340cd Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 12 Jan 2018 21:14:44 -0600
Subject: [PATCH 021/173] nix: simplify bzip2 dependency, this is handled
 properly now

---
 pkgs/tools/package-management/nix/default.nix | 16 +---------------
 1 file changed, 1 insertion(+), 15 deletions(-)

diff --git a/pkgs/tools/package-management/nix/default.nix b/pkgs/tools/package-management/nix/default.nix
index fc731c54f45..91844bdb9dd 100644
--- a/pkgs/tools/package-management/nix/default.nix
+++ b/pkgs/tools/package-management/nix/default.nix
@@ -27,7 +27,7 @@ let
       ++ lib.optionals (!is20) [ curl perl ]
       ++ lib.optionals fromGit [ autoreconfHook autoconf-archive bison flex libxml2 libxslt docbook5 docbook5_xsl ];
 
-    buildInputs = [ curl openssl sqlite xz ]
+    buildInputs = [ curl openssl sqlite xz bzip2 ]
       ++ lib.optional (stdenv.isLinux || stdenv.isDarwin) libsodium
       ++ lib.optionals fromGit [ brotli ] # Since 1.12
       ++ lib.optional stdenv.isLinux libseccomp
@@ -39,15 +39,6 @@ let
 
     propagatedBuildInputs = [ boehmgc ];
 
-    # Note: bzip2 is not passed as a build input, because the unpack phase
-    # would end up using the wrong bzip2 when cross-compiling.
-    # XXX: The right thing would be to reinstate `--with-bzip2' in Nix.
-    postUnpack =
-      '' export CPATH="${bzip2.dev}/include"
-         export LIBRARY_PATH="${bzip2.out}/lib"
-         export CXXFLAGS="-Wno-error=reserved-user-defined-literal"
-      '';
-
     configureFlags =
       [ "--with-store-dir=${storeDir}"
         "--localstatedir=${stateDir}"
@@ -75,11 +66,6 @@ let
     separateDebugInfo = stdenv.isLinux;
 
     crossAttrs = {
-      postUnpack =
-        '' export CPATH="${bzip2.crossDrv}/include"
-           export NIX_CROSS_LDFLAGS="-L${bzip2.crossDrv}/lib -rpath-link ${bzip2.crossDrv}/lib $NIX_CROSS_LDFLAGS"
-        '';
-
       configureFlags =
         ''
           --with-store-dir=${storeDir} --localstatedir=${stateDir}

From cec99d22f0176e8ea2a85ef20a87b17c589f1f9a Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Wed, 8 Nov 2017 10:46:43 -0600
Subject: [PATCH 022/173] npth: run tests, even if brief

---
 pkgs/development/libraries/npth/default.nix | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/pkgs/development/libraries/npth/default.nix b/pkgs/development/libraries/npth/default.nix
index dc4f4926e9d..a600938cdbb 100644
--- a/pkgs/development/libraries/npth/default.nix
+++ b/pkgs/development/libraries/npth/default.nix
@@ -8,6 +8,8 @@ stdenv.mkDerivation rec {
     sha256 = "1hmkkp6vzyrh8v01c2ynzf9vwikyagp7p1lxhbnr4ysk3w66jji9";
   };
 
+  doCheck = true;
+
   meta = with stdenv.lib; {
     description = "The New GNU Portable Threads Library";
     longDescription = ''

From 0a70dc6fd18942e423dca704fb1d215ba6aa0862 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Wed, 8 Nov 2017 04:36:43 -0600
Subject: [PATCH 023/173] kakuone: add pkgconfig to appease warning

---
 pkgs/applications/editors/kakoune/default.nix | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/pkgs/applications/editors/kakoune/default.nix b/pkgs/applications/editors/kakoune/default.nix
index 067aff5ee69..dae30c5ac0c 100644
--- a/pkgs/applications/editors/kakoune/default.nix
+++ b/pkgs/applications/editors/kakoune/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchFromGitHub, ncurses, boost, asciidoc, docbook_xsl, libxslt }:
+{ stdenv, fetchFromGitHub, ncurses, boost, asciidoc, docbook_xsl, libxslt, pkgconfig }:
 
 with stdenv.lib;
 
@@ -11,6 +11,7 @@ stdenv.mkDerivation rec {
     rev = "7482d117cc85523e840dff595134dcb9cdc62207";
     sha256 = "08j611y192n9vln9i94ldlvz3k0sg79dkmfc0b1vczrmaxhpgpfh";
   };
+  nativeBuildInputs = [ pkgconfig ];
   buildInputs = [ ncurses boost asciidoc docbook_xsl libxslt ];
 
   postPatch = ''

From d6b5ee8b5ab8429bdc711f1de0b422d36f35ce90 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Thu, 11 Jan 2018 11:50:07 -0600
Subject: [PATCH 024/173] pysqlite: quickfix for cross

---
 pkgs/top-level/python-packages.nix | 1 +
 1 file changed, 1 insertion(+)

diff --git a/pkgs/top-level/python-packages.nix b/pkgs/top-level/python-packages.nix
index a35dc124dd2..6ef5b2b4968 100644
--- a/pkgs/top-level/python-packages.nix
+++ b/pkgs/top-level/python-packages.nix
@@ -14657,6 +14657,7 @@ in {
       substituteInPlace "setup.cfg"                                     \
               --replace "/usr/local/include" "${pkgs.sqlite.dev}/include"   \
               --replace "/usr/local/lib" "${pkgs.sqlite.out}/lib"
+      export LDSHARED="$CC -pthread -shared"
     '';
 
     meta = {

From 81eef05f86fef3dc80149fd1b54b0b8a77fa4411 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Thu, 11 Jan 2018 22:27:55 -0600
Subject: [PATCH 025/173] postgresql: don't try to use 'locale' if not building
 to run on glibc

---
 pkgs/servers/sql/postgresql/default.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/servers/sql/postgresql/default.nix b/pkgs/servers/sql/postgresql/default.nix
index 1a721e90a8d..02a620cee53 100644
--- a/pkgs/servers/sql/postgresql/default.nix
+++ b/pkgs/servers/sql/postgresql/default.nix
@@ -69,7 +69,7 @@ let
         fi
       '';
 
-    postFixup = lib.optionalString (!stdenv.isDarwin)
+    postFixup = lib.optionalString (!stdenv.isDarwin && stdenv.hostPlatform.libc == "glibc")
       ''
         # initdb needs access to "locale" command from glibc.
         wrapProgram $out/bin/initdb --prefix PATH ":" ${glibc.bin}/bin

From 9138d055bfd0064c3cd7fb7458e104d5fde9ae4a Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 17 Oct 2017 23:39:19 -0500
Subject: [PATCH 026/173] boolector: fix missing stdint.h include

---
 pkgs/applications/science/logic/boolector/default.nix | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/pkgs/applications/science/logic/boolector/default.nix b/pkgs/applications/science/logic/boolector/default.nix
index 2b40995b743..9e40d0ce5f1 100644
--- a/pkgs/applications/science/logic/boolector/default.nix
+++ b/pkgs/applications/science/logic/boolector/default.nix
@@ -8,6 +8,10 @@ stdenv.mkDerivation rec {
     sha256 = "0mdf7hwix237pvknvrpazcx6s3ininj5k7vhysqjqgxa7lxgq045";
   };
 
+  prePatch = ''
+    sed -i -e 's@mv lingeling\* lingeling@\0 \&\& sed -i -e "1i#include <stdint.h>" lingeling/lglib.h@' makefile
+  '';
+
   installPhase = ''
     mkdir $out
     mv boolector/bin $out

From 46cbded0def3ad36b567bf2b81771196a9173dbc Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Mon, 5 Feb 2018 13:28:14 -0600
Subject: [PATCH 027/173] boolector: patch usage of ar and ranlib for cross

eep.
---
 .../science/logic/boolector/default.nix       | 20 ++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/pkgs/applications/science/logic/boolector/default.nix b/pkgs/applications/science/logic/boolector/default.nix
index 9e40d0ce5f1..aa815e48db4 100644
--- a/pkgs/applications/science/logic/boolector/default.nix
+++ b/pkgs/applications/science/logic/boolector/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl }:
+{ stdenv, fetchurl, writeShellScriptBin }:
 
 stdenv.mkDerivation rec {
   name    = "boolector-${version}";
@@ -8,8 +8,22 @@ stdenv.mkDerivation rec {
     sha256 = "0mdf7hwix237pvknvrpazcx6s3ininj5k7vhysqjqgxa7lxgq045";
   };
 
-  prePatch = ''
-    sed -i -e 's@mv lingeling\* lingeling@\0 \&\& sed -i -e "1i#include <stdint.h>" lingeling/lglib.h@' makefile
+  prePatch =
+    let
+      lingelingPatch = writeShellScriptBin "lingeling-patch" ''
+        sed -i -e "1i#include <stdint.h>" lingeling/lglib.h
+
+        ${crossFix}/bin/crossFix lingeling
+      '';
+      crossFix = writeShellScriptBin "crossFix" ''
+        # substituteInPlace not available here
+        sed -i $1/makefile.in \
+          -e 's@ar rc@$(AR) rc@' \
+          -e 's@ranlib@$(RANLIB)@'
+      '';
+    in ''
+    sed -i -e 's@mv lingeling\* lingeling@\0 \&\& ${lingelingPatch}/bin/lingeling-patch@' makefile
+    sed -i -e 's@mv boolector\* boolector@\0 \&\& ${crossFix}/bin/crossFix boolector@' makefile
   '';
 
   installPhase = ''

From 36b27069167a9f64cae4162145bf7f1462a53a7b Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 17 Oct 2017 20:41:37 -0500
Subject: [PATCH 028/173] picosat: fixup include for unistd.h

---
 pkgs/applications/science/logic/picosat/default.nix | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/pkgs/applications/science/logic/picosat/default.nix b/pkgs/applications/science/logic/picosat/default.nix
index e026cfad218..cd672ed00e7 100644
--- a/pkgs/applications/science/logic/picosat/default.nix
+++ b/pkgs/applications/science/logic/picosat/default.nix
@@ -9,6 +9,10 @@ stdenv.mkDerivation rec {
     sha256 = "0m578rpa5rdn08d10kr4lbsdwp4402hpavrz6n7n53xs517rn5hm";
   };
 
+  prePatch = ''
+    substituteInPlace picosat.c --replace "sys/unistd.h" "unistd.h"
+  '';
+
   configurePhase = "./configure.sh --shared --trace";
 
   installPhase = ''

From 4c767417ea6fe47a266359216e28f110f4f93db8 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Mon, 5 Feb 2018 13:22:14 -0600
Subject: [PATCH 029/173] picosat: fix ar and ranlib usage for cross

---
 pkgs/applications/science/logic/picosat/default.nix | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/pkgs/applications/science/logic/picosat/default.nix b/pkgs/applications/science/logic/picosat/default.nix
index cd672ed00e7..db252f97916 100644
--- a/pkgs/applications/science/logic/picosat/default.nix
+++ b/pkgs/applications/science/logic/picosat/default.nix
@@ -11,6 +11,10 @@ stdenv.mkDerivation rec {
 
   prePatch = ''
     substituteInPlace picosat.c --replace "sys/unistd.h" "unistd.h"
+
+    substituteInPlace makefile.in \
+      --replace 'ar rc' '$(AR) rc' \
+      --replace 'ranlib' '$(RANLIB)'
   '';
 
   configurePhase = "./configure.sh --shared --trace";

From b4fa6da92bd7e2ca2290db0d442f20cc0c78ce91 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 22 Sep 2017 14:32:33 -0500
Subject: [PATCH 030/173] siege: fixup u_int32_t here as well

---
 pkgs/tools/networking/siege/default.nix | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/pkgs/tools/networking/siege/default.nix b/pkgs/tools/networking/siege/default.nix
index 93fc28b48bf..cac3e3e6361 100644
--- a/pkgs/tools/networking/siege/default.nix
+++ b/pkgs/tools/networking/siege/default.nix
@@ -12,6 +12,10 @@ stdenv.mkDerivation rec {
 
   buildInputs = [ openssl zlib ];
 
+  prePatch = ''
+    sed -i -e 's/u_int32_t/uint32_t/g' -e '1i#include <stdint.h>' src/hash.c
+  '';
+
   configureFlags = [
     "--with-ssl=${openssl.dev}"
     "--with-zlib=${zlib.dev}"

From a2eb1f4bd68c42610451c5c217b4ab0355a7298c Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 22 Sep 2017 14:00:46 -0500
Subject: [PATCH 031/173] openntpd: patch missing includes to cdefs, fix build
 on musl

---
 pkgs/tools/networking/openntpd/default.nix | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/pkgs/tools/networking/openntpd/default.nix b/pkgs/tools/networking/openntpd/default.nix
index 7e1c257ec4b..d63df830d0e 100644
--- a/pkgs/tools/networking/openntpd/default.nix
+++ b/pkgs/tools/networking/openntpd/default.nix
@@ -12,6 +12,11 @@ stdenv.mkDerivation rec {
     sha256 = "1g6hi03ylhv47sbar3xxgsrar8schqfwn4glckh6m6lni67ndq85";
   };
 
+  prePatch = ''
+    sed -i '20i#include <sys/cdefs.h>' src/ntpd.h
+    sed -i '19i#include <sys/cdefs.h>' src/log.c
+  '';
+
   configureFlags = [
     "--with-privsep-path=${privsepPath}"
     "--with-privsep-user=${privsepUser}"

From 290c62e74956f2dc98ce2a1adac0b5d8e6b5ab80 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Thu, 4 Jan 2018 10:30:56 -0600
Subject: [PATCH 032/173] musl: multiple outputs

---
 pkgs/os-specific/linux/musl/default.nix | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/pkgs/os-specific/linux/musl/default.nix b/pkgs/os-specific/linux/musl/default.nix
index aaef5315b0f..9abfcd7adf6 100644
--- a/pkgs/os-specific/linux/musl/default.nix
+++ b/pkgs/os-specific/linux/musl/default.nix
@@ -23,8 +23,12 @@ stdenv.mkDerivation rec {
     "--enable-shared"
     "--enable-static"
     "CFLAGS=-fstack-protector-strong"
+    # Fix cycle between outputs
+    "--disable-wrapper"
   ];
 
+  outputs = [ "out" "dev" ];
+
   dontDisableStatic = true;
 
   meta = {

From d5916a84cf60398f6b10676a8c26b9c09d85f4d5 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 5 Jan 2018 12:36:58 -0600
Subject: [PATCH 033/173] bintools-wrapper: teach about musl dynamic linkers

---
 pkgs/build-support/bintools-wrapper/default.nix | 1 +
 1 file changed, 1 insertion(+)

diff --git a/pkgs/build-support/bintools-wrapper/default.nix b/pkgs/build-support/bintools-wrapper/default.nix
index bb0e6b82aa5..48fd8665cb4 100644
--- a/pkgs/build-support/bintools-wrapper/default.nix
+++ b/pkgs/build-support/bintools-wrapper/default.nix
@@ -51,6 +51,7 @@ let
   # shell glob that ought to match it.
   dynamicLinker =
     /**/ if libc == null then null
+    else if targetPlatform.libc == "musl"             then "${libc_lib}/lib/ld-musl-*"
     else if targetPlatform.system == "i686-linux"     then "${libc_lib}/lib/ld-linux.so.2"
     else if targetPlatform.system == "x86_64-linux"   then "${libc_lib}/lib/ld-linux-x86-64.so.2"
     # ARM with a wildcard, which can be "" or "-armhf".

From 1d538d51c3bc4bf0587c012d9e9ef58311edfe0d Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Thu, 4 Jan 2018 10:39:29 -0600
Subject: [PATCH 034/173] musl: linux headers, bsd compat headers, and ldd

---
 pkgs/os-specific/linux/musl/default.nix | 35 ++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)

diff --git a/pkgs/os-specific/linux/musl/default.nix b/pkgs/os-specific/linux/musl/default.nix
index 9abfcd7adf6..bfc7b717bf7 100644
--- a/pkgs/os-specific/linux/musl/default.nix
+++ b/pkgs/os-specific/linux/musl/default.nix
@@ -1,5 +1,19 @@
-{ stdenv, fetchurl, fetchpatch }:
+{ stdenv, fetchurl, linuxHeaders, useBSDCompatHeaders ? true }:
 
+let
+  cdefs_h = fetchurl {
+    url = "http://git.alpinelinux.org/cgit/aports/plain/main/libc-dev/sys-cdefs.h";
+    sha256 = "16l3dqnfq0f20rzbkhc38v74nqcsh9n3f343bpczqq8b1rz6vfrh";
+  };
+  queue_h = fetchurl {
+    url = "http://git.alpinelinux.org/cgit/aports/plain/main/libc-dev/sys-queue.h";
+    sha256 = "12qm82id7zys92a1qh2l1qf2wqgq6jr4qlbjmqyfffz3s3nhfd61";
+  };
+  tree_h = fetchurl {
+    url = "http://git.alpinelinux.org/cgit/aports/plain/main/libc-dev/sys-tree.h";
+    sha256 = "14igk6k00bnpfw660qhswagyhvr0gfqg4q55dxvaaq7ikfkrir71";
+  };
+in
 stdenv.mkDerivation rec {
   name    = "musl-${version}";
   version = "1.1.18";
@@ -30,6 +44,25 @@ stdenv.mkDerivation rec {
   outputs = [ "out" "dev" ];
 
   dontDisableStatic = true;
+  dontStrip = true;
+
+  postInstall =
+  ''
+    # Not sure why, but link in all but scsi directory as that's what uclibc/glibc do.
+    # Apparently glibc provides scsi itself?
+    (cd $dev/include && ln -s $(ls -d ${linuxHeaders}/include/* | grep -v "scsi$") .)
+  '' +
+  ''
+    mkdir -p $out/bin
+    # Create 'ldd' symlink, builtin
+    ln -s $out/lib/libc.so $out/bin/ldd
+  '' + stdenv.lib.optionalString useBSDCompatHeaders ''
+    install -D ${queue_h} $dev/include/sys/queue.h
+    install -D ${cdefs_h} $dev/include/sys/cdefs.h
+    install -D ${tree_h} $dev/include/sys/tree.h
+  '';
+
+  passthru.linuxHeaders = linuxHeaders;
 
   meta = {
     description = "An efficient, small, quality libc implementation";

From af6b756d984b4af3de046924d44c01da43c9a3b6 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 2 Jan 2018 20:12:18 -0600
Subject: [PATCH 035/173] muslCross: init

---
 pkgs/os-specific/linux/musl/default.nix | 18 +++++++++++-------
 pkgs/top-level/all-packages.nix         |  5 +++++
 2 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/pkgs/os-specific/linux/musl/default.nix b/pkgs/os-specific/linux/musl/default.nix
index bfc7b717bf7..9a97b4259b2 100644
--- a/pkgs/os-specific/linux/musl/default.nix
+++ b/pkgs/os-specific/linux/musl/default.nix
@@ -1,5 +1,8 @@
-{ stdenv, fetchurl, linuxHeaders, useBSDCompatHeaders ? true }:
-
+{ stdenv, lib, fetchurl
+, buildPackages
+, linuxHeaders ? null
+, useBSDCompatHeaders ? true
+}:
 let
   cdefs_h = fetchurl {
     url = "http://git.alpinelinux.org/cgit/aports/plain/main/libc-dev/sys-cdefs.h";
@@ -13,13 +16,14 @@ let
     url = "http://git.alpinelinux.org/cgit/aports/plain/main/libc-dev/sys-tree.h";
     sha256 = "14igk6k00bnpfw660qhswagyhvr0gfqg4q55dxvaaq7ikfkrir71";
   };
+
 in
 stdenv.mkDerivation rec {
   name    = "musl-${version}";
   version = "1.1.18";
 
   src = fetchurl {
-    url    = "http://www.musl-libc.org/releases/${name}.tar.gz";
+    url    = "http://www.musl-libc.org/releases/musl-${version}.tar.gz";
     sha256 = "0651lnj5spckqjf83nz116s8qhhydgqdy3rkl4icbh5f05fyw5yh";
   };
 
@@ -56,7 +60,7 @@ stdenv.mkDerivation rec {
     mkdir -p $out/bin
     # Create 'ldd' symlink, builtin
     ln -s $out/lib/libc.so $out/bin/ldd
-  '' + stdenv.lib.optionalString useBSDCompatHeaders ''
+  '' + lib.optionalString useBSDCompatHeaders ''
     install -D ${queue_h} $dev/include/sys/queue.h
     install -D ${cdefs_h} $dev/include/sys/cdefs.h
     install -D ${tree_h} $dev/include/sys/tree.h
@@ -67,8 +71,8 @@ stdenv.mkDerivation rec {
   meta = {
     description = "An efficient, small, quality libc implementation";
     homepage    = "http://www.musl-libc.org";
-    license     = stdenv.lib.licenses.mit;
-    platforms   = stdenv.lib.platforms.linux;
-    maintainers = [ stdenv.lib.maintainers.thoughtpolice ];
+    license     = lib.licenses.mit;
+    platforms   = lib.platforms.linux;
+    maintainers = [ lib.maintainers.thoughtpolice ];
   };
 }
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index 9b7c1863553..3208f0c8b4d 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -8741,12 +8741,17 @@ with pkgs;
     stdenv = crossLibcStdenv;
   };
 
+  muslCross = callPackage ../os-specific/linux/musl {
+    stdenv = crossLibcStdenv;
+  };
+
   # We can choose:
   libcCrossChooser = name:
     # libc is hackily often used from the previous stage. This `or`
     # hack fixes the hack, *sigh*.
     /**/ if name == "glibc" then targetPackages.glibcCross or glibcCross
     else if name == "uclibc" then uclibcCross
+    else if name == "musl" then targetPackages.muslCross or muslCross
     else if name == "msvcrt" then targetPackages.windows.mingw_w64 or windows.mingw_w64
     else if name == "libSystem" then darwin.xcode
     else throw "Unknown libc";

From 9df72c0290dcc709ecd09709adcb33134e331eac Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sun, 13 Aug 2017 00:06:28 -0500
Subject: [PATCH 036/173] Add musl-fts for bits that need it.

---
 pkgs/os-specific/linux/musl/fts.nix | 14 ++++++++++++++
 pkgs/top-level/all-packages.nix     |  1 +
 2 files changed, 15 insertions(+)
 create mode 100644 pkgs/os-specific/linux/musl/fts.nix

diff --git a/pkgs/os-specific/linux/musl/fts.nix b/pkgs/os-specific/linux/musl/fts.nix
new file mode 100644
index 00000000000..083541e5e93
--- /dev/null
+++ b/pkgs/os-specific/linux/musl/fts.nix
@@ -0,0 +1,14 @@
+{ stdenv, fetchFromGitHub, autoreconfHook, pkgconfig }:
+
+stdenv.mkDerivation rec {
+  name = "musl-fts-${version}";
+  version = "2017-01-13";
+  src = fetchFromGitHub {
+    owner = "pullmoll";
+    repo = "musl-fts";
+    rev = "0bde52df588e8969879a2cae51c3a4774ec62472";
+    sha256 = "1q8cpzisziysrs08b89wj0rm4p6dsyl177cclpfa0f7spjm3jg03";
+  };
+
+  nativeBuildInputs = [ autoreconfHook pkgconfig ];
+}
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index 3208f0c8b4d..db8fc1c2f08 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -13354,6 +13354,7 @@ with pkgs;
   multipath-tools = callPackage ../os-specific/linux/multipath-tools { };
 
   musl = callPackage ../os-specific/linux/musl { };
+  musl-fts = callPackage ../os-specific/linux/musl/fts.nix { };
 
   nettools = callPackage ../os-specific/linux/net-tools { };
 

From b12a7c92259a9066c7eadd847fdeb5d9e1af7383 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sat, 11 Nov 2017 11:01:43 -0600
Subject: [PATCH 037/173] add getconf, getent from Alpine for use w/musl

---
 pkgs/os-specific/linux/musl/getconf.nix | 19 +++++++++++++++++++
 pkgs/os-specific/linux/musl/getent.nix  | 18 ++++++++++++++++++
 pkgs/top-level/all-packages.nix         |  2 ++
 3 files changed, 39 insertions(+)
 create mode 100644 pkgs/os-specific/linux/musl/getconf.nix
 create mode 100644 pkgs/os-specific/linux/musl/getent.nix

diff --git a/pkgs/os-specific/linux/musl/getconf.nix b/pkgs/os-specific/linux/musl/getconf.nix
new file mode 100644
index 00000000000..dbfaca296bf
--- /dev/null
+++ b/pkgs/os-specific/linux/musl/getconf.nix
@@ -0,0 +1,19 @@
+{ stdenv, fetchurl }:
+
+stdenv.mkDerivation {
+  name = "musl-getconf";
+  src = fetchurl {
+    url = "https://raw.githubusercontent.com/alpinelinux/aports/48b16204aeeda5bc1f87e49c6b8e23d9abb07c73/main/musl/getconf.c";
+    sha256 = "0z14ml5343p5gapxw9fnbn2r72r7v2gk8662iifjrblh6sxhqzfq";
+  };
+
+  unpackPhase = ":";
+
+  buildPhase = ''$CC $src -o getconf'';
+  installPhase = ''
+    mkdir -p $out/bin
+    cp getconf $out/bin/
+  '';
+}
+
+
diff --git a/pkgs/os-specific/linux/musl/getent.nix b/pkgs/os-specific/linux/musl/getent.nix
new file mode 100644
index 00000000000..6eed17a76b0
--- /dev/null
+++ b/pkgs/os-specific/linux/musl/getent.nix
@@ -0,0 +1,18 @@
+{ stdenv, fetchurl }:
+
+stdenv.mkDerivation {
+  name = "musl-getent";
+  src = fetchurl {
+    url = "https://raw.githubusercontent.com/alpinelinux/aports/89a718d88ec7466e721f3bbe9ede5ffe58061d78/main/musl/getent.c";
+    sha256 = "0b4jqnsmv1hjgcz7db3vd61k682aphl59c3yhwya2q7mkc6g48xk";
+  };
+
+  unpackPhase = ":";
+
+  buildPhase = ''$CC $src -o getent'';
+  installPhase = ''
+    mkdir -p $out/bin
+    cp getent $out/bin/
+  '';
+}
+
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index db8fc1c2f08..461e255e400 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -13355,6 +13355,8 @@ with pkgs;
 
   musl = callPackage ../os-specific/linux/musl { };
   musl-fts = callPackage ../os-specific/linux/musl/fts.nix { };
+  musl-getconf = callPackage ../os-specific/linux/musl/getconf.nix { };
+  musl-getent = callPackage ../os-specific/linux/musl/getent.nix { };
 
   nettools = callPackage ../os-specific/linux/net-tools { };
 

From d4a2e336b4f126525f9e88dbf857dbaeb0d17d9d Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 23 Jan 2018 15:42:02 -0600
Subject: [PATCH 038/173] busybox: fixup after musl multiple outputs

---
 pkgs/os-specific/linux/busybox/default.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/os-specific/linux/busybox/default.nix b/pkgs/os-specific/linux/busybox/default.nix
index 5a485cdcf9f..35237d71390 100644
--- a/pkgs/os-specific/linux/busybox/default.nix
+++ b/pkgs/os-specific/linux/busybox/default.nix
@@ -87,7 +87,7 @@ stdenv.mkDerivation rec {
   '';
 
   postConfigure = lib.optionalString useMusl ''
-    makeFlagsArray+=("CC=${stdenv.cc.targetPrefix}gcc -isystem ${musl}/include -B${musl}/lib -L${musl}/lib")
+    makeFlagsArray+=("CC=${stdenv.cc.targetPrefix}gcc -isystem ${musl.dev}/include -B${musl}/lib -L${musl}/lib")
   '';
 
   depsBuildBuild = [ buildPackages.stdenv.cc ];

From 172af30d8a3d19749bbf00aa09e487510205ff01 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Thu, 11 Jan 2018 18:46:34 -0600
Subject: [PATCH 039/173] fish: on musl use musl-getent

---
 pkgs/shells/fish/default.nix | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/pkgs/shells/fish/default.nix b/pkgs/shells/fish/default.nix
index 490832d31d8..3078dd2dcab 100644
--- a/pkgs/shells/fish/default.nix
+++ b/pkgs/shells/fish/default.nix
@@ -1,6 +1,6 @@
 { stdenv, fetchurl, coreutils, utillinux,
   nettools, kbd, bc, which, gnused, gnugrep,
-  groff, man-db, glibc, libiconv, pcre2,
+  groff, man-db, glibc, musl-getent, libiconv, pcre2,
   gettext, ncurses, python3
 
   , writeText
@@ -12,6 +12,9 @@
 with stdenv.lib;
 
 let
+  getent = if stdenv.hostPlatform.isMusl
+             then "${musl-getent}/bin/getent"
+             else "${glibc.bin}/bin/getent";
   etcConfigAppendixText = ''
     ############### ↓ Nix hook for sourcing /etc/fish/config.fish ↓ ###############
     #                                                                             #
@@ -142,7 +145,7 @@ let
       sed -e "s| ul| ${utillinux}/bin/ul|" \
           -i "$out/share/fish/functions/__fish_print_help.fish"
       for cur in $out/share/fish/functions/*.fish; do
-        sed -e "s|/usr/bin/getent|${glibc.bin}/bin/getent|" \
+        sed -e "s|/usr/bin/getent|${getent}|" \
             -i "$cur"
       done
 

From 7debd78f065a1c479ec1d429478d18135aac21cf Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Wed, 3 Jan 2018 18:17:52 -0600
Subject: [PATCH 040/173] release-cross.nix: add entry for testing musl

---
 pkgs/top-level/release-cross.nix | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/pkgs/top-level/release-cross.nix b/pkgs/top-level/release-cross.nix
index 8e1213f2344..4c9cbde8100 100644
--- a/pkgs/top-level/release-cross.nix
+++ b/pkgs/top-level/release-cross.nix
@@ -125,6 +125,8 @@ in
   android = mapTestOnCross lib.systems.examples.aarch64-multiplatform (linuxCommon // {
   });
 
+  musl = mapTestOnCross lib.systems.examples.musl64 linuxCommon;
+
   /* Cross-built bootstrap tools for every supported platform */
   bootstrapTools = let
     tools = import ../stdenv/linux/make-bootstrap-tools-cross.nix { system = "x86_64-linux"; };

From 5ac5a3ebb5348d90d04ca473967d1050a6eb9b8a Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Thu, 4 Jan 2018 08:10:56 -0600
Subject: [PATCH 041/173] make-bootstrap-tools-cross.nix: add musl -> musl64

---
 pkgs/stdenv/linux/make-bootstrap-tools-cross.nix | 1 +
 1 file changed, 1 insertion(+)

diff --git a/pkgs/stdenv/linux/make-bootstrap-tools-cross.nix b/pkgs/stdenv/linux/make-bootstrap-tools-cross.nix
index ac1d57a2275..10adb0a635d 100644
--- a/pkgs/stdenv/linux/make-bootstrap-tools-cross.nix
+++ b/pkgs/stdenv/linux/make-bootstrap-tools-cross.nix
@@ -13,4 +13,5 @@ in with (import ../../../lib).systems.examples; {
   armv6l    = make raspberryPi;
   armv7l    = make armv7l-hf-multiplatform;
   aarch64   = make aarch64-multiplatform;
+  musl      = make musl64;
 }

From 41435ee8a62a0688eec81cc3934f47586fbcf64f Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 2 Jan 2018 20:16:14 -0600
Subject: [PATCH 042/173] libiconv: disable overly strict asssertion in
 libiconv

---
 pkgs/development/libraries/libiconv/default.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/development/libraries/libiconv/default.nix b/pkgs/development/libraries/libiconv/default.nix
index 89946512410..21abf7f8c07 100644
--- a/pkgs/development/libraries/libiconv/default.nix
+++ b/pkgs/development/libraries/libiconv/default.nix
@@ -2,7 +2,7 @@
 , buildPlatform, hostPlatform
 }:
 
-assert !stdenv.isLinux || hostPlatform != buildPlatform; # TODO: improve on cross
+# assert !stdenv.isLinux || hostPlatform != buildPlatform; # TODO: improve on cross
 
 stdenv.mkDerivation rec {
   name = "libiconv-${version}";

From 5dcf3cdf37c6892f32a71294cfa980f943309302 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Wed, 3 Jan 2018 16:13:15 -0600
Subject: [PATCH 043/173] stdenv: automatically update config.sub for musl

---
 pkgs/stdenv/adapters.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/stdenv/adapters.nix b/pkgs/stdenv/adapters.nix
index f7d2c49a66d..3090b6283e9 100644
--- a/pkgs/stdenv/adapters.nix
+++ b/pkgs/stdenv/adapters.nix
@@ -94,7 +94,7 @@ rec {
               # without proper `file` command, libtool sometimes fails
               # to recognize 64-bit DLLs
             ++ stdenv.lib.optional (hostPlatform.config == "x86_64-w64-mingw32") pkgs.file
-            ++ stdenv.lib.optional hostPlatform.isAarch64 pkgs.updateAutotoolsGnuConfigScriptsHook
+            ++ stdenv.lib.optional (hostPlatform.isAarch64 || hostPlatform.libc == "musl") pkgs.updateAutotoolsGnuConfigScriptsHook
             ;
 
           crossConfig = hostPlatform.config;

From b42c8af907f66127119ff47eeff8e63a9b5615d7 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Wed, 3 Jan 2018 00:47:16 -0600
Subject: [PATCH 044/173] bash: fix on musl

---
 pkgs/shells/bash/4.4.nix | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/pkgs/shells/bash/4.4.nix b/pkgs/shells/bash/4.4.nix
index 5635c6a73be..63c7fbc7a0e 100644
--- a/pkgs/shells/bash/4.4.nix
+++ b/pkgs/shells/bash/4.4.nix
@@ -1,5 +1,5 @@
 { stdenv, buildPackages
-, fetchurl, readline70 ? null, texinfo ? null, binutils ? null, bison
+, fetchurl, readline70 ? null, texinfo ? null, binutils ? null, bison, autoconf
 , buildPlatform, hostPlatform
 , interactive ? false
 }:
@@ -51,7 +51,12 @@ stdenv.mkDerivation rec {
 
   patchFlags = "-p0";
 
-  patches = upstreamPatches;
+  patches = upstreamPatches
+    # https://lists.gnu.org/archive/html/bug-bash/2016-10/msg00006.html
+    ++ optional (hostPlatform.libc == "musl") (fetchurl {
+      url = "https://lists.gnu.org/archive/html/bug-bash/2016-10/patchJxugOXrY2y.patch";
+      sha256 = "1m4v9imidb1cc1h91f2na0b8y9kc5c5fgmpvy9apcyv2kbdcghg1";
+  });
 
   postPatch = optionalString hostPlatform.isCygwin "patch -p2 < ${./cygwin-bash-4.4.11-2.src.patch}";
 
@@ -65,13 +70,17 @@ stdenv.mkDerivation rec {
     "bash_cv_dev_stdin=present"
     "bash_cv_dev_fd=standard"
     "bash_cv_termcap_lib=libncurses"
+  ] ++ optionals (hostPlatform.libc == "musl") [
+    "--without-bash-malloc"
+    "--disable-nls"
   ];
 
   # Note: Bison is needed because the patches above modify parse.y.
   depsBuildBuild = [ buildPackages.stdenv.cc ];
   nativeBuildInputs = [bison]
     ++ optional (texinfo != null) texinfo
-    ++ optional hostPlatform.isDarwin binutils;
+    ++ optional hostPlatform.isDarwin binutils
+    ++ optional (hostPlatform.libc == "musl") autoconf;
 
   buildInputs = optional interactive readline70;
 
@@ -86,7 +95,7 @@ stdenv.mkDerivation rec {
 
   postInstall = ''
     ln -s bash "$out/bin/sh"
-    rm $out/lib/bash/Makefile.inc
+    rm -f $out/lib/bash/Makefile.inc
   '';
 
   postFixup = if interactive
@@ -96,7 +105,7 @@ stdenv.mkDerivation rec {
     ''
     # most space is taken by locale data
     else ''
-      rm -r "$out/share" "$out/bin/bashbug"
+      rm -rf "$out/share" "$out/bin/bashbug"
     '';
 
   meta = with stdenv.lib; {

From 65c9c38a21c0b944ddcf1cd014663d6ee40b6793 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Wed, 3 Jan 2018 16:16:35 -0600
Subject: [PATCH 045/173] attr: patch to fix headers w/musl

---
 pkgs/development/libraries/attr/default.nix   |  4 +-
 .../libraries/attr/fix-headers-musl.patch     | 54 +++++++++++++++++++
 2 files changed, 57 insertions(+), 1 deletion(-)
 create mode 100644 pkgs/development/libraries/attr/fix-headers-musl.patch

diff --git a/pkgs/development/libraries/attr/default.nix b/pkgs/development/libraries/attr/default.nix
index 6a94cb0c0e2..05d9b842e65 100644
--- a/pkgs/development/libraries/attr/default.nix
+++ b/pkgs/development/libraries/attr/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, gettext }:
+{ stdenv, fetchurl, gettext, targetPlatform }:
 
 stdenv.mkDerivation rec {
   name = "attr-2.4.47";
@@ -16,6 +16,8 @@ stdenv.mkDerivation rec {
 
   installTargets = "install install-lib install-dev";
 
+  patches = if (targetPlatform.libc == "musl") then [ ./fix-headers-musl.patch ] else null;
+
   meta = {
     homepage = http://savannah.nongnu.org/projects/attr/;
     description = "Library and tools for manipulating extended attributes";
diff --git a/pkgs/development/libraries/attr/fix-headers-musl.patch b/pkgs/development/libraries/attr/fix-headers-musl.patch
new file mode 100644
index 00000000000..e969f640eee
--- /dev/null
+++ b/pkgs/development/libraries/attr/fix-headers-musl.patch
@@ -0,0 +1,54 @@
+--- attr-2.4.47/include/xattr.h
++++ attr-2.4.47/include/xattr.h
+@@ -31,33 +31,37 @@
+ #define XATTR_REPLACE 0x2       /* set value, fail if attr does not exist */
+ 
+ 
+-__BEGIN_DECLS
++#ifdef __cplusplus
++extern "C" {
++#endif
+ 
+ extern int setxattr (const char *__path, const char *__name,
+-		      const void *__value, size_t __size, int __flags) __THROW;
++		      const void *__value, size_t __size, int __flags);
+ extern int lsetxattr (const char *__path, const char *__name,
+-		      const void *__value, size_t __size, int __flags) __THROW;
++		      const void *__value, size_t __size, int __flags);
+ extern int fsetxattr (int __filedes, const char *__name,
+-		      const void *__value, size_t __size, int __flags) __THROW;
++		      const void *__value, size_t __size, int __flags);
+ 
+ extern ssize_t getxattr (const char *__path, const char *__name,
+-				void *__value, size_t __size) __THROW;
++				void *__value, size_t __size);
+ extern ssize_t lgetxattr (const char *__path, const char *__name,
+-				void *__value, size_t __size) __THROW;
++				void *__value, size_t __size);
+ extern ssize_t fgetxattr (int __filedes, const char *__name,
+-				void *__value, size_t __size) __THROW;
++				void *__value, size_t __size);
+ 
+ extern ssize_t listxattr (const char *__path, char *__list,
+-				size_t __size) __THROW;
++				size_t __size);
+ extern ssize_t llistxattr (const char *__path, char *__list,
+-				size_t __size) __THROW;
++				size_t __size);
+ extern ssize_t flistxattr (int __filedes, char *__list,
+-				size_t __size) __THROW;
++				size_t __size);
+ 
+-extern int removexattr (const char *__path, const char *__name) __THROW;
+-extern int lremovexattr (const char *__path, const char *__name) __THROW;
+-extern int fremovexattr (int __filedes,   const char *__name) __THROW;
++extern int removexattr (const char *__path, const char *__name);
++extern int lremovexattr (const char *__path, const char *__name);
++extern int fremovexattr (int __filedes,   const char *__name);
+ 
+-__END_DECLS
++#ifdef __cplusplus
++}
++#endif
+ 
+ #endif	/* __XATTR_H__ */

From cb521f75a6ef2cddd0dad75db7345a9b9603aedc Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Wed, 3 Jan 2018 16:49:45 -0600
Subject: [PATCH 046/173] linux-pam: fix build w/musl

---
 pkgs/os-specific/linux/pam/default.nix | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/pkgs/os-specific/linux/pam/default.nix b/pkgs/os-specific/linux/pam/default.nix
index fc2a7ea61d2..d9fd164bbc0 100644
--- a/pkgs/os-specific/linux/pam/default.nix
+++ b/pkgs/os-specific/linux/pam/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, buildPackages, fetchurl, flex, cracklib }:
+{ stdenv, buildPackages, targetPlatform, fetchurl, fetchpatch, flex, cracklib }:
 
 stdenv.mkDerivation rec {
   name = "linux-pam-${version}";
@@ -9,6 +9,21 @@ stdenv.mkDerivation rec {
     sha256 = "1fyi04d5nsh8ivd0rn2y0z83ylgc0licz7kifbb6xxi2ylgfs6i4";
   };
 
+  patches = stdenv.lib.optionals (targetPlatform.libc == "musl") [
+    (fetchpatch {
+      url = "https://git.alpinelinux.org/cgit/aports/plain/main/linux-pam/fix-compat.patch?id=05a62bda8ec255d7049a2bd4cf0fdc4b32bdb2cc";
+      sha256 = "1h5yp5h2mqp1fcwiwwklyfpa69a3i03ya32pivs60fd7g5bqa7sf";
+    })
+    (fetchpatch {
+      url = "https://git.alpinelinux.org/cgit/aports/plain/main/linux-pam/libpam-fix-build-with-eglibc-2.16.patch?id=05a62bda8ec255d7049a2bd4cf0fdc4b32bdb2cc";
+      sha256 = "1ib6shhvgzinjsc603k2x1lxh9dic6qq449fnk110gc359m23j81";
+    })
+    (fetchpatch {
+      url = "https://git.alpinelinux.org/cgit/aports/plain/main/linux-pam/musl-fix-pam_exec.patch?id=05a62bda8ec255d7049a2bd4cf0fdc4b32bdb2cc";
+      sha256 = "04dx6s9d8cxl40r7m7dc4si47ds4niaqm7902y1d6wcjvs11vrf0";
+    })
+  ];
+
   outputs = [ "out" "doc" "man" /* "modules" */ ];
 
   nativeBuildInputs = [ buildPackages.stdenv.cc flex ];
@@ -46,6 +61,12 @@ stdenv.mkDerivation rec {
 
   preConfigure = ''
     configureFlags="$configureFlags --includedir=$out/include/security"
+  '' + stdenv.lib.optionalString (targetPlatform.libc == "musl") ''
+      # export ac_cv_search_crypt=no
+      # (taken from Alpine linux, apparently insecure but also doesn't build O:))
+      # disable insecure modules
+      # sed -e 's/pam_rhosts//g' -i modules/Makefile.am
+      sed -e 's/pam_rhosts//g' -i modules/Makefile.in
   '';
 
   meta = {

From 48314959958a6203b0ff4368d8bc9c57d4651933 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Thu, 4 Jan 2018 15:52:45 -0600
Subject: [PATCH 047/173] make-bootstrap-tools: glibc -> libcCross

---
 pkgs/stdenv/linux/make-bootstrap-tools.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/stdenv/linux/make-bootstrap-tools.nix b/pkgs/stdenv/linux/make-bootstrap-tools.nix
index f51a39d5820..aba6e144b81 100644
--- a/pkgs/stdenv/linux/make-bootstrap-tools.nix
+++ b/pkgs/stdenv/linux/make-bootstrap-tools.nix
@@ -5,7 +5,7 @@
 let
   pkgs = import ../../.. { inherit localSystem crossSystem; };
   glibc = if pkgs.hostPlatform != pkgs.buildPlatform
-          then pkgs.glibcCross
+          then pkgs.libcCross
           else pkgs.glibc;
 in with pkgs; rec {
 

From 0ac504227d1ac76a247bd42a432a6a017299dba0 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 5 Jan 2018 15:36:24 -0600
Subject: [PATCH 048/173] make-bootstrap-tools: initial musl support, less
 glibc-specific

---
 pkgs/stdenv/linux/make-bootstrap-tools.nix | 47 +++++++++++++---------
 1 file changed, 29 insertions(+), 18 deletions(-)

diff --git a/pkgs/stdenv/linux/make-bootstrap-tools.nix b/pkgs/stdenv/linux/make-bootstrap-tools.nix
index aba6e144b81..aae0c135d30 100644
--- a/pkgs/stdenv/linux/make-bootstrap-tools.nix
+++ b/pkgs/stdenv/linux/make-bootstrap-tools.nix
@@ -4,7 +4,7 @@
 
 let
   pkgs = import ../../.. { inherit localSystem crossSystem; };
-  glibc = if pkgs.hostPlatform != pkgs.buildPlatform
+  libc = if pkgs.hostPlatform != pkgs.buildPlatform
           then pkgs.libcCross
           else pkgs.glibc;
 in with pkgs; rec {
@@ -46,29 +46,30 @@ in with pkgs; rec {
         set -x
         mkdir -p $out/bin $out/lib $out/libexec
 
+      '' + (if (targetPlatform.libc == "glibc") then ''
         # Copy what we need of Glibc.
-        cp -d ${glibc.out}/lib/ld*.so* $out/lib
-        cp -d ${glibc.out}/lib/libc*.so* $out/lib
-        cp -d ${glibc.out}/lib/libc_nonshared.a $out/lib
-        cp -d ${glibc.out}/lib/libm*.so* $out/lib
-        cp -d ${glibc.out}/lib/libdl*.so* $out/lib
-        cp -d ${glibc.out}/lib/librt*.so*  $out/lib
-        cp -d ${glibc.out}/lib/libpthread*.so* $out/lib
-        cp -d ${glibc.out}/lib/libnsl*.so* $out/lib
-        cp -d ${glibc.out}/lib/libutil*.so* $out/lib
-        cp -d ${glibc.out}/lib/libnss*.so* $out/lib
-        cp -d ${glibc.out}/lib/libresolv*.so* $out/lib
-        cp -d ${glibc.out}/lib/crt?.o $out/lib
+        cp -d ${libc.out}/lib/ld*.so* $out/lib
+        cp -d ${libc.out}/lib/libc*.so* $out/lib
+        cp -d ${libc.out}/lib/libc_nonshared.a $out/lib
+        cp -d ${libc.out}/lib/libm*.so* $out/lib
+        cp -d ${libc.out}/lib/libdl*.so* $out/lib
+        cp -d ${libc.out}/lib/librt*.so*  $out/lib
+        cp -d ${libc.out}/lib/libpthread*.so* $out/lib
+        cp -d ${libc.out}/lib/libnsl*.so* $out/lib
+        cp -d ${libc.out}/lib/libutil*.so* $out/lib
+        cp -d ${libc.out}/lib/libnss*.so* $out/lib
+        cp -d ${libc.out}/lib/libresolv*.so* $out/lib
+        cp -d ${libc.out}/lib/crt?.o $out/lib
 
-        cp -rL ${glibc.dev}/include $out
+        cp -rL ${libc.dev}/include $out
         chmod -R u+w "$out"
 
-        # glibc can contain linker scripts: find them, copy their deps,
+        # libc can contain linker scripts: find them, copy their deps,
         # and get rid of absolute paths (nuke-refs would make them useless)
         local lScripts=$(grep --files-with-matches --max-count=1 'GNU ld script' -R "$out/lib")
-        cp -d -t "$out/lib/" $(cat $lScripts | tr " " "\n" | grep -F '${glibc.out}' | sort -u)
+        cp -d -t "$out/lib/" $(cat $lScripts | tr " " "\n" | grep -F '${libc.out}' | sort -u)
         for f in $lScripts; do
-          substituteInPlace "$f" --replace '${glibc.out}/lib/' ""
+          substituteInPlace "$f" --replace '${libc.out}/lib/' ""
         done
 
         # Hopefully we won't need these.
@@ -76,7 +77,17 @@ in with pkgs; rec {
         find $out/include -name .install -exec rm {} \;
         find $out/include -name ..install.cmd -exec rm {} \;
         mv $out/include $out/include-glibc
+    '' else if (targetPlatform.libc == "musl") then ''
+        # Copy what we need from musl
+        cp ${libc.out}/lib/* $out/lib
+        cp -rL ${libc.dev}/include $out
+        chmod -R u+w "$out"
 
+        rm -rf $out/include/mtd $out/include/rdma $out/include/sound $out/include/video
+        find $out/include -name .install -exec rm {} \;
+        find $out/include -name ..install.cmd -exec rm {} \;
+    '' else throw "unsupported libc for bootstrap tools")
+    + ''
         # Copy coreutils, bash, etc.
         cp ${coreutilsMinimal.out}/bin/* $out/bin
         (cd $out/bin && rm vdir dir sha*sum pinky factor pathchk runcon shuf who whoami shred users)
@@ -115,7 +126,7 @@ in with pkgs; rec {
         cp -rd ${gcc.cc.out}/libexec/* $out/libexec
         chmod -R u+w $out/libexec
         rm -rf $out/libexec/gcc/*/*/plugin
-        mkdir $out/include
+        mkdir -p $out/include
         cp -rd ${gcc.cc.out}/include/c++ $out/include
         chmod -R u+w $out/include
         rm -rf $out/include/c++/*/ext/pb_ds

From a0af2aadb6aed0221de594026e4ba7af90524c3b Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 5 Jan 2018 15:54:09 -0600
Subject: [PATCH 049/173] for now, copy bootstrap-tools instead of modify
 in-place

---
 .../linux/bootstrap-tools-musl/default.nix    | 18 ++++++
 .../scripts/unpack-bootstrap-tools.sh         | 63 +++++++++++++++++++
 2 files changed, 81 insertions(+)
 create mode 100644 pkgs/stdenv/linux/bootstrap-tools-musl/default.nix
 create mode 100644 pkgs/stdenv/linux/bootstrap-tools-musl/scripts/unpack-bootstrap-tools.sh

diff --git a/pkgs/stdenv/linux/bootstrap-tools-musl/default.nix b/pkgs/stdenv/linux/bootstrap-tools-musl/default.nix
new file mode 100644
index 00000000000..6118585d545
--- /dev/null
+++ b/pkgs/stdenv/linux/bootstrap-tools-musl/default.nix
@@ -0,0 +1,18 @@
+{ system, bootstrapFiles }:
+
+derivation {
+  name = "bootstrap-tools";
+
+  builder = bootstrapFiles.busybox;
+
+  args = [ "ash" "-e" ./scripts/unpack-bootstrap-tools.sh ];
+
+  tarball = bootstrapFiles.bootstrapTools;
+
+  inherit system;
+
+  # Needed by the GCC wrapper.
+  langC = true;
+  langCC = true;
+  isGNU = true;
+}
diff --git a/pkgs/stdenv/linux/bootstrap-tools-musl/scripts/unpack-bootstrap-tools.sh b/pkgs/stdenv/linux/bootstrap-tools-musl/scripts/unpack-bootstrap-tools.sh
new file mode 100644
index 00000000000..85e74aea89e
--- /dev/null
+++ b/pkgs/stdenv/linux/bootstrap-tools-musl/scripts/unpack-bootstrap-tools.sh
@@ -0,0 +1,63 @@
+# Unpack the bootstrap tools tarball.
+echo Unpacking the bootstrap tools...
+$builder mkdir $out
+< $tarball $builder unxz | $builder tar x -C $out
+
+# Set the ELF interpreter / RPATH in the bootstrap binaries.
+echo Patching the bootstrap tools...
+
+if test -f $out/lib/ld.so.?; then
+   # MIPS case
+   LD_BINARY=$out/lib/ld.so.?
+else
+   # i686, x86_64 and armv5tel
+   LD_BINARY=$out/lib/ld-*so.?
+fi
+
+# On x86_64, ld-linux-x86-64.so.2 barfs on patchelf'ed programs.  So
+# use a copy of patchelf.
+LD_LIBRARY_PATH=$out/lib $LD_BINARY $out/bin/cp $out/bin/patchelf .
+
+for i in $out/bin/* $out/libexec/gcc/*/*/*; do
+    if [ -L "$i" ]; then continue; fi
+    if [ -z "${i##*/liblto*}" ]; then continue; fi
+    echo patching "$i"
+    LD_LIBRARY_PATH=$out/lib $LD_BINARY \
+        ./patchelf --set-interpreter $LD_BINARY --set-rpath $out/lib --force-rpath "$i"
+done
+
+for i in $out/lib/librt-*.so $out/lib/libpcre*; do
+    if [ -L "$i" ]; then continue; fi
+    echo patching "$i"
+    $out/bin/patchelf --set-rpath $out/lib --force-rpath "$i"
+done
+
+# Fix the libc linker script.
+export PATH=$out/bin
+cat $out/lib/libc.so | sed "s|/nix/store/e*-[^/]*/|$out/|g" > $out/lib/libc.so.tmp
+mv $out/lib/libc.so.tmp $out/lib/libc.so
+cat $out/lib/libpthread.so | sed "s|/nix/store/e*-[^/]*/|$out/|g" > $out/lib/libpthread.so.tmp
+mv $out/lib/libpthread.so.tmp $out/lib/libpthread.so
+
+# Provide some additional symlinks.
+ln -s bash $out/bin/sh
+ln -s bzip2 $out/bin/bunzip2
+
+# Provide a gunzip script.
+cat > $out/bin/gunzip <<EOF
+#!$out/bin/sh
+exec $out/bin/gzip -d "\$@"
+EOF
+chmod +x $out/bin/gunzip
+
+# Provide fgrep/egrep.
+echo "#! $out/bin/sh" > $out/bin/egrep
+echo "exec $out/bin/grep -E \"\$@\"" >> $out/bin/egrep
+echo "#! $out/bin/sh" > $out/bin/fgrep
+echo "exec $out/bin/grep -F \"\$@\"" >> $out/bin/fgrep
+
+# Provide xz (actually only xz -d will work).
+echo "#! $out/bin/sh" > $out/bin/xz
+echo "exec $builder unxz \"\$@\"" >> $out/bin/xz
+
+chmod +x $out/bin/egrep $out/bin/fgrep $out/bin/xz

From 0e16989d398d4bd5ea587f3b3fe01e2e47342b5d Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 5 Jan 2018 16:26:46 -0600
Subject: [PATCH 050/173] bootstrap-tools-musl: WIP

---
 .../linux/bootstrap-tools-musl/default.nix    |  2 +-
 .../scripts/unpack-bootstrap-tools.sh         | 13 +++++----
 pkgs/stdenv/linux/make-bootstrap-tools.nix    | 29 +++++++++++++++----
 3 files changed, 31 insertions(+), 13 deletions(-)

diff --git a/pkgs/stdenv/linux/bootstrap-tools-musl/default.nix b/pkgs/stdenv/linux/bootstrap-tools-musl/default.nix
index 6118585d545..fbadd447194 100644
--- a/pkgs/stdenv/linux/bootstrap-tools-musl/default.nix
+++ b/pkgs/stdenv/linux/bootstrap-tools-musl/default.nix
@@ -14,5 +14,5 @@ derivation {
   # Needed by the GCC wrapper.
   langC = true;
   langCC = true;
-  isGNU = true;
+  isGNU = false;
 }
diff --git a/pkgs/stdenv/linux/bootstrap-tools-musl/scripts/unpack-bootstrap-tools.sh b/pkgs/stdenv/linux/bootstrap-tools-musl/scripts/unpack-bootstrap-tools.sh
index 85e74aea89e..8b5070dc6d6 100644
--- a/pkgs/stdenv/linux/bootstrap-tools-musl/scripts/unpack-bootstrap-tools.sh
+++ b/pkgs/stdenv/linux/bootstrap-tools-musl/scripts/unpack-bootstrap-tools.sh
@@ -26,18 +26,19 @@ for i in $out/bin/* $out/libexec/gcc/*/*/*; do
         ./patchelf --set-interpreter $LD_BINARY --set-rpath $out/lib --force-rpath "$i"
 done
 
-for i in $out/lib/librt-*.so $out/lib/libpcre*; do
+for i in $out/lib/libiconv*.so $out/lib/libpcre* $out/lib/libc.so; do
     if [ -L "$i" ]; then continue; fi
     echo patching "$i"
     $out/bin/patchelf --set-rpath $out/lib --force-rpath "$i"
 done
 
-# Fix the libc linker script.
 export PATH=$out/bin
-cat $out/lib/libc.so | sed "s|/nix/store/e*-[^/]*/|$out/|g" > $out/lib/libc.so.tmp
-mv $out/lib/libc.so.tmp $out/lib/libc.so
-cat $out/lib/libpthread.so | sed "s|/nix/store/e*-[^/]*/|$out/|g" > $out/lib/libpthread.so.tmp
-mv $out/lib/libpthread.so.tmp $out/lib/libpthread.so
+
+# Fix the libc linker script.
+#cat $out/lib/libc.so | sed "s|/nix/store/e*-[^/]*/|$out/|g" > $out/lib/libc.so.tmp
+#mv $out/lib/libc.so.tmp $out/lib/libc.so
+#cat $out/lib/libpthread.so | sed "s|/nix/store/e*-[^/]*/|$out/|g" > $out/lib/libpthread.so.tmp
+#mv $out/lib/libpthread.so.tmp $out/lib/libpthread.so
 
 # Provide some additional symlinks.
 ln -s bash $out/bin/sh
diff --git a/pkgs/stdenv/linux/make-bootstrap-tools.nix b/pkgs/stdenv/linux/make-bootstrap-tools.nix
index aae0c135d30..b659e259ee1 100644
--- a/pkgs/stdenv/linux/make-bootstrap-tools.nix
+++ b/pkgs/stdenv/linux/make-bootstrap-tools.nix
@@ -46,7 +46,7 @@ in with pkgs; rec {
         set -x
         mkdir -p $out/bin $out/lib $out/libexec
 
-      '' + (if (targetPlatform.libc == "glibc") then ''
+      '' + (if (hostPlatform.libc == "glibc") then ''
         # Copy what we need of Glibc.
         cp -d ${libc.out}/lib/ld*.so* $out/lib
         cp -d ${libc.out}/lib/libc*.so* $out/lib
@@ -77,7 +77,7 @@ in with pkgs; rec {
         find $out/include -name .install -exec rm {} \;
         find $out/include -name ..install.cmd -exec rm {} \;
         mv $out/include $out/include-glibc
-    '' else if (targetPlatform.libc == "musl") then ''
+    '' else if (hostPlatform.libc == "musl") then ''
         # Copy what we need from musl
         cp ${libc.out}/lib/* $out/lib
         cp -rL ${libc.dev}/include $out
@@ -86,6 +86,7 @@ in with pkgs; rec {
         rm -rf $out/include/mtd $out/include/rdma $out/include/sound $out/include/video
         find $out/include -name .install -exec rm {} \;
         find $out/include -name ..install.cmd -exec rm {} \;
+        mv $out/include $out/include-libc
     '' else throw "unsupported libc for bootstrap tools")
     + ''
         # Copy coreutils, bash, etc.
@@ -137,6 +138,8 @@ in with pkgs; rec {
         cp -d ${libmpc.out}/lib/libmpc*.so* $out/lib
         cp -d ${zlib.out}/lib/libz.so* $out/lib
         cp -d ${libelf}/lib/libelf.so* $out/lib
+      '' + lib.optionalString (hostPlatform.libc == "musl") ''
+        cp -d ${libiconv.out}/lib/libiconv*.so* $out/lib
 
       '' + lib.optionalString (hostPlatform != buildPlatform) ''
         # These needed for cross but not native tools because the stdenv
@@ -200,10 +203,17 @@ in with pkgs; rec {
     bootstrapTools = runCommand "bootstrap-tools.tar.xz" {} "cp ${build}/on-server/bootstrap-tools.tar.xz $out";
   };
 
-  bootstrapTools = import ./bootstrap-tools {
-    inherit (hostPlatform) system;
-    inherit bootstrapFiles;
-  };
+  bootstrapTools = if (hostPlatform.libc == "glibc") then
+    import ./bootstrap-tools {
+      inherit (hostPlatform) system;
+      inherit bootstrapFiles;
+    }
+    else if (hostPlatform.libc == "musl") then
+    import ./bootstrap-tools-musl {
+      inherit (hostPlatform) system;
+      inherit bootstrapFiles;
+    }
+    else throw "unsupported libc";
 
   test = derivation {
     name = "test-bootstrap-tools";
@@ -226,10 +236,17 @@ in with pkgs; rec {
       grep --version
       gcc --version
 
+    '' + lib.optionalString (hostPlatform.libc == "glibc") ''
       ldlinux=$(echo ${bootstrapTools}/lib/ld-linux*.so.?)
       export CPP="cpp -idirafter ${bootstrapTools}/include-glibc -B${bootstrapTools}"
       export CC="gcc -idirafter ${bootstrapTools}/include-glibc -B${bootstrapTools} -Wl,-dynamic-linker,$ldlinux -Wl,-rpath,${bootstrapTools}/lib"
       export CXX="g++ -idirafter ${bootstrapTools}/include-glibc -B${bootstrapTools} -Wl,-dynamic-linker,$ldlinux -Wl,-rpath,${bootstrapTools}/lib"
+    '' + lib.optionalString (hostPlatform.libc == "musl") ''
+      ldmusl=$(echo ${bootstrapTools}/lib/ld-musl*.so.?)
+      export CPP="cpp -idirafter ${bootstrapTools}/include-libc -B${bootstrapTools}"
+      export CC="gcc -idirafter ${bootstrapTools}/include-libc -B${bootstrapTools} -Wl,-dynamic-linker,$ldmusl -Wl,-rpath,${bootstrapTools}/lib"
+      export CXX="g++ -idirafter ${bootstrapTools}/include-libc -B${bootstrapTools} -Wl,-dynamic-linker,$ldmusl -Wl,-rpath,${bootstrapTools}/lib"
+    '' + ''
 
       echo '#include <stdio.h>' >> foo.c
       echo '#include <limits.h>' >> foo.c

From 15d401dcfa24ca91f26f1bfb69c4a5eaf0f46aeb Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Wed, 3 Jan 2018 00:24:27 -0600
Subject: [PATCH 051/173] gcc: disable libsanitizer, libgomp on musl

---
 pkgs/development/compilers/gcc/4.8/default.nix | 2 +-
 pkgs/development/compilers/gcc/4.9/default.nix | 2 +-
 pkgs/development/compilers/gcc/5/default.nix   | 2 +-
 pkgs/development/compilers/gcc/6/default.nix   | 3 ++-
 pkgs/development/compilers/gcc/7/default.nix   | 2 +-
 5 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/pkgs/development/compilers/gcc/4.8/default.nix b/pkgs/development/compilers/gcc/4.8/default.nix
index 0105a159877..36adfd075df 100644
--- a/pkgs/development/compilers/gcc/4.8/default.nix
+++ b/pkgs/development/compilers/gcc/4.8/default.nix
@@ -169,7 +169,7 @@ let version = "4.8.5";
           # To keep ABI compatibility with upstream mingw-w64
           "--enable-fully-dynamic-string"
         ] else
-          optionals (targetPlatform.libc == "uclibc") [
+          optionals (targetPlatform.libc == "uclibc" || targetPlatform.libc == "musl") [
             # In uclibc cases, libgomp needs an additional '-ldl'
             # and as I don't know how to pass it, I disable libgomp.
             "--disable-libgomp"
diff --git a/pkgs/development/compilers/gcc/4.9/default.nix b/pkgs/development/compilers/gcc/4.9/default.nix
index 1b1492686d0..c436da678fd 100644
--- a/pkgs/development/compilers/gcc/4.9/default.nix
+++ b/pkgs/development/compilers/gcc/4.9/default.nix
@@ -160,7 +160,7 @@ let version = "4.9.4";
           # To keep ABI compatibility with upstream mingw-w64
           "--enable-fully-dynamic-string"
         ] else
-          optionals (targetPlatform.libc == "uclibc") [
+          optionals (targetPlatform.libc == "uclibc" || targetPlatform.libc == "musl") [
             # libsanitizer requires netrom/netrom.h which is not
             # available in uclibc.
             "--disable-libsanitizer"
diff --git a/pkgs/development/compilers/gcc/5/default.nix b/pkgs/development/compilers/gcc/5/default.nix
index 0636ce7381a..ea59d2826e2 100644
--- a/pkgs/development/compilers/gcc/5/default.nix
+++ b/pkgs/development/compilers/gcc/5/default.nix
@@ -160,7 +160,7 @@ let version = "5.5.0";
           # To keep ABI compatibility with upstream mingw-w64
           "--enable-fully-dynamic-string"
         ] else
-          optionals (targetPlatform.libc == "uclibc") [
+          optionals (targetPlatform.libc == "uclibc" || targetPlatform.libc == "musl") [
             # libsanitizer requires netrom/netrom.h which is not
             # available in uclibc.
             "--disable-libsanitizer"
diff --git a/pkgs/development/compilers/gcc/6/default.nix b/pkgs/development/compilers/gcc/6/default.nix
index 2614e96e1b7..91088a61233 100644
--- a/pkgs/development/compilers/gcc/6/default.nix
+++ b/pkgs/development/compilers/gcc/6/default.nix
@@ -158,7 +158,7 @@ let version = "6.4.0";
           # To keep ABI compatibility with upstream mingw-w64
           "--enable-fully-dynamic-string"
         ] else
-          optionals (targetPlatform.libc == "uclibc") [
+          optionals (targetPlatform.libc == "uclibc" || targetPlatform.libc == "musl") [
             # libsanitizer requires netrom/netrom.h which is not
             # available in uclibc.
             "--disable-libsanitizer"
@@ -399,6 +399,7 @@ stdenv.mkDerivation ({
       # On Illumos/Solaris GNU as is preferred
       "--with-gnu-as" "--without-gnu-ld"
     ]
+    ++ optional (targetPlatform == hostPlatform && targetPlatform.libc == "musl") "--disable-libsanitizer"
   ;
 
   targetConfig = if targetPlatform != hostPlatform then targetPlatform.config else null;
diff --git a/pkgs/development/compilers/gcc/7/default.nix b/pkgs/development/compilers/gcc/7/default.nix
index 0e5f69c2726..267e660e12d 100644
--- a/pkgs/development/compilers/gcc/7/default.nix
+++ b/pkgs/development/compilers/gcc/7/default.nix
@@ -156,7 +156,7 @@ let version = "7.3.0";
           # To keep ABI compatibility with upstream mingw-w64
           "--enable-fully-dynamic-string"
         ] else
-          optionals (targetPlatform.libc == "uclibc") [
+          optionals (targetPlatform.libc == "uclibc" || targetPlatform.libc == "musl") [
             # libsanitizer requires netrom/netrom.h which is not
             # available in uclibc.
             "--disable-libsanitizer"

From ece06deee410fd2f2adf613540c75278249b5e0f Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Mon, 15 Jan 2018 11:35:47 -0600
Subject: [PATCH 052/173] gcc6: try to fix gccCrossStageStati

---
 pkgs/development/compilers/gcc/6/default.nix | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/pkgs/development/compilers/gcc/6/default.nix b/pkgs/development/compilers/gcc/6/default.nix
index 91088a61233..78062ea4bb1 100644
--- a/pkgs/development/compilers/gcc/6/default.nix
+++ b/pkgs/development/compilers/gcc/6/default.nix
@@ -142,6 +142,9 @@ let version = "6.4.0";
         "--disable-shared"
         "--disable-libatomic"  # libatomic requires libc
         "--disable-decimal-float" # libdecnumber requires libc
+        # maybe only needed on musl, PATH_MAX
+        # https://github.com/richfelker/musl-cross-make/blob/0867cdf300618d1e3e87a0a939fa4427207ad9d7/litecross/Makefile#L62
+        "--disable-libmpx"
       ] else [
         (if crossDarwin then "--with-sysroot=${getLib libcCross}/share/sysroot"
          else                "--with-headers=${getDev libcCross}/include")

From 5eea9e82f4d6e8e5cf6a53a1a6bd96eecc5d7f29 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Thu, 4 Jan 2018 16:20:45 -0600
Subject: [PATCH 053/173] gcc6: try patching MUSL_DYNAMIC_LINKER as well

---
 pkgs/development/compilers/gcc/5/default.nix | 6 ++++--
 pkgs/development/compilers/gcc/6/default.nix | 7 ++++---
 pkgs/development/compilers/gcc/7/default.nix | 6 ++++--
 3 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/pkgs/development/compilers/gcc/5/default.nix b/pkgs/development/compilers/gcc/5/default.nix
index ea59d2826e2..3c0a0e675d5 100644
--- a/pkgs/development/compilers/gcc/5/default.nix
+++ b/pkgs/development/compilers/gcc/5/default.nix
@@ -258,13 +258,14 @@ stdenv.mkDerivation ({
       let
         libc = if libcCross != null then libcCross else stdenv.cc.libc;
       in
-        '' echo "fixing the \`GLIBC_DYNAMIC_LINKER' and \`UCLIBC_DYNAMIC_LINKER' macros..."
+        '' echo "fixing the \`GLIBC_DYNAMIC_LINKER', \`UCLIBC_DYNAMIC_LINKER', and \`MUSL_DYNAMIC_LINKER' macros..."
            for header in "gcc/config/"*-gnu.h "gcc/config/"*"/"*.h
            do
              grep -q LIBC_DYNAMIC_LINKER "$header" || continue
              echo "  fixing \`$header'..."
              sed -i "$header" \
-                 -e 's|define[[:blank:]]*\([UCG]\+\)LIBC_DYNAMIC_LINKER\([0-9]*\)[[:blank:]]"\([^\"]\+\)"$|define \1LIBC_DYNAMIC_LINKER\2 "${libc.out}\3"|g'
+                 -e 's|define[[:blank:]]*\([UCG]\+\)LIBC_DYNAMIC_LINKER\([0-9]*\)[[:blank:]]"\([^\"]\+\)"$|define \1LIBC_DYNAMIC_LINKER\2 "${libc.out}\3"|g' \
+                 -e 's|define[[:blank:]]*MUSL_DYNAMIC_LINKER\([0-9]*\)[[:blank:]]"\([^\"]\+\)"$|define MUSL_DYNAMIC_LINKER\1 "${libc.out}\2"|g'
            done
         ''
     else null;
@@ -396,6 +397,7 @@ stdenv.mkDerivation ({
       # On Illumos/Solaris GNU as is preferred
       "--with-gnu-as" "--without-gnu-ld"
     ]
+    ++ optional (targetPlatform == hostPlatform && targetPlatform.libc == "musl") "--disable-libsanitizer"
   ;
 
   targetConfig = if targetPlatform != hostPlatform then targetPlatform.config else null;
diff --git a/pkgs/development/compilers/gcc/6/default.nix b/pkgs/development/compilers/gcc/6/default.nix
index 78062ea4bb1..7fb2eec3f77 100644
--- a/pkgs/development/compilers/gcc/6/default.nix
+++ b/pkgs/development/compilers/gcc/6/default.nix
@@ -260,13 +260,14 @@ stdenv.mkDerivation ({
       let
         libc = if libcCross != null then libcCross else stdenv.cc.libc;
       in
-        '' echo "fixing the \`GLIBC_DYNAMIC_LINKER' and \`UCLIBC_DYNAMIC_LINKER' macros..."
+        '' echo "fixing the \`GLIBC_DYNAMIC_LINKER', \`UCLIBC_DYNAMIC_LINKER', and \`MUSL_DYNAMIC_LINKER' macros..."
            for header in "gcc/config/"*-gnu.h "gcc/config/"*"/"*.h
            do
-             grep -q LIBC_DYNAMIC_LINKER "$header" || continue
+             grep -q _DYNAMIC_LINKER "$header" || continue
              echo "  fixing \`$header'..."
              sed -i "$header" \
-                 -e 's|define[[:blank:]]*\([UCG]\+\)LIBC_DYNAMIC_LINKER\([0-9]*\)[[:blank:]]"\([^\"]\+\)"$|define \1LIBC_DYNAMIC_LINKER\2 "${libc.out}\3"|g'
+                 -e 's|define[[:blank:]]*\([UCG]\+\)LIBC_DYNAMIC_LINKER\([0-9]*\)[[:blank:]]"\([^\"]\+\)"$|define \1LIBC_DYNAMIC_LINKER\2 "${libc.out}\3"|g' \
+                 -e 's|define[[:blank:]]*MUSL_DYNAMIC_LINKER\([0-9]*\)[[:blank:]]"\([^\"]\+\)"$|define MUSL_DYNAMIC_LINKER\1 "${libc.out}\2"|g'
            done
         ''
     else null;
diff --git a/pkgs/development/compilers/gcc/7/default.nix b/pkgs/development/compilers/gcc/7/default.nix
index 267e660e12d..73768d65104 100644
--- a/pkgs/development/compilers/gcc/7/default.nix
+++ b/pkgs/development/compilers/gcc/7/default.nix
@@ -254,13 +254,14 @@ stdenv.mkDerivation ({
       let
         libc = if libcCross != null then libcCross else stdenv.cc.libc;
       in
-        '' echo "fixing the \`GLIBC_DYNAMIC_LINKER' and \`UCLIBC_DYNAMIC_LINKER' macros..."
+        '' echo "fixing the \`GLIBC_DYNAMIC_LINKER', \`UCLIBC_DYNAMIC_LINKER', and \`MUSL_DYNAMIC_LINKER' macros..."
            for header in "gcc/config/"*-gnu.h "gcc/config/"*"/"*.h
            do
              grep -q LIBC_DYNAMIC_LINKER "$header" || continue
              echo "  fixing \`$header'..."
              sed -i "$header" \
-                 -e 's|define[[:blank:]]*\([UCG]\+\)LIBC_DYNAMIC_LINKER\([0-9]*\)[[:blank:]]"\([^\"]\+\)"$|define \1LIBC_DYNAMIC_LINKER\2 "${libc.out}\3"|g'
+                 -e 's|define[[:blank:]]*\([UCG]\+\)LIBC_DYNAMIC_LINKER\([0-9]*\)[[:blank:]]"\([^\"]\+\)"$|define \1LIBC_DYNAMIC_LINKER\2 "${libc.out}\3"|g' \
+                 -e 's|define[[:blank:]]*MUSL_DYNAMIC_LINKER\([0-9]*\)[[:blank:]]"\([^\"]\+\)"$|define MUSL_DYNAMIC_LINKER\1 "${libc.out}\2"|g'
            done
         ''
     else null;
@@ -392,6 +393,7 @@ stdenv.mkDerivation ({
       # On Illumos/Solaris GNU as is preferred
       "--with-gnu-as" "--without-gnu-ld"
     ]
+    ++ optional (targetPlatform == hostPlatform && targetPlatform.libc == "musl") "--disable-libsanitizer"
   ;
 
   targetConfig = if targetPlatform != hostPlatform then targetPlatform.config else null;

From fbf41b9ee2b59847d9f53b4e25033e574c207546 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Thu, 4 Jan 2018 16:36:56 -0600
Subject: [PATCH 054/173] gcc6: try to fix LOCAL_INCLUDE_DIR problem w/musl

---
 pkgs/development/compilers/gcc/6/default.nix | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/pkgs/development/compilers/gcc/6/default.nix b/pkgs/development/compilers/gcc/6/default.nix
index 7fb2eec3f77..2b0c4bfdde3 100644
--- a/pkgs/development/compilers/gcc/6/default.nix
+++ b/pkgs/development/compilers/gcc/6/default.nix
@@ -260,6 +260,7 @@ stdenv.mkDerivation ({
       let
         libc = if libcCross != null then libcCross else stdenv.cc.libc;
       in
+        (
         '' echo "fixing the \`GLIBC_DYNAMIC_LINKER', \`UCLIBC_DYNAMIC_LINKER', and \`MUSL_DYNAMIC_LINKER' macros..."
            for header in "gcc/config/"*-gnu.h "gcc/config/"*"/"*.h
            do
@@ -270,6 +271,11 @@ stdenv.mkDerivation ({
                  -e 's|define[[:blank:]]*MUSL_DYNAMIC_LINKER\([0-9]*\)[[:blank:]]"\([^\"]\+\)"$|define MUSL_DYNAMIC_LINKER\1 "${libc.out}\2"|g'
            done
         ''
+        + stdenv.lib.optionalString (targetPlatform.libc == "musl")
+        ''
+            sed -i gcc/config/linux.h -e '1i#undef LOCAL_INCLUDE_DIR'
+        ''
+        )
     else null;
 
   # TODO(@Ericson2314): Make passthru instead. Weird to avoid mass rebuild,

From f146a3c31661e6ed9d4b964d7dd00e7838133d2f Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sun, 7 Jan 2018 18:02:57 -0600
Subject: [PATCH 055/173] gcc5, 7: same fixes as gcc6

---
 pkgs/development/compilers/gcc/5/default.nix | 8 +++++++-
 pkgs/development/compilers/gcc/7/default.nix | 8 +++++++-
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/pkgs/development/compilers/gcc/5/default.nix b/pkgs/development/compilers/gcc/5/default.nix
index 3c0a0e675d5..47973d9422c 100644
--- a/pkgs/development/compilers/gcc/5/default.nix
+++ b/pkgs/development/compilers/gcc/5/default.nix
@@ -258,16 +258,22 @@ stdenv.mkDerivation ({
       let
         libc = if libcCross != null then libcCross else stdenv.cc.libc;
       in
+        (
         '' echo "fixing the \`GLIBC_DYNAMIC_LINKER', \`UCLIBC_DYNAMIC_LINKER', and \`MUSL_DYNAMIC_LINKER' macros..."
            for header in "gcc/config/"*-gnu.h "gcc/config/"*"/"*.h
            do
-             grep -q LIBC_DYNAMIC_LINKER "$header" || continue
+             grep -q _DYNAMIC_LINKER "$header" || continue
              echo "  fixing \`$header'..."
              sed -i "$header" \
                  -e 's|define[[:blank:]]*\([UCG]\+\)LIBC_DYNAMIC_LINKER\([0-9]*\)[[:blank:]]"\([^\"]\+\)"$|define \1LIBC_DYNAMIC_LINKER\2 "${libc.out}\3"|g' \
                  -e 's|define[[:blank:]]*MUSL_DYNAMIC_LINKER\([0-9]*\)[[:blank:]]"\([^\"]\+\)"$|define MUSL_DYNAMIC_LINKER\1 "${libc.out}\2"|g'
            done
         ''
+        + stdenv.lib.optionalString (targetPlatform.libc == "musl")
+        ''
+            sed -i gcc/config/linux.h -e '1i#undef LOCAL_INCLUDE_DIR'
+        ''
+        )
     else null;
 
   # TODO(@Ericson2314): Make passthru instead. Weird to avoid mass rebuild,
diff --git a/pkgs/development/compilers/gcc/7/default.nix b/pkgs/development/compilers/gcc/7/default.nix
index 73768d65104..3f6aeeeabcd 100644
--- a/pkgs/development/compilers/gcc/7/default.nix
+++ b/pkgs/development/compilers/gcc/7/default.nix
@@ -254,16 +254,22 @@ stdenv.mkDerivation ({
       let
         libc = if libcCross != null then libcCross else stdenv.cc.libc;
       in
+        (
         '' echo "fixing the \`GLIBC_DYNAMIC_LINKER', \`UCLIBC_DYNAMIC_LINKER', and \`MUSL_DYNAMIC_LINKER' macros..."
            for header in "gcc/config/"*-gnu.h "gcc/config/"*"/"*.h
            do
-             grep -q LIBC_DYNAMIC_LINKER "$header" || continue
+             grep -q _DYNAMIC_LINKER "$header" || continue
              echo "  fixing \`$header'..."
              sed -i "$header" \
                  -e 's|define[[:blank:]]*\([UCG]\+\)LIBC_DYNAMIC_LINKER\([0-9]*\)[[:blank:]]"\([^\"]\+\)"$|define \1LIBC_DYNAMIC_LINKER\2 "${libc.out}\3"|g' \
                  -e 's|define[[:blank:]]*MUSL_DYNAMIC_LINKER\([0-9]*\)[[:blank:]]"\([^\"]\+\)"$|define MUSL_DYNAMIC_LINKER\1 "${libc.out}\2"|g'
            done
         ''
+        + stdenv.lib.optionalString (targetPlatform.libc == "musl")
+        ''
+            sed -i gcc/config/linux.h -e '1i#undef LOCAL_INCLUDE_DIR'
+        ''
+        )
     else null;
 
   # TODO(@Ericson2314): Make passthru instead. Weird to avoid mass rebuild,

From ff9b494103c6439795a4bed506e08945c92d19c2 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sun, 7 Jan 2018 18:49:29 -0600
Subject: [PATCH 056/173] gcc7: Apply patch to fix selftest w/musl?

---
 pkgs/development/compilers/gcc/7/default.nix | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/pkgs/development/compilers/gcc/7/default.nix b/pkgs/development/compilers/gcc/7/default.nix
index 3f6aeeeabcd..5a1152cffb5 100644
--- a/pkgs/development/compilers/gcc/7/default.nix
+++ b/pkgs/development/compilers/gcc/7/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, targetPackages, fetchurl, noSysDirs
+{ stdenv, targetPackages, fetchurl, fetchpatch, noSysDirs
 , langC ? true, langCC ? true, langFortran ? false
 , langObjC ? targetPlatform.isDarwin
 , langObjCpp ? targetPlatform.isDarwin
@@ -67,6 +67,10 @@ let version = "7.3.0";
       [ ]
       ++ optional (targetPlatform != hostPlatform) ../libstdc++-target.patch
       ++ optional noSysDirs ../no-sys-dirs.patch
+      ++ optional (targetPlatform.libc == "musl") (fetchpatch {
+        url = "https://git.busybox.net/buildroot/plain/package/gcc/7.1.0/0900-remove-selftests.patch?id=11271540bfe6adafbc133caf6b5b902a816f5f02";
+        sha256 = "0mrvxsdwip2p3l17dscpc1x8vhdsciqw1z5q9i6p5g9yg1cqnmgs";
+      })
       # The GNAT Makefiles did not pay attention to CFLAGS_FOR_TARGET for its
       # target libraries and tools.
       ++ optional langAda ../gnat-cflags.patch

From e4d5928ba37c705cf2a1b5468f87725a2d5e1dfa Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Thu, 11 Jan 2018 14:35:31 -0600
Subject: [PATCH 057/173] boehm-gc: fix missing include, fix build w/musl

Also disable-static, don't remember why
---
 pkgs/development/libraries/boehm-gc/default.nix | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/pkgs/development/libraries/boehm-gc/default.nix b/pkgs/development/libraries/boehm-gc/default.nix
index f3c58a93872..9efcf023a45 100644
--- a/pkgs/development/libraries/boehm-gc/default.nix
+++ b/pkgs/development/libraries/boehm-gc/default.nix
@@ -1,4 +1,4 @@
-{ lib, stdenv, fetchurl, pkgconfig, libatomic_ops, enableLargeConfig ? false
+{ lib, stdenv, fetchurl, fetchpatch, pkgconfig, libatomic_ops, enableLargeConfig ? false
 , buildPlatform, hostPlatform
 }:
 
@@ -20,9 +20,19 @@ stdenv.mkDerivation rec {
   outputs = [ "out" "dev" "doc" ];
   separateDebugInfo = stdenv.isLinux;
 
+  preConfigure = stdenv.lib.optionalString (stdenv.cc.libc == "musl") ''
+    export NIX_CFLAGS_COMPILE+="-D_GNU_SOURCE -DUSE_MMAP -DHAVE_DL_ITERATE_PHDR"
+  '';
+
+  patches = [ (fetchpatch {
+    url = "https://raw.githubusercontent.com/gentoo/musl/85b6a600996bdd71162b357e9ba93d8559342432/dev-libs/boehm-gc/files/boehm-gc-7.6.0-sys_select.patch";
+    sha256 = "1gydwlklvci30f5dpp5ccw2p2qpph5y41r55wx9idamjlq66fbb3";
+  }) ];
+
   configureFlags =
     [ "--enable-cplusplus" ]
-    ++ lib.optional enableLargeConfig "--enable-large-config";
+    ++ lib.optional enableLargeConfig "--enable-large-config"
+    ++ lib.optional (stdenv.cc.libc == "musl") "--disable-static";
 
   doCheck = true; # not cross;
 

From 3ee5094934a99ad40712c2b38b7bd346a920ed14 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Thu, 11 Jan 2018 15:09:34 -0600
Subject: [PATCH 058/173] linux bootstrap for musl: kludgery

---
 pkgs/stdenv/linux/bootstrap-tools-musl/default.nix | 2 +-
 pkgs/stdenv/linux/default.nix                      | 8 ++++++--
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/pkgs/stdenv/linux/bootstrap-tools-musl/default.nix b/pkgs/stdenv/linux/bootstrap-tools-musl/default.nix
index fbadd447194..6118585d545 100644
--- a/pkgs/stdenv/linux/bootstrap-tools-musl/default.nix
+++ b/pkgs/stdenv/linux/bootstrap-tools-musl/default.nix
@@ -14,5 +14,5 @@ derivation {
   # Needed by the GCC wrapper.
   langC = true;
   langCC = true;
-  isGNU = false;
+  isGNU = true;
 }
diff --git a/pkgs/stdenv/linux/default.nix b/pkgs/stdenv/linux/default.nix
index 858323e5cc5..603b8aeea8b 100644
--- a/pkgs/stdenv/linux/default.nix
+++ b/pkgs/stdenv/linux/default.nix
@@ -6,7 +6,8 @@
 { lib
 , localSystem, crossSystem, config, overlays
 
-, bootstrapFiles ? { # switch
+, bootstrapFiles ? if localSystem.libc == "musl" then import ./bootstrap-files/musl64.nix
+else { # switch
     "i686-linux" = import ./bootstrap-files/i686.nix;
     "x86_64-linux" = import ./bootstrap-files/x86_64.nix;
     "armv5tel-linux" = import ./bootstrap-files/armv5tel.nix;
@@ -40,7 +41,7 @@ let
 
 
   # Download and unpack the bootstrap tools (coreutils, GCC, Glibc, ...).
-  bootstrapTools = import ./bootstrap-tools { inherit system bootstrapFiles; };
+  bootstrapTools = import (if localSystem.libc == "musl" then ./bootstrap-tools-musl else ./bootstrap-tools) { inherit system bootstrapFiles; };
 
 
   # This function builds the various standard environments used during
@@ -140,7 +141,10 @@ in
         buildCommand = ''
           mkdir -p $out
           ln -s ${bootstrapTools}/lib $out/lib
+        '' + lib.optionalString (localSystem.libc == "glibc") ''
           ln -s ${bootstrapTools}/include-glibc $out/include
+        '' + lib.optionalString (localSystem.libc == "musl") ''
+          ln -s ${bootstrapTools}/include-libc $out/include
         '';
       };
       gcc-unwrapped = bootstrapTools;

From fd00d37b4fa1d53f661062e219fcadace6dd962a Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Thu, 11 Jan 2018 15:47:34 -0600
Subject: [PATCH 059/173] musl64 bootstrap: grab from gravity, requires netrc
 magic

---
 pkgs/stdenv/linux/bootstrap-files/musl64.nix | 10 ++++++++++
 1 file changed, 10 insertions(+)
 create mode 100644 pkgs/stdenv/linux/bootstrap-files/musl64.nix

diff --git a/pkgs/stdenv/linux/bootstrap-files/musl64.nix b/pkgs/stdenv/linux/bootstrap-files/musl64.nix
new file mode 100644
index 00000000000..65deff26c42
--- /dev/null
+++ b/pkgs/stdenv/linux/bootstrap-files/musl64.nix
@@ -0,0 +1,10 @@
+# Use busybox for i686-linux since it works on x86_64-linux as well.
+(import ./i686.nix) //
+
+{
+  bootstrapTools = import <nix/fetchurl.nix> {
+    # XXX: Find a permanent location for this
+    url = https://wdtz.org/files/5zfs7s729n4lrlxmhlnc6qmfrlhahy9s-stdenv-bootstrap-tools-x86_64-unknown-linux-musl/on-server/bootstrap-tools.tar.xz;
+    sha256 = "0lwi08c2v7ip2z9li597ixywix976561hr358z2fbd6sqi943axl";
+  };
+}

From 9dbbd75e547237407834d49a6d3a55fb03e79a57 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 12 Jan 2018 02:07:02 -0600
Subject: [PATCH 060/173] musl bootstrap: allow libiconv

---
 pkgs/stdenv/linux/default.nix | 1 +
 1 file changed, 1 insertion(+)

diff --git a/pkgs/stdenv/linux/default.nix b/pkgs/stdenv/linux/default.nix
index 603b8aeea8b..8522413ea03 100644
--- a/pkgs/stdenv/linux/default.nix
+++ b/pkgs/stdenv/linux/default.nix
@@ -340,6 +340,7 @@ in
             glibc.out glibc.dev glibc.bin/*propagated from .dev*/ linuxHeaders
             binutils gcc gcc.cc gcc.cc.lib gcc.expand-response-params
           ]
+          ++ lib.optional (localSystem.libc == "musl") libiconv
           ++ lib.optionals localSystem.isAarch64
             [ prevStage.updateAutotoolsGnuConfigScriptsHook prevStage.gnu-config ];
 

From 5a8002873e31038f25d6d42598b113d0dbe0c7e2 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Thu, 11 Jan 2018 18:15:40 -0600
Subject: [PATCH 061/173] fixup linux stdenv bootstrap

---
 pkgs/stdenv/linux/default.nix | 39 +++++++++++++++++++++--------------
 1 file changed, 23 insertions(+), 16 deletions(-)

diff --git a/pkgs/stdenv/linux/default.nix b/pkgs/stdenv/linux/default.nix
index 8522413ea03..da12eed7d64 100644
--- a/pkgs/stdenv/linux/default.nix
+++ b/pkgs/stdenv/linux/default.nix
@@ -43,6 +43,8 @@ let
   # Download and unpack the bootstrap tools (coreutils, GCC, Glibc, ...).
   bootstrapTools = import (if localSystem.libc == "musl" then ./bootstrap-tools-musl else ./bootstrap-tools) { inherit system bootstrapFiles; };
 
+  getLibc = stage: stage.${localSystem.libc};
+
 
   # This function builds the various standard environments used during
   # the bootstrap.  In all stages, we build an stdenv and the package
@@ -83,7 +85,7 @@ let
           cc = prevStage.gcc-unwrapped;
           bintools = prevStage.binutils;
           isGNU = true;
-          libc = prevStage.glibc;
+          libc = getLibc prevStage;
           inherit (prevStage) coreutils gnugrep;
           name = name;
           stdenvNoCC = prevStage.ccWrapperStdenv;
@@ -96,7 +98,7 @@ let
 
           # stdenv.glibc is used by GCC build to figure out the system-level
           # /usr/include directory.
-          inherit (prevStage) glibc;
+          # inherit (prevStage) glibc;
         };
         overrides = self: super: (overrides self super) // { fetchurl = thisStdenv.fetchurlBoot; };
       };
@@ -114,7 +116,8 @@ in
     __raw = true;
 
     gcc-unwrapped = null;
-    glibc = null;
+    glibc = assert false; null;
+    musl = assert false; null;
     binutils = null;
     coreutils = null;
     gnugrep = null;
@@ -136,8 +139,8 @@ in
       # will search the Glibc headers before the GCC headers).  So
       # create a dummy Glibc here, which will be used in the stdenv of
       # stage1.
-      glibc = self.stdenv.mkDerivation {
-        name = "bootstrap-glibc";
+      ${localSystem.libc} = self.stdenv.mkDerivation {
+        name = "bootstrap-${localSystem.libc}";
         buildCommand = ''
           mkdir -p $out
           ln -s ${bootstrapTools}/lib $out/lib
@@ -152,7 +155,7 @@ in
         nativeTools = false;
         nativeLibc = false;
         buildPackages = { };
-        libc = self.glibc;
+        libc = getLibc self;
         inherit (self) stdenvNoCC coreutils gnugrep;
         bintools = bootstrapTools;
         name = "bootstrap-binutils-wrapper";
@@ -181,7 +184,9 @@ in
       binutils = super.binutils_nogold;
       inherit (prevStage)
         ccWrapperStdenv
-        glibc gcc-unwrapped coreutils gnugrep;
+        gcc-unwrapped coreutils gnugrep;
+
+      ${localSystem.libc} = getLibc prevStage;
 
       # A threaded perl build needs glibc/libpthread_nonshared.a,
       # which is not included in bootstrapTools, so disable threading.
@@ -207,7 +212,7 @@ in
       binutils = prevStage.binutils.override {
         # Rewrap the binutils with the new glibc, so both the next
         # stage's wrappers use it.
-        libc = self.glibc;
+        libc = getLibc self;
       };
     };
   })
@@ -222,8 +227,9 @@ in
     overrides = self: super: rec {
       inherit (prevStage)
         ccWrapperStdenv
-        binutils glibc coreutils gnugrep
+        binutils coreutils gnugrep
         perl patchelf linuxHeaders gnum4 bison;
+      ${localSystem.libc} = getLibc prevStage;
       # Link GCC statically against GMP etc.  This makes sense because
       # these builds of the libraries are only used by GCC, so it
       # reduces the size of the stdenv closure.
@@ -251,8 +257,8 @@ in
       # because gcc (since JAR support) already depends on zlib, and
       # then if we already have a zlib we want to use that for the
       # other purposes (binutils and top-level pkgs) too.
-      inherit (prevStage) gettext gnum4 bison gmp perl glibc zlib linuxHeaders;
-
+      inherit (prevStage) gettext gnum4 bison gmp perl zlib linuxHeaders;
+      ${localSystem.libc} = getLibc prevStage;
       binutils = super.binutils.override {
         # Don't use stdenv's shell but our own
         shell = self.bash + "/bin/bash";
@@ -271,7 +277,7 @@ in
         };
         cc = prevStage.gcc-unwrapped;
         bintools = self.binutils;
-        libc = self.glibc;
+        libc = getLibc self;
         inherit (self) stdenvNoCC coreutils gnugrep;
         name = "";
         shell = self.bash + "/bin/bash";
@@ -318,7 +324,7 @@ in
       inherit (prevStage.stdenv) fetchurlBoot;
 
       extraAttrs = {
-        inherit (prevStage) glibc;
+        # inherit (prevStage) glibc;
         inherit platform bootstrapTools;
         shellPackage = prevStage.bash;
       };
@@ -336,8 +342,8 @@ in
             ++ lib.optional (gawk.libsigsegv != null) gawk.libsigsegv
           )
         # More complicated cases
-        ++ [
-            glibc.out glibc.dev glibc.bin/*propagated from .dev*/ linuxHeaders
+        ++ (map (x: getOutput x (getLibc prevStage)) [ "out" "dev" "bin" ] )
+        ++  [ /*propagated from .dev*/ linuxHeaders
             binutils gcc gcc.cc gcc.cc.lib gcc.expand-response-params
           ]
           ++ lib.optional (localSystem.libc == "musl") libiconv
@@ -347,8 +353,9 @@ in
       overrides = self: super: {
         inherit (prevStage)
           gzip bzip2 xz bash coreutils diffutils findutils gawk
-          glibc gnumake gnused gnutar gnugrep gnupatch patchelf
+          gnumake gnused gnutar gnugrep gnupatch patchelf
           attr acl paxctl zlib pcre;
+        ${localSystem.libc} = getLibc prevStage;
       } // lib.optionalAttrs (super.targetPlatform == localSystem) {
         # Need to get rid of these when cross-compiling.
         inherit (prevStage) binutils binutils-raw;

From e390b45c14b4efcfef39d3228db9fd641ed6ed6f Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Mon, 15 Jan 2018 11:15:29 -0600
Subject: [PATCH 062/173] gccCrossStageStatic: use musl to ensure dynamic
 linker is right

---
 pkgs/top-level/all-packages.nix | 1 +
 1 file changed, 1 insertion(+)

diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index 461e255e400..e0d7d879cda 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -5836,6 +5836,7 @@ with pkgs;
     libcCross1 =
       if targetPlatform.libc == "msvcrt" then targetPackages.windows.mingw_w64_headers
       else if targetPlatform.libc == "libSystem" then darwin.xcode
+      else if targetPlatform.libc == "musl" then musl
       else null;
     binutils1 = wrapBintoolsWith {
       bintools = binutils-unwrapped;

From 20203206d855adb9e069f310ba6c14bb7cd42592 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Thu, 11 Jan 2018 18:48:06 -0600
Subject: [PATCH 063/173] perl.TextWrapI18N: mark broken on musl

glibcLocales doesn't build currently.
---
 pkgs/top-level/perl-packages.nix | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/pkgs/top-level/perl-packages.nix b/pkgs/top-level/perl-packages.nix
index cbb730363ff..63091cd92f5 100644
--- a/pkgs/top-level/perl-packages.nix
+++ b/pkgs/top-level/perl-packages.nix
@@ -15065,6 +15065,8 @@ let self = _self // overrides; _self = with self; {
     meta = {
       description = "Line wrapping module with support for multibyte, fullwidth, and combining characters and languages without whitespaces between words";
       license = with stdenv.lib.licenses; [ artistic1 gpl2 ];
+      # bogus use of glibc, pretty sure, think this is what we have glibcLocales for?
+      broken = stdenv.hostPlatform.libc != "glibc";
     };
   };
 

From 8ad9400a6c075e0933686815b25e431d597ccaf5 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 12 Jan 2018 00:28:18 -0600
Subject: [PATCH 064/173] findutils: disable tests w/musl

---
 pkgs/tools/misc/findutils/default.nix | 1 +
 1 file changed, 1 insertion(+)

diff --git a/pkgs/tools/misc/findutils/default.nix b/pkgs/tools/misc/findutils/default.nix
index 4eef3f7a9d5..f79720289bf 100644
--- a/pkgs/tools/misc/findutils/default.nix
+++ b/pkgs/tools/misc/findutils/default.nix
@@ -21,6 +21,7 @@ stdenv.mkDerivation rec {
   doCheck
     =  !hostPlatform.isDarwin
     && !(hostPlatform.libc == "glibc" && hostPlatform.isi686)
+    && (hostPlatform.libc != "musl")
     && hostPlatform == buildPlatform;
 
   outputs = [ "out" "info" ];

From f969923bacf2d7d34dd86bcd7f232b899720a165 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 12 Jan 2018 02:02:32 -0600
Subject: [PATCH 065/173] gnupatch: disable tests on musl :(

---
 pkgs/tools/text/gnupatch/default.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/tools/text/gnupatch/default.nix b/pkgs/tools/text/gnupatch/default.nix
index dfdb25438cf..7eda640b6c7 100644
--- a/pkgs/tools/text/gnupatch/default.nix
+++ b/pkgs/tools/text/gnupatch/default.nix
@@ -17,7 +17,7 @@ stdenv.mkDerivation rec {
     "ac_cv_func_strnlen_working=yes"
   ];
 
-  doCheck = true; # not cross;
+  doCheck = hostPlatform.libc != "musl"; # not cross;
 
   meta = {
     description = "GNU Patch, a program to apply differences to files";

From 158c3c4864b55911e8be7db9c4b558c1a6dee207 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 12 Jan 2018 02:27:33 -0600
Subject: [PATCH 066/173] libxml2: disable tests also

---
 pkgs/development/libraries/libxml2/default.nix | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/pkgs/development/libraries/libxml2/default.nix b/pkgs/development/libraries/libxml2/default.nix
index b19f4a2953f..c4f3ff1efdd 100644
--- a/pkgs/development/libraries/libxml2/default.nix
+++ b/pkgs/development/libraries/libxml2/default.nix
@@ -36,7 +36,8 @@ in stdenv.mkDerivation rec {
 
   enableParallelBuilding = true;
 
-  doCheck = (stdenv.hostPlatform == stdenv.buildPlatform) && !stdenv.isDarwin;
+  doCheck = (stdenv.hostPlatform == stdenv.buildPlatform) && !stdenv.isDarwin &&
+    hostPlatform.libc != "musl";
 
   crossAttrs = lib.optionalAttrs (hostPlatform.libc == "msvcrt") {
     # creating the DLL is broken ATM

From 4aca016313e08370757ac33dd15e15afe986ea13 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 12 Jan 2018 16:18:55 -0600
Subject: [PATCH 067/173] changes some targetPlatform to hostPlatform checks

---
 pkgs/development/libraries/attr/default.nix     | 4 ++--
 pkgs/development/libraries/boehm-gc/default.nix | 4 ++--
 pkgs/os-specific/linux/pam/default.nix          | 6 +++---
 3 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/pkgs/development/libraries/attr/default.nix b/pkgs/development/libraries/attr/default.nix
index 05d9b842e65..34bf9aca974 100644
--- a/pkgs/development/libraries/attr/default.nix
+++ b/pkgs/development/libraries/attr/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, gettext, targetPlatform }:
+{ stdenv, fetchurl, gettext, hostPlatform }:
 
 stdenv.mkDerivation rec {
   name = "attr-2.4.47";
@@ -16,7 +16,7 @@ stdenv.mkDerivation rec {
 
   installTargets = "install install-lib install-dev";
 
-  patches = if (targetPlatform.libc == "musl") then [ ./fix-headers-musl.patch ] else null;
+  patches = if (hostPlatform.libc == "musl") then [ ./fix-headers-musl.patch ] else null;
 
   meta = {
     homepage = http://savannah.nongnu.org/projects/attr/;
diff --git a/pkgs/development/libraries/boehm-gc/default.nix b/pkgs/development/libraries/boehm-gc/default.nix
index 9efcf023a45..c1bcb46bae6 100644
--- a/pkgs/development/libraries/boehm-gc/default.nix
+++ b/pkgs/development/libraries/boehm-gc/default.nix
@@ -20,7 +20,7 @@ stdenv.mkDerivation rec {
   outputs = [ "out" "dev" "doc" ];
   separateDebugInfo = stdenv.isLinux;
 
-  preConfigure = stdenv.lib.optionalString (stdenv.cc.libc == "musl") ''
+  preConfigure = stdenv.lib.optionalString (stdenv.hostPlatform.libc == "musl") ''
     export NIX_CFLAGS_COMPILE+="-D_GNU_SOURCE -DUSE_MMAP -DHAVE_DL_ITERATE_PHDR"
   '';
 
@@ -32,7 +32,7 @@ stdenv.mkDerivation rec {
   configureFlags =
     [ "--enable-cplusplus" ]
     ++ lib.optional enableLargeConfig "--enable-large-config"
-    ++ lib.optional (stdenv.cc.libc == "musl") "--disable-static";
+    ++ lib.optional (stdenv.hostPlatform.libc == "musl") "--disable-static";
 
   doCheck = true; # not cross;
 
diff --git a/pkgs/os-specific/linux/pam/default.nix b/pkgs/os-specific/linux/pam/default.nix
index d9fd164bbc0..fb33f9555e5 100644
--- a/pkgs/os-specific/linux/pam/default.nix
+++ b/pkgs/os-specific/linux/pam/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, buildPackages, targetPlatform, fetchurl, fetchpatch, flex, cracklib }:
+{ stdenv, buildPackages, hostPlatform, fetchurl, fetchpatch, flex, cracklib }:
 
 stdenv.mkDerivation rec {
   name = "linux-pam-${version}";
@@ -9,7 +9,7 @@ stdenv.mkDerivation rec {
     sha256 = "1fyi04d5nsh8ivd0rn2y0z83ylgc0licz7kifbb6xxi2ylgfs6i4";
   };
 
-  patches = stdenv.lib.optionals (targetPlatform.libc == "musl") [
+  patches = stdenv.lib.optionals (hostPlatform.libc == "musl") [
     (fetchpatch {
       url = "https://git.alpinelinux.org/cgit/aports/plain/main/linux-pam/fix-compat.patch?id=05a62bda8ec255d7049a2bd4cf0fdc4b32bdb2cc";
       sha256 = "1h5yp5h2mqp1fcwiwwklyfpa69a3i03ya32pivs60fd7g5bqa7sf";
@@ -61,7 +61,7 @@ stdenv.mkDerivation rec {
 
   preConfigure = ''
     configureFlags="$configureFlags --includedir=$out/include/security"
-  '' + stdenv.lib.optionalString (targetPlatform.libc == "musl") ''
+  '' + stdenv.lib.optionalString (hostPlatform.libc == "musl") ''
       # export ac_cv_search_crypt=no
       # (taken from Alpine linux, apparently insecure but also doesn't build O:))
       # disable insecure modules

From 4b6b83f159e1f2e03a1001ec31ce4fcf62d2cd8e Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 12 Jan 2018 16:19:22 -0600
Subject: [PATCH 068/173] linux stdenv: find bootstrap files by libc, then arch

---
 pkgs/stdenv/linux/default.nix | 31 ++++++++++++++++++++-----------
 1 file changed, 20 insertions(+), 11 deletions(-)

diff --git a/pkgs/stdenv/linux/default.nix b/pkgs/stdenv/linux/default.nix
index da12eed7d64..a214f8a02c3 100644
--- a/pkgs/stdenv/linux/default.nix
+++ b/pkgs/stdenv/linux/default.nix
@@ -6,17 +6,26 @@
 { lib
 , localSystem, crossSystem, config, overlays
 
-, bootstrapFiles ? if localSystem.libc == "musl" then import ./bootstrap-files/musl64.nix
-else { # switch
-    "i686-linux" = import ./bootstrap-files/i686.nix;
-    "x86_64-linux" = import ./bootstrap-files/x86_64.nix;
-    "armv5tel-linux" = import ./bootstrap-files/armv5tel.nix;
-    "armv6l-linux" = import ./bootstrap-files/armv6l.nix;
-    "armv7l-linux" = import ./bootstrap-files/armv7l.nix;
-    "aarch64-linux" = import ./bootstrap-files/aarch64.nix;
-    "mips64el-linux" = import ./bootstrap-files/loongson2f.nix;
-  }.${localSystem.system}
-    or (abort "unsupported platform for the pure Linux stdenv")
+, bootstrapFiles ?
+  let table = {
+    "glibc" = {
+      "i686-linux" = import ./bootstrap-files/i686.nix;
+      "x86_64-linux" = import ./bootstrap-files/x86_64.nix;
+      "armv5tel-linux" = import ./bootstrap-files/armv5tel.nix;
+      "armv6l-linux" = import ./bootstrap-files/armv6l.nix;
+      "armv7l-linux" = import ./bootstrap-files/armv7l.nix;
+      "aarch64-linux" = import ./bootstrap-files/aarch64.nix;
+      "mips64el-linux" = import ./bootstrap-files/loongson2f.nix;
+    };
+    "musl" = {
+      "x86_64-linux" = import ./bootstrap-files/musl64.nix;
+    };
+  };
+  archLookupTable = table.${localSystem.libc}
+    or (abort "unsupported libc for the pure Linux stdenv");
+  files = archLookupTable.${localSystem.system}
+    or (abort "unsupported platform for the pure Linux stdenv");
+  in files
 }:
 
 assert crossSystem == null;

From f566e3f987869c2c269986efbe4e3d256c6846cb Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 12 Jan 2018 19:36:58 -0600
Subject: [PATCH 069/173] gcc7: more selftest disabling, whenever building
 cross

---
 pkgs/development/compilers/gcc/7/default.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/development/compilers/gcc/7/default.nix b/pkgs/development/compilers/gcc/7/default.nix
index 5a1152cffb5..d30575f5cea 100644
--- a/pkgs/development/compilers/gcc/7/default.nix
+++ b/pkgs/development/compilers/gcc/7/default.nix
@@ -67,7 +67,7 @@ let version = "7.3.0";
       [ ]
       ++ optional (targetPlatform != hostPlatform) ../libstdc++-target.patch
       ++ optional noSysDirs ../no-sys-dirs.patch
-      ++ optional (targetPlatform.libc == "musl") (fetchpatch {
+      ++ optional (hostPlatform != buildPlatform) (fetchpatch { # XXX: Refine when this should be applied
         url = "https://git.busybox.net/buildroot/plain/package/gcc/7.1.0/0900-remove-selftests.patch?id=11271540bfe6adafbc133caf6b5b902a816f5f02";
         sha256 = "0mrvxsdwip2p3l17dscpc1x8vhdsciqw1z5q9i6p5g9yg1cqnmgs";
       })

From 3b9e99ddae0fa45ef65ce4d5dfbb628fb1e218f3 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 12 Jan 2018 20:08:58 -0600
Subject: [PATCH 070/173] libffi: add patch to fix w/musl

also use patches from OE, fix musl softfloat build
---
 pkgs/development/libraries/libffi/default.nix | 25 ++++++++++++++++---
 1 file changed, 21 insertions(+), 4 deletions(-)

diff --git a/pkgs/development/libraries/libffi/default.nix b/pkgs/development/libraries/libffi/default.nix
index e48db6c9928..1339be451c2 100644
--- a/pkgs/development/libraries/libffi/default.nix
+++ b/pkgs/development/libraries/libffi/default.nix
@@ -1,4 +1,4 @@
-{ fetchurl, stdenv, dejagnu, doCheck ? false
+{ stdenv, fetchurl, fetchpatch,  dejagnu, doCheck ? false
 , buildPlatform, hostPlatform
 }:
 
@@ -10,11 +10,28 @@ stdenv.mkDerivation rec {
     sha256 = "0dya49bnhianl0r65m65xndz6ls2jn1xngyn72gd28ls3n7bnvnh";
   };
 
-  patches = stdenv.lib.optional stdenv.isCygwin ./3.2.1-cygwin.patch ++
-    stdenv.lib.optional stdenv.isAarch64 (fetchurl {
+  patches = stdenv.lib.optional stdenv.isCygwin ./3.2.1-cygwin.patch
+    ++ stdenv.lib.optional stdenv.isAarch64 (fetchpatch {
       url = https://src.fedoraproject.org/rpms/libffi/raw/ccffc1700abfadb0969495a6e51b964117fc03f6/f/libffi-aarch64-rhbz1174037.patch;
       sha256 = "1vpirrgny43hp0885rswgv3xski8hg7791vskpbg3wdjdpb20wbc";
-    });
+    })
+    ++ stdenv.lib.optional hostPlatform.isMusl (fetchpatch {
+      name = "gnu-linux-define.patch";
+      url = "https://git.alpinelinux.org/cgit/aports/plain/main/libffi/gnu-linux-define.patch?id=bb024fd8ec6f27a76d88396c9f7c5c4b5800d580";
+      sha256 = "11pvy3xkhyvnjfyy293v51f1xjy3x0azrahv1nw9y9mw8bifa2j2";
+    })
+    ++ stdenv.lib.optionals stdenv.isMips [
+      (fetchpatch {
+        name = "0001-mips-Use-compiler-internal-define-for-linux.patch";
+        url = "http://cgit.openembedded.org/openembedded-core/plain/meta/recipes-support/libffi/libffi/0001-mips-Use-compiler-internal-define-for-linux.patch?id=318e33a708378652edcf61ce7d9d7f3a07743000";
+        sha256 = "1gc53lw90p6hc0cmhj3csrwincfz7va5ss995ksw5gm0yrr9mrvb";
+      })
+      (fetchpatch {
+        name = "0001-mips-fix-MIPS-softfloat-build-issue.patch";
+        url = "http://cgit.openembedded.org/openembedded-core/plain/meta/recipes-support/libffi/libffi/0001-mips-fix-MIPS-softfloat-build-issue.patch?id=318e33a708378652edcf61ce7d9d7f3a07743000";
+        sha256 = "0l8xgdciqalg4z9rcwyk87h8fdxpfv4hfqxwsy2agpnpszl5jjdq";
+      })
+    ];
 
   outputs = [ "out" "dev" "man" "info" ];
 

From 3b0d3248c92ad14f8203c3634bcaf80751d5eb45 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 12 Jan 2018 20:28:20 -0600
Subject: [PATCH 071/173] gcc7: tweak check, target -> host

---
 pkgs/development/compilers/gcc/7/default.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/development/compilers/gcc/7/default.nix b/pkgs/development/compilers/gcc/7/default.nix
index d30575f5cea..42ac766d2cc 100644
--- a/pkgs/development/compilers/gcc/7/default.nix
+++ b/pkgs/development/compilers/gcc/7/default.nix
@@ -269,7 +269,7 @@ stdenv.mkDerivation ({
                  -e 's|define[[:blank:]]*MUSL_DYNAMIC_LINKER\([0-9]*\)[[:blank:]]"\([^\"]\+\)"$|define MUSL_DYNAMIC_LINKER\1 "${libc.out}\2"|g'
            done
         ''
-        + stdenv.lib.optionalString (targetPlatform.libc == "musl")
+        + stdenv.lib.optionalString (hostPlatform.libc == "musl")
         ''
             sed -i gcc/config/linux.h -e '1i#undef LOCAL_INCLUDE_DIR'
         ''

From 3f2619ad5221f155a0f17104247911b60a8cbeb3 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 12 Jan 2018 22:42:16 -0600
Subject: [PATCH 072/173] glibcLocales: not supported on non-glibc

---
 pkgs/top-level/all-packages.nix | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index e0d7d879cda..1a409e64f0d 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -8759,8 +8759,8 @@ with pkgs;
 
   libcCross = assert targetPlatform != buildPlatform; libcCrossChooser targetPlatform.libc;
 
-  # Only supported on Linux
-  glibcLocales = if stdenv.isLinux then callPackage ../development/libraries/glibc/locales.nix { } else null;
+  # Only supported on Linux, using glibc
+  glibcLocales = if hostPlatform.libc == "glibc" then callPackage ../development/libraries/glibc/locales.nix { } else null;
 
   glibcInfo = callPackage ../development/libraries/glibc/info.nix { };
 

From 38d17e33c35a7f49d913eb252f69daeacb0e0ff9 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 12 Jan 2018 23:38:40 -0600
Subject: [PATCH 073/173] libidn: don't run tests on musl

---
 pkgs/development/libraries/libidn/default.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/development/libraries/libidn/default.nix b/pkgs/development/libraries/libidn/default.nix
index df92693de5a..147d6bac2f9 100644
--- a/pkgs/development/libraries/libidn/default.nix
+++ b/pkgs/development/libraries/libidn/default.nix
@@ -10,7 +10,7 @@ stdenv.mkDerivation rec {
 
   outputs = [ "bin" "dev" "out" "info" "devdoc" ];
 
-  doCheck = (stdenv.buildPlatform == stdenv.hostPlatform) && !stdenv.isDarwin;
+  doCheck = !stdenv.isDarwin && !stdenv.hostPlatform.isMusl;
 
   hardeningDisable = [ "format" ];
 

From b47f502d64fd32cfdb10f2fa8ac543a861d8d5c5 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 12 Jan 2018 23:40:48 -0600
Subject: [PATCH 074/173] inetutils: fix build w/musl

---
 pkgs/tools/networking/inetutils/default.nix | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/pkgs/tools/networking/inetutils/default.nix b/pkgs/tools/networking/inetutils/default.nix
index ad058174cff..c5ecb5eb6da 100644
--- a/pkgs/tools/networking/inetutils/default.nix
+++ b/pkgs/tools/networking/inetutils/default.nix
@@ -15,7 +15,13 @@ stdenv.mkDerivation rec {
 
   buildInputs = [ ncurses /* for `talk' */ perl /* for `whois' */ help2man ];
 
-  configureFlags = "--with-ncurses-include-dir=${ncurses.dev}/include";
+  configureFlags = [ "--with-ncurses-include-dir=${ncurses.dev}/include" ]
+  ++ stdenv.lib.optionals stdenv.hostPlatform.isMusl [ # Musl doesn't define rcmd
+    "--disable-rcp"
+    "--disable-rsh"
+    "--disable-rlogin"
+    "--disable-rexec"
+  ];
 
   # Test fails with "UNIX socket name too long", probably because our
   # $TMPDIR is too long.
@@ -25,7 +31,7 @@ stdenv.mkDerivation rec {
   postInstall = ''
     # XXX: These programs are normally installed setuid but since it
     # fails, they end up being non-executable, hence this hack.
-    chmod +x $out/bin/{ping,ping6,rcp,rlogin,rsh,traceroute}
+    chmod +x $out/bin/{ping,ping6,${stdenv.lib.optionalString (!stdenv.hostPlatform.isMusl) ''rcp,rlogin,rsh,''}traceroute}
   '';
 
   meta = {

From bc999dcac5e0ca17c8dc1e51d12aac3c009a11ad Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 12 Jan 2018 23:54:17 -0600
Subject: [PATCH 075/173] libunistring: no tests on musl

---
 pkgs/development/libraries/libunistring/default.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/development/libraries/libunistring/default.nix b/pkgs/development/libraries/libunistring/default.nix
index e1b8c04b4ce..8d1f5115bdf 100644
--- a/pkgs/development/libraries/libunistring/default.nix
+++ b/pkgs/development/libraries/libunistring/default.nix
@@ -17,7 +17,7 @@ stdenv.mkDerivation rec {
     "--with-libiconv-prefix=${libiconv}"
   ];
 
-  doCheck = true;
+  doCheck = !stdenv.hostPlatform.isMusl;
 
   enableParallelBuilding = true;
 

From b905c0cb9c2b810a7e23c7edd6143dc0b11a0c20 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 5 Jan 2018 17:59:45 -0600
Subject: [PATCH 076/173] stfl: use libiconv fix on all non-glibc not just
 darwin

---
 pkgs/development/libraries/stfl/default.nix | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/pkgs/development/libraries/stfl/default.nix b/pkgs/development/libraries/stfl/default.nix
index 1430c3aa9c2..8a8680a498a 100644
--- a/pkgs/development/libraries/stfl/default.nix
+++ b/pkgs/development/libraries/stfl/default.nix
@@ -13,8 +13,9 @@ stdenv.mkDerivation rec {
   buildPhase = ''
     sed -i s/gcc/cc/g Makefile
     sed -i s%ncursesw/ncurses.h%ncurses.h% stfl_internals.h
-  '' + ( stdenv.lib.optionalString stdenv.isDarwin ''
+  '' + stdenv.lib.optionalString (stdenv.hostPlatform.libc != "glibc") ''
     sed -i 's/LDLIBS += -lncursesw/LDLIBS += -lncursesw -liconv/' Makefile
+  '' + ( stdenv.lib.optionalString stdenv.isDarwin ''
     sed -i s/-soname/-install_name/ Makefile
   '' ) + ''
     make

From 8fa7634819a8be91cec4ace7f64ac77e1c1caedb Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sat, 13 Jan 2018 10:39:31 -0600
Subject: [PATCH 077/173] xterm: patch from alpine to fix pty on musl

---
 pkgs/applications/misc/xterm/default.nix | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/pkgs/applications/misc/xterm/default.nix b/pkgs/applications/misc/xterm/default.nix
index 838043881e4..d807e8eb9ed 100644
--- a/pkgs/applications/misc/xterm/default.nix
+++ b/pkgs/applications/misc/xterm/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, xorg, ncurses, freetype, fontconfig, pkgconfig, makeWrapper
+{ stdenv, fetchurl, fetchpatch, xorg, ncurses, freetype, fontconfig, pkgconfig, makeWrapper
 , enableDecLocator ? true
 }:
 
@@ -20,7 +20,12 @@ stdenv.mkDerivation rec {
 
   patches = [
     ./sixel-256.support.patch
-  ];
+  ] ++ stdenv.lib.optional stdenv.hostPlatform.isMusl
+    (fetchpatch {
+      name = "posix-ptys.patch";
+      url = "https://git.alpinelinux.org/cgit/aports/plain/community/xterm/posix-ptys.patch?id=3aa532e77875fa1db18c7fcb938b16647031bcc1";
+      sha256 = "0czgnsxkkmkrk1idw69qxbprh0jb4sw3c24zpnqq2v76jkl7zvlr";
+    });
 
   configureFlags = [
     "--enable-wide-chars"

From 6c2e8b36cf1b8399a00b362411f19aa05677f7cf Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Thu, 7 Dec 2017 12:47:37 -0600
Subject: [PATCH 078/173] libnsl: Add alpine patches to fix w/musl

---
 pkgs/development/libraries/libnsl/cdefs.patch | 30 +++++++++++++
 pkgs/development/libraries/libnsl/default.nix |  2 +
 pkgs/development/libraries/libnsl/nis_h.patch | 45 +++++++++++++++++++
 3 files changed, 77 insertions(+)
 create mode 100644 pkgs/development/libraries/libnsl/cdefs.patch
 create mode 100644 pkgs/development/libraries/libnsl/nis_h.patch

diff --git a/pkgs/development/libraries/libnsl/cdefs.patch b/pkgs/development/libraries/libnsl/cdefs.patch
new file mode 100644
index 00000000000..dbbe800a347
--- /dev/null
+++ b/pkgs/development/libraries/libnsl/cdefs.patch
@@ -0,0 +1,30 @@
+--- a/src/rpcsvc/nislib.h
++++ b/src/rpcsvc/nislib.h
+@@ -19,6 +19,7 @@
+ #ifndef	__RPCSVC_NISLIB_H__
+ #define	__RPCSVC_NISLIB_H__
+ 
++#include <sys/cdefs.h>
+ #include <features.h>
+ 
+ __BEGIN_DECLS
+--- a/src/rpcsvc/ypclnt.h
++++ b/src/rpcsvc/ypclnt.h
+@@ -20,6 +20,7 @@
+ #ifndef	__RPCSVC_YPCLNT_H__
+ #define	__RPCSVC_YPCLNT_H__
+ 
++#include <sys/cdefs.h>
+ #include <features.h>
+ 
+ /* Some defines */
+--- a/src/rpcsvc/ypupd.h
++++ b/src/rpcsvc/ypupd.h
+@@ -33,6 +33,7 @@
+ #ifndef __RPCSVC_YPUPD_H__
+ #define __RPCSVC_YPUPD_H__
+ 
++#include <sys/cdefs.h>
+ #include <features.h>
+ 
+ #include <rpc/rpc.h>
diff --git a/pkgs/development/libraries/libnsl/default.nix b/pkgs/development/libraries/libnsl/default.nix
index d4af280c3cd..9e8a46b2e6b 100644
--- a/pkgs/development/libraries/libnsl/default.nix
+++ b/pkgs/development/libraries/libnsl/default.nix
@@ -14,6 +14,8 @@ stdenv.mkDerivation rec {
   nativeBuildInputs = [ autoreconfHook pkgconfig ];
   buildInputs = [ libtirpc ];
 
+  patches = stdenv.lib.optionals stdenv.hostPlatform.isMusl [ ./cdefs.patch ./nis_h.patch ];
+
   meta = with stdenv.lib; {
     description = "Client interface library for NIS(YP) and NIS+";
     homepage = https://github.com/thkukuk/libnsl;
diff --git a/pkgs/development/libraries/libnsl/nis_h.patch b/pkgs/development/libraries/libnsl/nis_h.patch
new file mode 100644
index 00000000000..199259df2e8
--- /dev/null
+++ b/pkgs/development/libraries/libnsl/nis_h.patch
@@ -0,0 +1,45 @@
+--- a/src/rpcsvc/nis.h
++++ b/src/rpcsvc/nis.h
+@@ -32,6 +32,7 @@
+ #ifndef _RPCSVC_NIS_H
+ #define _RPCSVC_NIS_H 1
+ 
++#include <sys/cdefs.h>
+ #include <features.h>
+ #include <rpc/rpc.h>
+ #include <rpcsvc/nis_tags.h>
+@@ -56,6 +57,34 @@
+  *                                              <kukuk@suse.de>
+  */
+ 
++#ifndef rawmemchr
++#define rawmemchr(s,c) memchr((s),(size_t)-1,(c))
++#endif
++
++#ifndef __asprintf
++#define __asprintf asprintf
++#endif
++
++#ifndef __mempcpy
++#define __mempcpy mempcpy
++#endif
++
++#ifndef __strtok_r
++#define __strtok_r strtok_r
++#endif
++
++#ifndef __always_inline
++#define __always_inline __attribute__((__always_inline__))
++#endif
++
++#ifndef TEMP_FAILURE_RETRY
++#define TEMP_FAILURE_RETRY(exp) ({ \
++typeof (exp) _rc; \
++ do { \
++  _rc = (exp); \
++ } while (_rc == -1 && errno == EINTR); \
++ _rc; })
++#endif
+ 
+ #ifndef __nis_object_h
+ #define __nis_object_h

From 4a5aa55d736bf80eae325d06f659c5c518ce5adc Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sat, 13 Jan 2018 10:56:33 -0600
Subject: [PATCH 079/173] ti-rpc: patch to fix w/musl

---
 pkgs/development/libraries/ti-rpc/default.nix | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/pkgs/development/libraries/ti-rpc/default.nix b/pkgs/development/libraries/ti-rpc/default.nix
index 0156c64306c..d34a6dca783 100644
--- a/pkgs/development/libraries/ti-rpc/default.nix
+++ b/pkgs/development/libraries/ti-rpc/default.nix
@@ -1,4 +1,4 @@
-{ fetchurl, stdenv, autoreconfHook, libkrb5 }:
+{ fetchurl, fetchpatch, stdenv, autoreconfHook, libkrb5 }:
 
 stdenv.mkDerivation rec {
   name = "libtirpc-1.0.2";
@@ -8,6 +8,12 @@ stdenv.mkDerivation rec {
     sha256 = "1xchbxy0xql7yl7z4n1icj8r7dmly46i22fvm00vdjq64zlmqg3j";
   };
 
+  patches = stdenv.lib.optional stdenv.hostPlatform.isMusl
+    (fetchpatch {
+      url = "https://raw.githubusercontent.com/openembedded/openembedded-core/2be873301420ec6ca2c70d899b7c49a7e2b0954d/meta/recipes-extended/libtirpc/libtirpc/0001-replace-__bzero-with-memset-API.patch";
+      sha256 = "1jmbn0j2bnjp0j9z5vzz5xiwyv3kd28w5pixbqsy2lz6q8nii7cf";
+    });
+
   postPatch = ''
     sed '1i#include <stdint.h>' -i src/xdr_sizeof.c
   '';

From 317ac441217944f614f51fb217c90e5d36241de6 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Wed, 8 Nov 2017 18:15:14 -0600
Subject: [PATCH 080/173] net-snmp: alpine patches

---
 pkgs/servers/monitoring/net-snmp/default.nix | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/pkgs/servers/monitoring/net-snmp/default.nix b/pkgs/servers/monitoring/net-snmp/default.nix
index 1d0b5fb8336..77dcfa43c43 100644
--- a/pkgs/servers/monitoring/net-snmp/default.nix
+++ b/pkgs/servers/monitoring/net-snmp/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, autoreconfHook, file, openssl, perl, unzip }:
+{ stdenv, fetchurl, fetchpatch, autoreconfHook, file, openssl, perl, unzip }:
 
 stdenv.mkDerivation rec {
   name = "net-snmp-5.7.3";
@@ -8,6 +8,19 @@ stdenv.mkDerivation rec {
     sha256 = "0gkss3zclm23zwpqfhddca8278id7pk6qx1mydpimdrrcndwgpz8";
   };
 
+  patches =
+    let fetchAlpinePatch = name: sha256: fetchpatch {
+      url = "https://git.alpinelinux.org/cgit/aports/plain/main/net-snmp/${name}?id=f25d3fb08341b60b6ccef424399f060dfcf3f1a5";
+      inherit name sha256;
+    };
+  in [
+    (fetchAlpinePatch "CVE-2015-5621.patch" "05098jyvd9ddr5q26z7scbbvk1bk6x4agpjm6pyprvpc1zpi0y09")
+    (fetchAlpinePatch "fix-Makefile-PL.patch" "14ilnkj3cr6mpi242hrmmmv8nv4dj0fdgn42qfk9aa7scwsc0lc7")
+    (fetchAlpinePatch "fix-includes.patch" "0zpkbb6k366qpq4dax5wknwprhwnhighcp402mlm7950d39zfa3m")
+    (fetchAlpinePatch "netsnmp-swinst-crash.patch" "0gh164wy6zfiwiszh58fsvr25k0ns14r3099664qykgpmickkqid")
+    (fetchAlpinePatch "remove-U64-typedef.patch" "1msxyhcqkvhqa03dwb50288g7f6nbrcd9cs036m9xc8jdgjb8k8j")
+  ];
+
   preConfigure =
     ''
       perlversion=$(perl -e 'use Config; print $Config{version};')

From 12e60c232a7691510dc8b13f069bd1c89e8eaaa7 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Wed, 8 Nov 2017 12:20:50 -0600
Subject: [PATCH 081/173] cvc4: patch up fpu_control usage

---
 .../science/logic/cvc4/default.nix            |  5 ++
 .../science/logic/cvc4/minisat-fenv.patch     | 65 +++++++++++++++++++
 2 files changed, 70 insertions(+)
 create mode 100644 pkgs/applications/science/logic/cvc4/minisat-fenv.patch

diff --git a/pkgs/applications/science/logic/cvc4/default.nix b/pkgs/applications/science/logic/cvc4/default.nix
index 6b213226635..25effdcd403 100644
--- a/pkgs/applications/science/logic/cvc4/default.nix
+++ b/pkgs/applications/science/logic/cvc4/default.nix
@@ -22,6 +22,11 @@ stdenv.mkDerivation rec {
     "--with-boost=${boost.dev}"
   ];
 
+  prePatch = ''
+    patch -p1 -i ${./minisat-fenv.patch} -d src/prop/minisat
+    patch -p1 -i ${./minisat-fenv.patch} -d src/prop/bvminisat
+  '';
+
   preConfigure = ''
     patchShebangs ./src/
   '';
diff --git a/pkgs/applications/science/logic/cvc4/minisat-fenv.patch b/pkgs/applications/science/logic/cvc4/minisat-fenv.patch
new file mode 100644
index 00000000000..686d5a1c5b4
--- /dev/null
+++ b/pkgs/applications/science/logic/cvc4/minisat-fenv.patch
@@ -0,0 +1,65 @@
+From 7f1016ceab9b0f57a935bd51ca6df3d18439b472 Mon Sep 17 00:00:00 2001
+From: Will Dietz <w@wdtz.org>
+Date: Tue, 17 Oct 2017 22:57:02 -0500
+Subject: [PATCH] use fenv instead of non-standard fpu_control
+
+---
+ core/Main.cc   | 8 ++++++--
+ simp/Main.cc   | 8 ++++++--
+ utils/System.h | 2 +-
+ 3 files changed, 13 insertions(+), 5 deletions(-)
+
+diff --git a/core/Main.cc b/core/Main.cc
+index 2b0d97b..8ad95fb 100644
+--- a/core/Main.cc
++++ b/core/Main.cc
+@@ -78,8 +78,12 @@ int main(int argc, char** argv)
+         // printf("This is MiniSat 2.0 beta\n");
+         
+ #if defined(__linux__)
+-        fpu_control_t oldcw, newcw;
+-        _FPU_GETCW(oldcw); newcw = (oldcw & ~_FPU_EXTENDED) | _FPU_DOUBLE; _FPU_SETCW(newcw);
++        fenv_t fenv;
++
++        fegetenv(&fenv);
++        fenv.__control_word &= ~0x300; /* _FPU_EXTENDED */
++        fenv.__control_word |= 0x200; /* _FPU_DOUBLE */
++        fesetenv(&fenv);
+         printf("WARNING: for repeatability, setting FPU to use double precision\n");
+ #endif
+         // Extra options:
+diff --git a/simp/Main.cc b/simp/Main.cc
+index 2804d7f..39bfb71 100644
+--- a/simp/Main.cc
++++ b/simp/Main.cc
+@@ -79,8 +79,12 @@ int main(int argc, char** argv)
+         // printf("This is MiniSat 2.0 beta\n");
+         
+ #if defined(__linux__)
+-        fpu_control_t oldcw, newcw;
+-        _FPU_GETCW(oldcw); newcw = (oldcw & ~_FPU_EXTENDED) | _FPU_DOUBLE; _FPU_SETCW(newcw);
++        fenv_t fenv;
++
++        fegetenv(&fenv);
++        fenv.__control_word &= ~0x300; /* _FPU_EXTENDED */
++        fenv.__control_word |= 0x200; /* _FPU_DOUBLE */
++        fesetenv(&fenv);
+         printf("WARNING: for repeatability, setting FPU to use double precision\n");
+ #endif
+         // Extra options:
+diff --git a/utils/System.h b/utils/System.h
+index 1758192..c0ad13a 100644
+--- a/utils/System.h
++++ b/utils/System.h
+@@ -22,7 +22,7 @@ OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWA
+ #define Minisat_System_h
+ 
+ #if defined(__linux__)
+-#include <fpu_control.h>
++#include <fenv.h>
+ #endif
+ 
+ #include "mtl/IntTypes.h"
+-- 
+2.14.2
+

From cbdd20878ead3d3d9c4b169f8123e637e426339d Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Wed, 8 Nov 2017 12:16:16 -0600
Subject: [PATCH 082/173] cvc4: build in parallel

---
 pkgs/applications/science/logic/cvc4/default.nix | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/pkgs/applications/science/logic/cvc4/default.nix b/pkgs/applications/science/logic/cvc4/default.nix
index 25effdcd403..403eff216f5 100644
--- a/pkgs/applications/science/logic/cvc4/default.nix
+++ b/pkgs/applications/science/logic/cvc4/default.nix
@@ -31,6 +31,8 @@ stdenv.mkDerivation rec {
     patchShebangs ./src/
   '';
 
+  enableParallelBuilding = true;
+
   meta = with stdenv.lib; {
     description = "A high-performance theorem prover and SMT solver";
     homepage    = http://cvc4.cs.nyu.edu/web/;

From 32b472dd9d69f7cb5fff32058767c72d0d9799e3 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sun, 16 Apr 2017 21:10:39 -0500
Subject: [PATCH 083/173] audit: tweaks to make musl compat

---
 pkgs/os-specific/linux/audit/default.nix | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/pkgs/os-specific/linux/audit/default.nix b/pkgs/os-specific/linux/audit/default.nix
index 4adc321d930..390bab849c2 100644
--- a/pkgs/os-specific/linux/audit/default.nix
+++ b/pkgs/os-specific/linux/audit/default.nix
@@ -1,5 +1,5 @@
 {
-  stdenv, buildPackages, fetchurl,
+  stdenv, buildPackages, fetchurl, fetchpatch,
   enablePython ? false, python ? null,
 }:
 
@@ -27,6 +27,22 @@ stdenv.mkDerivation rec {
 
   enableParallelBuilding = true;
 
+  patches = stdenv.lib.optional stdenv.hostPlatform.isMusl [
+    (fetchpatch {
+      url = "https://git.alpinelinux.org/cgit/aports/plain/main/audit/0002-auparse-remove-use-of-rawmemchr.patch?id=3e57180fdf3f90c30a25aea44f57846efc93a696";
+      name = "0002-auparse-remove-use-of-rawmemchr.patch";
+      sha256 = "1caaqbfgb2rq3ria5bz4n8x30ihgihln6w9w9a46k62ba0wh9rkz";
+    })
+    (fetchpatch {
+      url = "https://git.alpinelinux.org/cgit/aports/plain/main/audit/0003-all-get-rid-of-strndupa.patch?id=3e57180fdf3f90c30a25aea44f57846efc93a696";
+      name = "0003-all-get-rid-of-strndupa.patch";
+      sha256 = "1ddrm6a0ijrf7caw1wpw2kkbjp2lkxkmc16v51j5j7dvdalc6591";
+    })
+  ];
+
+  prePatch = ''
+    sed -i 's,#include <sys/poll.h>,#include <poll.h>\n#include <limits.h>,' audisp/audispd.c
+  '';
   meta = {
     description = "Audit Library";
     homepage = http://people.redhat.com/sgrubb/audit/;

From f96d0d114e8d0788d3328f86d055f54a421ba2ed Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Mon, 30 Oct 2017 16:25:05 -0500
Subject: [PATCH 084/173] libnet: Add patch from alpine to fix build on musl

---
 pkgs/development/libraries/libnet/default.nix |  2 ++
 .../libraries/libnet/fix-includes.patch       | 29 +++++++++++++++++++
 2 files changed, 31 insertions(+)
 create mode 100644 pkgs/development/libraries/libnet/fix-includes.patch

diff --git a/pkgs/development/libraries/libnet/default.nix b/pkgs/development/libraries/libnet/default.nix
index a93c16d784d..f642ad5ccfa 100644
--- a/pkgs/development/libraries/libnet/default.nix
+++ b/pkgs/development/libraries/libnet/default.nix
@@ -9,6 +9,8 @@ stdenv.mkDerivation rec {
     sha256 = "0qsapqa7dzq9f6lb19kzilif0pj82b64fjv5bq086hflb9w81hvj";
   };
 
+  patches = [ ./fix-includes.patch ];
+
   meta = with stdenv.lib; {
     homepage = https://github.com/sam-github/libnet;
     description = "Portable framework for low-level network packet construction";
diff --git a/pkgs/development/libraries/libnet/fix-includes.patch b/pkgs/development/libraries/libnet/fix-includes.patch
new file mode 100644
index 00000000000..5eb86bc37b7
--- /dev/null
+++ b/pkgs/development/libraries/libnet/fix-includes.patch
@@ -0,0 +1,29 @@
+--- libnet-1.1.6.orig/src/libnet_link_linux.c
++++ libnet-1.1.6/src/libnet_link_linux.c
+@@ -30,26 +30,15 @@
+ #include <sys/time.h>
+ 
+ #include <net/if.h>
+-#if (__GLIBC__)
+ #include <netinet/if_ether.h>
+ #include <net/if_arp.h>
+-#else
+-#include <linux/if_arp.h>
+-#include <linux/if_ether.h>
+-#endif
+ 
+ #if (HAVE_PACKET_SOCKET)
+ #ifndef SOL_PACKET
+ #define SOL_PACKET 263
+ #endif  /* SOL_PACKET */
+-#if __GLIBC__ >= 2 && __GLIBC_MINOR >= 1
+ #include <netpacket/packet.h>
+ #include <net/ethernet.h>     /* the L2 protocols */
+-#else
+-#include <asm/types.h>
+-#include <linux/if_packet.h>
+-#include <linux/if_ether.h>   /* The L2 protocols */
+-#endif
+ #endif  /* HAVE_PACKET_SOCKET */
+ 
+ #include "../include/libnet.h"

From 29b012e17715cd5ea4cb3ca297febce9d038180d Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sun, 22 Oct 2017 21:02:39 -0500
Subject: [PATCH 085/173] tbb: mallinfo patch to fix build w/musl, fix w/clang

---
 pkgs/development/libraries/tbb/default.nix    |  2 +
 .../libraries/tbb/glibc-struct-mallinfo.patch | 43 +++++++++++++++++++
 2 files changed, 45 insertions(+)
 create mode 100644 pkgs/development/libraries/tbb/glibc-struct-mallinfo.patch

diff --git a/pkgs/development/libraries/tbb/default.nix b/pkgs/development/libraries/tbb/default.nix
index 13b1970866e..182fbb35d49 100644
--- a/pkgs/development/libraries/tbb/default.nix
+++ b/pkgs/development/libraries/tbb/default.nix
@@ -16,6 +16,8 @@ with stdenv.lib; stdenv.mkDerivation rec {
     optional (stdver != null) "stdver=${stdver}"
   );
 
+  patches = stdenv.lib.optional stdenv.hostPlatform.isMusl ./glibc-struct-mallinfo.patch;
+
   installPhase = ''
     mkdir -p $out/{lib,share/doc}
     cp "build/"*release*"/"*${stdenv.hostPlatform.extensions.sharedLibrary}* $out/lib/
diff --git a/pkgs/development/libraries/tbb/glibc-struct-mallinfo.patch b/pkgs/development/libraries/tbb/glibc-struct-mallinfo.patch
new file mode 100644
index 00000000000..64056ecb146
--- /dev/null
+++ b/pkgs/development/libraries/tbb/glibc-struct-mallinfo.patch
@@ -0,0 +1,43 @@
+From b577153a10c98f4e13405dc93ea2ab1a7b990e07 Mon Sep 17 00:00:00 2001
+From: David Huffman <storedbox+alpine@outlook.com>
+Date: Wed, 6 Jan 2016 07:09:30 -0500
+Subject: [PATCH] hard-code glibc's definition of struct mallinfo
+
+---
+ src/tbbmalloc/proxy.h | 20 ++++++++++++++++++++
+ 1 file changed, 20 insertions(+)
+
+diff --git a/src/tbbmalloc/proxy.h b/src/tbbmalloc/proxy.h
+index 781cadc..e1ea1ae 100644
+--- a/src/tbbmalloc/proxy.h
++++ b/src/tbbmalloc/proxy.h
+@@ -32,6 +32,26 @@
+ 
+ #include <stddef.h>
+ 
++// The following definition was taken from /usr/include/malloc.h as provided by
++// the glibc-devel-2.19-17.4.x86_64 package on openSUSE Leap 42.1; it is
++// made available under the GNU Lesser General Public License v2.1 or later.
++// See <https://www.gnu.org/licenses>.
++//
++// Copyright (C) 1996-2014 Free Software Foundation, Inc.
++struct mallinfo
++{
++  int arena;    /* non-mmapped space allocated from system */
++  int ordblks;  /* number of free chunks */
++  int smblks;   /* number of fastbin blocks */
++  int hblks;    /* number of mmapped regions */
++  int hblkhd;   /* space in mmapped regions */
++  int usmblks;  /* maximum total allocated space */
++  int fsmblks;  /* space available in freed fastbin blocks */
++  int uordblks; /* total allocated space */
++  int fordblks; /* total free space */
++  int keepcost; /* top-most, releasable (via malloc_trim) space */
++};
++
+ extern "C" {
+     void * scalable_malloc(size_t size);
+     void * scalable_calloc(size_t nobj, size_t size);
+-- 
+2.6.2
+

From 77c58c9de14bac5b22ce20e7a704059c12d89c46 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 3 Oct 2017 18:04:30 -0500
Subject: [PATCH 086/173] libusb: fix header usage on musl, patch from Alpine

---
 pkgs/development/libraries/libusb/default.nix       |  2 ++
 pkgs/development/libraries/libusb/fix-headers.patch | 10 ++++++++++
 2 files changed, 12 insertions(+)
 create mode 100644 pkgs/development/libraries/libusb/fix-headers.patch

diff --git a/pkgs/development/libraries/libusb/default.nix b/pkgs/development/libraries/libusb/default.nix
index 1fd3cb39bbf..4704a1e5c33 100644
--- a/pkgs/development/libraries/libusb/default.nix
+++ b/pkgs/development/libraries/libusb/default.nix
@@ -14,6 +14,8 @@ stdenv.mkDerivation {
     sha256 = "0nn5icrfm9lkhzw1xjvaks9bq3w6mjg86ggv3fn7kgi4nfvg8kj0";
   };
 
+  patches = stdenv.lib.optional stdenv.hostPlatform.isMusl ./fix-headers.patch;
+
   meta = {
     platforms = stdenv.lib.platforms.unix;
   };
diff --git a/pkgs/development/libraries/libusb/fix-headers.patch b/pkgs/development/libraries/libusb/fix-headers.patch
new file mode 100644
index 00000000000..ea9cbc34978
--- /dev/null
+++ b/pkgs/development/libraries/libusb/fix-headers.patch
@@ -0,0 +1,10 @@
+--- libusb-compat-0.1.5.orig/libusb/usb.h
++++ libusb-compat-0.1.5/libusb/usb.h
+@@ -25,6 +25,7 @@
+ #ifndef __USB_H__
+ #define __USB_H__
+ 
++#include <sys/types.h>
+ #include <unistd.h>
+ #include <stdlib.h>
+ #include <limits.h>

From 08e70f7a0c45df7f110994b7ba27214d5b60ca25 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 22 Sep 2017 13:45:03 -0500
Subject: [PATCH 087/173] ppp: grab alpine patch, fix build on musl

---
 pkgs/tools/networking/ppp/default.nix         |   1 +
 .../networking/ppp/musl-fix-headers.patch     | 137 ++++++++++++++++++
 2 files changed, 138 insertions(+)
 create mode 100644 pkgs/tools/networking/ppp/musl-fix-headers.patch

diff --git a/pkgs/tools/networking/ppp/default.nix b/pkgs/tools/networking/ppp/default.nix
index d07770260ff..570e85c3281 100644
--- a/pkgs/tools/networking/ppp/default.nix
+++ b/pkgs/tools/networking/ppp/default.nix
@@ -23,6 +23,7 @@ stdenv.mkDerivation rec {
         url = "https://anonscm.debian.org/git/collab-maint/pkg-ppp.git/plain/debian/patches/rc_mksid-no-buffer-overflow?h=debian/2.4.7-1%2b4";
         sha256 = "1dk00j7bg9nfgskw39fagnwv1xgsmyv0xnkd6n1v5gy0psw0lvqh";
       })
+      ./musl-fix-headers.patch
     ];
 
   buildInputs = [ libpcap ];
diff --git a/pkgs/tools/networking/ppp/musl-fix-headers.patch b/pkgs/tools/networking/ppp/musl-fix-headers.patch
new file mode 100644
index 00000000000..d24f6289684
--- /dev/null
+++ b/pkgs/tools/networking/ppp/musl-fix-headers.patch
@@ -0,0 +1,137 @@
+diff --git a/include/net/ppp_defs.h b/include/net/ppp_defs.h
+index b06eda5..dafa36c 100644
+--- a/include/net/ppp_defs.h
++++ b/include/net/ppp_defs.h
+@@ -38,6 +38,8 @@
+ #ifndef _PPP_DEFS_H_
+ #define _PPP_DEFS_H_
+ 
++#include <sys/time.h>
++
+ /*
+  * The basic PPP frame.
+  */
+diff --git a/pppd/Makefile.linux b/pppd/Makefile.linux
+index a74c914..7acd2cf 100644
+--- a/pppd/Makefile.linux
++++ b/pppd/Makefile.linux
+@@ -126,7 +126,7 @@ LIBS	+= -lcrypt
+ endif
+ 
+ ifdef USE_LIBUTIL
+-CFLAGS	+= -DHAVE_LOGWTMP=1
++#CFLAGS	+= -DHAVE_LOGWTMP=1
+ LIBS	+= -lutil
+ endif
+ 
+diff --git a/pppd/magic.h b/pppd/magic.h
+index c81213b..305aece 100644
+--- a/pppd/magic.h
++++ b/pppd/magic.h
+@@ -42,6 +42,8 @@
+  * $Id: magic.h,v 1.5 2003/06/11 23:56:26 paulus Exp $
+  */
+ 
++#include <sys/cdefs.h>
++
+ void magic_init __P((void));	/* Initialize the magic number generator */
+ u_int32_t magic __P((void));	/* Returns the next magic number */
+ 
+diff --git a/pppd/plugins/rp-pppoe/if.c b/pppd/plugins/rp-pppoe/if.c
+index 91e9a57..9c0fac3 100644
+--- a/pppd/plugins/rp-pppoe/if.c
++++ b/pppd/plugins/rp-pppoe/if.c
+@@ -30,10 +30,6 @@ static char const RCSID[] =
+ #include <linux/if_packet.h>
+ #endif
+ 
+-#ifdef HAVE_NET_ETHERNET_H
+-#include <net/ethernet.h>
+-#endif
+-
+ #ifdef HAVE_ASM_TYPES_H
+ #include <asm/types.h>
+ #endif
+diff --git a/pppd/plugins/rp-pppoe/plugin.c b/pppd/plugins/rp-pppoe/plugin.c
+index a8c2bb4..ca34d79 100644
+--- a/pppd/plugins/rp-pppoe/plugin.c
++++ b/pppd/plugins/rp-pppoe/plugin.c
+@@ -46,7 +46,6 @@ static char const RCSID[] =
+ #include <unistd.h>
+ #include <fcntl.h>
+ #include <signal.h>
+-#include <net/ethernet.h>
+ #include <net/if_arp.h>
+ #include <linux/ppp_defs.h>
+ #include <linux/if_pppox.h>
+diff --git a/pppd/plugins/rp-pppoe/pppoe-discovery.c b/pppd/plugins/rp-pppoe/pppoe-discovery.c
+index 3d3bf4e..b5f82d3 100644
+--- a/pppd/plugins/rp-pppoe/pppoe-discovery.c
++++ b/pppd/plugins/rp-pppoe/pppoe-discovery.c
+@@ -27,10 +27,6 @@
+ #include <linux/if_packet.h>
+ #endif
+ 
+-#ifdef HAVE_NET_ETHERNET_H
+-#include <net/ethernet.h>
+-#endif
+-
+ #ifdef HAVE_ASM_TYPES_H
+ #include <asm/types.h>
+ #endif
+@@ -55,6 +51,8 @@ void die(int status)
+ 	exit(status);
+ }
+ 
++#define error(x...) fprintf(stderr, x)
++
+ /* Initialize frame types to RFC 2516 values.  Some broken peers apparently
+    use different frame types... sigh... */
+ 
+diff --git a/pppd/plugins/rp-pppoe/pppoe.h b/pppd/plugins/rp-pppoe/pppoe.h
+index 9ab2eee..4d68147 100644
+--- a/pppd/plugins/rp-pppoe/pppoe.h
++++ b/pppd/plugins/rp-pppoe/pppoe.h
+@@ -86,18 +86,6 @@ typedef unsigned long UINT32_t;
+ 
+ #include <netinet/in.h>
+ 
+-#ifdef HAVE_NETINET_IF_ETHER_H
+-#include <sys/types.h>
+-
+-#ifdef HAVE_SYS_SOCKET_H
+-#include <sys/socket.h>
+-#endif
+-#ifndef HAVE_SYS_DLPI_H
+-#include <netinet/if_ether.h>
+-#endif
+-#endif
+-
+-
+ 
+ /* Ethernet frame types according to RFC 2516 */
+ #define ETH_PPPOE_DISCOVERY 0x8863
+diff --git a/pppd/sys-linux.c b/pppd/sys-linux.c
+index 6d71530..86d224e 100644
+--- a/pppd/sys-linux.c
++++ b/pppd/sys-linux.c
+@@ -102,19 +102,11 @@
+ #define MAX_ADDR_LEN 7
+ #endif
+ 
+-#if __GLIBC__ >= 2
+ #include <asm/types.h>		/* glibc 2 conflicts with linux/types.h */
+ #include <net/if.h>
+ #include <net/if_arp.h>
+ #include <net/route.h>
+ #include <netinet/if_ether.h>
+-#else
+-#include <linux/types.h>
+-#include <linux/if.h>
+-#include <linux/if_arp.h>
+-#include <linux/route.h>
+-#include <linux/if_ether.h>
+-#endif
+ #include <netinet/in.h>
+ #include <arpa/inet.h>
+ 

From 54b3967edc6d83e5c43f4d100a51dc8be8d95b33 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 22 Sep 2017 11:34:46 -0500
Subject: [PATCH 088/173] libnl: use patch from gentoo-musl, fixes! \o/

---
 pkgs/os-specific/linux/libnl/default.nix | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/pkgs/os-specific/linux/libnl/default.nix b/pkgs/os-specific/linux/libnl/default.nix
index 81a3af54628..f66df8163ff 100644
--- a/pkgs/os-specific/linux/libnl/default.nix
+++ b/pkgs/os-specific/linux/libnl/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, lib, fetchFromGitHub, autoreconfHook, bison, flex, pkgconfig }:
+{ stdenv, lib, fetchFromGitHub, fetchpatch, autoreconfHook, bison, flex, pkgconfig }:
 
 let version = "3.3.0"; in
 stdenv.mkDerivation {
@@ -13,6 +13,12 @@ stdenv.mkDerivation {
 
   outputs = [ "bin" "dev" "out" "man" ];
 
+  patches = stdenv.lib.optional stdenv.hostPlatform.isMusl
+    (fetchpatch {
+      url = "https://raw.githubusercontent.com/gentoo/musl/48d2a28710ae40877fd3e178ead1fb1bb0baa62c/dev-libs/libnl/files/libnl-3.3.0_rc1-musl.patch";
+      sha256 = "0dd7xxikib201i99k2if066hh7gwf2i4ffckrjplq6lr206jn00r";
+    });
+
   nativeBuildInputs = [ autoreconfHook bison flex pkgconfig ];
 
   meta = with lib; {

From 1ac2b797459e76432219c4d2ec9c1aea9cca4a8b Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Wed, 17 Jan 2018 17:28:09 -0600
Subject: [PATCH 089/173] squid{,4}: only enable netfilter bits on non-musl

---
 pkgs/servers/squid/4.nix       | 4 +---
 pkgs/servers/squid/default.nix | 4 +---
 2 files changed, 2 insertions(+), 6 deletions(-)

diff --git a/pkgs/servers/squid/4.nix b/pkgs/servers/squid/4.nix
index 4a4502a6939..777e910038a 100644
--- a/pkgs/servers/squid/4.nix
+++ b/pkgs/servers/squid/4.nix
@@ -27,9 +27,7 @@ stdenv.mkDerivation rec {
     "--enable-removal-policies=lru,heap"
     "--enable-delay-pools"
     "--enable-x-accelerator-vary"
-  ] ++ stdenv.lib.optionals stdenv.isLinux [
-    "--enable-linux-netfilter"
-  ];
+  ] ++ stdenv.lib.optional (stdenv.isLinux && !stdenv.hostPlatform.isMusl) "--enable-linux-netfilter";
 
   meta = with stdenv.lib; {
     description = "A caching proxy for the Web supporting HTTP, HTTPS, FTP, and more";
diff --git a/pkgs/servers/squid/default.nix b/pkgs/servers/squid/default.nix
index 95f4233df10..8d39fbbcef4 100644
--- a/pkgs/servers/squid/default.nix
+++ b/pkgs/servers/squid/default.nix
@@ -36,9 +36,7 @@ stdenv.mkDerivation rec {
     "--enable-removal-policies=lru,heap"
     "--enable-delay-pools"
     "--enable-x-accelerator-vary"
-  ] ++ stdenv.lib.optionals stdenv.isLinux [
-    "--enable-linux-netfilter"
-  ];
+  ] ++ stdenv.lib.optional (stdenv.isLinux && !stdenv.hostPlatform.isMusl) "--enable-linux-netfilter";
 
   meta = with stdenv.lib; {
     description = "A caching proxy for the Web supporting HTTP, HTTPS, FTP, and more";

From f2aae455148b80bc7fa557efd3cafeab1604553d Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 8 Sep 2017 18:23:22 -0500
Subject: [PATCH 090/173] git: Disable use of <sys/poll.h> on musl, disable
 gettext

---
 .../version-management/git-and-tools/git/default.nix         | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/pkgs/applications/version-management/git-and-tools/git/default.nix b/pkgs/applications/version-management/git-and-tools/git/default.nix
index 8b64e2d375b..afb3716f04c 100644
--- a/pkgs/applications/version-management/git-and-tools/git/default.nix
+++ b/pkgs/applications/version-management/git-and-tools/git/default.nix
@@ -57,7 +57,10 @@ stdenv.mkDerivation {
   makeFlags = "prefix=\${out} PERL_PATH=${perl}/bin/perl SHELL_PATH=${stdenv.shell} "
       + (if pythonSupport then "PYTHON_PATH=${python}/bin/python" else "NO_PYTHON=1")
       + (if stdenv.isSunOS then " INSTALL=install NO_INET_NTOP= NO_INET_PTON=" else "")
-      + (if stdenv.isDarwin then " NO_APPLE_COMMON_CRYPTO=1" else " sysconfdir=/etc/ ");
+      + (if stdenv.isDarwin then " NO_APPLE_COMMON_CRYPTO=1" else " sysconfdir=/etc/ ")
+      # XXX: USE_PCRE2 might be useful in general, look into it
+      # XXX other alpine options?
+      + (if stdenv.hostPlatform.isMusl then "NO_SYS_POLL_H=1 NO_GETTEXT=YesPlease" else "");
 
   # build git-credential-osxkeychain if darwin
   postBuild = stdenv.lib.optionalString stdenv.isDarwin ''

From 49b7e4337f59b0433a079b93876e559726904a4a Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 8 Sep 2017 00:27:26 -0500
Subject: [PATCH 091/173] libexecinfo: init at 1.1, patches from Alpine

Not sure if ALLVM should use this, but might be good
to have it on hand just-in-case :).

Maybe part of libnone?
---
 .../libraries/libexecinfo/10-execinfo.patch   | 64 +++++++++++++++++++
 .../libexecinfo/20-define-gnu-source.patch    | 24 +++++++
 .../libexecinfo/30-linux-makefile.patch       | 44 +++++++++++++
 .../libraries/libexecinfo/default.nix         | 25 ++++++++
 pkgs/top-level/all-packages.nix               |  2 +
 5 files changed, 159 insertions(+)
 create mode 100644 pkgs/development/libraries/libexecinfo/10-execinfo.patch
 create mode 100644 pkgs/development/libraries/libexecinfo/20-define-gnu-source.patch
 create mode 100644 pkgs/development/libraries/libexecinfo/30-linux-makefile.patch
 create mode 100644 pkgs/development/libraries/libexecinfo/default.nix

diff --git a/pkgs/development/libraries/libexecinfo/10-execinfo.patch b/pkgs/development/libraries/libexecinfo/10-execinfo.patch
new file mode 100644
index 00000000000..03b5af0ca20
--- /dev/null
+++ b/pkgs/development/libraries/libexecinfo/10-execinfo.patch
@@ -0,0 +1,64 @@
+--- execinfo.c.orig
++++ execinfo.c
+@@ -69,7 +69,8 @@
+ char **
+ backtrace_symbols(void *const *buffer, int size)
+ {
+-    int i, clen, alen, offset;
++    size_t clen, alen;
++    int i, offset;
+     char **rval;
+     char *cp;
+     Dl_info info;
+@@ -78,7 +79,6 @@
+     rval = malloc(clen);
+     if (rval == NULL)
+         return NULL;
+-    (char **)cp = &(rval[size]);
+     for (i = 0; i < size; i++) {
+         if (dladdr(buffer[i], &info) != 0) {
+             if (info.dli_sname == NULL)
+@@ -92,14 +92,14 @@
+                    2 +                      /* " <" */
+                    strlen(info.dli_sname) + /* "function" */
+                    1 +                      /* "+" */
+-                   D10(offset) +            /* "offset */
++                   10 +                     /* "offset */
+                    5 +                      /* "> at " */
+                    strlen(info.dli_fname) + /* "filename" */
+                    1;                       /* "\0" */
+             rval = realloc_safe(rval, clen + alen);
+             if (rval == NULL)
+                 return NULL;
+-            snprintf(cp, alen, "%p <%s+%d> at %s",
++            snprintf((char *) rval + clen, alen, "%p <%s+%d> at %s",
+               buffer[i], info.dli_sname, offset, info.dli_fname);
+         } else {
+             alen = 2 +                      /* "0x" */
+@@ -108,12 +108,15 @@
+             rval = realloc_safe(rval, clen + alen);
+             if (rval == NULL)
+                 return NULL;
+-            snprintf(cp, alen, "%p", buffer[i]);
++            snprintf((char *) rval + clen, alen, "%p", buffer[i]);
+         }
+-        rval[i] = cp;
+-        cp += alen;
++        rval[i] = (char *) clen;
++        clen += alen;
+     }
+ 
++    for (i = 0; i < size; i++)
++        rval[i] += (long) rval;
++
+     return rval;
+ }
+ 
+@@ -155,6 +158,6 @@
+                 return;
+             snprintf(buf, len, "%p\n", buffer[i]);
+         }
+-        write(fd, buf, len - 1);
++        write(fd, buf, strlen(buf));
+     }
+ }
diff --git a/pkgs/development/libraries/libexecinfo/20-define-gnu-source.patch b/pkgs/development/libraries/libexecinfo/20-define-gnu-source.patch
new file mode 100644
index 00000000000..0ba1fc1f7e6
--- /dev/null
+++ b/pkgs/development/libraries/libexecinfo/20-define-gnu-source.patch
@@ -0,0 +1,24 @@
+--- execinfo.c.orig
++++ execinfo.c
+@@ -26,6 +26,7 @@
+  * $Id: execinfo.c,v 1.3 2004/07/19 05:21:09 sobomax Exp $
+  */
+ 
++#define _GNU_SOURCE
+ #include <sys/types.h>
+ #include <sys/uio.h>
+ #include <dlfcn.h>
+--- stacktraverse.c.orig
++++ stacktraverse.c
+@@ -1,3 +1,4 @@
++#define _GNU_SOURCE
+ #include <stddef.h>
+ 
+ #include "stacktraverse.h"
+--- test.c.orig
++++ test.c
+@@ -1,3 +1,4 @@
++#define _GNU_SOURCE
+ #include <stdio.h>
+ #include <stdlib.h>
+ 
diff --git a/pkgs/development/libraries/libexecinfo/30-linux-makefile.patch b/pkgs/development/libraries/libexecinfo/30-linux-makefile.patch
new file mode 100644
index 00000000000..3bc89279547
--- /dev/null
+++ b/pkgs/development/libraries/libexecinfo/30-linux-makefile.patch
@@ -0,0 +1,44 @@
+--- Makefile.orig
++++ Makefile
+@@ -23,24 +23,25 @@
+ # SUCH DAMAGE.
+ #
+ # $Id: Makefile,v 1.3 2004/07/19 05:19:55 sobomax Exp $
++#
++# Linux Makefile by Matt Smith <mcs@darkregion.net>, 2011/01/04
+ 
+-LIB=	execinfo
++CC=cc
++AR=ar
++EXECINFO_CFLAGS=$(CFLAGS) -O2 -pipe -fno-strict-aliasing -std=gnu99 -fstack-protector -c
++EXECINFO_LDFLAGS=$(LDFLAGS)
+ 
+-SRCS=	stacktraverse.c stacktraverse.h execinfo.c execinfo.h
++all: static dynamic
+ 
+-INCS=	execinfo.h
++static:
++	$(CC) $(EXECINFO_CFLAGS) $(EXECINFO_LDFLAGS) stacktraverse.c
++	$(CC) $(EXECINFO_CFLAGS) $(EXECINFO_LDFLAGS) execinfo.c
++	$(AR) rcs libexecinfo.a stacktraverse.o execinfo.o
+ 
+-SHLIB_MAJOR=	1
+-SHLIB_MINOR=	0
++dynamic:
++	$(CC) -fpic -DPIC $(EXECINFO_CFLAGS) $(EXECINFO_LDFLAGS) stacktraverse.c -o stacktraverse.So
++	$(CC) -fpic -DPIC $(EXECINFO_CFLAGS) $(EXECINFO_LDFLAGS) execinfo.c -o execinfo.So
++	$(CC) -shared -Wl,-soname,libexecinfo.so.1 -o libexecinfo.so.1 stacktraverse.So execinfo.So
+ 
+-NOPROFILE=	yes
+-
+-DPADD=		${LIBM}
+-LDADD=		-lm
+-
+-#WARNS?=	4
+-
+-#stacktraverse.c: gen.py
+-#	./gen.py > stacktraverse.c
+-
+-.include <bsd.lib.mk>
++clean:
++	rm -rf *.o *.So *.a *.so
diff --git a/pkgs/development/libraries/libexecinfo/default.nix b/pkgs/development/libraries/libexecinfo/default.nix
new file mode 100644
index 00000000000..15c4cb2661f
--- /dev/null
+++ b/pkgs/development/libraries/libexecinfo/default.nix
@@ -0,0 +1,25 @@
+{ stdenv, fetchurl }:
+
+stdenv.mkDerivation rec {
+  name = "libexecinfo-${version}";
+  version = "1.1";
+
+  src = fetchurl {
+    url = "http://distcache.freebsd.org/local-distfiles/itetcu/${name}.tar.bz2";
+    sha256 = "07wvlpc1jk1sj4k5w53ml6wagh0zm9kv2l1jngv8xb7xww9ik8n9";
+  };
+
+  patches = [
+    ./10-execinfo.patch
+    ./20-define-gnu-source.patch
+    ./30-linux-makefile.patch
+  ];
+
+  patchFlags = "-p0";
+
+  installPhase = ''
+    install -Dm644 execinfo.h stacktraverse.h -t $out/include
+    install -Dm755 libexecinfo.{a,so.1} -t $out/lib
+    ln -s $out/lib/libexecinfo.so{.1,}
+  '';
+}
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index 1a409e64f0d..89e1243146b 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -9484,6 +9484,8 @@ with pkgs;
 
   libetpan = callPackage ../development/libraries/libetpan { };
 
+  libexecinfo = callPackage ../development/libraries/libexecinfo { };
+
   libfaketime = callPackage ../development/libraries/libfaketime { };
 
   libfakekey = callPackage ../development/libraries/libfakekey { };

From c8081971f4d35feac0342cf64dbfe0d603f99b25 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sat, 13 Jan 2018 12:10:21 -0600
Subject: [PATCH 092/173] openblas: NO_AFFINITY=1 fix for musl

---
 pkgs/development/libraries/science/math/openblas/default.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/development/libraries/science/math/openblas/default.nix b/pkgs/development/libraries/science/math/openblas/default.nix
index 42eaf71942e..5b3fd57551d 100644
--- a/pkgs/development/libraries/science/math/openblas/default.nix
+++ b/pkgs/development/libraries/science/math/openblas/default.nix
@@ -115,7 +115,7 @@ stdenv.mkDerivation {
       "NUM_THREADS=64"
       "INTERFACE64=${if blas64 then "1" else "0"}"
       "NO_STATIC=1"
-    ]
+    ] ++ stdenv.lib.optional (stdenv.hostPlatform.libc == "musl") "NO_AFFINITY=1"
     ++ mapAttrsToList (var: val: var + "=" + val) config;
 
   doCheck = true;

From 8dfe068f862a3b2e336a246655000aa73949201d Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sat, 13 Jan 2018 12:20:03 -0600
Subject: [PATCH 093/173] openblas: patch to fix on non-glibc

---
 .../libraries/science/math/openblas/default.nix          | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/pkgs/development/libraries/science/math/openblas/default.nix b/pkgs/development/libraries/science/math/openblas/default.nix
index 5b3fd57551d..07d2a04c5e2 100644
--- a/pkgs/development/libraries/science/math/openblas/default.nix
+++ b/pkgs/development/libraries/science/math/openblas/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, gfortran, perl, which, config, coreutils
+{ stdenv, fetchurl, fetchpatch, gfortran, perl, which, config, coreutils
 # Most packages depending on openblas expect integer width to match
 # pointer width, but some expect to use 32-bit integers always
 # (for compatibility with reference BLAS).
@@ -118,6 +118,13 @@ stdenv.mkDerivation {
     ] ++ stdenv.lib.optional (stdenv.hostPlatform.libc == "musl") "NO_AFFINITY=1"
     ++ mapAttrsToList (var: val: var + "=" + val) config;
 
+  patches = stdenv.lib.optional (stdenv.hostPlatform.libc != "glibc")
+    # https://github.com/xianyi/OpenBLAS/pull/1247
+    (fetchpatch {
+      url = "https://github.com/xianyi/OpenBLAS/commit/88a35ff457f55e527e0e8a503a0dc61976c1846d.patch";
+      sha256 = "1a3qrhvl5hp06c53fjqghq4zgf6ls7narm06l0shcvs57hznh09n";
+    });
+
   doCheck = true;
   checkTarget = "tests";
 

From e1e6e94c5de064b5573f9bc9efbe0b9b679d9c0f Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Mon, 4 Sep 2017 09:56:34 -0500
Subject: [PATCH 094/173] webkitgtk: Add musl patch from Alpine re:execinfo,
 use libc++ on ALLVM

---
 pkgs/development/libraries/webkitgtk/2.4.nix  |  2 ++
 .../libraries/webkitgtk/fix-execinfo.patch    | 20 +++++++++++++++++++
 2 files changed, 22 insertions(+)
 create mode 100644 pkgs/development/libraries/webkitgtk/fix-execinfo.patch

diff --git a/pkgs/development/libraries/webkitgtk/2.4.nix b/pkgs/development/libraries/webkitgtk/2.4.nix
index 6669b562400..18d20bf9c78 100644
--- a/pkgs/development/libraries/webkitgtk/2.4.nix
+++ b/pkgs/development/libraries/webkitgtk/2.4.nix
@@ -53,6 +53,8 @@ stdenv.mkDerivation rec {
     ./quartz-webcore.patch
     ./libc++.patch
     ./plugin-none.patch
+  ] ++ optionals stdenv.hostPlatform.isMusl [
+    ./fix-execinfo.patch
   ];
 
   configureFlags = with stdenv.lib; [
diff --git a/pkgs/development/libraries/webkitgtk/fix-execinfo.patch b/pkgs/development/libraries/webkitgtk/fix-execinfo.patch
new file mode 100644
index 00000000000..eb825312f3b
--- /dev/null
+++ b/pkgs/development/libraries/webkitgtk/fix-execinfo.patch
@@ -0,0 +1,20 @@
+--- webkitgtk-2.2.0.orig/Source/WTF/wtf/Assertions.cpp
++++ webkitgtk-2.2.0/Source/WTF/wtf/Assertions.cpp
+@@ -64,7 +64,7 @@
+ #include <windows.h>
+ #endif
+ 
+-#if OS(DARWIN) || (OS(LINUX) && !defined(__UCLIBC__))
++#if OS(DARWIN) || (OS(LINUX) && defined(__GLIBC__) && !defined(__UCLIBC__))
+ #include <cxxabi.h>
+ #include <dlfcn.h>
+ #include <execinfo.h>
+@@ -242,7 +242,7 @@
+ 
+ void WTFGetBacktrace(void** stack, int* size)
+ {
+-#if OS(DARWIN) || (OS(LINUX) && !defined(__UCLIBC__))
++#if OS(DARWIN) || (OS(LINUX) && defined(__GLIBC__) && !defined(__UCLIBC__))
+     *size = backtrace(stack, *size);
+ #elif OS(WINDOWS) && !OS(WINCE)
+     // The CaptureStackBackTrace function is available in XP, but it is not defined

From c7399423d733a56f0f923321b905a2863ec5db45 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Wed, 31 May 2017 08:56:29 -0500
Subject: [PATCH 095/173] armagetronad: enable parallel building

---
 pkgs/games/armagetronad/default.nix | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/pkgs/games/armagetronad/default.nix b/pkgs/games/armagetronad/default.nix
index 21f545100b3..0fb75b3d345 100644
--- a/pkgs/games/armagetronad/default.nix
+++ b/pkgs/games/armagetronad/default.nix
@@ -15,6 +15,8 @@ stdenv.mkDerivation {
 
   NIX_LDFLAGS = [ "-lSDL_image" ];
 
+  enableParallelBuilding = true;
+
   configureFlags = [ "--disable-etc" ];
   buildInputs = [ SDL SDL_image libxml2 libjpeg libpng mesa zlib ];
 

From 3b5e1131a7e4bff57a6870f7065fd25826595a24 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 25 Aug 2017 08:39:03 -0500
Subject: [PATCH 096/173] libapparmor: Add musl patches from Alpine

(update: drop patches no longer needed on updated version)
---
 pkgs/os-specific/linux/apparmor/default.nix | 27 ++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)

diff --git a/pkgs/os-specific/linux/apparmor/default.nix b/pkgs/os-specific/linux/apparmor/default.nix
index 29e1357d38a..0bb5561e957 100644
--- a/pkgs/os-specific/linux/apparmor/default.nix
+++ b/pkgs/os-specific/linux/apparmor/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, makeWrapper, autoreconfHook
+{ stdenv, fetchurl, fetchpatch, makeWrapper, autoreconfHook
 , pkgconfig, which
 , flex, bison
 , linuxHeaders ? stdenv.cc.libc.linuxHeaders
@@ -35,6 +35,27 @@ let
     substituteInPlace ./common/Make.rules --replace "/usr/share/man" "share/man"
   '';
 
+  # use 'if c then x else null' to avoid rebuilding
+  # patches = stdenv.lib.optionals stdenv.hostPlatform.isMusl [
+  patches = if stdenv.hostPlatform.isMusl then [
+    (fetchpatch {
+      url = "https://git.alpinelinux.org/cgit/aports/plain/testing/apparmor/0002-Provide-missing-secure_getenv-and-scandirat-function.patch?id=74b8427cc21f04e32030d047ae92caa618105b53";
+      name = "0002-Provide-missing-secure_getenv-and-scandirat-function.patch";
+      sha256 = "0pj1bzifghxwxlc39j8hyy17dkjr9fk64kkj94ayymyprz4i4nac";
+    })
+    (fetchpatch {
+      url = "https://git.alpinelinux.org/cgit/aports/plain/testing/apparmor/0003-Added-missing-typedef-definitions-on-parser.patch?id=74b8427cc21f04e32030d047ae92caa618105b53";
+      name = "0003-Added-missing-typedef-definitions-on-parser.patch";
+      sha256 = "0yyaqz8jlmn1bm37arggprqz0njb4lhjni2d9c8qfqj0kll0bam0";
+    })
+    (fetchpatch {
+      url = "https://git.alpinelinux.org/cgit/aports/plain/testing/apparmor/0007-Do-not-build-install-vim-file-with-utils-package.patch?id=74b8427cc21f04e32030d047ae92caa618105b53";
+      name = "0007-Do-not-build-install-vim-file-with-utils-package.patch";
+      sha256 = "1m4dx901biqgnr4w4wz8a2z9r9dxyw7wv6m6mqglqwf2lxinqmp4";
+    })
+    # (alpine patches {1,4,5,6,8} are needed for apparmor 2.11, but not 2.12)
+  ] else null;
+
   # FIXME: convert these to a single multiple-outputs package?
 
   libapparmor = stdenv.mkDerivation {
@@ -63,6 +84,8 @@ let
       substituteInPlace ./libraries/libapparmor/src/Makefile.am --replace "/usr/include/netinet/in.h" "${stdenv.cc.libc.dev}/include/netinet/in.h"
       substituteInPlace ./libraries/libapparmor/src/Makefile.in --replace "/usr/include/netinet/in.h" "${stdenv.cc.libc.dev}/include/netinet/in.h"
     '';
+    inherit patches;
+
     postPatch = "cd ./libraries/libapparmor";
     configureFlags = "--with-python --with-perl";
 
@@ -90,6 +113,7 @@ let
     ];
 
     prePatch = prePatchCommon;
+    inherit patches;
     postPatch = "cd ./utils";
     makeFlags = ''LANGS='';
     installFlags = ''DESTDIR=$(out) BINDIR=$(out)/bin VIM_INSTALL_PATH=$(out)/share PYPREFIX='';
@@ -145,6 +169,7 @@ let
       ## techdoc.pdf still doesn't build ...
       substituteInPlace ./parser/Makefile --replace "manpages htmlmanpages pdf" "manpages htmlmanpages"
     '';
+    inherit patches;
     postPatch = "cd ./parser";
     makeFlags = ''LANGS= USE_SYSTEM=1 INCLUDEDIR=${libapparmor}/include'';
     installFlags = ''DESTDIR=$(out) DISTRO=unknown'';

From aa00d5370824683587ec0c33fe6d880d4d77c466 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sun, 13 Aug 2017 15:32:50 -0500
Subject: [PATCH 097/173] openssl 1.1.0: disable 'async' bits relating to
 setcontext/etc on musl

---
 pkgs/development/libraries/openssl/default.nix | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/pkgs/development/libraries/openssl/default.nix b/pkgs/development/libraries/openssl/default.nix
index 775e6056dff..cce205bb2c8 100644
--- a/pkgs/development/libraries/openssl/default.nix
+++ b/pkgs/development/libraries/openssl/default.nix
@@ -24,6 +24,12 @@ let
       ++ optional (versionOlder version "1.0.2" && hostPlatform.isDarwin)
            ./darwin-arch.patch;
 
+  postPatch = if (versionAtLeast version "1.1.0" && stdenv.isMusl) then ''
+    substituteInPlace crypto/async/arch/async_posix.h \
+      --replace '!defined(__ANDROID__) && !defined(__OpenBSD__)' \
+                '!defined(__ANDROID__) && !defined(__OpenBSD__) && 0'
+  '' else null;
+
     outputs = [ "bin" "dev" "out" "man" ];
     setOutputFlags = false;
     separateDebugInfo = hostPlatform.isLinux;

From 6593d882a9cd34602b58c5dc4afa90d5754e0e3c Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sat, 13 Jan 2018 12:51:32 -0600
Subject: [PATCH 098/173] openssl: fix cross, ensure 'Configure' has shebang
 patched

---
 pkgs/development/libraries/openssl/default.nix | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/pkgs/development/libraries/openssl/default.nix b/pkgs/development/libraries/openssl/default.nix
index cce205bb2c8..e72efe4bb0c 100644
--- a/pkgs/development/libraries/openssl/default.nix
+++ b/pkgs/development/libraries/openssl/default.nix
@@ -55,6 +55,10 @@ let
     # TODO(@Ericson2314): Make unconditional on mass rebuild
     ${if buildPlatform != hostPlatform then "configurePlatforms" else null} = [];
 
+    preConfigure = ''
+      patchShebangs Configure
+    '';
+
     configureFlags = [
       "shared"
       "--libdir=lib"

From cccf48ca0c5eeb9bf9815ce821e13c6390c32024 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sun, 11 Feb 2018 16:57:48 -0600
Subject: [PATCH 099/173] openssl: isMusl

---
 pkgs/development/libraries/openssl/default.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/development/libraries/openssl/default.nix b/pkgs/development/libraries/openssl/default.nix
index e72efe4bb0c..af6a540b97c 100644
--- a/pkgs/development/libraries/openssl/default.nix
+++ b/pkgs/development/libraries/openssl/default.nix
@@ -24,7 +24,7 @@ let
       ++ optional (versionOlder version "1.0.2" && hostPlatform.isDarwin)
            ./darwin-arch.patch;
 
-  postPatch = if (versionAtLeast version "1.1.0" && stdenv.isMusl) then ''
+  postPatch = if (versionAtLeast version "1.1.0" && stdenv.hostPlatform.isMusl) then ''
     substituteInPlace crypto/async/arch/async_posix.h \
       --replace '!defined(__ANDROID__) && !defined(__OpenBSD__)' \
                 '!defined(__ANDROID__) && !defined(__OpenBSD__) && 0'

From 5d3af4225074cecbf43fc70740e616ee03406f02 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sat, 13 Jan 2018 13:06:23 -0600
Subject: [PATCH 100/173] unbound: don't build twice w/musl, second time fails
 :(

---
 pkgs/tools/networking/unbound/default.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/tools/networking/unbound/default.nix b/pkgs/tools/networking/unbound/default.nix
index 614447cd5ec..cc2f994d0d5 100644
--- a/pkgs/tools/networking/unbound/default.nix
+++ b/pkgs/tools/networking/unbound/default.nix
@@ -27,7 +27,7 @@ stdenv.mkDerivation rec {
 
   installFlags = [ "configfile=\${out}/etc/unbound/unbound.conf" ];
 
-  preFixup = stdenv.lib.optionalString stdenv.isLinux
+  preFixup = stdenv.lib.optionalString (stdenv.isLinux && !stdenv.hostPlatform.isMusl) # XXX: revisit
     # Build libunbound again, but only against nettle instead of openssl.
     # This avoids gnutls.out -> unbound.lib -> openssl.out.
     # There was some problem with this on Darwin; let's not complicate non-Linux.

From e2ef21d9b5dddc0996c453f65a686ff871b6ac94 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sun, 28 May 2017 07:48:17 -0500
Subject: [PATCH 101/173] lm_sensors: add musl patch from alpine.

Doesn't fix build yet due to use of unconventional
object file extensions (which shouldn't matter but do).
---
 pkgs/os-specific/linux/lm-sensors/default.nix |  2 +
 .../linux/lm-sensors/musl-fix-includes.patch  | 62 +++++++++++++++++++
 2 files changed, 64 insertions(+)
 create mode 100644 pkgs/os-specific/linux/lm-sensors/musl-fix-includes.patch

diff --git a/pkgs/os-specific/linux/lm-sensors/default.nix b/pkgs/os-specific/linux/lm-sensors/default.nix
index 25b64499838..4f35d546538 100644
--- a/pkgs/os-specific/linux/lm-sensors/default.nix
+++ b/pkgs/os-specific/linux/lm-sensors/default.nix
@@ -20,6 +20,8 @@ stdenv.mkDerivation rec {
   buildInputs = [ bison flex which perl ]
    ++ stdenv.lib.optional sensord rrdtool;
 
+  patches = [ ./musl-fix-includes.patch ];
+
   preBuild = ''
     makeFlagsArray=(PREFIX=$out ETCDIR=$out/etc
     ${stdenv.lib.optionalString sensord "PROG_EXTRA=sensord"})
diff --git a/pkgs/os-specific/linux/lm-sensors/musl-fix-includes.patch b/pkgs/os-specific/linux/lm-sensors/musl-fix-includes.patch
new file mode 100644
index 00000000000..501f2dd762c
--- /dev/null
+++ b/pkgs/os-specific/linux/lm-sensors/musl-fix-includes.patch
@@ -0,0 +1,62 @@
+--- lm_sensors-3.3.4.orig/prog/dump/isadump.c
++++ lm_sensors-3.3.4/prog/dump/isadump.c
+@@ -36,13 +36,7 @@
+ #include "util.h"
+ #include "superio.h"
+ 
+-
+-/* To keep glibc2 happy */
+-#if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ >= 0
+ #include <sys/io.h>
+-#else
+-#include <asm/io.h>
+-#endif
+ 
+ #ifdef __powerpc__
+ unsigned long isa_io_base = 0; /* XXX for now */
+--- lm_sensors-3.3.4.orig/prog/dump/isaset.c
++++ lm_sensors-3.3.4/prog/dump/isaset.c
+@@ -32,13 +32,7 @@
+ #include <string.h>
+ #include "util.h"
+ 
+-
+-/* To keep glibc2 happy */
+-#if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ >= 0
+ #include <sys/io.h>
+-#else
+-#include <asm/io.h>
+-#endif
+ 
+ #ifdef __powerpc__
+ unsigned long isa_io_base = 0; /* XXX for now */
+--- lm_sensors-3.3.4.orig/prog/dump/superio.c
++++ lm_sensors-3.3.4/prog/dump/superio.c
+@@ -20,12 +20,7 @@
+ */
+ 
+ #include <stdlib.h>
+-
+-#if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ >= 0
+ #include <sys/io.h>
+-#else
+-#include <asm/io.h>
+-#endif
+ 
+ #include "superio.h"
+ 
+--- lm_sensors-3.3.4.orig/prog/dump/util.c
++++ lm_sensors-3.3.4/prog/dump/util.c
+@@ -11,12 +11,7 @@
+ #include <stdio.h>
+ #include "util.h"
+ 
+-/* To keep glibc2 happy */
+-#if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ >= 0
+ #include <sys/io.h>
+-#else
+-#include <asm/io.h>
+-#endif
+ 
+ /* Return 1 if we should continue, 0 if we should abort */
+ int user_ack(int def)

From 0153599164e7ef2266c9de4979369daadb3a3cad Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sat, 27 May 2017 18:40:27 -0500
Subject: [PATCH 102/173] libdaemon: Add patch to fix musl build

---
 pkgs/development/libraries/libdaemon/default.nix    |  8 ++++++--
 .../libraries/libdaemon/fix-includes.patch          | 13 +++++++++++++
 2 files changed, 19 insertions(+), 2 deletions(-)
 create mode 100644 pkgs/development/libraries/libdaemon/fix-includes.patch

diff --git a/pkgs/development/libraries/libdaemon/default.nix b/pkgs/development/libraries/libdaemon/default.nix
index af832a70a73..59e576fd392 100644
--- a/pkgs/development/libraries/libdaemon/default.nix
+++ b/pkgs/development/libraries/libdaemon/default.nix
@@ -1,6 +1,6 @@
 {stdenv, fetchurl}:
 
-stdenv.mkDerivation rec {
+stdenv.mkDerivation (rec {
   name = "libdaemon-0.14";
 
   src = fetchurl {
@@ -24,4 +24,8 @@ stdenv.mkDerivation rec {
     platforms = stdenv.lib.platforms.unix;
     maintainers = [ ];
   };
-}
+} // stdenv.lib.optionalAttrs stdenv.hostPlatform.isMusl {
+  # This patch should be applied unconditionally, but doing so will cause mass rebuild.
+  patches = ./fix-includes.patch;
+})
+
diff --git a/pkgs/development/libraries/libdaemon/fix-includes.patch b/pkgs/development/libraries/libdaemon/fix-includes.patch
new file mode 100644
index 00000000000..51c5133afed
--- /dev/null
+++ b/pkgs/development/libraries/libdaemon/fix-includes.patch
@@ -0,0 +1,13 @@
+--- libdaemon-0.14.orig/examples/testd.c
++++ libdaemon-0.14/examples/testd.c
+@@ -21,9 +21,9 @@
+ #include <signal.h>
+ #include <errno.h>
+ #include <string.h>
++#include <unistd.h>
+ #include <sys/types.h>
+ #include <sys/time.h>
+-#include <sys/unistd.h>
+ #include <sys/select.h>
+ 
+ #include <libdaemon/dfork.h>

From 3564ae11bc48587938d70bd7a364e5b50b41ae4a Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sun, 28 May 2017 07:25:22 -0500
Subject: [PATCH 103/173] libmemcached: add musl patch from alpine

---
 .../libraries/libmemcached/default.nix        |  3 +-
 .../libraries/libmemcached/musl-fixes.patch   | 58 +++++++++++++++++++
 2 files changed, 60 insertions(+), 1 deletion(-)
 create mode 100644 pkgs/development/libraries/libmemcached/musl-fixes.patch

diff --git a/pkgs/development/libraries/libmemcached/default.nix b/pkgs/development/libraries/libmemcached/default.nix
index 086ba8f32d2..6d895290e2c 100644
--- a/pkgs/development/libraries/libmemcached/default.nix
+++ b/pkgs/development/libraries/libmemcached/default.nix
@@ -17,7 +17,8 @@ stdenv.mkDerivation rec {
     ++ stdenv.lib.optional stdenv.isDarwin (fetchpatch {
       url = "https://raw.githubusercontent.com/Homebrew/homebrew/bfd4a0a4626b61c2511fdf573bcbbc6bbe86340e/Library/Formula/libmemcached.rb";
       sha256 = "1gjf3vd7hiyzxjvlg2zfc3y2j0lyr6nhbws4xb5dmin3csyp8qb8";
-    });
+    })
+    ++ stdenv.lib.optional stdenv.hostPlatform.isMusl ./musl-fixes.patch;
 
   buildInputs = [ libevent ];
   propagatedBuildInputs = [ cyrus_sasl ];
diff --git a/pkgs/development/libraries/libmemcached/musl-fixes.patch b/pkgs/development/libraries/libmemcached/musl-fixes.patch
new file mode 100644
index 00000000000..eb2a6bc980e
--- /dev/null
+++ b/pkgs/development/libraries/libmemcached/musl-fixes.patch
@@ -0,0 +1,58 @@
+diff --git a/libhashkit/fnv_64.cc b/libhashkit/fnv_64.cc
+index 68e4dd0..64656b7 100644
+--- a/libhashkit/fnv_64.cc
++++ b/libhashkit/fnv_64.cc
+@@ -37,8 +37,9 @@
+ 
+ 
+ #include <libhashkit/common.h>
++#include <limits.h>
+ 
+-#if __WORDSIZE == 64 && defined(HAVE_FNV64_HASH)
++#if (LONG_BITS == 64) && defined(HAVE_FNV64_HASH)
+ 
+ /* FNV hash'es lifted from Dustin Sallings work */
+ static uint64_t FNV_64_INIT= 0xcbf29ce484222325;
+diff --git a/libhashkit/has.cc b/libhashkit/has.cc
+index 843e32e..4153e5e 100644
+--- a/libhashkit/has.cc
++++ b/libhashkit/has.cc
+@@ -37,6 +37,7 @@
+ 
+ 
+ #include <libhashkit/common.h>
++#include <limits.h>
+ 
+ bool libhashkit_has_algorithm(const hashkit_hash_algorithm_t algo)
+ {
+@@ -44,7 +45,7 @@ bool libhashkit_has_algorithm(const hashkit_hash_algorithm_t algo)
+   {
+   case HASHKIT_HASH_FNV1_64:
+   case HASHKIT_HASH_FNV1A_64:
+-#if __WORDSIZE == 64 && defined(HAVE_FNV64_HASH)
++#if (LONG_BITS == 64) && defined(HAVE_FNV64_HASH)
+     return true;
+ #else
+     return false;
+diff --git a/libtest/cmdline.cc b/libtest/cmdline.cc
+index 29a22de..161c646 100644
+--- a/libtest/cmdline.cc
++++ b/libtest/cmdline.cc
+@@ -61,7 +61,7 @@ using namespace libtest;
+ #include <algorithm>
+ #include <stdexcept>
+ 
+-#ifndef __USE_GNU
++#ifndef _GNU_SOURCE
+ static char **environ= NULL;
+ #endif
+ 
+@@ -201,7 +201,7 @@ Application::error_t Application::run(const char *args[])
+ 
+   fatal_assert(posix_spawnattr_setsigmask(&spawnattr, &mask) == 0);
+ 
+-#if defined(POSIX_SPAWN_USEVFORK) || defined(__linux__)
++#if defined(POSIX_SPAWN_USEVFORK) || defined(__GLIBC__)
+   // Use USEVFORK on linux
+   flags |= POSIX_SPAWN_USEVFORK;
+ #endif

From cd50057a6106a3f230ae8d08c50ba48dbd6b0328 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sat, 13 Jan 2018 13:17:11 -0600
Subject: [PATCH 104/173] webrtc-audio-processing: don't use execinfo w/musl

---
 pkgs/development/libraries/webrtc-audio-processing/default.nix | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/pkgs/development/libraries/webrtc-audio-processing/default.nix b/pkgs/development/libraries/webrtc-audio-processing/default.nix
index f5d49290484..b5a3aed91d9 100644
--- a/pkgs/development/libraries/webrtc-audio-processing/default.nix
+++ b/pkgs/development/libraries/webrtc-audio-processing/default.nix
@@ -12,6 +12,8 @@ stdenv.mkDerivation rec {
   # signal_processing/filter_ar_fast_q12_armv7.S:88: Error: selected processor does not support `sbfx r11,r6,#12,#16' in ARM mode
   patchPhase = stdenv.lib.optionalString stdenv.isArm ''
     substituteInPlace configure --replace 'armv7*|armv8*' 'disabled'
+  '' + stdenv.lib.optionalString stdenv.hostPlatform.isMusl ''
+    substituteInPlace webrtc/base/checks.cc --replace 'defined(__UCLIBC__)' 1
   '';
 
   meta = with stdenv.lib; {

From be2997353de11a06b58f0a5f0184ca1218ba41b0 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sat, 13 Jan 2018 13:34:27 -0600
Subject: [PATCH 105/173] agrep: patch to fix on musl, darwin

---
 pkgs/tools/text/agrep/default.nix | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/pkgs/tools/text/agrep/default.nix b/pkgs/tools/text/agrep/default.nix
index 5d6eed458c0..10d3a47cc85 100644
--- a/pkgs/tools/text/agrep/default.nix
+++ b/pkgs/tools/text/agrep/default.nix
@@ -12,6 +12,10 @@ stdenv.mkDerivation rec {
     sha256 = "14addnwspdf2mxpqyrw8b84bb2257y43g5ccy4ipgrr91fmxq2sk";
   };
 
+  # Related: https://github.com/Wikinaut/agrep/pull/11
+  prePatch = stdenv.lib.optionalString (stdenv.hostPlatform.isMusl || stdenv.isDarwin) ''
+    sed -i '1i#include <sys/stat.h>' checkfil.c newmgrep.c recursiv.c
+  '';
   installPhase = ''
     install -Dm 555 agrep -t "$out/bin"
     install -Dm 444 docs/* -t "$out/doc"

From 6ae1bd450295b57ddce03402893f3bdfdedac8ff Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sat, 13 Jan 2018 13:36:44 -0600
Subject: [PATCH 106/173] go: don't require glibc (.static), dunno if go works
 w/musl

---
 pkgs/development/compilers/go/1.8.nix | 3 ++-
 pkgs/development/compilers/go/1.9.nix | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/pkgs/development/compilers/go/1.8.nix b/pkgs/development/compilers/go/1.8.nix
index e23d742b0c3..7775a17a646 100644
--- a/pkgs/development/compilers/go/1.8.nix
+++ b/pkgs/development/compilers/go/1.8.nix
@@ -38,7 +38,8 @@ stdenv.mkDerivation rec {
   nativeBuildInputs = [ perl which pkgconfig patch makeWrapper ]
     ++ optionals stdenv.isLinux [ procps ];
   buildInputs = [ cacert pcre ]
-    ++ optionals stdenv.isLinux [ stdenv.glibc.out stdenv.glibc.static ];
+    ++ optionals stdenv.isLinux [ stdenv.cc.libc.out ]
+    ++ optionals (stdenv.hostPlatform.libc == "glibc") [ stdenv.cc.libc.static ];
   propagatedBuildInputs = optionals stdenv.isDarwin [ Security Foundation ];
 
   hardeningDisable = [ "all" ];
diff --git a/pkgs/development/compilers/go/1.9.nix b/pkgs/development/compilers/go/1.9.nix
index 56091791d71..f1017d4a48c 100644
--- a/pkgs/development/compilers/go/1.9.nix
+++ b/pkgs/development/compilers/go/1.9.nix
@@ -38,7 +38,8 @@ stdenv.mkDerivation rec {
   nativeBuildInputs = [ perl which pkgconfig patch makeWrapper ]
     ++ optionals stdenv.isLinux [ procps ];
   buildInputs = [ cacert pcre ]
-    ++ optionals stdenv.isLinux [ stdenv.glibc.out stdenv.glibc.static ];
+    ++ optionals stdenv.isLinux [ stdenv.cc.libc.out ]
+    ++ optionals (stdenv.hostPlatform.libc == "glibc") [ stdenv.cc.libc.static ];
   propagatedBuildInputs = optionals stdenv.isDarwin [ Security Foundation ];
 
   hardeningDisable = [ "all" ];

From ab92d028f6210f737517ffe56c7ff0a75f2ce0fe Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sat, 13 Jan 2018 13:45:20 -0600
Subject: [PATCH 107/173] python-cffi: disable tests on musl :3

---
 pkgs/development/python-modules/cffi/default.nix | 1 +
 1 file changed, 1 insertion(+)

diff --git a/pkgs/development/python-modules/cffi/default.nix b/pkgs/development/python-modules/cffi/default.nix
index 28d4a36aca7..5e186a8f2f8 100644
--- a/pkgs/development/python-modules/cffi/default.nix
+++ b/pkgs/development/python-modules/cffi/default.nix
@@ -32,6 +32,7 @@ if isPyPy then null else buildPythonPackage rec {
   # The tests use -Werror but with python3.6 clang detects some unreachable code.
   NIX_CFLAGS_COMPILE = stdenv.lib.optionals stdenv.cc.isClang [ "-Wno-unused-command-line-argument" "-Wno-unreachable-code" ];
 
+  doCheck = !stdenv.hostPlatform.isMusl; # TODO: Investigate
   checkPhase = ''
     py.test
   '';

From 4a605161b37c010175a441b6ac0829ff1738aed2 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 6 Sep 2016 13:44:46 -0500
Subject: [PATCH 108/173] iperf: Fix build with musl.

---
 pkgs/tools/networking/iperf/3.nix | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/pkgs/tools/networking/iperf/3.nix b/pkgs/tools/networking/iperf/3.nix
index d69b52a5001..b9e47ecd7f9 100644
--- a/pkgs/tools/networking/iperf/3.nix
+++ b/pkgs/tools/networking/iperf/3.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, openssl }:
+{ stdenv, fetchurl, openssl, fetchpatch }:
 
 stdenv.mkDerivation rec {
   name = "iperf-3.3";
@@ -10,6 +10,23 @@ stdenv.mkDerivation rec {
 
   buildInputs = [ openssl ];
 
+  preConfigure = stdenv.lib.optionalString stdenv.hostPlatform.isMusl ''
+    NIX_CFLAGS_COMPILE+=" -D_GNU_SOURCE"
+  '';
+
+  patches = stdenv.lib.optionals stdenv.hostPlatform.isMusl [
+    (fetchpatch {
+      url = "http://git.alpinelinux.org/cgit/aports/plain/main/iperf3/build-fixes.patch";
+      name = "fix-musl-build.patch";
+      sha256 = "0zvfjnqdldh6rc6qggyb310swdnl9qk0m3z1kklnqzgjsh8dskvl";
+    })
+    (fetchpatch {
+      url = "http://git.alpinelinux.org/cgit/aports/plain/main/iperf3/remove-pg-flags.patch";
+      name = "remove-pg-flags.patch";
+      sha256 = "0lnczhass24kgq59drgdipnhjnw4l1cy6gqza7f2ah1qr4q104rm";
+    })
+];
+
   postInstall = ''
     ln -s iperf3 $out/bin/iperf
   '';

From 7eba45d06321691827802b6d4da718d8883eca7c Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sat, 13 Jan 2018 14:18:02 -0600
Subject: [PATCH 109/173] gcc5: patch for musl compat/support

---
 pkgs/development/compilers/gcc/5/default.nix | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/pkgs/development/compilers/gcc/5/default.nix b/pkgs/development/compilers/gcc/5/default.nix
index 47973d9422c..b9ca8696d4e 100644
--- a/pkgs/development/compilers/gcc/5/default.nix
+++ b/pkgs/development/compilers/gcc/5/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, targetPackages, fetchurl, noSysDirs
+{ stdenv, targetPackages, fetchurl, fetchpatch, noSysDirs
 , langC ? true, langCC ? true, langFortran ? false
 , langObjC ? targetPlatform.isDarwin
 , langObjCpp ? targetPlatform.isDarwin
@@ -74,7 +74,11 @@ let version = "5.5.0";
 
       # This could be applied unconditionally but I don't want to cause a full
       # Linux rebuild.
-      ++ optional stdenv.cc.isClang ./libcxx38-and-above.patch;
+      ++ optional stdenv.cc.isClang ./libcxx38-and-above.patch
+      ++ optional stdenv.hostPlatform.isMusl (fetchpatch {
+        url = https://raw.githubusercontent.com/richfelker/musl-cross-make/e84b1bd1fc12a3def33111ca6df522cd6e5ec361/patches/gcc-5.3.0/0001-musl.diff;
+        sha256 = "0pppbf8myi2kjhm3z3479ihn1cm60kycfv60gj8yy1bs0pl1qcfm";
+      });
 
     javaEcj = fetchurl {
       # The `$(top_srcdir)/ecj.jar' file is automatically picked up at

From 2974ed9266174ca579ff3094f9d6d1da71348046 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 17 Oct 2017 21:08:48 -0500
Subject: [PATCH 110/173] avy: fixes to work with clang and musl

---
 .../science/logic/avy/default.nix             | 20 +++++-
 .../science/logic/avy/glucose-fenv.patch      | 65 +++++++++++++++++++
 .../science/logic/avy/minisat-fenv.patch      | 65 +++++++++++++++++++
 3 files changed, 149 insertions(+), 1 deletion(-)
 create mode 100644 pkgs/applications/science/logic/avy/glucose-fenv.patch
 create mode 100644 pkgs/applications/science/logic/avy/minisat-fenv.patch

diff --git a/pkgs/applications/science/logic/avy/default.nix b/pkgs/applications/science/logic/avy/default.nix
index 379224c73f8..218006e15d5 100644
--- a/pkgs/applications/science/logic/avy/default.nix
+++ b/pkgs/applications/science/logic/avy/default.nix
@@ -12,7 +12,25 @@ stdenv.mkDerivation rec {
   };
 
   buildInputs = [ cmake zlib boost.out boost.dev ];
-  NIX_CFLAGS_COMPILE = [ "-Wno-narrowing" ];
+  NIX_CFLAGS_COMPILE = [ "-Wno-narrowing" ]
+    # Squelch endless stream of warnings on same few things
+    ++ stdenv.lib.optionals stdenv.cc.isClang [
+      "-Wno-empty-body"
+      "-Wno-tautological-compare"
+      "-Wc++11-compat-deprecated-writable-strings"
+      "-Wno-deprecated"
+    ];
+
+  prePatch = ''
+    sed -i -e '1i#include <stdint.h>' abc/src/bdd/dsd/dsd.h
+    substituteInPlace abc/src/bdd/dsd/dsd.h --replace \
+               '((Child = Dsd_NodeReadDec(Node,Index))>=0);' \
+               '((intptr_t)(Child = Dsd_NodeReadDec(Node,Index))>=0);'
+
+    patch -p1 -d minisat -i ${./minisat-fenv.patch}
+    patch -p1 -d glucose -i ${./glucose-fenv.patch}
+  '';
+
   patches =
     [ ./0001-no-static-boost-libs.patch
     ];
diff --git a/pkgs/applications/science/logic/avy/glucose-fenv.patch b/pkgs/applications/science/logic/avy/glucose-fenv.patch
new file mode 100644
index 00000000000..dd19f7ec80e
--- /dev/null
+++ b/pkgs/applications/science/logic/avy/glucose-fenv.patch
@@ -0,0 +1,65 @@
+From d6e0cb60270e8653bda3f339e3a07ce2cd2d6eb0 Mon Sep 17 00:00:00 2001
+From: Will Dietz <w@wdtz.org>
+Date: Tue, 17 Oct 2017 23:01:36 -0500
+Subject: [PATCH] glucose: use fenv to set double precision
+
+---
+ core/Main.cc   | 8 ++++++--
+ simp/Main.cc   | 8 ++++++--
+ utils/System.h | 2 +-
+ 3 files changed, 13 insertions(+), 5 deletions(-)
+
+diff --git a/core/Main.cc b/core/Main.cc
+index c96aadd..994132b 100644
+--- a/core/Main.cc
++++ b/core/Main.cc
+@@ -96,8 +96,12 @@ int main(int argc, char** argv)
+         // printf("This is MiniSat 2.0 beta\n");
+         
+ #if defined(__linux__)
+-        fpu_control_t oldcw, newcw;
+-        _FPU_GETCW(oldcw); newcw = (oldcw & ~_FPU_EXTENDED) | _FPU_DOUBLE; _FPU_SETCW(newcw);
++        fenv_t fenv;
++
++        fegetenv(&fenv);
++        fenv.__control_word &= ~0x300; /* _FPU_EXTENDED */
++        fenv.__control_word |= 0x200; /* _FPU_DOUBLE */
++        fesetenv(&fenv);
+         printf("c WARNING: for repeatability, setting FPU to use double precision\n");
+ #endif
+         // Extra options:
+diff --git a/simp/Main.cc b/simp/Main.cc
+index 4f4772d..70c2e4b 100644
+--- a/simp/Main.cc
++++ b/simp/Main.cc
+@@ -97,8 +97,12 @@ int main(int argc, char** argv)
+         
+         
+ #if defined(__linux__)
+-        fpu_control_t oldcw, newcw;
+-        _FPU_GETCW(oldcw); newcw = (oldcw & ~_FPU_EXTENDED) | _FPU_DOUBLE; _FPU_SETCW(newcw);
++        fenv_t fenv;
++
++        fegetenv(&fenv);
++        fenv.__control_word &= ~0x300; /* _FPU_EXTENDED */
++        fenv.__control_word |= 0x200; /* _FPU_DOUBLE */
++        fesetenv(&fenv);
+         printf("WARNING: for repeatability, setting FPU to use double precision\n");
+ #endif
+         // Extra options:
+diff --git a/utils/System.h b/utils/System.h
+index 004d498..a768e99 100644
+--- a/utils/System.h
++++ b/utils/System.h
+@@ -22,7 +22,7 @@ OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWA
+ #define Glucose_System_h
+ 
+ #if defined(__linux__)
+-#include <fpu_control.h>
++#include <fenv.h>
+ #endif
+ 
+ #include "glucose/mtl/IntTypes.h"
+-- 
+2.14.2
+
diff --git a/pkgs/applications/science/logic/avy/minisat-fenv.patch b/pkgs/applications/science/logic/avy/minisat-fenv.patch
new file mode 100644
index 00000000000..686d5a1c5b4
--- /dev/null
+++ b/pkgs/applications/science/logic/avy/minisat-fenv.patch
@@ -0,0 +1,65 @@
+From 7f1016ceab9b0f57a935bd51ca6df3d18439b472 Mon Sep 17 00:00:00 2001
+From: Will Dietz <w@wdtz.org>
+Date: Tue, 17 Oct 2017 22:57:02 -0500
+Subject: [PATCH] use fenv instead of non-standard fpu_control
+
+---
+ core/Main.cc   | 8 ++++++--
+ simp/Main.cc   | 8 ++++++--
+ utils/System.h | 2 +-
+ 3 files changed, 13 insertions(+), 5 deletions(-)
+
+diff --git a/core/Main.cc b/core/Main.cc
+index 2b0d97b..8ad95fb 100644
+--- a/core/Main.cc
++++ b/core/Main.cc
+@@ -78,8 +78,12 @@ int main(int argc, char** argv)
+         // printf("This is MiniSat 2.0 beta\n");
+         
+ #if defined(__linux__)
+-        fpu_control_t oldcw, newcw;
+-        _FPU_GETCW(oldcw); newcw = (oldcw & ~_FPU_EXTENDED) | _FPU_DOUBLE; _FPU_SETCW(newcw);
++        fenv_t fenv;
++
++        fegetenv(&fenv);
++        fenv.__control_word &= ~0x300; /* _FPU_EXTENDED */
++        fenv.__control_word |= 0x200; /* _FPU_DOUBLE */
++        fesetenv(&fenv);
+         printf("WARNING: for repeatability, setting FPU to use double precision\n");
+ #endif
+         // Extra options:
+diff --git a/simp/Main.cc b/simp/Main.cc
+index 2804d7f..39bfb71 100644
+--- a/simp/Main.cc
++++ b/simp/Main.cc
+@@ -79,8 +79,12 @@ int main(int argc, char** argv)
+         // printf("This is MiniSat 2.0 beta\n");
+         
+ #if defined(__linux__)
+-        fpu_control_t oldcw, newcw;
+-        _FPU_GETCW(oldcw); newcw = (oldcw & ~_FPU_EXTENDED) | _FPU_DOUBLE; _FPU_SETCW(newcw);
++        fenv_t fenv;
++
++        fegetenv(&fenv);
++        fenv.__control_word &= ~0x300; /* _FPU_EXTENDED */
++        fenv.__control_word |= 0x200; /* _FPU_DOUBLE */
++        fesetenv(&fenv);
+         printf("WARNING: for repeatability, setting FPU to use double precision\n");
+ #endif
+         // Extra options:
+diff --git a/utils/System.h b/utils/System.h
+index 1758192..c0ad13a 100644
+--- a/utils/System.h
++++ b/utils/System.h
+@@ -22,7 +22,7 @@ OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWA
+ #define Minisat_System_h
+ 
+ #if defined(__linux__)
+-#include <fpu_control.h>
++#include <fenv.h>
+ #endif
+ 
+ #include "mtl/IntTypes.h"
+-- 
+2.14.2
+

From 1289cdaf75c76634f664e79268667f094acd3dbc Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sat, 13 Jan 2018 15:34:57 -0600
Subject: [PATCH 111/173] mesa: patch to fix w/musl

---
 pkgs/development/libraries/mesa/default.nix   |  2 +-
 .../libraries/mesa/musl-fixes.patch           | 22 +++++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)
 create mode 100644 pkgs/development/libraries/mesa/musl-fixes.patch

diff --git a/pkgs/development/libraries/mesa/default.nix b/pkgs/development/libraries/mesa/default.nix
index 8df248e3e46..fa3336c4cea 100644
--- a/pkgs/development/libraries/mesa/default.nix
+++ b/pkgs/development/libraries/mesa/default.nix
@@ -92,7 +92,7 @@ stdenv.mkDerivation {
   patches = [
     ./glx_ro_text_segm.patch # fix for grsecurity/PaX
     ./symlink-drivers.patch
-  ];
+  ] ++ stdenv.lib.optional stdenv.hostPlatform.isMusl ./musl-fixes.patch;
 
   outputs = [ "out" "dev" "drivers" "osmesa" ];
 
diff --git a/pkgs/development/libraries/mesa/musl-fixes.patch b/pkgs/development/libraries/mesa/musl-fixes.patch
new file mode 100644
index 00000000000..60140d445ae
--- /dev/null
+++ b/pkgs/development/libraries/mesa/musl-fixes.patch
@@ -0,0 +1,22 @@
+--- ./src/gallium/winsys/svga/drm/vmw_screen.h.orig
++++ ./src/gallium/winsys/svga/drm/vmw_screen.h
+@@ -34,7 +34,7 @@
+ #ifndef VMW_SCREEN_H_
+ #define VMW_SCREEN_H_
+ 
+-
++#include <sys/stat.h>
+ #include "pipe/p_compiler.h"
+ #include "pipe/p_state.h"
+ 
+--- a/src/util/u_endian.h.orig	2016-11-04 12:16:00.480356454 +0100
++++ b/src/util/u_endian.h	2016-11-04 12:16:11.984347944 +0100
+@@ -27,7 +27,7 @@
+ #ifndef U_ENDIAN_H
+ #define U_ENDIAN_H
+ 
+-#if defined(__GLIBC__) || defined(ANDROID) || defined(__CYGWIN__)
++#if defined(__linux__) || defined(ANDROID) || defined(__CYGWIN__)
+ #include <endian.h>
+ 
+ #if __BYTE_ORDER == __LITTLE_ENDIAN

From bc76f767f6fc0f1e15a5991a17adececbfb7b095 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sat, 13 Jan 2018 15:28:11 -0600
Subject: [PATCH 112/173] linuxHeaders: allow multiple versions, use newest +
 patches w/musl

Hash unchanged for non-musl case.
---
 pkgs/os-specific/linux/kernel-headers/4.4.nix | 61 -------------
 .../linux/kernel-headers/default.nix          | 89 +++++++++++++++++++
 pkgs/top-level/all-packages.nix               |  5 +-
 3 files changed, 92 insertions(+), 63 deletions(-)
 delete mode 100644 pkgs/os-specific/linux/kernel-headers/4.4.nix
 create mode 100644 pkgs/os-specific/linux/kernel-headers/default.nix

diff --git a/pkgs/os-specific/linux/kernel-headers/4.4.nix b/pkgs/os-specific/linux/kernel-headers/4.4.nix
deleted file mode 100644
index e8e041f48eb..00000000000
--- a/pkgs/os-specific/linux/kernel-headers/4.4.nix
+++ /dev/null
@@ -1,61 +0,0 @@
-{ stdenvNoCC, lib, buildPackages
-, buildPlatform, hostPlatform
-, fetchurl, perl
-}:
-
-assert hostPlatform.isLinux;
-
-let
-  version = "4.4.10";
-  inherit (hostPlatform.platform) kernelHeadersBaseConfig;
-in
-
-stdenvNoCC.mkDerivation {
-  name = "linux-headers-${version}";
-
-  src = fetchurl {
-    url = "mirror://kernel/linux/kernel/v4.x/linux-${version}.tar.xz";
-    sha256 = "1kpjvvd9q9wwr3314q5ymvxii4dv2d27295bzly225wlc552xhja";
-  };
-
-  targetConfig = if hostPlatform != buildPlatform then hostPlatform.config else null;
-
-  platform = hostPlatform.platform.kernelArch;
-
-  # It may look odd that we use `stdenvNoCC`, and yet explicit depend on a cc.
-  # We do this so we have a build->build, not build->host, C compiler.
-  depsBuildBuild = [ buildPackages.stdenv.cc ];
-  nativeBuildInputs = [ perl ];
-
-  extraIncludeDirs = lib.optional hostPlatform.isPowerPC ["ppc"];
-
-  buildPhase = ''
-    if test -n "$targetConfig"; then
-       export ARCH=$platform
-    fi
-    make ${kernelHeadersBaseConfig} SHELL=bash
-    make mrproper headers_check SHELL=bash
-  '';
-
-  installPhase = ''
-    make INSTALL_HDR_PATH=$out headers_install
-
-    # Some builds (e.g. KVM) want a kernel.release.
-    mkdir -p $out/include/config
-    echo "${version}-default" > $out/include/config/kernel.release
-  '';
-
-  # !!! hacky
-  fixupPhase = ''
-    ln -s asm $out/include/asm-$platform
-    if test "$platform" = "i386" -o "$platform" = "x86_64"; then
-      ln -s asm $out/include/asm-x86
-    fi
-  '';
-
-  meta = with lib; {
-    description = "Header files and scripts for Linux kernel";
-    license = licenses.gpl2;
-    platforms = platforms.linux;
-  };
-}
diff --git a/pkgs/os-specific/linux/kernel-headers/default.nix b/pkgs/os-specific/linux/kernel-headers/default.nix
new file mode 100644
index 00000000000..a2b37111dc7
--- /dev/null
+++ b/pkgs/os-specific/linux/kernel-headers/default.nix
@@ -0,0 +1,89 @@
+{ stdenvNoCC, lib, buildPackages
+, buildPlatform, hostPlatform
+, fetchurl, perl
+}:
+
+assert hostPlatform.isLinux;
+
+with hostPlatform.platform.kernelHeadersBaseConfig;
+
+let
+  inherit (hostPlatform.platform) kernelHeadersBaseConfig;
+  common = { version, sha256, patches ? null }: stdenvNoCC.mkDerivation {
+    name = "linux-headers-${version}";
+
+    src = fetchurl {
+      url = "mirror://kernel/linux/kernel/v4.x/linux-${version}.tar.xz";
+      inherit sha256;
+    };
+
+    targetConfig = if hostPlatform != buildPlatform then hostPlatform.config else null;
+
+    platform = hostPlatform.platform.kernelArch;
+
+    # It may look odd that we use `stdenvNoCC`, and yet explicit depend on a cc.
+    # We do this so we have a build->build, not build->host, C compiler.
+    depsBuildBuild = [ buildPackages.stdenv.cc ];
+    nativeBuildInputs = [ perl ];
+
+    extraIncludeDirs = lib.optional hostPlatform.isPowerPC ["ppc"];
+
+    # "patches" array defaults to 'null' to avoid changing hash
+    # and causing mass rebuild
+    inherit patches;
+
+    buildPhase = ''
+      if test -n "$targetConfig"; then
+         export ARCH=$platform
+      fi
+      make ${kernelHeadersBaseConfig} SHELL=bash
+      make mrproper headers_check SHELL=bash
+    '';
+
+    installPhase = ''
+      make INSTALL_HDR_PATH=$out headers_install
+
+      # Some builds (e.g. KVM) want a kernel.release.
+      mkdir -p $out/include/config
+      echo "${version}-default" > $out/include/config/kernel.release
+    '';
+
+    # !!! hacky
+    fixupPhase = ''
+      ln -s asm $out/include/asm-$platform
+      if test "$platform" = "i386" -o "$platform" = "x86_64"; then
+        ln -s asm $out/include/asm-x86
+      fi
+    '';
+
+    meta = with lib; {
+      description = "Header files and scripts for Linux kernel";
+      license = licenses.gpl2;
+      platforms = platforms.linux;
+    };
+  };
+in {
+
+  linuxHeaders_4_4 = common {
+    version = "4.4.10";
+    sha256 = "1kpjvvd9q9wwr3314q5ymvxii4dv2d27295bzly225wlc552xhja";
+  };
+
+  linuxHeaders_4_14 = common {
+    version = "4.14.13";
+    sha256 = "0wjpwhrnnvf6l3zpkkxk34dl722w9yp8j3vnh0xzi3hgb8dnvd2a";
+
+    patches = [
+      (fetchurl {
+        name = "uapi_libc_compat.patch";
+        url = "https://patchwork.ozlabs.org/patch/854342/raw/";
+        sha256 = "0qczlgqfbw0czx63wg2zgla15zpmcc76d00cb7qwl514ysm4ihmj";
+      })
+      (fetchurl {
+        name = "struct_ethhdr.patch";
+        url = "https://patchwork.ozlabs.org/patch/855293/raw/";
+        sha256 = "0019nxilbgv986sswxyvii50l5l3n9yp4ysgnjdp9104plcq9956";
+      })
+    ];
+  };
+}
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index 89e1243146b..c8c143abddb 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -12927,8 +12927,9 @@ with pkgs;
 
   lkl = callPackage ../applications/virtualization/lkl { };
 
-  linuxHeaders_4_4 = callPackage ../os-specific/linux/kernel-headers/4.4.nix { };
-  linuxHeaders = linuxHeaders_4_4;
+  inherit (callPackages ../os-specific/linux/kernel-headers { })
+    linuxHeaders_4_4 linuxHeaders_4_14;
+  linuxHeaders = if hostPlatform.isMusl then linuxHeaders_4_14 else linuxHeaders_4_4;
 
   kernelPatches = callPackage ../os-specific/linux/kernel/patches.nix { };
 

From f29bbd779ced01d89c1c1b317e15339eba161ca9 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sat, 13 Jan 2018 18:21:03 -0600
Subject: [PATCH 113/173] musl: add patch to make full use of new guards in
 kernel headers

---
 pkgs/os-specific/linux/musl/default.nix       |  2 +
 .../linux/musl/few-more-uapi-fixes.patch      | 71 +++++++++++++++++++
 2 files changed, 73 insertions(+)
 create mode 100644 pkgs/os-specific/linux/musl/few-more-uapi-fixes.patch

diff --git a/pkgs/os-specific/linux/musl/default.nix b/pkgs/os-specific/linux/musl/default.nix
index 9a97b4259b2..f74ac9c41ee 100644
--- a/pkgs/os-specific/linux/musl/default.nix
+++ b/pkgs/os-specific/linux/musl/default.nix
@@ -47,6 +47,8 @@ stdenv.mkDerivation rec {
 
   outputs = [ "out" "dev" ];
 
+  patches = [ ./few-more-uapi-fixes.patch ];
+
   dontDisableStatic = true;
   dontStrip = true;
 
diff --git a/pkgs/os-specific/linux/musl/few-more-uapi-fixes.patch b/pkgs/os-specific/linux/musl/few-more-uapi-fixes.patch
new file mode 100644
index 00000000000..f84ca2c5aec
--- /dev/null
+++ b/pkgs/os-specific/linux/musl/few-more-uapi-fixes.patch
@@ -0,0 +1,71 @@
+http://www.openwall.com/lists/musl/2018/01/06/3
+
+Date: Sat,  6 Jan 2018 23:32:52 +0100
+From: Hauke Mehrtens <hauke@...ke-m.de>
+To: musl@...ts.openwall.com
+Cc: felix.janda@...teo.de,
+	Hauke Mehrtens <hauke@...ke-m.de>
+Subject: [PATCH v2] Add additional uapi guards for Linux kernel header files
+
+With Linux kernel 4.16 it will be possible to guard more parts of the
+Linux header files from a libc. Make use of this in musl to guard all
+the structures and other definitions from the Linux header files which
+are also defined by the header files provided by musl. This will make
+musl compile with the unmodified Linux kernel user space headers.
+
+This extends the definitions done in commit 04983f227238 ("make
+netinet/in.h suppress clashing definitions from kernel headers")
+
+The needed patches were recently accepted into the netdev tree and will be integrated in Linux 4.16:
+https://patchwork.ozlabs.org/patch/854342/
+https://patchwork.ozlabs.org/patch/855293/
+---
+ include/net/if.h           | 7 +++++++
+ include/netinet/if_ether.h | 1 +
+ include/sys/xattr.h        | 2 ++
+ 3 files changed, 10 insertions(+)
+
+diff --git a/include/net/if.h b/include/net/if.h
+index 2f2fcc10..0ee48cd7 100644
+--- a/include/net/if.h
++++ b/include/net/if.h
+@@ -125,6 +125,13 @@ struct ifconf {
+ #define ifc_req		ifc_ifcu.ifcu_req
+ #define _IOT_ifconf _IOT(_IOTS(struct ifconf),1,0,0,0,0)
+ 
++#define __UAPI_DEF_IF_IFCONF					0
++#define __UAPI_DEF_IF_IFMAP					0
++#define __UAPI_DEF_IF_IFNAMSIZ					0
++#define __UAPI_DEF_IF_IFREQ					0
++#define __UAPI_DEF_IF_NET_DEVICE_FLAGS				0
++#define __UAPI_DEF_IF_NET_DEVICE_FLAGS_LOWER_UP_DORMANT_ECHO	0
++
+ #endif
+ 
+ #ifdef __cplusplus
+diff --git a/include/netinet/if_ether.h b/include/netinet/if_ether.h
+index d9a131aa..c2c6e944 100644
+--- a/include/netinet/if_ether.h
++++ b/include/netinet/if_ether.h
+@@ -133,5 +133,6 @@ do { \
+ 	(enaddr)[5] = ((uint8_t *)ipaddr)[3]; \
+ } while(0)
+ 
++#define __UAPI_DEF_ETHHDR	0
+ 
+ #endif
+diff --git a/include/sys/xattr.h b/include/sys/xattr.h
+index 6479fcc6..52e3dd89 100644
+--- a/include/sys/xattr.h
++++ b/include/sys/xattr.h
+@@ -24,6 +24,8 @@ int removexattr(const char *, const char *);
+ int lremovexattr(const char *, const char *);
+ int fremovexattr(int, const char *);
+ 
++#define __UAPI_DEF_XATTR	0
++
+ #ifdef __cplusplus
+ }
+ #endif
+-- 
+2.11.0

From bd23fc77d35d50497ac36c84e9ef11ff999d41f0 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sun, 13 Aug 2017 21:29:17 -0500
Subject: [PATCH 114/173] 2048-in-terminal: fix on musl

---
 pkgs/games/2048-in-terminal/default.nix | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/pkgs/games/2048-in-terminal/default.nix b/pkgs/games/2048-in-terminal/default.nix
index 288c852b11e..5d91bc04114 100644
--- a/pkgs/games/2048-in-terminal/default.nix
+++ b/pkgs/games/2048-in-terminal/default.nix
@@ -13,6 +13,10 @@ stdenv.mkDerivation rec {
 
   buildInputs = [ ncurses ];
 
+  prePatch = ''
+    sed -i '1i#include <fcntl.h>\n' save.c
+  '';
+
   enableParallelBuilding = true;
 
   preInstall = ''

From a0ee888f1dcf1a597bdc81bf32e499a90f3210d0 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sat, 13 Jan 2018 22:46:33 -0600
Subject: [PATCH 115/173] pulseaudio: patch for musl

---
 pkgs/servers/pulseaudio/default.nix | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/pkgs/servers/pulseaudio/default.nix b/pkgs/servers/pulseaudio/default.nix
index 85ee3ddcf74..2f102b58f14 100644
--- a/pkgs/servers/pulseaudio/default.nix
+++ b/pkgs/servers/pulseaudio/default.nix
@@ -43,7 +43,12 @@ stdenv.mkDerivation rec {
     sha256 = "0sf92knqkvqmfhrbz4vlsagzqlps72wycpmln5dygicg07a0a8q7";
   };
 
-  patches = [ ./caps-fix.patch ];
+  patches = [ ./caps-fix.patch ]
+    ++ stdenv.lib.optional stdenv.hostPlatform.isMusl (fetchpatch {
+      name = "padsp-fix.patch";
+      url = "https://git.alpinelinux.org/cgit/aports/plain/testing/pulseaudio/0001-padsp-Make-it-compile-on-musl.patch?id=167be02bf4618a90328e2b234f6a63a5dc05f244";
+      sha256 = "0gf4w25zi123ghk0njapysvrlljkc3hyanacgiswfnnm1i8sab1q";
+    });
 
   outputs = [ "out" "dev" ];
 

From 0e15473fd185c9284a41499efc51df9a01a5a578 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Thu, 1 Jun 2017 08:20:43 -0500
Subject: [PATCH 116/173] cdparanoia: use part of darwin patch to fix utils.h
 on musl

---
 .../applications/audio/cdparanoia/default.nix |  2 +-
 .../applications/audio/cdparanoia/utils.patch | 68 +++++++++++++++++++
 2 files changed, 69 insertions(+), 1 deletion(-)
 create mode 100644 pkgs/applications/audio/cdparanoia/utils.patch

diff --git a/pkgs/applications/audio/cdparanoia/default.nix b/pkgs/applications/audio/cdparanoia/default.nix
index 34dba5e206f..d4d302f07d2 100644
--- a/pkgs/applications/audio/cdparanoia/default.nix
+++ b/pkgs/applications/audio/cdparanoia/default.nix
@@ -17,7 +17,7 @@ stdenv.mkDerivation rec {
       url = "https://trac.macports.org/export/70964/trunk/dports/audio/cdparanoia/files/patch-paranoia_paranoia.c.10.4.diff";
       sha256 = "17l2qhn8sh4jy6ryy5si6ll6dndcm0r537rlmk4a6a8vkn852vad";
     })
-  ];
+  ] ++ stdenv.lib.optional stdenv.hostPlatform.isMusl ./utils.patch;
 
   buildInputs = stdenv.lib.optional stdenv.isAarch64 autoreconfHook;
 
diff --git a/pkgs/applications/audio/cdparanoia/utils.patch b/pkgs/applications/audio/cdparanoia/utils.patch
new file mode 100644
index 00000000000..338e5303dcd
--- /dev/null
+++ b/pkgs/applications/audio/cdparanoia/utils.patch
@@ -0,0 +1,68 @@
+diff --git cdparanoia-III-10.2/interface/utils.h cdparanoia-III-10.2/interface/utils.h
+index c9647da..68c1a3a 100644
+--- cdparanoia-III-10.2/interface/utils.h
++++ cdparanoia-III-10.2/interface/utils.h
+@@ -1,4 +1,6 @@
+-#include <endian.h>
++#include <unistd.h>
++#include <stdint.h>
++#include <stdlib.h>
+ #include <stdio.h>
+ #include <errno.h>
+ #include <string.h>
+@@ -14,15 +16,15 @@ static inline int bigendianp(void){
+ }
+ 
+ static inline int32_t swap32(int32_t x){
+-  return((((u_int32_t)x & 0x000000ffU) << 24) | 
+-	 (((u_int32_t)x & 0x0000ff00U) <<  8) | 
+-	 (((u_int32_t)x & 0x00ff0000U) >>  8) | 
+-	 (((u_int32_t)x & 0xff000000U) >> 24));
++  return((((uint32_t)x & 0x000000ffU) << 24) | 
++	 (((uint32_t)x & 0x0000ff00U) <<  8) | 
++	 (((uint32_t)x & 0x00ff0000U) >>  8) | 
++	 (((uint32_t)x & 0xff000000U) >> 24));
+ }
+ 
+ static inline int16_t swap16(int16_t x){
+-  return((((u_int16_t)x & 0x00ffU) <<  8) | 
+-	 (((u_int16_t)x & 0xff00U) >>  8));
++  return((((uint16_t)x & 0x00ffU) <<  8) | 
++	 (((uint16_t)x & 0xff00U) >>  8));
+ }
+ 
+ #if BYTE_ORDER == LITTLE_ENDIAN
+diff --git cdparanoia-III-10.2/utils.h cdparanoia-III-10.2/utils.h
+index 10dce58..6211ce3 100644
+--- cdparanoia-III-10.2/utils.h
++++ cdparanoia-III-10.2/utils.h
+@@ -1,5 +1,6 @@
++#include <unistd.h>
++#include <stdint.h>
+ #include <stdlib.h>
+-#include <endian.h>
+ #include <stdio.h>
+ #include <errno.h>
+ #include <string.h>
+@@ -18,15 +19,15 @@ static inline int bigendianp(void){
+ }
+ 
+ static inline int32_t swap32(int32_t x){
+-  return((((u_int32_t)x & 0x000000ffU) << 24) | 
+-	 (((u_int32_t)x & 0x0000ff00U) <<  8) | 
+-	 (((u_int32_t)x & 0x00ff0000U) >>  8) | 
+-	 (((u_int32_t)x & 0xff000000U) >> 24));
++  return((((uint32_t)x & 0x000000ffU) << 24) | 
++	 (((uint32_t)x & 0x0000ff00U) <<  8) | 
++	 (((uint32_t)x & 0x00ff0000U) >>  8) | 
++	 (((uint32_t)x & 0xff000000U) >> 24));
+ }
+ 
+ static inline int16_t swap16(int16_t x){
+-  return((((u_int16_t)x & 0x00ffU) <<  8) | 
+-	 (((u_int16_t)x & 0xff00U) >>  8));
++  return((((uint16_t)x & 0x00ffU) <<  8) | 
++	 (((uint16_t)x & 0xff00U) >>  8));
+ }
+ 
+ #if BYTE_ORDER == LITTLE_ENDIAN

From 2e2517c9c94fce696c7d85f76547d0aca2cdff3f Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sun, 28 May 2017 13:52:30 -0500
Subject: [PATCH 117/173] SDL: add libiconv to fix build

---
 pkgs/development/libraries/SDL/default.nix  | 5 +++--
 pkgs/development/libraries/SDL2/default.nix | 4 +++-
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/pkgs/development/libraries/SDL/default.nix b/pkgs/development/libraries/SDL/default.nix
index e10d62bb98d..82051854799 100644
--- a/pkgs/development/libraries/SDL/default.nix
+++ b/pkgs/development/libraries/SDL/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, fetchpatch, pkgconfig, audiofile, libcap
+{ stdenv, fetchurl, fetchpatch, pkgconfig, audiofile, libcap, libiconv
 , openglSupport ? false, mesa_noglu, mesa_glu
 , alsaSupport ? true, alsaLib
 , x11Support ? hostPlatform == buildPlatform, libXext, libICE, libXrandr
@@ -40,7 +40,8 @@ stdenv.mkDerivation rec {
   buildInputs = let
     notMingw = !hostPlatform.isMinGW;
   in optional notMingw audiofile
-  ++ optionals stdenv.isDarwin [ OpenGL CoreAudio CoreServices AudioUnit Kernel ];
+  ++ optionals stdenv.isDarwin [ OpenGL CoreAudio CoreServices AudioUnit Kernel ]
+  ++ [ libiconv ];
 
   # XXX: By default, SDL wants to dlopen() PulseAudio, in which case
   # we must arrange to add it to its RPATH; however, `patchelf' seems
diff --git a/pkgs/development/libraries/SDL2/default.nix b/pkgs/development/libraries/SDL2/default.nix
index d75aea726e1..6b6c9599c28 100644
--- a/pkgs/development/libraries/SDL2/default.nix
+++ b/pkgs/development/libraries/SDL2/default.nix
@@ -8,6 +8,7 @@
 , ibusSupport ? false, ibus
 , pulseaudioSupport ? true, libpulseaudio
 , AudioUnit, Cocoa, CoreAudio, CoreServices, ForceFeedback, OpenGL
+, libiconv
 }:
 
 # OSS is no longer supported, for it's much crappier than ALSA and
@@ -41,7 +42,8 @@ stdenv.mkDerivation rec {
   # Since `libpulse*.la' contain `-lgdbm', PulseAudio must be propagated.
   propagatedBuildInputs = lib.optionals x11Support [ libICE libXi libXScrnSaver libXcursor libXinerama libXext libXrandr libXxf86vm ] ++
     lib.optionals waylandSupport [ wayland wayland-protocols libxkbcommon ] ++
-    lib.optional pulseaudioSupport libpulseaudio;
+    lib.optional pulseaudioSupport libpulseaudio
+    ++ [ libiconv ];
 
   buildInputs = [ audiofile ] ++
     lib.optional openglSupport mesa_noglu ++

From c48974c9527159dc2f643d83d24d44c0defe367d Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Thu, 1 Jun 2017 08:31:55 -0500
Subject: [PATCH 118/173] qt4: Add patch for socklen_t on musl, from alpine

---
 .../libraries/qt-4.x/4.8/default.nix          |  8 ++-
 .../qt-4.x/4.8/patch-qthread-stacksize.diff   | 54 +++++++++++++++++++
 .../qsettings-recursive-global-mutex.patch    | 17 ++++++
 .../qt-4.x/4.8/qt-musl-iconv-no-bom.patch     | 11 ++++
 .../libraries/qt-4.x/4.8/qt-musl.patch        | 14 +++++
 5 files changed, 103 insertions(+), 1 deletion(-)
 create mode 100644 pkgs/development/libraries/qt-4.x/4.8/patch-qthread-stacksize.diff
 create mode 100644 pkgs/development/libraries/qt-4.x/4.8/qsettings-recursive-global-mutex.patch
 create mode 100644 pkgs/development/libraries/qt-4.x/4.8/qt-musl-iconv-no-bom.patch
 create mode 100644 pkgs/development/libraries/qt-4.x/4.8/qt-musl.patch

diff --git a/pkgs/development/libraries/qt-4.x/4.8/default.nix b/pkgs/development/libraries/qt-4.x/4.8/default.nix
index 488306fc1ce..a9bd25ed28b 100644
--- a/pkgs/development/libraries/qt-4.x/4.8/default.nix
+++ b/pkgs/development/libraries/qt-4.x/4.8/default.nix
@@ -106,7 +106,13 @@ stdenv.mkDerivation rec {
     ++ stdenv.lib.optional stdenv.isAarch64 (fetchpatch {
         url = "https://src.fedoraproject.org/rpms/qt/raw/ecf530486e0fb7fe31bad26805cde61115562b2b/f/qt-aarch64.patch";
         sha256 = "1fbjh78nmafqmj7yk67qwjbhl3f6ylkp6x33b1dqxfw9gld8b3gl";
-      });
+      })
+    ++ stdenv.lib.optionals stdenv.hostPlatform.isMusl [
+        ./qt-musl.patch
+        ./qt-musl-iconv-no-bom.patch
+        ./patch-qthread-stacksize.diff
+        ./qsettings-recursive-global-mutex.patch
+      ];
 
   preConfigure = ''
     export LD_LIBRARY_PATH="`pwd`/lib:$LD_LIBRARY_PATH"
diff --git a/pkgs/development/libraries/qt-4.x/4.8/patch-qthread-stacksize.diff b/pkgs/development/libraries/qt-4.x/4.8/patch-qthread-stacksize.diff
new file mode 100644
index 00000000000..53a4c70ac3a
--- /dev/null
+++ b/pkgs/development/libraries/qt-4.x/4.8/patch-qthread-stacksize.diff
@@ -0,0 +1,54 @@
+--- a/src/corelib/thread/qthread_unix.cpp.orig	2015-11-23 19:05:40.000000000 +0100
++++ b/src/corelib/thread/qthread_unix.cpp	2015-11-24 11:22:31.000000000 +0100
+@@ -79,6 +79,7 @@
+ #endif
+ 
++#include <sys/resource.h> // getrlimit/setrlimit
+ #if defined(Q_OS_MAC)
+ # ifdef qDebug
+ #   define old_qDebug qDebug
+ #   undef qDebug
+@@ -649,6 +650,43 @@
+ #endif // QT_HAS_THREAD_PRIORITY_SCHEDULING
+ 
+ 
++    if (d->stackSize == 0) {
++        // Fix the default (too small) stack size for threads on OS X,
++        // which also affects the thread pool.
++        // See also:
++        // https://bugreports.qt.io/browse/QTBUG-2568
++        // This fix can also be found in Chromium:
++        // https://chromium.googlesource.com/chromium/src.git/+/master/base/threading/platform_thread_mac.mm#186
++
++        // The Mac OS X default for a pthread stack size is 512kB.
++        // Libc-594.1.4/pthreads/pthread.c's pthread_attr_init uses
++        // DEFAULT_STACK_SIZE for this purpose.
++        //
++        // 512kB isn't quite generous enough for some deeply recursive threads that
++        // otherwise request the default stack size by specifying 0. Here, adopt
++        // glibc's behavior as on Linux, which is to use the current stack size
++        // limit (ulimit -s) as the default stack size. See
++        // glibc-2.11.1/nptl/nptl-init.c's __pthread_initialize_minimal_internal. To
++        // avoid setting the limit below the Mac OS X default or the minimum usable
++        // stack size, these values are also considered. If any of these values
++        // can't be determined, or if stack size is unlimited (ulimit -s unlimited),
++        // stack_size is left at 0 to get the system default.
++        //
++        // Mac OS X normally only applies ulimit -s to the main thread stack. On
++        // contemporary OS X and Linux systems alike, this value is generally 8MB
++        // or in that neighborhood.
++        size_t default_stack_size = 0;
++        struct rlimit stack_rlimit;
++        if (pthread_attr_getstacksize(&attr, &default_stack_size) == 0 &&
++            getrlimit(RLIMIT_STACK, &stack_rlimit) == 0 &&
++            stack_rlimit.rlim_cur != RLIM_INFINITY) {
++            default_stack_size =
++                    std::max(std::max(default_stack_size,
++                                      static_cast<size_t>(PTHREAD_STACK_MIN)),
++                             static_cast<size_t>(stack_rlimit.rlim_cur));
++        }
++        d->stackSize = default_stack_size;
++    }
+     if (d->stackSize > 0) {
+ #if defined(_POSIX_THREAD_ATTR_STACKSIZE) && (_POSIX_THREAD_ATTR_STACKSIZE-0 > 0)
+         int code = pthread_attr_setstacksize(&attr, d->stackSize);
diff --git a/pkgs/development/libraries/qt-4.x/4.8/qsettings-recursive-global-mutex.patch b/pkgs/development/libraries/qt-4.x/4.8/qsettings-recursive-global-mutex.patch
new file mode 100644
index 00000000000..229123c54f7
--- /dev/null
+++ b/pkgs/development/libraries/qt-4.x/4.8/qsettings-recursive-global-mutex.patch
@@ -0,0 +1,17 @@
+Calling qsettings before constructing qapplications causes a dead-lock.
+
+http://sourceforge.net/tracker/?func=detail&aid=3168620&group_id=4932&atid=104932
+http://developer.qt.nokia.com/forums/viewthread/10365
+
+
+--- ./src/corelib/io/qsettings.cpp.orig
++++ ./src/corelib/io/qsettings.cpp
+@@ -122,7 +122,7 @@
+ Q_GLOBAL_STATIC(ConfFileCache, unusedCacheFunc)
+ Q_GLOBAL_STATIC(PathHash, pathHashFunc)
+ Q_GLOBAL_STATIC(CustomFormatVector, customFormatVectorFunc)
+-Q_GLOBAL_STATIC(QMutex, globalMutex)
++Q_GLOBAL_STATIC_WITH_ARGS(QMutex, globalMutex, (QMutex::Recursive))
+ static QSettings::Format globalDefaultFormat = QSettings::NativeFormat;
+ 
+ #ifndef Q_OS_WIN
diff --git a/pkgs/development/libraries/qt-4.x/4.8/qt-musl-iconv-no-bom.patch b/pkgs/development/libraries/qt-4.x/4.8/qt-musl-iconv-no-bom.patch
new file mode 100644
index 00000000000..35380ad6714
--- /dev/null
+++ b/pkgs/development/libraries/qt-4.x/4.8/qt-musl-iconv-no-bom.patch
@@ -0,0 +1,11 @@
+--- qt-everywhere-opensource-src-4.8.5/src/corelib/codecs/qiconvcodec.cpp.orig
++++ qt-everywhere-opensource-src-4.8.5/src/corelib/codecs/qiconvcodec.cpp
+@@ -62,7 +62,7 @@
+ #elif defined(Q_OS_AIX)
+ #  define NO_BOM
+ #  define UTF16 "UCS-2"
+-#elif defined(Q_OS_FREEBSD) || defined(Q_OS_MAC)
++#elif defined(Q_OS_FREEBSD) || defined(Q_OS_MAC) || (defined(Q_OS_LINUX) && !defined(__GLIBC__))
+ #  define NO_BOM
+ #  if Q_BYTE_ORDER == Q_BIG_ENDIAN
+ #    define UTF16 "UTF-16BE"
diff --git a/pkgs/development/libraries/qt-4.x/4.8/qt-musl.patch b/pkgs/development/libraries/qt-4.x/4.8/qt-musl.patch
new file mode 100644
index 00000000000..90b9ccda08c
--- /dev/null
+++ b/pkgs/development/libraries/qt-4.x/4.8/qt-musl.patch
@@ -0,0 +1,14 @@
+--- qt-everywhere-opensource-src-4.8.5/mkspecs/linux-g++/qplatformdefs.h.orig
++++ qt-everywhere-opensource-src-4.8.5/mkspecs/linux-g++/qplatformdefs.h
+@@ -86,11 +86,7 @@
+ 
+ #undef QT_SOCKLEN_T
+ 
+-#if defined(__GLIBC__) && (__GLIBC__ >= 2)
+ #define QT_SOCKLEN_T            socklen_t
+-#else
+-#define QT_SOCKLEN_T            int
+-#endif
+ 
+ #if defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE >= 500)
+ #define QT_SNPRINTF		::snprintf

From e7580d0f92fddadbd8c67ed759f55c81d2f5a25f Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Thu, 20 Apr 2017 17:12:14 -0500
Subject: [PATCH 119/173] xorgserver: Builds! small workaround re:bad types

---
 pkgs/servers/x11/xorg/overrides.nix | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/pkgs/servers/x11/xorg/overrides.nix b/pkgs/servers/x11/xorg/overrides.nix
index f56d22d7b7e..c6466598195 100644
--- a/pkgs/servers/x11/xorg/overrides.nix
+++ b/pkgs/servers/x11/xorg/overrides.nix
@@ -493,6 +493,9 @@ in
           args.udev
         ];
         patches = commonPatches;
+        prePatch = stdenv.lib.optionalString stdenv.hostPlatform.isMusl ''
+          export CFLAGS+=" -D__uid_t=uid_t -D__gid_t=gid_t"
+        '';
         configureFlags = [
           "--enable-kdrive"             # not built by default
           "--enable-xephyr"

From e25153a9b860153a28f1d4e11156c49d9d8df908 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sun, 14 Jan 2018 00:46:32 -0600
Subject: [PATCH 120/173] gamin: patch from alpine to fix non-portable pthread
 mutex init

---
 pkgs/development/libraries/gamin/default.nix | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/pkgs/development/libraries/gamin/default.nix b/pkgs/development/libraries/gamin/default.nix
index e6b1875a9e0..2dd32b6f110 100644
--- a/pkgs/development/libraries/gamin/default.nix
+++ b/pkgs/development/libraries/gamin/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, python, pkgconfig, glib }:
+{ stdenv, fetchurl, fetchpatch, python, pkgconfig, glib }:
 
 stdenv.mkDerivation (rec {
   name = "gamin-0.1.10";
@@ -18,7 +18,12 @@ stdenv.mkDerivation (rec {
 
   patches = [ ./deadlock.patch ]
     ++ map fetchurl (import ./debian-patches.nix)
-    ++ stdenv.lib.optional stdenv.cc.isClang ./returnval.patch;
+    ++ stdenv.lib.optional stdenv.cc.isClang ./returnval.patch
+    ++ stdenv.lib.optional stdenv.hostPlatform.isMusl (fetchpatch {
+      name = "fix-pthread-mutex.patch";
+      url = "https://git.alpinelinux.org/cgit/aports/plain/main/gamin/fix-pthread-mutex.patch?h=3.4-stable&id=a1a836b089573752c1b0da7d144c0948b04e8ea8";
+      sha256 = "13igdbqsxb3sz0h417k6ifmq2n4siwqspj6slhc7fdl5wd1fxmdz";
+    });
 
 
   meta = with stdenv.lib; {

From e606e4d6a9280eccca9a15e08c7e5b62b95575b0 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sun, 14 Jan 2018 00:55:12 -0600
Subject: [PATCH 121/173] lvm2: musl patches from alpine

---
 pkgs/os-specific/linux/lvm2/default.nix | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/pkgs/os-specific/linux/lvm2/default.nix b/pkgs/os-specific/linux/lvm2/default.nix
index d6c1504fdf4..bd84e121a7d 100644
--- a/pkgs/os-specific/linux/lvm2/default.nix
+++ b/pkgs/os-specific/linux/lvm2/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, pkgconfig, systemd, libudev, utillinux, coreutils, libuuid
+{ stdenv, fetchurl, fetchpatch, pkgconfig, systemd, libudev, utillinux, coreutils, libuuid
 , thin-provisioning-tools, enable_dmeventd ? false }:
 
 let
@@ -41,6 +41,23 @@ stdenv.mkDerivation {
   enableParallelBuilding = true;
 
   #patches = [ ./purity.patch ];
+  patches = stdenv.lib.optionals stdenv.hostPlatform.isMusl [
+    (fetchpatch {
+      name = "fix-stdio-usage.patch";
+      url = "https://git.alpinelinux.org/cgit/aports/plain/main/lvm2/fix-stdio-usage.patch?h=3.7-stable&id=31bd4a8c2dc00ae79a821f6fe0ad2f23e1534f50";
+      sha256 = "0m6wr6qrvxqi2d2h054cnv974jq1v65lqxy05g1znz946ga73k3p";
+    })
+    (fetchpatch {
+      name = "mallinfo.patch";
+      url = "https://git.alpinelinux.org/cgit/aports/plain/main/lvm2/mallinfo.patch?h=3.7-stable&id=31bd4a8c2dc00ae79a821f6fe0ad2f23e1534f50";
+      sha256 = "0g6wlqi215i5s30bnbkn8w7axrs27y3bnygbpbnf64wwx7rxxlj0";
+    })
+    (fetchpatch {
+      name = "mlockall-default-config.patch";
+      url = "https://git.alpinelinux.org/cgit/aports/plain/main/lvm2/mlockall-default-config.patch?h=3.7-stable&id=31bd4a8c2dc00ae79a821f6fe0ad2f23e1534f50";
+      sha256 = "1ivbj3sphgf8n1ykfiv5rbw7s8dgnj5jcr9jl2v8cwf28lkacw5l";
+    })
+  ];
 
   # To prevent make install from failing.
   preInstall = "installFlags=\"OWNER= GROUP= confdir=$out/etc\"";

From 5aed799ae0923b94c5a38c07c5885d20b9d42156 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sun, 14 Jan 2018 01:02:05 -0600
Subject: [PATCH 122/173] iputils: only use libidn on non-musl

---
 pkgs/os-specific/linux/iputils/default.nix | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/pkgs/os-specific/linux/iputils/default.nix b/pkgs/os-specific/linux/iputils/default.nix
index dd5770744bf..fd07f365e48 100644
--- a/pkgs/os-specific/linux/iputils/default.nix
+++ b/pkgs/os-specific/linux/iputils/default.nix
@@ -23,11 +23,12 @@ stdenv.mkDerivation rec {
       -i doc/Makefile
   '';
 
-  makeFlags = "USE_GNUTLS=no";
+  # Disable idn usage w/musl: https://github.com/iputils/iputils/pull/111
+  makeFlags = [ "USE_GNUTLS=no" ] ++ stdenv.lib.optional stdenv.hostPlatform.isMusl "USE_IDN=no";
 
   buildInputs = [
-    libsysfs opensp openssl libcap docbook_sgml_dtd_31 SGMLSpm libgcrypt libidn nettle
-  ];
+    libsysfs opensp openssl libcap docbook_sgml_dtd_31 SGMLSpm libgcrypt nettle
+  ] ++ stdenv.lib.optional (!stdenv.hostPlatform.isMusl) libidn;
 
   buildFlags = "man all ninfod";
 

From 551f0702c24536e4918c6239de9f32772a6ef165 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sun, 14 Jan 2018 15:14:28 -0600
Subject: [PATCH 123/173] polkit: patch to fix w/musl, POSIX

---
 pkgs/development/libraries/polkit/default.nix | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/pkgs/development/libraries/polkit/default.nix b/pkgs/development/libraries/polkit/default.nix
index b0d8b2d89bc..6bf9bcb954c 100644
--- a/pkgs/development/libraries/polkit/default.nix
+++ b/pkgs/development/libraries/polkit/default.nix
@@ -37,7 +37,15 @@ stdenv.mkDerivation rec {
       url = "http://src.fedoraproject.org/cgit/rpms/polkit.git/plain/polkit-0.113-itstool.patch?id=fa6fd575804de92886c95d3bc2b7eb2abcd13760";
       sha256 = "0bxmjwp8ahy1y5g1l0kxmld0l3mlvb2l0i5n1qabia3d5iyjkyfh";
     })
-  ];
+  ]
+  # Could be applied uncondtionally but don't want to trigger rebuild
+  # https://bugs.freedesktop.org/show_bug.cgi?id=50145
+  ++ stdenv.lib.optional stdenv.hostPlatform.isMusl (fetchpatch {
+    name = "netgroup-optional.patch";
+    url = "https://bugs.freedesktop.org/attachment.cgi?id=118753";
+    sha256 = "1zq51dhmqi9zi86bj9dq4i4pxlxm41k3k4a091j07bd78cjba038";
+  });
+
 
   outputs = [ "bin" "dev" "out" ]; # small man pages in $bin
 

From f48258fed25a77011e2cc7d629c7b7c22b1a2547 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sun, 14 Jan 2018 15:31:19 -0600
Subject: [PATCH 124/173] smartmontools: patch from upstream project

---
 pkgs/tools/system/smartmontools/default.nix | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/pkgs/tools/system/smartmontools/default.nix b/pkgs/tools/system/smartmontools/default.nix
index 5fa55b275fe..e5b2d54e585 100644
--- a/pkgs/tools/system/smartmontools/default.nix
+++ b/pkgs/tools/system/smartmontools/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, autoreconfHook
+{ stdenv, fetchurl, fetchpatch, autoreconfHook
 , IOKit ? null , ApplicationServices ? null }:
 
 let
@@ -20,7 +20,14 @@ in stdenv.mkDerivation rec {
     sha256 = "0m1hllbb78rr6cxkbalmz1gqkl0psgq8rrmv4gwcmz34n07kvx2i";
   };
 
-  patches = [ ./smartmontools.patch ];
+  patches = [ ./smartmontools.patch ]
+    # https://www.smartmontools.org/changeset/4603
+    ++ stdenv.lib.optional stdenv.hostPlatform.isMusl (fetchpatch {
+      name = "musl-canonicalize_file_name.patch";
+      url = "https://www.smartmontools.org/changeset/4603?format=diff&new=4603";
+      sha256 = "06s9pcd95snjkrbfrsjby2lln3lnwjd21bgabmvr4p7fx19b75zp";
+      stripLen = 2;
+    });
   postPatch = "cp -v ${driverdb} drivedb.h";
 
   nativeBuildInputs = [ autoreconfHook ];

From 3c750912e1b092d338d1456d248d9008dca3c850 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sun, 14 Jan 2018 15:49:30 -0600
Subject: [PATCH 125/173] wireshark: fix build w/musl using upstream commit

---
 .../networking/sniffers/wireshark/default.nix             | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/pkgs/applications/networking/sniffers/wireshark/default.nix b/pkgs/applications/networking/sniffers/wireshark/default.nix
index 5556a58ec56..84e10fb18bc 100644
--- a/pkgs/applications/networking/sniffers/wireshark/default.nix
+++ b/pkgs/applications/networking/sniffers/wireshark/default.nix
@@ -40,7 +40,13 @@ in stdenv.mkDerivation {
     ++ optionals stdenv.isLinux  [ libcap libnl ]
     ++ optionals stdenv.isDarwin [ SystemConfiguration ApplicationServices gmp ];
 
-  patches = [ ./wireshark-lookup-dumpcap-in-path.patch ];
+  patches = [ ./wireshark-lookup-dumpcap-in-path.patch ]
+    # https://code.wireshark.org/review/#/c/23728/
+    ++ stdenv.lib.optional stdenv.hostPlatform.isMusl (fetchpatch {
+      name = "fix-timeout.patch";
+      url = "https://code.wireshark.org/review/gitweb?p=wireshark.git;a=commitdiff_plain;h=8b5b843fcbc3e03e0fc45f3caf8cf5fc477e8613;hp=94af9724d140fd132896b650d10c4d060788e4f0";
+      sha256 = "1g2dm7lwsnanwp68b9xr9swspx7hfj4v3z44sz3yrfmynygk8zlv";
+    });
 
   postInstall = optionalString (withQt || withGtk) ''
     ${optionalString withGtk ''

From 9bc8127dc3cb2354851d8f6859b82ca67c4fe2b8 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Mon, 15 Jan 2018 11:20:33 -0600
Subject: [PATCH 126/173] bootstrap-tools-cross, release-cross: add various
 musl entries

---
 .../stdenv/linux/make-bootstrap-tools-cross.nix | 17 ++++++++++-------
 pkgs/top-level/release-cross.nix                | 11 ++++++++++-
 2 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/pkgs/stdenv/linux/make-bootstrap-tools-cross.nix b/pkgs/stdenv/linux/make-bootstrap-tools-cross.nix
index 10adb0a635d..d08be79e6c5 100644
--- a/pkgs/stdenv/linux/make-bootstrap-tools-cross.nix
+++ b/pkgs/stdenv/linux/make-bootstrap-tools-cross.nix
@@ -7,11 +7,14 @@ let
   };
 
 in with (import ../../../lib).systems.examples; {
-  armv5tel  = make sheevaplug;
-  scaleway  = make scaleway-c1;
-  pogoplug4 = make pogoplug4;
-  armv6l    = make raspberryPi;
-  armv7l    = make armv7l-hf-multiplatform;
-  aarch64   = make aarch64-multiplatform;
-  musl      = make musl64;
+  armv5tel   = make sheevaplug;
+  scaleway   = make scaleway-c1;
+  pogoplug4  = make pogoplug4;
+  armv6l     = make raspberryPi;
+  armv7l     = make armv7l-hf-multiplatform;
+  aarch64    = make aarch64-multiplatform;
+  x86_64-musl  = make musl64;
+  i686-musl    = make musl32;
+  armv6l-musl  = make muslpi;
+  aarch64-musl = make aarch64-multiplatform-musl;
 }
diff --git a/pkgs/top-level/release-cross.nix b/pkgs/top-level/release-cross.nix
index 4c9cbde8100..61cf865371d 100644
--- a/pkgs/top-level/release-cross.nix
+++ b/pkgs/top-level/release-cross.nix
@@ -125,7 +125,16 @@ in
   android = mapTestOnCross lib.systems.examples.aarch64-multiplatform (linuxCommon // {
   });
 
-  musl = mapTestOnCross lib.systems.examples.musl64 linuxCommon;
+  x86_64-musl = mapTestOnCross lib.systems.examples.musl64 linuxCommon;
+  rpi-musl = mapTestOnCross lib.systems.examples.muslpi  (linuxCommon // {
+    vim = nativePlatforms;
+    unzip = nativePlatforms;
+    ddrescue = nativePlatforms;
+    lynx = nativePlatforms;
+    patchelf = nativePlatforms;
+    buildPackages.binutils = nativePlatforms;
+    mpg123 = nativePlatforms;
+  });
 
   /* Cross-built bootstrap tools for every supported platform */
   bootstrapTools = let

From c8d03e6298dfca9ed340320ffb05acf30871b4d5 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Mon, 5 Feb 2018 18:12:47 -0600
Subject: [PATCH 127/173] make-bootstrap-tools: grab libc from stdenv.cc.libc

This is the same in current cases AFAICT,
other than uses musl instead of glibc when musl-native.
---
 pkgs/stdenv/linux/make-bootstrap-tools.nix | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/pkgs/stdenv/linux/make-bootstrap-tools.nix b/pkgs/stdenv/linux/make-bootstrap-tools.nix
index b659e259ee1..b0c4b284f75 100644
--- a/pkgs/stdenv/linux/make-bootstrap-tools.nix
+++ b/pkgs/stdenv/linux/make-bootstrap-tools.nix
@@ -4,9 +4,7 @@
 
 let
   pkgs = import ../../.. { inherit localSystem crossSystem; };
-  libc = if pkgs.hostPlatform != pkgs.buildPlatform
-          then pkgs.libcCross
-          else pkgs.glibc;
+  libc = pkgs.stdenv.cc.libc;
 in with pkgs; rec {
 
 

From db9f302cf39d758dfe47c011ffe80e78e7b66755 Mon Sep 17 00:00:00 2001
From: Ben Gamari <ben@smart-cactus.org>
Date: Tue, 17 Oct 2017 16:38:10 -0400
Subject: [PATCH 128/173] libcap: Fix cross-compilation

---
 pkgs/os-specific/linux/libcap/default.nix | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/pkgs/os-specific/linux/libcap/default.nix b/pkgs/os-specific/linux/libcap/default.nix
index 17cd40e427b..15fa8708315 100644
--- a/pkgs/os-specific/linux/libcap/default.nix
+++ b/pkgs/os-specific/linux/libcap/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, attr, perl, pam ? null }:
+{ stdenv, buildPackages, fetchurl, attr, perl, pam ? null }:
 assert pam != null -> stdenv.isLinux;
 
 stdenv.mkDerivation rec {
@@ -13,7 +13,7 @@ stdenv.mkDerivation rec {
   outputs = [ "out" "dev" "lib" "man" "doc" ]
     ++ stdenv.lib.optional (pam != null) "pam";
 
-  nativeBuildInputs = [ perl ];
+  nativeBuildInputs = [ perl buildPackages.stdenv.cc ];
 
   buildInputs = [ pam ];
 
@@ -22,6 +22,8 @@ stdenv.mkDerivation rec {
   makeFlags = [
     "lib=lib"
     (stdenv.lib.optional (pam != null) "PAM_CAP=yes")
+    "BUILD_CC=${buildPackages.stdenv.cc.targetPrefix}gcc"
+    "CC=${stdenv.cc.targetPrefix}gcc"
   ];
 
   prePatch = ''

From cea0765a299d3497bad285d533e527ba98f02304 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 6 Feb 2018 07:30:52 -0600
Subject: [PATCH 129/173] libcap: depsBuildBuild, don't set CC/BUILD_CC

---
 pkgs/os-specific/linux/libcap/default.nix | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/pkgs/os-specific/linux/libcap/default.nix b/pkgs/os-specific/linux/libcap/default.nix
index 15fa8708315..f00d6272902 100644
--- a/pkgs/os-specific/linux/libcap/default.nix
+++ b/pkgs/os-specific/linux/libcap/default.nix
@@ -13,7 +13,8 @@ stdenv.mkDerivation rec {
   outputs = [ "out" "dev" "lib" "man" "doc" ]
     ++ stdenv.lib.optional (pam != null) "pam";
 
-  nativeBuildInputs = [ perl buildPackages.stdenv.cc ];
+  depsBuildBuild = [ buildPackages.stdenv.cc ];
+  nativeBuildInputs = [ perl ];
 
   buildInputs = [ pam ];
 
@@ -22,8 +23,8 @@ stdenv.mkDerivation rec {
   makeFlags = [
     "lib=lib"
     (stdenv.lib.optional (pam != null) "PAM_CAP=yes")
-    "BUILD_CC=${buildPackages.stdenv.cc.targetPrefix}gcc"
-    "CC=${stdenv.cc.targetPrefix}gcc"
+    "BUILD_CC=$(CC_FOR_BUILD)"
+    "CC:=$(CC)"
   ];
 
   prePatch = ''

From 5b761a86871583485ebc8673080d16b93ce87eae Mon Sep 17 00:00:00 2001
From: Ben Gamari <ben@smart-cactus.org>
Date: Wed, 18 Oct 2017 00:10:08 -0400
Subject: [PATCH 130/173] nettle: Enable cross-compilation

---
 pkgs/development/libraries/nettle/generic.nix | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/pkgs/development/libraries/nettle/generic.nix b/pkgs/development/libraries/nettle/generic.nix
index 9633dacd68f..8999b1c2a9a 100644
--- a/pkgs/development/libraries/nettle/generic.nix
+++ b/pkgs/development/libraries/nettle/generic.nix
@@ -1,4 +1,4 @@
-{ stdenv, gmp, gnum4
+{ stdenv, buildPackages, gmp, gnum4
 
 # Version specific args
 , version, src
@@ -12,16 +12,18 @@ stdenv.mkDerivation (rec {
   outputs = [ "out" "dev" ];
   outputBin = "dev";
 
-  buildInputs = [ gnum4 ];
+  nativeBuildInputs = [ gnum4 buildPackages.stdenv.cc ];
   propagatedBuildInputs = [ gmp ];
 
-  doCheck = (stdenv.system != "i686-cygwin" && !stdenv.isDarwin);
+  doCheck = (stdenv.system != "i686-cygwin" && !stdenv.isDarwin && stdenv.buildPlatform == stdenv.hostPlatform);
 
   enableParallelBuilding = true;
 
   patches = stdenv.lib.optional (stdenv.system == "i686-cygwin")
               ./cygwin.patch;
 
+  configureFlags = '' CC_FOR_BUILD=${buildPackages.stdenv.cc.targetPrefix}gcc '';
+
   meta = with stdenv.lib; {
     description = "Cryptographic library";
 

From c0d343bc5ff08078356080f821af3d0d101d4092 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Mon, 22 Jan 2018 15:03:37 -0600
Subject: [PATCH 131/173] patchelfUnstable: fix build on musl, disable failing
 test

---
 pkgs/development/tools/misc/patchelf/unstable.nix | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/pkgs/development/tools/misc/patchelf/unstable.nix b/pkgs/development/tools/misc/patchelf/unstable.nix
index 62647879865..de68a4066d7 100644
--- a/pkgs/development/tools/misc/patchelf/unstable.nix
+++ b/pkgs/development/tools/misc/patchelf/unstable.nix
@@ -10,6 +10,12 @@ stdenv.mkDerivation rec {
     sha256 = "1f1s8q3as3nrhcc1a8qc2z7imm644jfz44msn9sfv4mdynp2m2yb";
   };
 
+  # Drop test that fails on musl (?)
+  postPatch = stdenv.lib.optionalString stdenv.hostPlatform.isMusl ''
+    substituteInPlace tests/Makefile.am \
+      --replace "set-rpath-library.sh" ""
+  '';
+
   setupHook = [ ./setup-hook.sh ];
 
   nativeBuildInputs = [ autoreconfHook ];

From d67638cc13a5032f06566ba32dbfdc44e440da96 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 23 Jan 2018 15:15:48 -0600
Subject: [PATCH 132/173] screen: fix utmp messages w/musl

/dev/null/utmp
---
 pkgs/tools/misc/screen/default.nix | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/pkgs/tools/misc/screen/default.nix b/pkgs/tools/misc/screen/default.nix
index 156db640acd..1753f52ab1b 100644
--- a/pkgs/tools/misc/screen/default.nix
+++ b/pkgs/tools/misc/screen/default.nix
@@ -16,6 +16,13 @@ stdenv.mkDerivation rec {
     "--enable-colors256"
   ];
 
+  patches = stdenv.lib.optional stdenv.hostPlatform.isMusl
+    (fetchpatch {
+      url = https://gist.githubusercontent.com/yujinakayama/4608863/raw/76b9f89af5e5a2e97d9a0f36aac989fb56cf1447/gistfile1.diff;
+      sha256 = "0f9bf83p8zdxaa1pr75jyf5g8xr3r8kv7cyzzbpraa1q4j15ss1p";
+      stripLen = 1;
+    });
+
   buildInputs = [ ncurses ] ++ stdenv.lib.optional stdenv.isLinux pam
                             ++ stdenv.lib.optional stdenv.isDarwin utmp;
 

From 94ab0da6b13d861f53da0e28a9bf30bbeca55d55 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sun, 14 Jan 2018 01:38:17 -0600
Subject: [PATCH 133/173] lsof: fix build w/musl

---
 pkgs/development/tools/misc/lsof/default.nix | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/pkgs/development/tools/misc/lsof/default.nix b/pkgs/development/tools/misc/lsof/default.nix
index aa6bd003ed3..224e0aba6ef 100644
--- a/pkgs/development/tools/misc/lsof/default.nix
+++ b/pkgs/development/tools/misc/lsof/default.nix
@@ -30,6 +30,10 @@ stdenv.mkDerivation rec {
 
   patches = [ ./dfile.patch ];
 
+  postPatch = stdenv.lib.optionalString stdenv.hostPlatform.isMusl ''
+    substituteInPlace dialects/linux/dlsof.h --replace "defined(__UCLIBC__)" 1
+  '';
+
   # Stop build scripts from searching global include paths
   LSOF_INCLUDE = "${stdenv.cc.libc}/include";
   configurePhase = "LINUX_CONF_CC=$CC_FOR_BUILD LSOF_CC=$CC LSOF_AR=\"$AR cr\" LSOF_RANLIB=$RANLIB ./Configure -n ${dialect}";

From 5d6e229ec0dbd99d2aba37a31c65b2e9849e9eb5 Mon Sep 17 00:00:00 2001
From: Ben Gamari <ben@smart-cactus.org>
Date: Sun, 10 Dec 2017 01:33:27 -0500
Subject: [PATCH 134/173] nixUnstable: Enable cross-compilation

Older nix versions likely aren't feasible due to the perl dependency.
---
 pkgs/tools/package-management/nix/default.nix | 25 +++++--------------
 1 file changed, 6 insertions(+), 19 deletions(-)

diff --git a/pkgs/tools/package-management/nix/default.nix b/pkgs/tools/package-management/nix/default.nix
index 91844bdb9dd..ce95f31112d 100644
--- a/pkgs/tools/package-management/nix/default.nix
+++ b/pkgs/tools/package-management/nix/default.nix
@@ -2,7 +2,7 @@
 , pkgconfig, boehmgc, perlPackages, libsodium, aws-sdk-cpp, brotli
 , autoreconfHook, autoconf-archive, bison, flex, libxml2, libxslt, docbook5, docbook5_xsl
 , libseccomp, busybox-sandbox-shell
-, hostPlatform
+, hostPlatform, buildPlatform
 , storeDir ? "/nix/store"
 , stateDir ? "/nix/var"
 , confDir ? "/etc"
@@ -52,35 +52,22 @@ let
         "--with-www-curl=${perlPackages.WWWCurl}/${perl.libPrefix}"
       ] ++ lib.optionals (is20 && stdenv.isLinux) [
         "--with-sandbox-shell=${sh}/bin/busybox"
-      ];
+      ]
+      ++ lib.optional (
+          hostPlatform != buildPlatform && hostPlatform ? nix && hostPlatform.nix ? system
+      ) ''--with-system=${hostPlatform.nix.system}'';
 
     makeFlags = "profiledir=$(out)/etc/profile.d";
 
     installFlags = "sysconfdir=$(out)/etc";
 
-    doInstallCheck = true;
+    doInstallCheck = hostPlatform == buildPlatform;
 
     # socket path becomes too long otherwise
     preInstallCheck = lib.optional stdenv.isDarwin "export TMPDIR=/tmp";
 
     separateDebugInfo = stdenv.isLinux;
 
-    crossAttrs = {
-      configureFlags =
-        ''
-          --with-store-dir=${storeDir} --localstatedir=${stateDir}
-          --with-dbi=${perlPackages.DBI}/${perl.libPrefix}
-          --with-dbd-sqlite=${perlPackages.DBDSQLite}/${perl.libPrefix}
-          --with-www-curl=${perlPackages.WWWCurl}/${perl.libPrefix}
-          --disable-init-state
-          --enable-gc
-        '' + stdenv.lib.optionalString (
-            hostPlatform ? nix && hostPlatform.nix ? system
-        ) ''--with-system=${hostPlatform.nix.system}'';
-
-      doInstallCheck = false;
-    };
-
     enableParallelBuilding = true;
 
     meta = {

From 5a802afb750a575b369aa69843d96f85f89607bb Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sun, 28 Jan 2018 09:39:07 +0900
Subject: [PATCH 135/173] nix: doInstallCheck is disabled on cross already.

---
 pkgs/tools/package-management/nix/default.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/tools/package-management/nix/default.nix b/pkgs/tools/package-management/nix/default.nix
index ce95f31112d..ee5789d6265 100644
--- a/pkgs/tools/package-management/nix/default.nix
+++ b/pkgs/tools/package-management/nix/default.nix
@@ -61,7 +61,7 @@ let
 
     installFlags = "sysconfdir=$(out)/etc";
 
-    doInstallCheck = hostPlatform == buildPlatform;
+    doInstallCheck = true; # not cross
 
     # socket path becomes too long otherwise
     preInstallCheck = lib.optional stdenv.isDarwin "export TMPDIR=/tmp";

From ee4f04dcd7783331833361e783a7b3fbd420521c Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sun, 28 Jan 2018 09:46:12 +0900
Subject: [PATCH 136/173] aws-sdk-cpp: disable tests on cross

---
 pkgs/development/libraries/aws-sdk-cpp/default.nix | 1 +
 1 file changed, 1 insertion(+)

diff --git a/pkgs/development/libraries/aws-sdk-cpp/default.nix b/pkgs/development/libraries/aws-sdk-cpp/default.nix
index 47d9e7dba4c..26511c22d0b 100644
--- a/pkgs/development/libraries/aws-sdk-cpp/default.nix
+++ b/pkgs/development/libraries/aws-sdk-cpp/default.nix
@@ -33,6 +33,7 @@ in stdenv.mkDerivation rec {
 
   cmakeFlags =
     lib.optional (!customMemoryManagement) "-DCUSTOM_MEMORY_MANAGEMENT=0"
+    ++ lib.optional (stdenv.buildPlatform != stdenv.hostPlatform) "-DENABLE_TESTING=OFF"
     ++ lib.optional (apis != ["*"])
       "-DBUILD_ONLY=${lib.concatStringsSep ";" apis}";
 

From 7fbb42eabb540bd15378e87156cca262f8c4065d Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 30 Jan 2018 17:17:45 -0600
Subject: [PATCH 137/173] Similarly, use 4.15 kernel headers instead of
 patching 4.14 w/uapi fixes

---
 .../linux/kernel-headers/default.nix          | 19 +++----------------
 pkgs/top-level/all-packages.nix               |  4 ++--
 2 files changed, 5 insertions(+), 18 deletions(-)

diff --git a/pkgs/os-specific/linux/kernel-headers/default.nix b/pkgs/os-specific/linux/kernel-headers/default.nix
index a2b37111dc7..36aeb71264f 100644
--- a/pkgs/os-specific/linux/kernel-headers/default.nix
+++ b/pkgs/os-specific/linux/kernel-headers/default.nix
@@ -69,21 +69,8 @@ in {
     sha256 = "1kpjvvd9q9wwr3314q5ymvxii4dv2d27295bzly225wlc552xhja";
   };
 
-  linuxHeaders_4_14 = common {
-    version = "4.14.13";
-    sha256 = "0wjpwhrnnvf6l3zpkkxk34dl722w9yp8j3vnh0xzi3hgb8dnvd2a";
-
-    patches = [
-      (fetchurl {
-        name = "uapi_libc_compat.patch";
-        url = "https://patchwork.ozlabs.org/patch/854342/raw/";
-        sha256 = "0qczlgqfbw0czx63wg2zgla15zpmcc76d00cb7qwl514ysm4ihmj";
-      })
-      (fetchurl {
-        name = "struct_ethhdr.patch";
-        url = "https://patchwork.ozlabs.org/patch/855293/raw/";
-        sha256 = "0019nxilbgv986sswxyvii50l5l3n9yp4ysgnjdp9104plcq9956";
-      })
-    ];
+  linuxHeaders_4_15 = common {
+    version = "4.15";
+    sha256 = "0sd7l9n9h7vf9c6gd6ciji28hawda60yj0llh17my06m0s4lf9js";
   };
 }
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index c8c143abddb..945c3a58036 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -12928,8 +12928,8 @@ with pkgs;
   lkl = callPackage ../applications/virtualization/lkl { };
 
   inherit (callPackages ../os-specific/linux/kernel-headers { })
-    linuxHeaders_4_4 linuxHeaders_4_14;
-  linuxHeaders = if hostPlatform.isMusl then linuxHeaders_4_14 else linuxHeaders_4_4;
+    linuxHeaders_4_4 linuxHeaders_4_15;
+  linuxHeaders = if hostPlatform.isMusl then linuxHeaders_4_15 else linuxHeaders_4_4;
 
   kernelPatches = callPackage ../os-specific/linux/kernel/patches.nix { };
 

From d8b15f8b05404957897cbb2b92580c57d97b2de9 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 30 Jan 2018 17:47:50 -0600
Subject: [PATCH 138/173] numactl: 2.0.10 -> 2.0.11

grab release from github, old homepage seems defunct?
---
 pkgs/os-specific/linux/numactl/default.nix | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/pkgs/os-specific/linux/numactl/default.nix b/pkgs/os-specific/linux/numactl/default.nix
index a5ed242e323..3892a90745b 100644
--- a/pkgs/os-specific/linux/numactl/default.nix
+++ b/pkgs/os-specific/linux/numactl/default.nix
@@ -1,11 +1,14 @@
-{ stdenv, fetchurl, autoreconfHook }:
+{ stdenv, fetchFromGitHub, autoreconfHook }:
 
 stdenv.mkDerivation rec {
-  name = "numactl-2.0.10";
+  name = "numactl-${version}";
+  version = "2.0.11";
 
-  src = fetchurl {
-    url = "ftp://oss.sgi.com/www/projects/libnuma/download/${name}.tar.gz";
-    sha256 = "0qfv2ks6d3gm0mw5sj4cbhsd7cbsb7qm58xvchl2wfzifkzcinnv";
+  src = fetchFromGitHub {
+    owner = "numactl";
+    repo = "numactl";
+    rev = "v${version}";
+    sha256 = "0bcffqawwbyrnza8np0whii25mfd0dria35zal9v3l55xcrya3j9";
   };
 
   nativeBuildInputs = [ autoreconfHook ];

From ad09de4d13c9244c86ab55a2269ccbb16cbfbd3d Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 30 Jan 2018 18:00:53 -0600
Subject: [PATCH 139/173] numactl: fix use of deprecated macros, patch to fix
 w/musl

---
 pkgs/os-specific/linux/numactl/default.nix | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/pkgs/os-specific/linux/numactl/default.nix b/pkgs/os-specific/linux/numactl/default.nix
index 3892a90745b..9928897ae4d 100644
--- a/pkgs/os-specific/linux/numactl/default.nix
+++ b/pkgs/os-specific/linux/numactl/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchFromGitHub, autoreconfHook }:
+{ stdenv, fetchFromGitHub, fetchpatch, autoreconfHook }:
 
 stdenv.mkDerivation rec {
   name = "numactl-${version}";
@@ -13,6 +13,16 @@ stdenv.mkDerivation rec {
 
   nativeBuildInputs = [ autoreconfHook ];
 
+  patches = [
+    (fetchpatch {
+      url = https://raw.githubusercontent.com/gentoo/gentoo/b64d15e731e3d6a7671f0ec6c34a20203cf2609d/sys-process/numactl/files/numactl-2.0.11-sysmacros.patch;
+      sha256 = "05277kv3x12n2xlh3fgnmxclxfc384mkwb0v9pd91046khj6h843";
+    })
+  ] ++ stdenv.lib.optional stdenv.hostPlatform.isMusl (fetchpatch {
+      url = https://git.alpinelinux.org/cgit/aports/plain/testing/numactl/musl.patch?id=0592b128c71c3e70d493bc7a13caed0d7fae91dd;
+      sha256 = "080b0sygmg7104qbbh1amh3b322yyiajwi2d3d0vayffgva0720v";
+    });
+
   meta = with stdenv.lib; {
     description = "Library and tools for non-uniform memory access (NUMA) machines";
     homepage = http://oss.sgi.com/projects/libnuma/;

From 3413562111a141cb7ffb687819dc9d63c52f11c7 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 30 Jan 2018 18:03:17 -0600
Subject: [PATCH 140/173] fftw: disable using openmp w/musl

For now anyway, since we build w/o support for it IIRC.
---
 pkgs/development/libraries/fftw/default.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/development/libraries/fftw/default.nix b/pkgs/development/libraries/fftw/default.nix
index 36c824c7528..70120997107 100644
--- a/pkgs/development/libraries/fftw/default.nix
+++ b/pkgs/development/libraries/fftw/default.nix
@@ -29,7 +29,7 @@ stdenv.mkDerivation rec {
     # all x86_64 have sse2
     # however, not all float sizes fit
     ++ optional (stdenv.isx86_64 && (precision == "single" || precision == "double") )  "--enable-sse2"
-    ++ optional stdenv.cc.isGNU "--enable-openmp"
+    ++ optional (stdenv.cc.isGNU && !stdenv.hostPlatform.isMusl) "--enable-openmp"
     # doc generation causes Fortran wrapper generation which hard-codes gcc
     ++ optional (!withDoc) "--disable-doc";
 

From f33a75a658299e50ee3d01bcc7ec9e4a1d84e5ca Mon Sep 17 00:00:00 2001
From: Ben Gamari <ben@smart-cactus.org>
Date: Tue, 17 Oct 2017 16:05:03 -0400
Subject: [PATCH 141/173] kexectools: Add dependency on build CC

Seems to allow cross-compilation.
---
 pkgs/os-specific/linux/kexectools/default.nix | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/pkgs/os-specific/linux/kexectools/default.nix b/pkgs/os-specific/linux/kexectools/default.nix
index 021353c4709..085ff142096 100644
--- a/pkgs/os-specific/linux/kexectools/default.nix
+++ b/pkgs/os-specific/linux/kexectools/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl, zlib }:
+{ stdenv, buildPackages, fetchurl, zlib }:
 
 stdenv.mkDerivation rec {
   name = "kexec-tools-${version}";
@@ -14,6 +14,7 @@ stdenv.mkDerivation rec {
 
   hardeningDisable = [ "format" "pic" "relro" ];
 
+  nativeBuildInputs = [ buildPackages.stdenv.cc ];
   buildInputs = [ zlib ];
 
   meta = with stdenv.lib; {

From ef5d3d1c9fcba3c8e54a7a8a16ce13ba6119f8aa Mon Sep 17 00:00:00 2001
From: Ben Gamari <ben@smart-cactus.org>
Date: Sun, 10 Dec 2017 00:22:19 -0500
Subject: [PATCH 142/173] kexectools: Fix cross compilation

---
 pkgs/os-specific/linux/kexectools/default.nix | 1 +
 1 file changed, 1 insertion(+)

diff --git a/pkgs/os-specific/linux/kexectools/default.nix b/pkgs/os-specific/linux/kexectools/default.nix
index 085ff142096..c4c5b7cc3b9 100644
--- a/pkgs/os-specific/linux/kexectools/default.nix
+++ b/pkgs/os-specific/linux/kexectools/default.nix
@@ -14,6 +14,7 @@ stdenv.mkDerivation rec {
 
   hardeningDisable = [ "format" "pic" "relro" ];
 
+  configureFlags = [ "BUILD_CC=${buildPackages.stdenv.cc.targetPrefix}cc" ];
   nativeBuildInputs = [ buildPackages.stdenv.cc ];
   buildInputs = [ zlib ];
 

From 6923737bb4a8c03c4d63b478c764199add829423 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 30 Jan 2018 18:19:23 -0600
Subject: [PATCH 143/173] libgpg-error: hack-fix missing header?

---
 pkgs/development/libraries/libgpg-error/default.nix | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/pkgs/development/libraries/libgpg-error/default.nix b/pkgs/development/libraries/libgpg-error/default.nix
index 9faf7a40458..e6cb62330c4 100644
--- a/pkgs/development/libraries/libgpg-error/default.nix
+++ b/pkgs/development/libraries/libgpg-error/default.nix
@@ -9,7 +9,11 @@ stdenv.mkDerivation rec {
     sha256 = "1li95ni122fzinzlmxbln63nmgij63irxfvi52ws4zfbzv3am4sg";
   };
 
-  postPatch = "sed '/BUILD_TIMESTAMP=/s/=.*/=1970-01-01T00:01+0000/' -i ./configure";
+  postPatch = ''
+    sed '/BUILD_TIMESTAMP=/s/=.*/=1970-01-01T00:01+0000/' -i ./configure
+  '' + stdenv.lib.optionalString stdenv.hostPlatform.isMusl ''
+    ln -s lock-obj-pub.x86_64-pc-linux-musl.h src/syscfg/lock-obj-pub.linux-musl.h
+  '';
 
   outputs = [ "out" "dev" "info" ];
   outputBin = "dev"; # deps want just the lib, most likely

From e93dc20ed4a4b630855e96a7d05053cfabe85983 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 2 Feb 2018 06:50:26 -0600
Subject: [PATCH 144/173] icu: xlocale.h fixup needed w/musl as well

---
 pkgs/development/libraries/icu/base.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/development/libraries/icu/base.nix b/pkgs/development/libraries/icu/base.nix
index 6d9a9725cc2..2c8392c8bac 100644
--- a/pkgs/development/libraries/icu/base.nix
+++ b/pkgs/development/libraries/icu/base.nix
@@ -26,7 +26,7 @@ stdenv.mkDerivation {
   '';
 
   # https://sourceware.org/glibc/wiki/Release/2.26#Removal_of_.27xlocale.h.27
-  postPatch = if stdenv ? glibc
+  postPatch = if (stdenv.hostPlatform.libc == "glibc" || stdenv.hostPlatform.libc == "musl")
     then "substituteInPlace i18n/digitlst.cpp --replace '<xlocale.h>' '<locale.h>'"
     else null; # won't find locale_t on darwin
 

From d9b8f173413064e205fe712821087cdbb9f50f0f Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sun, 4 Feb 2018 10:55:31 -0600
Subject: [PATCH 145/173] shadow: fix attempt to access 'stdenv.glibc'

---
 pkgs/os-specific/linux/shadow/default.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/os-specific/linux/shadow/default.nix b/pkgs/os-specific/linux/shadow/default.nix
index ab53dcca025..8875d7ec4b3 100644
--- a/pkgs/os-specific/linux/shadow/default.nix
+++ b/pkgs/os-specific/linux/shadow/default.nix
@@ -9,7 +9,7 @@ let
   glibc =
     if hostPlatform != buildPlatform
     then glibcCross
-    else assert stdenv ? glibc; stdenv.glibc;
+    else assert hostPlatform.libc == "glibc"; stdenv.cc.libc;
 
   dots_in_usernames = fetchpatch {
     url = http://sources.gentoo.org/cgi-bin/viewvc.cgi/gentoo-x86/sys-apps/shadow/files/shadow-4.1.3-dots-in-usernames.patch;

From b281cb20d54801c4e5287d827951e337d6426caa Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sun, 4 Feb 2018 15:31:50 -0600
Subject: [PATCH 146/173] iputils: drop glibc assert

---
 pkgs/os-specific/linux/iputils/default.nix | 2 --
 1 file changed, 2 deletions(-)

diff --git a/pkgs/os-specific/linux/iputils/default.nix b/pkgs/os-specific/linux/iputils/default.nix
index fd07f365e48..a7fbcce3175 100644
--- a/pkgs/os-specific/linux/iputils/default.nix
+++ b/pkgs/os-specific/linux/iputils/default.nix
@@ -4,8 +4,6 @@
 , libidn, nettle
 , SGMLSpm, libgcrypt }:
 
-assert stdenv ? glibc;
-
 let
   time = "20161105";
 in

From 5bf5e6eebfd7077cfb6c513426a179256e009c53 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Sun, 4 Feb 2018 21:18:04 -0600
Subject: [PATCH 147/173] llvm4: always apply fix, improve compat, fix w/musl

---
 pkgs/development/compilers/llvm/4/llvm.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/development/compilers/llvm/4/llvm.nix b/pkgs/development/compilers/llvm/4/llvm.nix
index 17a25889e64..ceaa45fe8d7 100644
--- a/pkgs/development/compilers/llvm/4/llvm.nix
+++ b/pkgs/development/compilers/llvm/4/llvm.nix
@@ -65,7 +65,7 @@ in stdenv.mkDerivation (rec {
     substitute '${./llvm-outputs.patch}' ./llvm-outputs.patch --subst-var lib
     patch -p1 < ./llvm-outputs.patch
   ''
-  + stdenv.lib.optionalString (stdenv ? glibc) ''
+  + ''
     (
       cd projects/compiler-rt
       patch -p1 < ${

From 24721aad180b089e49059cc9745a7aa3feb93026 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Mon, 5 Feb 2018 17:16:20 -0600
Subject: [PATCH 148/173] mariadb: disable tokudb on musl, per docs (and fix
 build)

---
 pkgs/servers/sql/mariadb/default.nix | 1 +
 1 file changed, 1 insertion(+)

diff --git a/pkgs/servers/sql/mariadb/default.nix b/pkgs/servers/sql/mariadb/default.nix
index 88d2ae3d68a..b8ee85d65e7 100644
--- a/pkgs/servers/sql/mariadb/default.nix
+++ b/pkgs/servers/sql/mariadb/default.nix
@@ -62,6 +62,7 @@ common = rec { # attributes common to both builds
     "-DPLUGIN_AUTH_GSSAPI_CLIENT=NO"
   ]
     ++ optional stdenv.isDarwin "-DCURSES_LIBRARY=${ncurses.out}/lib/libncurses.dylib"
+    ++ optional stdenv.hostPlatform.isMusl "-DWITHOUT_TOKUDB=1" # mariadb docs say disable this for musl
     ;
 
   preConfigure = ''

From 7522d6b02fdee3781c46e30eed81c50f4e49dda5 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 6 Feb 2018 07:29:21 -0600
Subject: [PATCH 149/173] busybox: don't force gcc

---
 pkgs/os-specific/linux/busybox/default.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/os-specific/linux/busybox/default.nix b/pkgs/os-specific/linux/busybox/default.nix
index 35237d71390..c83a526b246 100644
--- a/pkgs/os-specific/linux/busybox/default.nix
+++ b/pkgs/os-specific/linux/busybox/default.nix
@@ -87,7 +87,7 @@ stdenv.mkDerivation rec {
   '';
 
   postConfigure = lib.optionalString useMusl ''
-    makeFlagsArray+=("CC=${stdenv.cc.targetPrefix}gcc -isystem ${musl.dev}/include -B${musl}/lib -L${musl}/lib")
+    makeFlagsArray+=("CC=${stdenv.cc.targetPrefix}cc -isystem ${musl.dev}/include -B${musl}/lib -L${musl}/lib")
   '';
 
   depsBuildBuild = [ buildPackages.stdenv.cc ];

From 22a5393041bd263111e0a7935205876851063969 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 6 Feb 2018 08:55:52 -0600
Subject: [PATCH 150/173] pam: depsBuildBuild for buildPackages.stdenv.cc

---
 pkgs/os-specific/linux/pam/default.nix | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/pkgs/os-specific/linux/pam/default.nix b/pkgs/os-specific/linux/pam/default.nix
index fb33f9555e5..5f92dfcc839 100644
--- a/pkgs/os-specific/linux/pam/default.nix
+++ b/pkgs/os-specific/linux/pam/default.nix
@@ -26,7 +26,8 @@ stdenv.mkDerivation rec {
 
   outputs = [ "out" "doc" "man" /* "modules" */ ];
 
-  nativeBuildInputs = [ buildPackages.stdenv.cc flex ];
+  depsBuildBuild = [ buildPackages.stdenv.cc ];
+  nativeBuildInputs = [ flex ];
 
   buildInputs = [ cracklib ];
 

From 119920faa6717f8075e0ea44553203171a71f9ea Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 9 Feb 2018 08:43:39 -0600
Subject: [PATCH 151/173] restore stdenv.glibc, will remove separately.

---
 pkgs/stdenv/linux/default.nix | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/pkgs/stdenv/linux/default.nix b/pkgs/stdenv/linux/default.nix
index a214f8a02c3..2baa8c0479c 100644
--- a/pkgs/stdenv/linux/default.nix
+++ b/pkgs/stdenv/linux/default.nix
@@ -107,7 +107,8 @@ let
 
           # stdenv.glibc is used by GCC build to figure out the system-level
           # /usr/include directory.
-          # inherit (prevStage) glibc;
+          # TODO: Remove this!
+          inherit (prevStage) glibc;
         };
         overrides = self: super: (overrides self super) // { fetchurl = thisStdenv.fetchurlBoot; };
       };
@@ -333,7 +334,9 @@ in
       inherit (prevStage.stdenv) fetchurlBoot;
 
       extraAttrs = {
-        # inherit (prevStage) glibc;
+        # TODO: remove this!
+        inherit (prevStage) glibc;
+
         inherit platform bootstrapTools;
         shellPackage = prevStage.bash;
       };

From 9bd4e00ce3698ded0c6a03745d16f51355fa119f Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Fri, 9 Feb 2018 10:08:36 -0600
Subject: [PATCH 152/173] gcc6: cross-musl disable libmpx

---
 pkgs/development/compilers/gcc/6/default.nix | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/pkgs/development/compilers/gcc/6/default.nix b/pkgs/development/compilers/gcc/6/default.nix
index 2b0c4bfdde3..df0c1578dae 100644
--- a/pkgs/development/compilers/gcc/6/default.nix
+++ b/pkgs/development/compilers/gcc/6/default.nix
@@ -168,6 +168,8 @@ let version = "6.4.0";
             # In uclibc cases, libgomp needs an additional '-ldl'
             # and as I don't know how to pass it, I disable libgomp.
             "--disable-libgomp"
+            # musl at least, disable: https://git.buildroot.net/buildroot/commit/?id=873d4019f7fb00f6a80592224236b3ba7d657865
+            "--disable-libmpx"
           ] ++ [
           "--enable-threads=posix"
           "--enable-nls"

From 4f8292fb35dfff1fad141cffd27ac0710857ec15 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Mon, 12 Feb 2018 13:55:29 -0600
Subject: [PATCH 153/173] python2.7: drop unused cross-compile patch

---
 .../python/cpython/2.7/cross-compile.patch    | 44 -------------------
 .../python/cpython/2.7/default.nix            |  5 ---
 2 files changed, 49 deletions(-)
 delete mode 100644 pkgs/development/interpreters/python/cpython/2.7/cross-compile.patch

diff --git a/pkgs/development/interpreters/python/cpython/2.7/cross-compile.patch b/pkgs/development/interpreters/python/cpython/2.7/cross-compile.patch
deleted file mode 100644
index 4d0c72675cc..00000000000
--- a/pkgs/development/interpreters/python/cpython/2.7/cross-compile.patch
+++ /dev/null
@@ -1,44 +0,0 @@
-commit 9ac1104b075ecea6175e2214b81c6538adbb9e84
-Author: Ben Gamari <ben@smart-cactus.org>
-Date:   Tue Oct 17 23:53:25 2017 -0400
-
-    Fix it
-
-diff --git a/Makefile.pre.in b/Makefile.pre.in
-index e288964..ddc7428 100644
---- a/Makefile.pre.in
-+++ b/Makefile.pre.in
-@@ -1120,27 +1120,27 @@ libinstall:	build_all $(srcdir)/Lib/$(PLATDIR) $(srcdir)/Modules/xxmodule.c
- 		$(INSTALL_DATA) $(srcdir)/Modules/xxmodule.c \
- 			$(DESTDIR)$(LIBDEST)/distutils/tests ; \
- 	fi
--	PYTHONPATH=$(DESTDIR)$(LIBDEST)  $(RUNSHARED) \
-+	$(RUNSHARED) \
- 		$(PYTHON_FOR_BUILD) -Wi -tt $(DESTDIR)$(LIBDEST)/compileall.py \
- 		-d $(LIBDEST) -f \
- 		-x 'bad_coding|badsyntax|site-packages|lib2to3/tests/data' \
- 		$(DESTDIR)$(LIBDEST)
--	PYTHONPATH=$(DESTDIR)$(LIBDEST) $(RUNSHARED) \
-+	$(RUNSHARED) \
- 		$(PYTHON_FOR_BUILD) -Wi -tt -O $(DESTDIR)$(LIBDEST)/compileall.py \
- 		-d $(LIBDEST) -f \
- 		-x 'bad_coding|badsyntax|site-packages|lib2to3/tests/data' \
- 		$(DESTDIR)$(LIBDEST)
--	-PYTHONPATH=$(DESTDIR)$(LIBDEST)  $(RUNSHARED) \
-+	-$(RUNSHARED) \
- 		$(PYTHON_FOR_BUILD) -Wi -t $(DESTDIR)$(LIBDEST)/compileall.py \
- 		-d $(LIBDEST)/site-packages -f \
- 		-x badsyntax $(DESTDIR)$(LIBDEST)/site-packages
--	-PYTHONPATH=$(DESTDIR)$(LIBDEST) $(RUNSHARED) \
-+	-$(RUNSHARED) \
- 		$(PYTHON_FOR_BUILD) -Wi -t -O $(DESTDIR)$(LIBDEST)/compileall.py \
- 		-d $(LIBDEST)/site-packages -f \
- 		-x badsyntax $(DESTDIR)$(LIBDEST)/site-packages
--	-PYTHONPATH=$(DESTDIR)$(LIBDEST) $(RUNSHARED) \
-+	-$(RUNSHARED) \
- 		$(PYTHON_FOR_BUILD) -m lib2to3.pgen2.driver $(DESTDIR)$(LIBDEST)/lib2to3/Grammar.txt
--	-PYTHONPATH=$(DESTDIR)$(LIBDEST) $(RUNSHARED) \
-+	-$(RUNSHARED) \
- 		$(PYTHON_FOR_BUILD) -m lib2to3.pgen2.driver $(DESTDIR)$(LIBDEST)/lib2to3/PatternGrammar.txt
- 
- # Create the PLATDIR source directory, if one wasn't distributed..
diff --git a/pkgs/development/interpreters/python/cpython/2.7/default.nix b/pkgs/development/interpreters/python/cpython/2.7/default.nix
index 28ad3aeca91..61f17a959bd 100644
--- a/pkgs/development/interpreters/python/cpython/2.7/default.nix
+++ b/pkgs/development/interpreters/python/cpython/2.7/default.nix
@@ -88,11 +88,6 @@ let
       # only works for GCC and Apple Clang. This makes distutils to call C++
       # compiler when needed.
       ./python-2.7-distutils-C++.patch
-
-    ] ++ [
-
-      # Don't use pass host PYTHONPATH to build python
-      #./cross-compile.patch
     ];
 
   preConfigure = ''

From d00f200f4eeb3085368693618fda2a1110eec3af Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Mon, 12 Feb 2018 14:06:17 -0600
Subject: [PATCH 154/173] bootstrap-files: nicer naming

---
 .../linux/bootstrap-files/{musl64.nix => x86_64-musl.nix}       | 0
 pkgs/stdenv/linux/default.nix                                   | 2 +-
 2 files changed, 1 insertion(+), 1 deletion(-)
 rename pkgs/stdenv/linux/bootstrap-files/{musl64.nix => x86_64-musl.nix} (100%)

diff --git a/pkgs/stdenv/linux/bootstrap-files/musl64.nix b/pkgs/stdenv/linux/bootstrap-files/x86_64-musl.nix
similarity index 100%
rename from pkgs/stdenv/linux/bootstrap-files/musl64.nix
rename to pkgs/stdenv/linux/bootstrap-files/x86_64-musl.nix
diff --git a/pkgs/stdenv/linux/default.nix b/pkgs/stdenv/linux/default.nix
index 2baa8c0479c..8a8e52d1dfb 100644
--- a/pkgs/stdenv/linux/default.nix
+++ b/pkgs/stdenv/linux/default.nix
@@ -18,7 +18,7 @@
       "mips64el-linux" = import ./bootstrap-files/loongson2f.nix;
     };
     "musl" = {
-      "x86_64-linux" = import ./bootstrap-files/musl64.nix;
+      "x86_64-linux" = import ./bootstrap-files/x86_64-musl.nix;
     };
   };
   archLookupTable = table.${localSystem.libc}

From 8bedb690a2295b7330380624c9c6241966bb9d1a Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Mon, 12 Feb 2018 14:14:57 -0600
Subject: [PATCH 155/173] add musl bootstrap paths for aarch64, armv6l

Aarch64 tools tested briefly with qemu-aarch64,
but neither have been actually used yet :).

For now only "host" indirectly via binary cache
at cache.allvm.org.
---
 pkgs/stdenv/linux/bootstrap-files/aarch64-musl.nix | 5 +++++
 pkgs/stdenv/linux/bootstrap-files/armv6l-musl.nix  | 5 +++++
 pkgs/stdenv/linux/default.nix                      | 4 +++-
 3 files changed, 13 insertions(+), 1 deletion(-)
 create mode 100644 pkgs/stdenv/linux/bootstrap-files/aarch64-musl.nix
 create mode 100644 pkgs/stdenv/linux/bootstrap-files/armv6l-musl.nix

diff --git a/pkgs/stdenv/linux/bootstrap-files/aarch64-musl.nix b/pkgs/stdenv/linux/bootstrap-files/aarch64-musl.nix
new file mode 100644
index 00000000000..91eeea1abb3
--- /dev/null
+++ b/pkgs/stdenv/linux/bootstrap-files/aarch64-musl.nix
@@ -0,0 +1,5 @@
+{
+  # https://cache.allvm.org
+  busybox = builtins.storePath /nix/store/88snbnw04cldx4c3k9qrd0y2vifld2b2-stdenv-bootstrap-tools-aarch64-unknown-linux-musl/on-server/busybox;
+  bootstrapTools = builtins.storePath /nix/store/88snbnw04cldx4c3k9qrd0y2vifld2b2-stdenv-bootstrap-tools-aarch64-unknown-linux-musl/on-server/bootstrap-tools.tar.xz;
+}
diff --git a/pkgs/stdenv/linux/bootstrap-files/armv6l-musl.nix b/pkgs/stdenv/linux/bootstrap-files/armv6l-musl.nix
new file mode 100644
index 00000000000..ff2cffda932
--- /dev/null
+++ b/pkgs/stdenv/linux/bootstrap-files/armv6l-musl.nix
@@ -0,0 +1,5 @@
+{
+  # https://cache.allvm.org
+  busybox = builtins.storePath /nix/store/m4n9p43b7q8k1wqbpw264947rm70mj1z-stdenv-bootstrap-tools-armv6l-unknown-linux-musleabihf/on-server/busybox;
+  bootstrapTools = builtins.storePath /nix/store/m4n9p43b7q8k1wqbpw264947rm70mj1z-stdenv-bootstrap-tools-armv6l-unknown-linux-musleabihf/on-server/bootstrap-tools.tar.xz;
+}
diff --git a/pkgs/stdenv/linux/default.nix b/pkgs/stdenv/linux/default.nix
index 8a8e52d1dfb..249f788c17a 100644
--- a/pkgs/stdenv/linux/default.nix
+++ b/pkgs/stdenv/linux/default.nix
@@ -18,7 +18,9 @@
       "mips64el-linux" = import ./bootstrap-files/loongson2f.nix;
     };
     "musl" = {
-      "x86_64-linux" = import ./bootstrap-files/x86_64-musl.nix;
+      "aarch64-linux" = import ./bootstrap-files/aarch64-musl.nix;
+      "armv6l-linux"  = import ./bootstrap-files/armv6l-musl.nix;
+      "x86_64-linux"  = import ./bootstrap-files/x86_64-musl.nix;
     };
   };
   archLookupTable = table.${localSystem.libc}

From a4c69744dcfbe8d92da8b6439d36ea1c7c970a55 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Mon, 12 Feb 2018 16:28:20 -0600
Subject: [PATCH 156/173] aarch64-musl, armv6l-musl: fetch from wdtz.org
 instead of storePath

---
 pkgs/stdenv/linux/bootstrap-files/aarch64-musl.nix | 12 +++++++++---
 pkgs/stdenv/linux/bootstrap-files/armv6l-musl.nix  | 12 +++++++++---
 2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/pkgs/stdenv/linux/bootstrap-files/aarch64-musl.nix b/pkgs/stdenv/linux/bootstrap-files/aarch64-musl.nix
index 91eeea1abb3..b5cb9973905 100644
--- a/pkgs/stdenv/linux/bootstrap-files/aarch64-musl.nix
+++ b/pkgs/stdenv/linux/bootstrap-files/aarch64-musl.nix
@@ -1,5 +1,11 @@
 {
-  # https://cache.allvm.org
-  busybox = builtins.storePath /nix/store/88snbnw04cldx4c3k9qrd0y2vifld2b2-stdenv-bootstrap-tools-aarch64-unknown-linux-musl/on-server/busybox;
-  bootstrapTools = builtins.storePath /nix/store/88snbnw04cldx4c3k9qrd0y2vifld2b2-stdenv-bootstrap-tools-aarch64-unknown-linux-musl/on-server/bootstrap-tools.tar.xz;
+  busybox = import <nix/fetchurl.nix> {
+    url = https://wdtz.org/files/88snbnw04cldx4c3k9qrd0y2vifld2b2-stdenv-bootstrap-tools-aarch64-unknown-linux-musl/on-server/busybox;
+    sha256 = "01s6bwq84wyrjh3rdsgxni9gkzp7ss8rghg0cmp8zd87l79y8y4g";
+    executable = true;
+  };
+  bootstrapTools = import <nix/fetchurl.nix> {
+    url = https://wdtz.org/files/88snbnw04cldx4c3k9qrd0y2vifld2b2-stdenv-bootstrap-tools-aarch64-unknown-linux-musl/on-server/bootstrap-tools.tar.xz;
+    sha256 = "0n97q17h9i9hyc8c5qklfn6vii1vr87kj4k9acdc52jayv6c3kas";
+  };
 }
diff --git a/pkgs/stdenv/linux/bootstrap-files/armv6l-musl.nix b/pkgs/stdenv/linux/bootstrap-files/armv6l-musl.nix
index ff2cffda932..659b593b187 100644
--- a/pkgs/stdenv/linux/bootstrap-files/armv6l-musl.nix
+++ b/pkgs/stdenv/linux/bootstrap-files/armv6l-musl.nix
@@ -1,5 +1,11 @@
 {
-  # https://cache.allvm.org
-  busybox = builtins.storePath /nix/store/m4n9p43b7q8k1wqbpw264947rm70mj1z-stdenv-bootstrap-tools-armv6l-unknown-linux-musleabihf/on-server/busybox;
-  bootstrapTools = builtins.storePath /nix/store/m4n9p43b7q8k1wqbpw264947rm70mj1z-stdenv-bootstrap-tools-armv6l-unknown-linux-musleabihf/on-server/bootstrap-tools.tar.xz;
+  busybox = import <nix/fetchurl.nix> {
+    url = https://wdtz.org/files/m4n9p43b7q8k1wqbpw264947rm70mj1z-stdenv-bootstrap-tools-armv6l-unknown-linux-musleabihf/on-server/busybox;
+    sha256 = "01d0hp1xgrriiy9w0sd9vbqzwxnpwiyah80pi4vrpcmbwji36j1i";
+    executable = true;
+  };
+  bootstrapTools = import <nix/fetchurl.nix> {
+    url = https://wdtz.org/files/m4n9p43b7q8k1wqbpw264947rm70mj1z-stdenv-bootstrap-tools-armv6l-unknown-linux-musleabihf/on-server/bootstrap-tools.tar.xz;
+    sha256 = "1n9wm08fg99wa1q3ngim6n6bg7kxlkzx2v7fqw013rb3d5drjwrq";
+  };
 }

From 268bff61857b2576ba0272a86c4cc47ff26b1476 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Mon, 12 Feb 2018 16:53:18 -0600
Subject: [PATCH 157/173] make-bootstrap-tools: set XZ to maximum level, big
 wins

x86_64 bootstrap tarball goes from 37M -> 21M (!)
---
 pkgs/stdenv/linux/make-bootstrap-tools.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/stdenv/linux/make-bootstrap-tools.nix b/pkgs/stdenv/linux/make-bootstrap-tools.nix
index b0c4b284f75..86801e3f044 100644
--- a/pkgs/stdenv/linux/make-bootstrap-tools.nix
+++ b/pkgs/stdenv/linux/make-bootstrap-tools.nix
@@ -173,7 +173,7 @@ in with pkgs; rec {
         mv $out/.pack $out/pack
 
         mkdir $out/on-server
-        tar cvfJ $out/on-server/bootstrap-tools.tar.xz --hard-dereference --sort=name --numeric-owner --owner=0 --group=0 --mtime=@1 -C $out/pack .
+        XZ_OPT=-9 tar cvJf $out/on-server/bootstrap-tools.tar.xz --hard-dereference --sort=name --numeric-owner --owner=0 --group=0 --mtime=@1 -C $out/pack .
         cp ${busyboxMinimal}/bin/busybox $out/on-server
         chmod u+w $out/on-server/busybox
         nuke-refs $out/on-server/busybox

From 4619f8e06d33b72a424cdc0a603953d275c62220 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Mon, 12 Feb 2018 17:33:21 -0600
Subject: [PATCH 158/173] new musl bootstrap URL's, much smaller.  rebuild all
 the things.

---
 pkgs/stdenv/linux/bootstrap-files/aarch64-musl.nix | 6 +++---
 pkgs/stdenv/linux/bootstrap-files/armv6l-musl.nix  | 6 +++---
 pkgs/stdenv/linux/bootstrap-files/x86_64-musl.nix  | 5 ++---
 3 files changed, 8 insertions(+), 9 deletions(-)

diff --git a/pkgs/stdenv/linux/bootstrap-files/aarch64-musl.nix b/pkgs/stdenv/linux/bootstrap-files/aarch64-musl.nix
index b5cb9973905..ff0eec893f2 100644
--- a/pkgs/stdenv/linux/bootstrap-files/aarch64-musl.nix
+++ b/pkgs/stdenv/linux/bootstrap-files/aarch64-musl.nix
@@ -1,11 +1,11 @@
 {
   busybox = import <nix/fetchurl.nix> {
-    url = https://wdtz.org/files/88snbnw04cldx4c3k9qrd0y2vifld2b2-stdenv-bootstrap-tools-aarch64-unknown-linux-musl/on-server/busybox;
+    url = https://wdtz.org/files/wjzsj9cmdkc70f78yh072483x8656nci-stdenv-bootstrap-tools-aarch64-unknown-linux-musl/on-server/busybox;
     sha256 = "01s6bwq84wyrjh3rdsgxni9gkzp7ss8rghg0cmp8zd87l79y8y4g";
     executable = true;
   };
   bootstrapTools = import <nix/fetchurl.nix> {
-    url = https://wdtz.org/files/88snbnw04cldx4c3k9qrd0y2vifld2b2-stdenv-bootstrap-tools-aarch64-unknown-linux-musl/on-server/bootstrap-tools.tar.xz;
-    sha256 = "0n97q17h9i9hyc8c5qklfn6vii1vr87kj4k9acdc52jayv6c3kas";
+    url = https://wdtz.org/files/wjzsj9cmdkc70f78yh072483x8656nci-stdenv-bootstrap-tools-aarch64-unknown-linux-musl/on-server/bootstrap-tools.tar.xz;
+    sha256 = "0pbqrw9z4ifkijpfpx15l2dzi00rq8c5zg9ghimz5qgr5dx7f7cl";
   };
 }
diff --git a/pkgs/stdenv/linux/bootstrap-files/armv6l-musl.nix b/pkgs/stdenv/linux/bootstrap-files/armv6l-musl.nix
index 659b593b187..45ac0d5f9ac 100644
--- a/pkgs/stdenv/linux/bootstrap-files/armv6l-musl.nix
+++ b/pkgs/stdenv/linux/bootstrap-files/armv6l-musl.nix
@@ -1,11 +1,11 @@
 {
   busybox = import <nix/fetchurl.nix> {
-    url = https://wdtz.org/files/m4n9p43b7q8k1wqbpw264947rm70mj1z-stdenv-bootstrap-tools-armv6l-unknown-linux-musleabihf/on-server/busybox;
+    url = https://wdtz.org/files/xmz441m69qrlfdw47l2k10zf87fsya6r-stdenv-bootstrap-tools-armv6l-unknown-linux-musleabihf/on-server/busybox;
     sha256 = "01d0hp1xgrriiy9w0sd9vbqzwxnpwiyah80pi4vrpcmbwji36j1i";
     executable = true;
   };
   bootstrapTools = import <nix/fetchurl.nix> {
-    url = https://wdtz.org/files/m4n9p43b7q8k1wqbpw264947rm70mj1z-stdenv-bootstrap-tools-armv6l-unknown-linux-musleabihf/on-server/bootstrap-tools.tar.xz;
-    sha256 = "1n9wm08fg99wa1q3ngim6n6bg7kxlkzx2v7fqw013rb3d5drjwrq";
+    url = https://wdtz.org/files/xmz441m69qrlfdw47l2k10zf87fsya6r-stdenv-bootstrap-tools-armv6l-unknown-linux-musleabihf/on-server/bootstrap-tools.tar.xz;
+    sha256 = "1r9mz9w8y5jd7gfwfsrvs20qarzxy7bvrp5dlm41hnx6z617if1h";
   };
 }
diff --git a/pkgs/stdenv/linux/bootstrap-files/x86_64-musl.nix b/pkgs/stdenv/linux/bootstrap-files/x86_64-musl.nix
index 65deff26c42..b3bf804ca38 100644
--- a/pkgs/stdenv/linux/bootstrap-files/x86_64-musl.nix
+++ b/pkgs/stdenv/linux/bootstrap-files/x86_64-musl.nix
@@ -3,8 +3,7 @@
 
 {
   bootstrapTools = import <nix/fetchurl.nix> {
-    # XXX: Find a permanent location for this
-    url = https://wdtz.org/files/5zfs7s729n4lrlxmhlnc6qmfrlhahy9s-stdenv-bootstrap-tools-x86_64-unknown-linux-musl/on-server/bootstrap-tools.tar.xz;
-    sha256 = "0lwi08c2v7ip2z9li597ixywix976561hr358z2fbd6sqi943axl";
+    url = https://wdtz.org/files/030q34q7fk6jdfxkgcqp5rzr4yhw3pgx-stdenv-bootstrap-tools-x86_64-unknown-linux-musl/on-server/bootstrap-tools.tar.xz;
+    sha256 = "0ly0wj8wzbikn2j8sn727vikk90bq36drh98qvfx1kkh5k5azm2j";
   };
 }

From 84a527ea5e8db2a1ec095ff95dd66d6271246bc4 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 13 Feb 2018 08:04:51 -0600
Subject: [PATCH 159/173] x86_64-musl bootstrap: use separate 64bit busybox

Avoid issues like #24954.
---
 pkgs/stdenv/linux/bootstrap-files/x86_64-musl.nix | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/pkgs/stdenv/linux/bootstrap-files/x86_64-musl.nix b/pkgs/stdenv/linux/bootstrap-files/x86_64-musl.nix
index b3bf804ca38..12093f34000 100644
--- a/pkgs/stdenv/linux/bootstrap-files/x86_64-musl.nix
+++ b/pkgs/stdenv/linux/bootstrap-files/x86_64-musl.nix
@@ -1,7 +1,9 @@
-# Use busybox for i686-linux since it works on x86_64-linux as well.
-(import ./i686.nix) //
-
 {
+  busybox = import <nix/fetchurl.nix> {
+    url = https://wdtz.org/files/030q34q7fk6jdfxkgcqp5rzr4yhw3pgx-stdenv-bootstrap-tools-x86_64-unknown-linux-musl/on-server/busybox;
+    sha256 = "16lzrwwvdk6q3g08gs45pldz0rh6xpln2343xr444960h6wqxl5v";
+    executable = true;
+  };
   bootstrapTools = import <nix/fetchurl.nix> {
     url = https://wdtz.org/files/030q34q7fk6jdfxkgcqp5rzr4yhw3pgx-stdenv-bootstrap-tools-x86_64-unknown-linux-musl/on-server/bootstrap-tools.tar.xz;
     sha256 = "0ly0wj8wzbikn2j8sn727vikk90bq36drh98qvfx1kkh5k5azm2j";

From 8323123e95d2995933aa415ae90ae387b5ca2bc6 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Mon, 12 Feb 2018 20:20:14 -0600
Subject: [PATCH 160/173] systemd: use musl-getent on musl instead of
 glibc.bin's getent

---
 pkgs/os-specific/linux/systemd/default.nix | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/pkgs/os-specific/linux/systemd/default.nix b/pkgs/os-specific/linux/systemd/default.nix
index 0a2ebb05277..f8093921e6e 100644
--- a/pkgs/os-specific/linux/systemd/default.nix
+++ b/pkgs/os-specific/linux/systemd/default.nix
@@ -7,11 +7,18 @@
 , autoreconfHook, gettext, docbook_xsl, docbook_xml_dtd_42, docbook_xml_dtd_45
 , ninja, meson, python3Packages, glibcLocales
 , patchelf
+, musl-getent ? null
 }:
 
 assert stdenv.isLinux;
 
-let pythonLxmlEnv = python3Packages.python.withPackages ( ps: with ps; [ python3Packages.lxml ]);
+let
+  pythonLxmlEnv = python3Packages.python.withPackages ( ps: with ps; [ python3Packages.lxml ]);
+  getent-bin =
+         if stdenv.hostPlatform.libc == "glibc" then stdenv.cc.libc.bin
+    else if stdenv.hostPlatform.isMusl then "${musl-getent}"
+    else throw "unsupported abi for systemd";
+  getent = "${getent-bin}/bin/getent";
 
 in
 
@@ -107,7 +114,7 @@ in
         for i in src/remount-fs/remount-fs.c src/core/mount.c src/core/swap.c src/fsck/fsck.c units/emergency.service.in units/rescue.service.in src/journal/cat.c src/core/shutdown.c src/nspawn/nspawn.c src/shared/generator.c; do
           test -e $i
           substituteInPlace $i \
-            --replace /usr/bin/getent ${stdenv.glibc.bin}/bin/getent \
+            --replace /usr/bin/getent ${getent} \
             --replace /sbin/swapon ${utillinux.bin}/sbin/swapon \
             --replace /sbin/swapoff ${utillinux.bin}/sbin/swapoff \
             --replace /sbin/fsck ${utillinux.bin}/sbin/fsck \

From 82dfab87fa7bd338efc459c7c65a4d20cac8f029 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Mon, 12 Feb 2018 20:55:24 -0600
Subject: [PATCH 161/173] systemd: add getent-bin to nativeBuildInputs, meson
 searches for getent

---
 pkgs/os-specific/linux/systemd/default.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/os-specific/linux/systemd/default.nix b/pkgs/os-specific/linux/systemd/default.nix
index f8093921e6e..519e83539d4 100644
--- a/pkgs/os-specific/linux/systemd/default.nix
+++ b/pkgs/os-specific/linux/systemd/default.nix
@@ -40,7 +40,7 @@ in
         ninja meson
         coreutils # meson calls date, stat etc.
         pythonLxmlEnv glibcLocales
-        patchelf
+        patchelf getent-bin
       ];
     buildInputs =
       [ linuxHeaders libcap kmod xz pam acl

From 7f3527d33f19150dff5cfc9d6dbef51532a78da2 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 13 Feb 2018 11:03:07 -0600
Subject: [PATCH 162/173] libexecinfo: fetch patches, add meta, maintain

---
 .../libraries/libexecinfo/10-execinfo.patch   | 64 -------------------
 .../libexecinfo/20-define-gnu-source.patch    | 24 -------
 .../libexecinfo/30-linux-makefile.patch       | 44 -------------
 .../libraries/libexecinfo/default.nix         | 27 ++++++--
 4 files changed, 23 insertions(+), 136 deletions(-)
 delete mode 100644 pkgs/development/libraries/libexecinfo/10-execinfo.patch
 delete mode 100644 pkgs/development/libraries/libexecinfo/20-define-gnu-source.patch
 delete mode 100644 pkgs/development/libraries/libexecinfo/30-linux-makefile.patch

diff --git a/pkgs/development/libraries/libexecinfo/10-execinfo.patch b/pkgs/development/libraries/libexecinfo/10-execinfo.patch
deleted file mode 100644
index 03b5af0ca20..00000000000
--- a/pkgs/development/libraries/libexecinfo/10-execinfo.patch
+++ /dev/null
@@ -1,64 +0,0 @@
---- execinfo.c.orig
-+++ execinfo.c
-@@ -69,7 +69,8 @@
- char **
- backtrace_symbols(void *const *buffer, int size)
- {
--    int i, clen, alen, offset;
-+    size_t clen, alen;
-+    int i, offset;
-     char **rval;
-     char *cp;
-     Dl_info info;
-@@ -78,7 +79,6 @@
-     rval = malloc(clen);
-     if (rval == NULL)
-         return NULL;
--    (char **)cp = &(rval[size]);
-     for (i = 0; i < size; i++) {
-         if (dladdr(buffer[i], &info) != 0) {
-             if (info.dli_sname == NULL)
-@@ -92,14 +92,14 @@
-                    2 +                      /* " <" */
-                    strlen(info.dli_sname) + /* "function" */
-                    1 +                      /* "+" */
--                   D10(offset) +            /* "offset */
-+                   10 +                     /* "offset */
-                    5 +                      /* "> at " */
-                    strlen(info.dli_fname) + /* "filename" */
-                    1;                       /* "\0" */
-             rval = realloc_safe(rval, clen + alen);
-             if (rval == NULL)
-                 return NULL;
--            snprintf(cp, alen, "%p <%s+%d> at %s",
-+            snprintf((char *) rval + clen, alen, "%p <%s+%d> at %s",
-               buffer[i], info.dli_sname, offset, info.dli_fname);
-         } else {
-             alen = 2 +                      /* "0x" */
-@@ -108,12 +108,15 @@
-             rval = realloc_safe(rval, clen + alen);
-             if (rval == NULL)
-                 return NULL;
--            snprintf(cp, alen, "%p", buffer[i]);
-+            snprintf((char *) rval + clen, alen, "%p", buffer[i]);
-         }
--        rval[i] = cp;
--        cp += alen;
-+        rval[i] = (char *) clen;
-+        clen += alen;
-     }
- 
-+    for (i = 0; i < size; i++)
-+        rval[i] += (long) rval;
-+
-     return rval;
- }
- 
-@@ -155,6 +158,6 @@
-                 return;
-             snprintf(buf, len, "%p\n", buffer[i]);
-         }
--        write(fd, buf, len - 1);
-+        write(fd, buf, strlen(buf));
-     }
- }
diff --git a/pkgs/development/libraries/libexecinfo/20-define-gnu-source.patch b/pkgs/development/libraries/libexecinfo/20-define-gnu-source.patch
deleted file mode 100644
index 0ba1fc1f7e6..00000000000
--- a/pkgs/development/libraries/libexecinfo/20-define-gnu-source.patch
+++ /dev/null
@@ -1,24 +0,0 @@
---- execinfo.c.orig
-+++ execinfo.c
-@@ -26,6 +26,7 @@
-  * $Id: execinfo.c,v 1.3 2004/07/19 05:21:09 sobomax Exp $
-  */
- 
-+#define _GNU_SOURCE
- #include <sys/types.h>
- #include <sys/uio.h>
- #include <dlfcn.h>
---- stacktraverse.c.orig
-+++ stacktraverse.c
-@@ -1,3 +1,4 @@
-+#define _GNU_SOURCE
- #include <stddef.h>
- 
- #include "stacktraverse.h"
---- test.c.orig
-+++ test.c
-@@ -1,3 +1,4 @@
-+#define _GNU_SOURCE
- #include <stdio.h>
- #include <stdlib.h>
- 
diff --git a/pkgs/development/libraries/libexecinfo/30-linux-makefile.patch b/pkgs/development/libraries/libexecinfo/30-linux-makefile.patch
deleted file mode 100644
index 3bc89279547..00000000000
--- a/pkgs/development/libraries/libexecinfo/30-linux-makefile.patch
+++ /dev/null
@@ -1,44 +0,0 @@
---- Makefile.orig
-+++ Makefile
-@@ -23,24 +23,25 @@
- # SUCH DAMAGE.
- #
- # $Id: Makefile,v 1.3 2004/07/19 05:19:55 sobomax Exp $
-+#
-+# Linux Makefile by Matt Smith <mcs@darkregion.net>, 2011/01/04
- 
--LIB=	execinfo
-+CC=cc
-+AR=ar
-+EXECINFO_CFLAGS=$(CFLAGS) -O2 -pipe -fno-strict-aliasing -std=gnu99 -fstack-protector -c
-+EXECINFO_LDFLAGS=$(LDFLAGS)
- 
--SRCS=	stacktraverse.c stacktraverse.h execinfo.c execinfo.h
-+all: static dynamic
- 
--INCS=	execinfo.h
-+static:
-+	$(CC) $(EXECINFO_CFLAGS) $(EXECINFO_LDFLAGS) stacktraverse.c
-+	$(CC) $(EXECINFO_CFLAGS) $(EXECINFO_LDFLAGS) execinfo.c
-+	$(AR) rcs libexecinfo.a stacktraverse.o execinfo.o
- 
--SHLIB_MAJOR=	1
--SHLIB_MINOR=	0
-+dynamic:
-+	$(CC) -fpic -DPIC $(EXECINFO_CFLAGS) $(EXECINFO_LDFLAGS) stacktraverse.c -o stacktraverse.So
-+	$(CC) -fpic -DPIC $(EXECINFO_CFLAGS) $(EXECINFO_LDFLAGS) execinfo.c -o execinfo.So
-+	$(CC) -shared -Wl,-soname,libexecinfo.so.1 -o libexecinfo.so.1 stacktraverse.So execinfo.So
- 
--NOPROFILE=	yes
--
--DPADD=		${LIBM}
--LDADD=		-lm
--
--#WARNS?=	4
--
--#stacktraverse.c: gen.py
--#	./gen.py > stacktraverse.c
--
--.include <bsd.lib.mk>
-+clean:
-+	rm -rf *.o *.So *.a *.so
diff --git a/pkgs/development/libraries/libexecinfo/default.nix b/pkgs/development/libraries/libexecinfo/default.nix
index 15c4cb2661f..14d5f7f45df 100644
--- a/pkgs/development/libraries/libexecinfo/default.nix
+++ b/pkgs/development/libraries/libexecinfo/default.nix
@@ -1,4 +1,4 @@
-{ stdenv, fetchurl }:
+{ stdenv, fetchurl, fetchpatch }:
 
 stdenv.mkDerivation rec {
   name = "libexecinfo-${version}";
@@ -10,9 +10,21 @@ stdenv.mkDerivation rec {
   };
 
   patches = [
-    ./10-execinfo.patch
-    ./20-define-gnu-source.patch
-    ./30-linux-makefile.patch
+    (fetchpatch {
+      name = "10-execinfo.patch";
+      url = https://git.alpinelinux.org/cgit/aports/plain/main/libexecinfo/10-execinfo.patch?id=730cdcef6901750f4029d4c3b8639ce02ee3ead1;
+      sha256 = "0lnphrad4vspyljnvmm62dyxj98vgp3wabj4w3vfzfph7j8piw7g";
+    })
+    (fetchpatch {
+      name = "20-define-gnu-source.patch";
+      url = https://git.alpinelinux.org/cgit/aports/plain/main/libexecinfo/20-define-gnu-source.patch?id=730cdcef6901750f4029d4c3b8639ce02ee3ead1;
+      sha256 = "1mp8mc639b0h2s69m5z6s2h3q3n1zl298j9j0plzj7f979j76302";
+    })
+    (fetchpatch {
+      name = "30-linux-makefile.patch";
+      url = https://git.alpinelinux.org/cgit/aports/plain/main/libexecinfo/30-linux-makefile.patch?id=730cdcef6901750f4029d4c3b8639ce02ee3ead1;
+      sha256 = "1jwjz22z5cjy5h2bfghn62yl9ar8jiqhdvbwrcfavv17ihbhwcaf";
+    })
   ];
 
   patchFlags = "-p0";
@@ -22,4 +34,11 @@ stdenv.mkDerivation rec {
     install -Dm755 libexecinfo.{a,so.1} -t $out/lib
     ln -s $out/lib/libexecinfo.so{.1,}
   '';
+
+  meta = with stdenv.lib; {
+    description = "Library for inspecting program's backtrace";
+    license = licenses.bsd2;
+    homepage = https://www.freshports.org/devel/libexecinfo;
+    maintainers = with maintainers; [ dtzWill ];
+  };
 }

From 2bfe036f42a3e71a6577a899f46d075ea55e2704 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 13 Feb 2018 11:05:49 -0600
Subject: [PATCH 163/173] libexecinfo: fix CC/AR

---
 pkgs/development/libraries/libexecinfo/default.nix | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/pkgs/development/libraries/libexecinfo/default.nix b/pkgs/development/libraries/libexecinfo/default.nix
index 14d5f7f45df..a61d51aa6b7 100644
--- a/pkgs/development/libraries/libexecinfo/default.nix
+++ b/pkgs/development/libraries/libexecinfo/default.nix
@@ -27,6 +27,8 @@ stdenv.mkDerivation rec {
     })
   ];
 
+  makeFlags = [ "CC:=$(CC)" "AR:=$(AR)" ];
+
   patchFlags = "-p0";
 
   installPhase = ''
@@ -36,7 +38,7 @@ stdenv.mkDerivation rec {
   '';
 
   meta = with stdenv.lib; {
-    description = "Library for inspecting program's backtrace";
+    description = "Quick-n-dirty BSD licensed clone of the GNU libc backtrace facility";
     license = licenses.bsd2;
     homepage = https://www.freshports.org/devel/libexecinfo;
     maintainers = with maintainers; [ dtzWill ];

From a65aa78e14da5d850f7f9ced30cb614ec024b979 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 13 Feb 2018 11:10:35 -0600
Subject: [PATCH 164/173] libbfd: simplify per reviewer feedback

Changing postPatch to postAutoreconf is needed
when updateAutotoolsGnuConfigScriptsHook is used
or the directory change happens too early.
---
 pkgs/development/libraries/libbfd/default.nix | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)

diff --git a/pkgs/development/libraries/libbfd/default.nix b/pkgs/development/libraries/libbfd/default.nix
index b41fe0ad47e..3c71a5106d2 100644
--- a/pkgs/development/libraries/libbfd/default.nix
+++ b/pkgs/development/libraries/libbfd/default.nix
@@ -1,5 +1,5 @@
 { stdenv
-, fetchurl, fetchpatch, autoreconfHook264, buildPackages, bison, binutils-raw
+, fetchurl, fetchpatch, autoreconfHook264, bison, binutils-raw
 , libiberty, zlib
 }:
 
@@ -18,30 +18,20 @@ stdenv.mkDerivation rec {
   ];
 
   # We just want to build libbfd
-  preConfigure = ''
+  postAutoreconf = ''
     cd bfd
   '';
 
-  depsBuildBuilds = [ buildPackages.stdenv.cc ];
   nativeBuildInputs = [ autoreconfHook264 bison ];
   buildInputs = [ libiberty zlib ];
 
-  configurePlatforms = [ "build" "host" "target" ];
   configureFlags = [
     "--enable-targets=all" "--enable-64-bit-bfd"
     "--enable-install-libbfd"
     "--enable-shared"
     "--with-system-zlib"
-    "CC_FOR_BUILD=$(CC)"
   ];
 
-  postInstall = stdenv.lib.optionalString (stdenv.hostPlatform != stdenv.targetPlatform) ''
-    # the build system likes to move things into atypical locations
-    mkdir -p $dev
-    mv $out/${stdenv.hostPlatform.config}/${stdenv.targetPlatform.config}/include $dev/include
-    mv $out/${stdenv.hostPlatform.config}/${stdenv.targetPlatform.config}/lib $out/lib
-  '';
-
   enableParallelBuilding = true;
 
   meta = with stdenv.lib; {

From 6c3df22c5a861827a62cae31bde622ce668d5446 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 13 Feb 2018 11:27:27 -0600
Subject: [PATCH 165/173] libintlOrEmpty: use actual gettext on non-glibc too
 (musl)

---
 pkgs/top-level/all-packages.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index 945c3a58036..78860b3f0f0 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -9762,7 +9762,7 @@ with pkgs;
   };
 
   # On non-GNU systems we need GNU Gettext for libintl.
-  libintlOrEmpty = stdenv.lib.optional (!stdenv.isLinux) gettext;
+  libintlOrEmpty = stdenv.lib.optional (!stdenv.isLinux || hostPlatform.libc != "glibc") gettext;
 
   libid3tag = callPackage ../development/libraries/libid3tag {
     gperf = gperf_3_0;

From d3b59f864a3dfe37432095568673c97df79107bc Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 13 Feb 2018 11:38:14 -0600
Subject: [PATCH 166/173] define top-level attrs for getent, getconf

Note that currently getent is explicitly taken from "glibc.bin",
regardless of build configuration which this preserves for now.
(on non-musl anyway)
---
 pkgs/os-specific/linux/systemd/default.nix | 11 +++--------
 pkgs/shells/fish/default.nix               |  7 ++-----
 pkgs/top-level/all-packages.nix            |  8 ++++++++
 3 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/pkgs/os-specific/linux/systemd/default.nix b/pkgs/os-specific/linux/systemd/default.nix
index 519e83539d4..9085b1703c0 100644
--- a/pkgs/os-specific/linux/systemd/default.nix
+++ b/pkgs/os-specific/linux/systemd/default.nix
@@ -7,18 +7,13 @@
 , autoreconfHook, gettext, docbook_xsl, docbook_xml_dtd_42, docbook_xml_dtd_45
 , ninja, meson, python3Packages, glibcLocales
 , patchelf
-, musl-getent ? null
+, getent
 }:
 
 assert stdenv.isLinux;
 
 let
   pythonLxmlEnv = python3Packages.python.withPackages ( ps: with ps; [ python3Packages.lxml ]);
-  getent-bin =
-         if stdenv.hostPlatform.libc == "glibc" then stdenv.cc.libc.bin
-    else if stdenv.hostPlatform.isMusl then "${musl-getent}"
-    else throw "unsupported abi for systemd";
-  getent = "${getent-bin}/bin/getent";
 
 in
 
@@ -40,7 +35,7 @@ in
         ninja meson
         coreutils # meson calls date, stat etc.
         pythonLxmlEnv glibcLocales
-        patchelf getent-bin
+        patchelf getent
       ];
     buildInputs =
       [ linuxHeaders libcap kmod xz pam acl
@@ -114,7 +109,7 @@ in
         for i in src/remount-fs/remount-fs.c src/core/mount.c src/core/swap.c src/fsck/fsck.c units/emergency.service.in units/rescue.service.in src/journal/cat.c src/core/shutdown.c src/nspawn/nspawn.c src/shared/generator.c; do
           test -e $i
           substituteInPlace $i \
-            --replace /usr/bin/getent ${getent} \
+            --replace /usr/bin/getent ${getent}/bin/getent \
             --replace /sbin/swapon ${utillinux.bin}/sbin/swapon \
             --replace /sbin/swapoff ${utillinux.bin}/sbin/swapoff \
             --replace /sbin/fsck ${utillinux.bin}/sbin/fsck \
diff --git a/pkgs/shells/fish/default.nix b/pkgs/shells/fish/default.nix
index 3078dd2dcab..d6c107e9953 100644
--- a/pkgs/shells/fish/default.nix
+++ b/pkgs/shells/fish/default.nix
@@ -1,6 +1,6 @@
 { stdenv, fetchurl, coreutils, utillinux,
   nettools, kbd, bc, which, gnused, gnugrep,
-  groff, man-db, glibc, musl-getent, libiconv, pcre2,
+  groff, man-db, getent, libiconv, pcre2,
   gettext, ncurses, python3
 
   , writeText
@@ -12,9 +12,6 @@
 with stdenv.lib;
 
 let
-  getent = if stdenv.hostPlatform.isMusl
-             then "${musl-getent}/bin/getent"
-             else "${glibc.bin}/bin/getent";
   etcConfigAppendixText = ''
     ############### ↓ Nix hook for sourcing /etc/fish/config.fish ↓ ###############
     #                                                                             #
@@ -145,7 +142,7 @@ let
       sed -e "s| ul| ${utillinux}/bin/ul|" \
           -i "$out/share/fish/functions/__fish_print_help.fish"
       for cur in $out/share/fish/functions/*.fish; do
-        sed -e "s|/usr/bin/getent|${getent}|" \
+        sed -e "s|/usr/bin/getent|${getent}/bin/getent|" \
             -i "$cur"
       done
 
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index 78860b3f0f0..84fda4d6386 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -13361,6 +13361,14 @@ with pkgs;
   musl-fts = callPackage ../os-specific/linux/musl/fts.nix { };
   musl-getconf = callPackage ../os-specific/linux/musl/getconf.nix { };
   musl-getent = callPackage ../os-specific/linux/musl/getent.nix { };
+  getent =
+    if hostPlatform.isMusl then musl-getent
+    # This may not be right on other platforms, but preserves existing behavior
+    else /* if hostPlatform.libc == "glibc" then */ glibc.bin;
+  getconf =
+    if hostPlatform.isMusl then musl-getconf
+    # This may not be right on other platforms, but preserves existing behavior
+    else /* if hostPlatform.libc == "glibc" then */ glibc.bin;
 
   nettools = callPackage ../os-specific/linux/net-tools { };
 

From 804ae999f56e8e44304cccb0c0d2d128574935e8 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 13 Feb 2018 11:53:02 -0600
Subject: [PATCH 167/173] getconf: nothing uses glibc.bin for getconf,
 generalize to cc.libc

---
 pkgs/top-level/all-packages.nix | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index 84fda4d6386..00ea9145b3e 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -13365,10 +13365,10 @@ with pkgs;
     if hostPlatform.isMusl then musl-getent
     # This may not be right on other platforms, but preserves existing behavior
     else /* if hostPlatform.libc == "glibc" then */ glibc.bin;
+
   getconf =
     if hostPlatform.isMusl then musl-getconf
-    # This may not be right on other platforms, but preserves existing behavior
-    else /* if hostPlatform.libc == "glibc" then */ glibc.bin;
+    else lib.getBin stdenv.cc.libc;
 
   nettools = callPackage ../os-specific/linux/net-tools { };
 

From 6825c0c18be3d90ee8d05599a7bebf0a4cfebe2a Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 13 Feb 2018 12:36:54 -0600
Subject: [PATCH 168/173] nettle: cleanup cross changes

reviewer feedback + minor other things I noticed
while revisiting.
---
 pkgs/development/libraries/nettle/generic.nix | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/pkgs/development/libraries/nettle/generic.nix b/pkgs/development/libraries/nettle/generic.nix
index 8999b1c2a9a..3af93469cf1 100644
--- a/pkgs/development/libraries/nettle/generic.nix
+++ b/pkgs/development/libraries/nettle/generic.nix
@@ -12,18 +12,17 @@ stdenv.mkDerivation (rec {
   outputs = [ "out" "dev" ];
   outputBin = "dev";
 
-  nativeBuildInputs = [ gnum4 buildPackages.stdenv.cc ];
+  depsBuildBuild = [ buildPackages.stdenv.cc ];
+  nativeBuildInputs = [ gnum4 ];
   propagatedBuildInputs = [ gmp ];
 
-  doCheck = (stdenv.system != "i686-cygwin" && !stdenv.isDarwin && stdenv.buildPlatform == stdenv.hostPlatform);
+  doCheck = (stdenv.system != "i686-cygwin" && !stdenv.isDarwin);
 
   enableParallelBuilding = true;
 
   patches = stdenv.lib.optional (stdenv.system == "i686-cygwin")
               ./cygwin.patch;
 
-  configureFlags = '' CC_FOR_BUILD=${buildPackages.stdenv.cc.targetPrefix}gcc '';
-
   meta = with stdenv.lib; {
     description = "Cryptographic library";
 

From e25a34371645180e69ece2f781bcbdaf472b8b25 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 13 Feb 2018 12:39:34 -0600
Subject: [PATCH 169/173] cmake: don't force gcc, use more general cc/c++

---
 pkgs/development/tools/build-managers/cmake/default.nix   | 4 ++--
 pkgs/development/tools/build-managers/cmake/setup-hook.sh | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/pkgs/development/tools/build-managers/cmake/default.nix b/pkgs/development/tools/build-managers/cmake/default.nix
index 7dd97b8a206..97c02cd9cc1 100644
--- a/pkgs/development/tools/build-managers/cmake/default.nix
+++ b/pkgs/development/tools/build-managers/cmake/default.nix
@@ -86,8 +86,8 @@ stdenv.mkDerivation rec {
     ++ ["--"]
     ++ optionals (!useNcurses) [ "-DBUILD_CursesDialog=OFF" ]
     ++ optionals (stdenv.buildPlatform != stdenv.hostPlatform) [
-      "-DCMAKE_CXX_COMPILER=${stdenv.cc.targetPrefix}g++"
-      "-DCMAKE_C_COMPILER=${stdenv.cc.targetPrefix}gcc"
+      "-DCMAKE_CXX_COMPILER=${stdenv.cc.targetPrefix}c++"
+      "-DCMAKE_C_COMPILER=${stdenv.cc.targetPrefix}cc"
       "-DCMAKE_AR=${getBin stdenv.cc.bintools.bintools}/bin/${stdenv.cc.targetPrefix}ar"
       "-DCMAKE_RANLIB=${getBin stdenv.cc.bintools.bintools}/bin/${stdenv.cc.targetPrefix}ranlib"
       "-DCMAKE_STRIP=${getBin stdenv.cc.bintools.bintools}/bin/${stdenv.cc.targetPrefix}strip"
diff --git a/pkgs/development/tools/build-managers/cmake/setup-hook.sh b/pkgs/development/tools/build-managers/cmake/setup-hook.sh
index 50e9e93b015..c796c31cb70 100755
--- a/pkgs/development/tools/build-managers/cmake/setup-hook.sh
+++ b/pkgs/development/tools/build-managers/cmake/setup-hook.sh
@@ -37,8 +37,8 @@ cmakeConfigurePhase() {
         # Unfortunately cmake seems to expect absolute paths for ar, ranlib, and
         # strip. Otherwise they are taken to be relative to the source root of
         # the package being built.
-        cmakeFlags="-DCMAKE_CXX_COMPILER=$crossConfig-g++ $cmakeFlags"
-        cmakeFlags="-DCMAKE_C_COMPILER=$crossConfig-gcc $cmakeFlags"
+        cmakeFlags="-DCMAKE_CXX_COMPILER=$crossConfig-c++ $cmakeFlags"
+        cmakeFlags="-DCMAKE_C_COMPILER=$crossConfig-cc $cmakeFlags"
         cmakeFlags="-DCMAKE_AR=$(command -v $crossConfig-ar) $cmakeFlags"
         cmakeFlags="-DCMAKE_RANLIB=$(command -v $crossConfig-ranlib) $cmakeFlags"
         cmakeFlags="-DCMAKE_STRIP=$(command -v $crossConfig-strip) $cmakeFlags"

From 42a17d638b4c7b9246750b71b82d19fa98502047 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 13 Feb 2018 13:19:47 -0600
Subject: [PATCH 170/173] libbfd: back to postPatch, restore configurePlatforms

---
 pkgs/development/libraries/libbfd/default.nix | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/pkgs/development/libraries/libbfd/default.nix b/pkgs/development/libraries/libbfd/default.nix
index 3c71a5106d2..4b7b5b8dfa5 100644
--- a/pkgs/development/libraries/libbfd/default.nix
+++ b/pkgs/development/libraries/libbfd/default.nix
@@ -18,13 +18,14 @@ stdenv.mkDerivation rec {
   ];
 
   # We just want to build libbfd
-  postAutoreconf = ''
+  postPatch = ''
     cd bfd
   '';
 
   nativeBuildInputs = [ autoreconfHook264 bison ];
   buildInputs = [ libiberty zlib ];
 
+  configurePlatforms = [ "build" "host" ];
   configureFlags = [
     "--enable-targets=all" "--enable-64-bit-bfd"
     "--enable-install-libbfd"

From 767bddbd80133ad4dab7a78dc95a84e616407629 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 13 Feb 2018 13:39:42 -0600
Subject: [PATCH 171/173] libbfd: manually update config.guess/config.sub

Workaround for building "from" the bfd directory but needing
to update files a level above.

This needs to be done *after* autoreconf since autoreconf
will replace these itself, apparently.
---
 pkgs/development/libraries/libbfd/default.nix | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/pkgs/development/libraries/libbfd/default.nix b/pkgs/development/libraries/libbfd/default.nix
index 4b7b5b8dfa5..018239a3e77 100644
--- a/pkgs/development/libraries/libbfd/default.nix
+++ b/pkgs/development/libraries/libbfd/default.nix
@@ -1,5 +1,5 @@
 { stdenv
-, fetchurl, fetchpatch, autoreconfHook264, bison, binutils-raw
+, fetchurl, fetchpatch, gnu-config, autoreconfHook264, bison, binutils-raw
 , libiberty, zlib
 }:
 
@@ -22,6 +22,14 @@ stdenv.mkDerivation rec {
     cd bfd
   '';
 
+  postAutoreconf = ''
+    echo "Updating config.guess and config.sub from ${gnu-config}"
+    cp -f ${gnu-config}/config.{guess,sub} ../
+  '';
+
+  # We update these ourselves
+  dontUpdateAutotoolsGnuConfigScripts = true;
+
   nativeBuildInputs = [ autoreconfHook264 bison ];
   buildInputs = [ libiberty zlib ];
 

From b0c4138a3b49c499cba8665963fc33fa361c8d5f Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 13 Feb 2018 13:55:43 -0600
Subject: [PATCH 172/173] iproute: patchshebangs to work on 'hostPlatform' (not
 build)

---
 pkgs/os-specific/linux/iproute/default.nix | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/pkgs/os-specific/linux/iproute/default.nix b/pkgs/os-specific/linux/iproute/default.nix
index a78107310c3..269c59f5e6f 100644
--- a/pkgs/os-specific/linux/iproute/default.nix
+++ b/pkgs/os-specific/linux/iproute/default.nix
@@ -1,4 +1,4 @@
-{ fetchurl, stdenv, lib, flex, bison, db, iptables, pkgconfig }:
+{ fetchurl, stdenv, lib, flex, bash, bison, db, iptables, pkgconfig }:
 
 stdenv.mkDerivation rec {
   name = "iproute2-${version}";
@@ -40,7 +40,7 @@ stdenv.mkDerivation rec {
   enableParallelBuilding = true;
 
   postInstall = ''
-    PATH=${stdenv.shell}/bin:$PATH patchShebangs $out/sbin
+    PATH=${bash}/bin:$PATH patchShebangs $out/sbin
   '';
 
   meta = with stdenv.lib; {

From bd11ffd2677081870971e583b3ed69d3e2b468b9 Mon Sep 17 00:00:00 2001
From: Will Dietz <w@wdtz.org>
Date: Tue, 13 Feb 2018 14:11:17 -0600
Subject: [PATCH 173/173] ruby: don't reference groff if not being used (not
 used by default)

Fixes cross build by removing false dependency.

If this is needed again, be careful about *which* "groff" is needed.
(which in terms of build vs host)
---
 pkgs/development/interpreters/ruby/default.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/development/interpreters/ruby/default.nix b/pkgs/development/interpreters/ruby/default.nix
index a0c563e3fb2..21a4433c9eb 100644
--- a/pkgs/development/interpreters/ruby/default.nix
+++ b/pkgs/development/interpreters/ruby/default.nix
@@ -73,7 +73,7 @@ let
             unpackdir rubySrc;
 
         # Have `configure' avoid `/usr/bin/nroff' in non-chroot builds.
-        NROFF = "${groff}/bin/nroff";
+        NROFF = if docSupport then "${groff}/bin/nroff" else null;
 
         nativeBuildInputs =
              ops useRailsExpress [ autoreconfHook bison ]