@@ -73,6 +73,35 @@ let
7373 ${lib.concatMapStringsSep "\n" (p: exactDep "--package-db ${p.components.library}/package.conf.d" p.identifier.name) component.depends}
7474 ${lib.concatMapStringsSep "\n" (exactDep "") nonReinstallablePkgs}
7575
76+ ''
77+ # This code originates in the `generic-builder.nix` from nixpkgs. However GHC has been fixed
78+ # to drop unused libraries referneced from libraries; and this patch is usually included in the
79+ # nixpkgs's GHC builds. This doesn't sadly make this stupid hack unnecessary. It resurfes in
80+ # the form of Cabal trying to be smart. Cabal when linking a library figures out that you likely
81+ # need those `rpath` entries, and passes `-optl-Wl,-rpath,...` for each dynamic library path to
82+ # GHC, thus subverting the linker and forcing it to insert all those RPATHs weather or not they
83+ # are needed. We therfore reuse the linker hack here to move all al dynamic lirbaries into a
84+ # common folder (as links) and thus prevent Cabal from going nuts.
85+ #
86+ # TODO: Fix Cabal.
87+ # TODO: this is only needed if we do dynamic libraries.
88+ + lib.optionalString stdenv.isDarwin ''
89+ # Work around a limit in the macOS Sierra linker on the number of paths
90+ # referenced by any one dynamic library:
91+ #
92+ # Create a local directory with symlinks of the *.dylib (macOS shared
93+ # libraries) from all the dependencies.
94+ local dynamicLinksDir="$out/lib/links"
95+ mkdir -p $dynamicLinksDir
96+ for d in $(grep dynamic-library-dirs "$out/package.conf.d/"*|awk '{print $2}'|sort -u); do
97+ ln -s "$d/"*.dylib $dynamicLinksDir
98+ done
99+ # Edit the local package DB to reference the links directory.
100+ for f in "$out/package.conf.d/"*.conf; do
101+ sed -i "s,dynamic-library-dirs: .*,dynamic-library-dirs: $dynamicLinksDir," $f
102+ done
103+ '' + ''
104+ ${ghc.targetPrefix}ghc-pkg --package-db $out/package.conf.d recache
76105 '' + ''
77106 ${ghc.targetPrefix}ghc-pkg --package-db $out/package.conf.d check
78107 '');
@@ -138,7 +167,7 @@ in stdenv.mkDerivation ({
138167 '';
139168
140169 buildPhase = ''
141− $SETUP_HS build -j$NIX_BUILD_CORES
170+ $SETUP_HS build -j$NIX_BUILD_CORES ${lib.concatStringsSep " " component.setupBuildFlags}
142171 '';
143172
144173 checkPhase = ''