1# TODO(@Ericson2314): Remove `pkgs` param, which is only used for
2# `buildStackProject`, `justStaticExecutables` and `checkUnusedPackages`
3{ pkgs, lib }:
4
5rec {
6
7 /* This function takes a file like `hackage-packages.nix` and constructs
8 a full package set out of that.
9 */
10 makePackageSet = import ../make-package-set.nix;
11
12 /* The function overrideCabal lets you alter the arguments to the
13 mkDerivation function.
14
15 Example:
16
17 First, note how the aeson package is constructed in hackage-packages.nix:
18
19 "aeson" = callPackage ({ mkDerivation, attoparsec, <snip>
20 }:
21 mkDerivation {
22 pname = "aeson";
23 <snip>
24 homepage = "https://github.com/bos/aeson";
25 })
26
27 The mkDerivation function of haskellPackages will take care of putting
28 the homepage in the right place, in meta.
29
30 > haskellPackages.aeson.meta.homepage
31 "https://github.com/bos/aeson"
32
33 > x = haskell.lib.compose.overrideCabal (old: { homepage = old.homepage + "#readme"; }) haskellPackages.aeson
34 > x.meta.homepage
35 "https://github.com/bos/aeson#readme"
36
37 */
38 overrideCabal = f: drv: (drv.override (args: args // {
39 mkDerivation = drv: (args.mkDerivation drv).override f;
40 })) // {
41 overrideScope = scope: overrideCabal f (drv.overrideScope scope);
42 };
43
44 # : Map Name (Either Path VersionNumber) -> HaskellPackageOverrideSet
45 # Given a set whose values are either paths or version strings, produces
46 # a package override set (i.e. (self: super: { etc. })) that sets
47 # the packages named in the input set to the corresponding versions
48 packageSourceOverrides =
49 overrides: self: super: pkgs.lib.mapAttrs (name: src:
50 let isPath = x: builtins.substring 0 1 (toString x) == "/";
51 generateExprs = if isPath src
52 then self.callCabal2nix
53 else self.callHackage;
54 in generateExprs name src {}) overrides;
55
56 /* doCoverage modifies a haskell package to enable the generation
57 and installation of a coverage report.
58
59 See https://wiki.haskell.org/Haskell_program_coverage
60 */
61 doCoverage = overrideCabal (drv: { doCoverage = true; });
62
63 /* dontCoverage modifies a haskell package to disable the generation
64 and installation of a coverage report.
65 */
66 dontCoverage = overrideCabal (drv: { doCoverage = false; });
67
68 /* doHaddock modifies a haskell package to enable the generation and
69 installation of API documentation from code comments using the
70 haddock tool.
71 */
72 doHaddock = overrideCabal (drv: { doHaddock = true; });
73
74 /* dontHaddock modifies a haskell package to disable the generation and
75 installation of API documentation from code comments using the
76 haddock tool.
77 */
78 dontHaddock = overrideCabal (drv: { doHaddock = false; });
79
80 /* doJailbreak enables the removal of version bounds from the cabal
81 file. You may want to avoid this function.
82
83 This is useful when a package reports that it can not be built
84 due to version mismatches. In some cases, removing the version
85 bounds entirely is an easy way to make a package build, but at
86 the risk of breaking software in non-obvious ways now or in the
87 future.
88
89 Instead of jailbreaking, you can patch the cabal file.
90
91 Note that jailbreaking at this time, doesn't lift bounds on
92 conditional branches.
93 https://github.com/peti/jailbreak-cabal/issues/7 has further details.
94
95 */
96 doJailbreak = overrideCabal (drv: { jailbreak = true; });
97
98 /* dontJailbreak restores the use of the version bounds the check
99 the use of dependencies in the package description.
100 */
101 dontJailbreak = overrideCabal (drv: { jailbreak = false; });
102
103 /* doCheck enables dependency checking, compilation and execution
104 of test suites listed in the package description file.
105 */
106 doCheck = overrideCabal (drv: { doCheck = true; });
107 /* dontCheck disables dependency checking, compilation and execution
108 of test suites listed in the package description file.
109 */
110 dontCheck = overrideCabal (drv: { doCheck = false; });
111
112 /* doBenchmark enables dependency checking and compilation
113 for benchmarks listed in the package description file.
114 Benchmarks are, however, not executed at the moment.
115 */
116 doBenchmark = overrideCabal (drv: { doBenchmark = true; });
117 /* dontBenchmark disables dependency checking, compilation and execution
118 for benchmarks listed in the package description file.
119 */
120 dontBenchmark = overrideCabal (drv: { doBenchmark = false; });
121
122 /* doDistribute enables the distribution of binaries for the package
123 via hydra.
124 */
125 doDistribute = overrideCabal (drv: {
126 # lib.platforms.all is the default value for platforms (since GHC can cross-compile)
127 hydraPlatforms = lib.subtractLists (drv.badPlatforms or [])
128 (drv.platforms or lib.platforms.all);
129 });
130 /* dontDistribute disables the distribution of binaries for the package
131 via hydra.
132 */
133 dontDistribute = overrideCabal (drv: { hydraPlatforms = []; });
134
135 /* appendConfigureFlag adds a single argument that will be passed to the
136 cabal configure command, after the arguments that have been defined
137 in the initial declaration or previous overrides.
138
139 Example:
140
141 > haskell.lib.compose.appendConfigureFlag "--profiling-detail=all-functions" haskellPackages.servant
142 */
143 appendConfigureFlag = x: appendConfigureFlags [x];
144 appendConfigureFlags = xs: overrideCabal (drv: { configureFlags = (drv.configureFlags or []) ++ xs; });
145
146 appendBuildFlag = x: overrideCabal (drv: { buildFlags = (drv.buildFlags or []) ++ [x]; });
147 appendBuildFlags = xs: overrideCabal (drv: { buildFlags = (drv.buildFlags or []) ++ xs; });
148
149 /* removeConfigureFlag drv x is a Haskell package like drv, but with
150 all cabal configure arguments that are equal to x removed.
151
152 > haskell.lib.compose.removeConfigureFlag "--verbose" haskellPackages.servant
153 */
154 removeConfigureFlag = x: overrideCabal (drv: { configureFlags = lib.remove x (drv.configureFlags or []); });
155
156 addBuildTool = x: addBuildTools [x];
157 addBuildTools = xs: overrideCabal (drv: { buildTools = (drv.buildTools or []) ++ xs; });
158
159 addExtraLibrary = x: addExtraLibraries [x];
160 addExtraLibraries = xs: overrideCabal (drv: { extraLibraries = (drv.extraLibraries or []) ++ xs; });
161
162 addBuildDepend = x: addBuildDepends [x];
163 addBuildDepends = xs: overrideCabal (drv: { buildDepends = (drv.buildDepends or []) ++ xs; });
164
165 addTestToolDepend = x: addTestToolDepends [x];
166 addTestToolDepends = xs: overrideCabal (drv: { testToolDepends = (drv.testToolDepends or []) ++ xs; });
167
168 addPkgconfigDepend = x: addPkgconfigDepends [x];
169 addPkgconfigDepends = xs: overrideCabal (drv: { pkg-configDepends = (drv.pkg-configDepends or []) ++ xs; });
170
171 addSetupDepend = x: addSetupDepends [x];
172 addSetupDepends = xs: overrideCabal (drv: { setupHaskellDepends = (drv.setupHaskellDepends or []) ++ xs; });
173
174 enableCabalFlag = x: drv: appendConfigureFlag "-f${x}" (removeConfigureFlag "-f-${x}" drv);
175 disableCabalFlag = x: drv: appendConfigureFlag "-f-${x}" (removeConfigureFlag "-f${x}" drv);
176
177 markBroken = overrideCabal (drv: { broken = true; hydraPlatforms = []; });
178 unmarkBroken = overrideCabal (drv: { broken = false; });
179 markBrokenVersion = version: drv: assert drv.version == version; markBroken drv;
180 markUnbroken = overrideCabal (drv: { broken = false; });
181
182 enableLibraryProfiling = overrideCabal (drv: { enableLibraryProfiling = true; });
183 disableLibraryProfiling = overrideCabal (drv: { enableLibraryProfiling = false; });
184
185 enableExecutableProfiling = overrideCabal (drv: { enableExecutableProfiling = true; });
186 disableExecutableProfiling = overrideCabal (drv: { enableExecutableProfiling = false; });
187
188 enableSharedExecutables = overrideCabal (drv: { enableSharedExecutables = true; });
189 disableSharedExecutables = overrideCabal (drv: { enableSharedExecutables = false; });
190
191 enableSharedLibraries = overrideCabal (drv: { enableSharedLibraries = true; });
192 disableSharedLibraries = overrideCabal (drv: { enableSharedLibraries = false; });
193
194 enableDeadCodeElimination = overrideCabal (drv: { enableDeadCodeElimination = true; });
195 disableDeadCodeElimination = overrideCabal (drv: { enableDeadCodeElimination = false; });
196
197 enableStaticLibraries = overrideCabal (drv: { enableStaticLibraries = true; });
198 disableStaticLibraries = overrideCabal (drv: { enableStaticLibraries = false; });
199
200 enableSeparateBinOutput = overrideCabal (drv: { enableSeparateBinOutput = true; });
201
202 appendPatch = x: appendPatches [x];
203 appendPatches = xs: overrideCabal (drv: { patches = (drv.patches or []) ++ xs; });
204
205 /* Set a specific build target instead of compiling all targets in the package.
206 * For example, imagine we have a .cabal file with a library, and 2 executables "dev" and "server".
207 * We can build only "server" and not wait on the compilation of "dev" by using setBuildTarget as follows:
208 *
209 * > setBuildTarget "server" (callCabal2nix "thePackageName" thePackageSrc {})
210 *
211 */
212 setBuildTargets = xs: overrideCabal (drv: { buildTarget = lib.concatStringsSep " " xs; });
213 setBuildTarget = x: setBuildTargets [x];
214
215 doHyperlinkSource = overrideCabal (drv: { hyperlinkSource = true; });
216 dontHyperlinkSource = overrideCabal (drv: { hyperlinkSource = false; });
217
218 disableHardening = flags: overrideCabal (drv: { hardeningDisable = flags; });
219
220 /* Let Nix strip the binary files.
221 * This removes debugging symbols.
222 */
223 doStrip = overrideCabal (drv: { dontStrip = false; });
224
225 /* Stop Nix from stripping the binary files.
226 * This keeps debugging symbols.
227 */
228 dontStrip = overrideCabal (drv: { dontStrip = true; });
229
230 /* Useful for debugging segfaults with gdb.
231 * This includes dontStrip.
232 */
233 enableDWARFDebugging = drv:
234 # -g: enables debugging symbols
235 # --disable-*-stripping: tell GHC not to strip resulting binaries
236 # dontStrip: see above
237 appendConfigureFlag "--ghc-options=-g --disable-executable-stripping --disable-library-stripping" (dontStrip drv);
238
239 /* Create a source distribution tarball like those found on hackage,
240 instead of building the package.
241 */
242 sdistTarball = pkg: lib.overrideDerivation pkg (drv: {
243 name = "${drv.pname}-source-${drv.version}";
244 # Since we disable the haddock phase, we also need to override the
245 # outputs since the separate doc output will not be produced.
246 outputs = ["out"];
247 buildPhase = "./Setup sdist";
248 haddockPhase = ":";
249 checkPhase = ":";
250 installPhase = "install -D dist/${drv.pname}-*.tar.gz $out/${drv.pname}-${drv.version}.tar.gz";
251 fixupPhase = ":";
252 });
253
254 /* Create a documentation tarball suitable for uploading to Hackage instead
255 of building the package.
256 */
257 documentationTarball = pkg:
258 pkgs.lib.overrideDerivation pkg (drv: {
259 name = "${drv.name}-docs";
260 # Like sdistTarball, disable the "doc" output here.
261 outputs = [ "out" ];
262 buildPhase = ''
263 runHook preHaddock
264 ./Setup haddock --for-hackage
265 runHook postHaddock
266 '';
267 haddockPhase = ":";
268 checkPhase = ":";
269 installPhase = ''
270 runHook preInstall
271 mkdir -p "$out"
272 tar --format=ustar \
273 -czf "$out/${drv.name}-docs.tar.gz" \
274 -C dist/doc/html "${drv.name}-docs"
275 runHook postInstall
276 '';
277 });
278
279 /* Use the gold linker. It is a linker for ELF that is designed
280 "to run as fast as possible on modern systems"
281 */
282 linkWithGold = appendConfigureFlag
283 "--ghc-option=-optl-fuse-ld=gold --ld-option=-fuse-ld=gold --with-ld=ld.gold";
284
285 /* link executables statically against haskell libs to reduce
286 closure size
287 */
288 justStaticExecutables = overrideCabal (drv: {
289 enableSharedExecutables = false;
290 enableLibraryProfiling = false;
291 isLibrary = false;
292 doHaddock = false;
293 postFixup = drv.postFixup or "" + ''
294
295 # Remove every directory which could have links to other store paths.
296 rm -rf $out/lib $out/nix-support $out/share/doc
297 '';
298 });
299
300 /* Build a source distribution tarball instead of using the source files
301 directly. The effect is that the package is built as if it were published
302 on hackage. This can be used as a test for the source distribution,
303 assuming the build fails when packaging mistakes are in the cabal file.
304
305 A faster implementation using `cabal-install` is available as
306 `buildFromCabalSdist` in your Haskell package set.
307 */
308 buildFromSdist = pkg: overrideCabal (drv: {
309 src = "${sdistTarball pkg}/${pkg.pname}-${pkg.version}.tar.gz";
310
311 # Revising and jailbreaking the cabal file has been handled in sdistTarball
312 revision = null;
313 editedCabalFile = null;
314 jailbreak = false;
315 }) pkg;
316
317 /* Build the package in a strict way to uncover potential problems.
318 This includes buildFromSdist and failOnAllWarnings.
319 */
320 buildStrictly = pkg: buildFromSdist (failOnAllWarnings pkg);
321
322 /* Disable core optimizations, significantly speeds up build time */
323 disableOptimization = appendConfigureFlag "--disable-optimization";
324
325 /* Turn on most of the compiler warnings and fail the build if any
326 of them occur. */
327 failOnAllWarnings = appendConfigureFlag "--ghc-option=-Wall --ghc-option=-Werror";
328
329 /* Add a post-build check to verify that dependencies declared in
330 the cabal file are actually used.
331
332 The first attrset argument can be used to configure the strictness
333 of this check and a list of ignored package names that would otherwise
334 cause false alarms.
335 */
336 checkUnusedPackages =
337 { ignoreEmptyImports ? false
338 , ignoreMainModule ? false
339 , ignorePackages ? []
340 } : drv :
341 overrideCabal (_drv: {
342 postBuild = with lib;
343 let args = concatStringsSep " " (
344 optional ignoreEmptyImports "--ignore-empty-imports" ++
345 optional ignoreMainModule "--ignore-main-module" ++
346 map (pkg: "--ignore-package ${pkg}") ignorePackages
347 );
348 in "${pkgs.haskellPackages.packunused}/bin/packunused" +
349 optionalString (args != "") " ${args}";
350 }) (appendConfigureFlag "--ghc-option=-ddump-minimal-imports" drv);
351
352 buildStackProject = pkgs.callPackage ../generic-stack-builder.nix { };
353
354 /* Add a dummy command to trigger a build despite an equivalent
355 earlier build that is present in the store or cache.
356 */
357 triggerRebuild = i: overrideCabal (drv: { postUnpack = ": trigger rebuild ${toString i}"; });
358
359 /* Override the sources for the package and optionally the version.
360 This also takes of removing editedCabalFile.
361 */
362 overrideSrc = { src, version ? null }: drv:
363 overrideCabal (_: { inherit src; version = if version == null then drv.version else version; editedCabalFile = null; }) drv;
364
365 # Get all of the build inputs of a haskell package, divided by category.
366 getBuildInputs = p: p.getBuildInputs;
367
368 # Extract the haskell build inputs of a haskell package.
369 # This is useful to build environments for developing on that
370 # package.
371 getHaskellBuildInputs = p: (getBuildInputs p).haskellBuildInputs;
372
373 # Under normal evaluation, simply return the original package. Under
374 # nix-shell evaluation, return a nix-shell optimized environment.
375 shellAware = p: if lib.inNixShell then p.env else p;
376
377 ghcInfo = ghc:
378 rec { isCross = (ghc.cross or null) != null;
379 isGhcjs = ghc.isGhcjs or false;
380 nativeGhc = if isCross || isGhcjs
381 then ghc.bootPkgs.ghc
382 else ghc;
383 };
384
385 ### mkDerivation helpers
386 # These allow external users of a haskell package to extract
387 # information about how it is built in the same way that the
388 # generic haskell builder does, by reusing the same functions.
389 # Each function here has the same interface as mkDerivation and thus
390 # can be called for a given package simply by overriding the
391 # mkDerivation argument it used. See getHaskellBuildInputs above for
392 # an example of this.
393
394 # Some information about which phases should be run.
395 controlPhases = ghc: let inherit (ghcInfo ghc) isCross; in
396 { doCheck ? !isCross && (lib.versionOlder "7.4" ghc.version)
397 , doBenchmark ? false
398 , ...
399 }: { inherit doCheck doBenchmark; };
400
401 # Utility to convert a directory full of `cabal2nix`-generated files into a
402 # package override set
403 #
404 # packagesFromDirectory : { directory : Directory, ... } -> HaskellPackageOverrideSet
405 packagesFromDirectory =
406 { directory, ... }:
407
408 self: super:
409 let
410 haskellPaths = builtins.attrNames (builtins.readDir directory);
411
412 toKeyVal = file: {
413 name = builtins.replaceStrings [ ".nix" ] [ "" ] file;
414
415 value = self.callPackage (directory + "/${file}") { };
416 };
417
418 in
419 builtins.listToAttrs (map toKeyVal haskellPaths);
420
421 /*
422 INTERNAL function retained for backwards compatibility, use
423 haskell.packages.*.generateOptparseApplicativeCompletions instead!
424 */
425 __generateOptparseApplicativeCompletion = exeName: overrideCabal (drv: {
426 postInstall = (drv.postInstall or "") + ''
427 bashCompDir="''${!outputBin}/share/bash-completion/completions"
428 zshCompDir="''${!outputBin}/share/zsh/vendor-completions"
429 fishCompDir="''${!outputBin}/share/fish/vendor_completions.d"
430 mkdir -p "$bashCompDir" "$zshCompDir" "$fishCompDir"
431 "''${!outputBin}/bin/${exeName}" --bash-completion-script "''${!outputBin}/bin/${exeName}" >"$bashCompDir/${exeName}"
432 "''${!outputBin}/bin/${exeName}" --zsh-completion-script "''${!outputBin}/bin/${exeName}" >"$zshCompDir/_${exeName}"
433 "''${!outputBin}/bin/${exeName}" --fish-completion-script "''${!outputBin}/bin/${exeName}" >"$fishCompDir/${exeName}.fish"
434
435 # Sanity check
436 grep -F ${exeName} <$bashCompDir/${exeName} >/dev/null || {
437 echo 'Could not find ${exeName} in completion script.'
438 exit 1
439 }
440 '';
441 });
442
443 /*
444 Retained for backwards compatibility.
445 Use haskell.packages.*.generateOptparseApplicativeCompletions
446 which is cross aware instead.
447 */
448 generateOptparseApplicativeCompletions = commands: pkg:
449 lib.warnIf (lib.isInOldestRelease 2211) "haskellLib.generateOptparseApplicativeCompletions is deprecated in favor of haskellPackages.generateOptparseApplicativeCompletions. Please change ${pkg.name} to use the latter and make sure it uses its matching haskell.packages set!"
450 (pkgs.lib.foldr __generateOptparseApplicativeCompletion pkg commands);
451
452 /*
453 Retained for backwards compatibility.
454 Use haskell.packages.*.generateOptparseApplicativeCompletions
455 which is cross aware instead.
456 */
457 generateOptparseApplicativeCompletion = command: pkg:
458 lib.warnIf (lib.isInOldestRelease 2211) "haskellLib.generateOptparseApplicativeCompletion is deprecated in favor of haskellPackages.generateOptparseApplicativeCompletions (plural!). Please change ${pkg.name} to use the latter and make sure it uses its matching haskell.packages set!"
459 (__generateOptparseApplicativeCompletion command pkg);
460
461 # Don't fail at configure time if there are multiple versions of the
462 # same package in the (recursive) dependencies of the package being
463 # built. Will delay failures, if any, to compile time.
464 allowInconsistentDependencies = overrideCabal (drv: {
465 allowInconsistentDependencies = true;
466 });
467
468 # Work around a Cabal bug requiring pkg-config --static --libs to work even
469 # when linking dynamically, affecting Cabal 3.8 and 3.9.
470 # https://github.com/haskell/cabal/issues/8455
471 #
472 # For this, we treat the runtime system/pkg-config dependencies of a Haskell
473 # derivation as if they were propagated from their dependencies which allows
474 # pkg-config --static to work in most cases.
475 #
476 # Warning: This function may change or be removed at any time, e.g. if we find
477 # a different workaround, upstream fixes the bug or we patch Cabal.
478 __CabalEagerPkgConfigWorkaround =
479 let
480 # Take list of derivations and return list of the transitive dependency
481 # closure, only taking into account buildInputs. Loosely based on
482 # closePropagationFast.
483 propagatedPlainBuildInputs = drvs:
484 builtins.map (i: i.val) (
485 builtins.genericClosure {
486 startSet = builtins.map (drv:
487 { key = drv.outPath; val = drv; }
488 ) drvs;
489 operator = { val, ... }:
490 if !lib.isDerivation val
491 then [ ]
492 else
493 builtins.concatMap (drv:
494 if !lib.isDerivation drv
495 then [ ]
496 else [ { key = drv.outPath; val = drv; } ]
497 ) (val.buildInputs or [ ] ++ val.propagatedBuildInputs or [ ]);
498 }
499 );
500 in
501 overrideCabal (old: {
502 benchmarkPkgconfigDepends = propagatedPlainBuildInputs old.benchmarkPkgconfigDepends or [ ];
503 executablePkgconfigDepends = propagatedPlainBuildInputs old.executablePkgconfigDepends or [ ];
504 libraryPkgconfigDepends = propagatedPlainBuildInputs old.libraryPkgconfigDepends or [ ];
505 testPkgconfigDepends = propagatedPlainBuildInputs old.testPkgconfigDepends or [ ];
506 });
507}