@@ -56,6 +56,10 @@ let isCross = (ghc.cross or null) != null; in
5656, hardeningDisable ? lib.optional (ghc.isHaLVM or false) "all"
5757, enableSeparateDataOutput ? false
5858, enableSeparateDocOutput ? doHaddock
59+, enableSeparateBinOutput ? isExecutable
60+, outputsToInstall ? []
61+, enableSeparateLibOutput ? true
62+, enableSeparateEtcOutput ? (stdenv.lib.versionOlder "7.7" ghc.version)
5963} @ args:
6064
6165assert editedCabalFile != null -> revision != null;
@@ -79,9 +83,6 @@ let
7983 then "package-db"
8084 else "package-conf";
8185
82− # the target dir for haddock documentation
83− docdir = docoutput: docoutput + "/share/doc";
84−
8586 newCabalFileUrl = "http://hackage.haskell.org/package/${pname}-${version}/revision/${revision}.cabal";
8687 newCabalFile = fetchurl {
8788 url = newCabalFileUrl;
@@ -95,6 +96,13 @@ let
9596 '';
9697
9798 hasActiveLibrary = isLibrary && (enableStaticLibraries || enableSharedLibraries || enableLibraryProfiling);
99+ hasLibOutput = enableSeparateLibOutput && hasActiveLibrary;
100+ libDir = if hasLibOutput then "$lib/lib/${ghc.name}" else "$out/lib/${ghc.name}";
101+ binDir = if enableSeparateBinOutput then "$bin/bin" else "$out/bin";
102+ libexecDir = if enableSeparateBinOutput then "$libexec/bin" else "$out/libexec";
103+ etcDir = if enableSeparateEtcOutput then "$etc/etc" else "$out/etc";
104+ docDir = if enableSeparateDocOutput then "$doc/share/doc" else "$out/share/doc";
105+ dataDir = if enableSeparateDataOutput then "$data/share/${ghc.name}" else "$out/share/${ghc.name}";
98106
99107 # We cannot enable -j<n> parallelism for libraries because GHC is far more
100108 # likely to generate a non-determistic library ID in that case. Further
@@ -113,12 +121,20 @@ let
113121 stdenv.lib.optionalString isCross (" " + stdenv.lib.concatStringsSep " " crossCabalFlags);
114122
115123 defaultConfigureFlags = [
116− "--verbose" "--prefix=$out" "--libdir=\\$prefix/lib/\\$compiler" "--libsubdir=\\$pkgid"
117− (optionalString enableSeparateDataOutput "--datadir=$data/share/${ghc.name}")
118− (optionalString enableSeparateDocOutput "--docdir=${docdir "$doc"}")
124+ "--verbose" "--prefix=$out"
125+ # Binary directory has to be $bin/bin instead of just $bin: this
126+ # is so that the package is added to the PATH when it's used as a
127+ # build input. Sadly mkDerivation won't add inputs that don't have
128+ # bin subdirectory.
129+ "--bindir=${binDir}"
130+ "--libdir=${libDir}" "--libsubdir=\\$pkgid"
131+ "--libexecdir=${libexecDir}"
132+ (optionalString (enableSeparateEtcOutput) "--sysconfdir=${etcDir}") # Old versions of cabal don't support this flag.
133+ "--datadir=${dataDir}"
134+ "--docdir=${docDir}"
119135 "--with-gcc=$CC" # Clang won't work without that extra information.
120136 "--package-db=$packageConfDir"
121− (optionalString (enableSharedExecutables && stdenv.isLinux) "--ghc-option=-optl=-Wl,-rpath=$out/lib/${ghc.name}/${pname}-${version}")
137+ (optionalString (enableSharedExecutables && stdenv.isLinux) "--ghc-option=-optl=-Wl,-rpath=${libDir}/${pname}-${version}")
122138 (optionalString (enableSharedExecutables && stdenv.isDarwin) "--ghc-option=-optl=-Wl,-headerpad_max_install_names")
123139 (optionalString enableParallelBuilding "--ghc-option=-j$NIX_BUILD_CORES")
124140 (optionalString useCpphs "--with-cpphs=${cpphs}/bin/cpphs --ghc-options=-cpp --ghc-options=-pgmP${cpphs}/bin/cpphs --ghc-options=-optP--cpp")
@@ -152,8 +168,11 @@ let
152168 allPkgconfigDepends = pkgconfigDepends ++ libraryPkgconfigDepends ++ executablePkgconfigDepends ++
153169 optionals doCheck testPkgconfigDepends ++ optionals doBenchmark benchmarkPkgconfigDepends;
154170
155− nativeBuildInputs = optional (allPkgconfigDepends != []) pkgconfig ++
156− buildTools ++ libraryToolDepends ++ executableToolDepends ++ [ removeReferencesTo ];
171+ nativeBuildInputs = map stdenv.lib.getBin
172+ ( optional (allPkgconfigDepends != []) pkgconfig
173+ ++ buildTools ++ libraryToolDepends ++ executableToolDepends
174+ ++ [ removeReferencesTo ]
175+ );
157176 propagatedBuildInputs = buildDepends ++ libraryHaskellDepends ++ executableHaskellDepends;
158177 otherBuildInputs = setupHaskellDepends ++ extraLibraries ++ librarySystemDepends ++ executableSystemDepends ++
159178 optionals (allPkgconfigDepends != []) allPkgconfigDepends ++
@@ -182,7 +201,15 @@ assert allPkgconfigDepends != [] -> pkgconfig != null;
182201stdenv.mkDerivation ({
183202 name = "${pname}-${version}";
184203
185− outputs = if (args ? outputs) then args.outputs else ([ "out" ] ++ (optional enableSeparateDataOutput "data") ++ (optional enableSeparateDocOutput "doc"));
204+ outputs = if (args ? outputs) then args.outputs else
205+ ( (optional enableSeparateBinOutput "bin")
206+ ++ (optional enableSeparateBinOutput "libexec")
207+ ++ [ "out" ]
208+ ++ (optional enableSeparateDataOutput "data")
209+ ++ (optional enableSeparateDocOutput "doc")
210+ ++ (optional enableSeparateEtcOutput "etc")
211+ ++ (optional hasLibOutput "lib")
212+ );
186213 setOutputFlags = false;
187214
188215 pos = builtins.unsafeGetAttrPos "pname" args;
@@ -206,7 +233,7 @@ stdenv.mkDerivation ({
206233
207234 postPatch = optionalString jailbreak ''
208235 echo "Run jailbreak-cabal to lift version restrictions on build inputs."
209− ${jailbreak-cabal}/bin/jailbreak-cabal ${pname}.cabal
236+ ${stdenv.lib.getBin jailbreak-cabal}/bin/jailbreak-cabal ${pname}.cabal
210237 '' + postPatch;
211238
212239 setupCompilerEnvironmentPhase = ''
@@ -214,7 +241,7 @@ stdenv.mkDerivation ({
214241
215242 echo "Build with ${ghc}."
216243 export PATH="${ghc}/bin:$PATH"
217− ${optionalString (hasActiveLibrary && hyperlinkSource) "export PATH=${hscolour}/bin:$PATH"}
244+ ${optionalString (hasActiveLibrary && hyperlinkSource) "export PATH=${stdenv.lib.getBin hscolour}/bin:$PATH"}
218245
219246 packageConfDir="$TMPDIR/package.conf.d"
220247 mkdir -p $packageConfDir
@@ -241,7 +268,7 @@ stdenv.mkDerivation ({
241268 #
242269 # Create a local directory with symlinks of the *.dylib (macOS shared
243270 # libraries) from all the dependencies.
244− local dynamicLinksDir="$out/lib/links"
271+ local dynamicLinksDir="${libDir}/links"
245272 mkdir -p $dynamicLinksDir
246273 for d in $(grep dynamic-library-dirs "$packageConfDir/"*|awk '{print $2}'); do
247274 ln -s "$d/"*.dylib $dynamicLinksDir
@@ -313,7 +340,7 @@ stdenv.mkDerivation ({
313340
314341 ${if !hasActiveLibrary then "${setupCommand} install" else ''
315342 ${setupCommand} copy
316− local packageConfDir="$out/lib/${ghc.name}/package.conf.d"
343+ local packageConfDir="${libDir}/package.conf.d"
317344 local packageConfFile="$packageConfDir/${pname}-${version}.conf"
318345 mkdir -p "$packageConfDir"
319346 ${setupCommand} register --gen-pkg-config=$packageConfFile
@@ -321,7 +348,7 @@ stdenv.mkDerivation ({
321348 mv $packageConfFile $packageConfDir/$pkgId.conf
322349 ''}
323350 ${optionalString isGhcjs ''
324− for exeDir in "$out/bin/"*.jsexe; do
351+ for exeDir in "${binDir}/"*.jsexe; do
325352 exe="''${exeDir%.jsexe}"
326353 printWords '#!${nodejs}/bin/node' > "$exe"
327354 cat "$exeDir/all.js" >> "$exe"
@@ -330,18 +357,68 @@ stdenv.mkDerivation ({
330357 ''}
331358 ${optionalString doCoverage "mkdir -p $out/share && cp -r dist/hpc $out/share"}
332359 ${optionalString (enableSharedExecutables && isExecutable && !isGhcjs && stdenv.isDarwin && stdenv.lib.versionOlder ghc.version "7.10") ''
333− for exe in "$out/bin/"* ; do
334− install_name_tool -add_rpath "$out/lib/ghc-${ghc.version}/${pname}-${version}" "$exe"
360+ for exe in "${binDir}/"* ; do
361+ install_name_tool -add_rpath "${libDir}/${pname}-${version}" "$exe"
335362 done
336363 ''}
337364
338365 ${optionalString enableSeparateDocOutput ''
339− for x in ${docdir "$doc"}/html/src/*.html; do
340− remove-references-to -t $out $x
366+ # Remove references back to $out but also back to $lib if we have
367+ # docs. $lib is needed as it stores path to haddock interfaces in the
368+ # conf file which creates a cycle if docs refer back to library
369+ # path.
370+ mkdir -p ${docDir}
371+
372+ for x in ${docDir}/html/src/*.html; do
373+ remove-references-to -t $out -t ${libDir} -t ${binDir} ${optionalString enableSeparateDataOutput "-t $data"} $x
341374 done
342− mkdir -p $doc
343375 ''}
344− ${optionalString enableSeparateDataOutput "mkdir -p $data"}
376+
377+ ${optionalString hasLibOutput ''
378+ # Even if we don't have binary output for the package, things like
379+ # Paths files will embed paths to bin/libexec directories in themselves
380+ # which results in .lib <-> $out cyclic store reference. We
381+ # therefore patch out the paths from separate library if we don't have
382+ # separate bin output too.
383+ #
384+ # If we _do_ have separate bin and lib outputs, we may still be in
385+ # trouble in case of shared executables: executable contains path to
386+ # .lib, .lib contains path (through Paths) to .bin and we have a
387+ # cycle.
388+ #
389+ # Lastly we have to deal with references from .lib back into
390+ # $out/share if we're not splitting out data directory.
391+ #
392+ # It may happen that we have hasLibOutput set but the library
393+ # directory was not created: this happens in the case that library
394+ # section is not exposing any modules. See "fail" package for an
395+ # example where no modules are exposed for GHC >= 8.0.
396+ if [ -d ${libDir} ]; then
397+ find ${libDir} -type f -exec \
398+ remove-references-to -t ${binDir} -t ${libexecDir} "{}" \;
399+ fi
400+ ''}
401+
402+ ${optionalString (hasLibOutput && ! enableSeparateDocOutput) ''
403+ # If we don't have separate docs, we have to patch out the ref to
404+ # docs in package conf. This will likely break Haddock
405+ # cross-package links but is necessary to break store cycle…
406+ find ${libDir}/ -type f -name '*.conf' -exec \
407+ remove-references-to -t ${docDir} "{}" \;
408+ ''}
409+
410+ ${optionalString (hasLibOutput && ! enableSeparateDataOutput) ''
411+ # Just like for doc output path in $out potentially landing in
412+ # *.conf, we have to also remove the data directory so that it
413+ # doesn't appear under data-dir field creating a cycle.
414+ find ${libDir}/ -type f -exec echo Removing ${dataDir} refs from "{}" \;
415+ find ${libDir}/ -type f -exec \
416+ remove-references-to -t ${dataDir} "{}" \;
417+ ''}
418+
419+ ${optionalString enableSeparateDataOutput "mkdir -p ${dataDir}"}
420+ ${optionalString enableSeparateBinOutput "mkdir -p ${binDir} ${libexecDir}"}
421+ ${optionalString enableSeparateEtcOutput "mkdir -p ${etcDir}"}
345422
346423 runHook postInstall
347424 '';
@@ -358,7 +435,7 @@ stdenv.mkDerivation ({
358435 # the directory containing the haddock documentation.
359436 # `null' if no haddock documentation was built.
360437 # TODO: fetch the self from the fixpoint instead
361− haddockDir = self: if doHaddock then "${docdir self.doc}/html" else null;
438+ haddockDir = self: if doHaddock then "${docDir}/html" else null;
362439
363440 env = stdenv.mkDerivation {
364441 name = "interactive-${pname}-${version}-environment";
@@ -386,6 +463,7 @@ stdenv.mkDerivation ({
386463 // optionalAttrs (description != "") { inherit description; }
387464 // optionalAttrs (maintainers != []) { inherit maintainers; }
388465 // optionalAttrs (hydraPlatforms != platforms) { inherit hydraPlatforms; }
466+ // optionalAttrs (outputsToInstall != []) { inherit outputsToInstall; }
389467 ;
390468
391469}