Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1# Haskell {#haskell} 2 3The Haskell infrastructure in Nixpkgs has two main purposes: The primary purpose 4is to provide a Haskell compiler and build tools as well as infrastructure for 5packaging Haskell-based packages. 6 7The secondary purpose is to provide support for Haskell development environments 8including prebuilt Haskell libraries. However, in this area sacrifices have been 9made due to self-imposed restrictions in Nixpkgs, to lessen the maintenance 10effort and to improve performance. (More details in the subsection 11[Limitations.](#haskell-limitations)) 12 13## Available packages {#haskell-available-packages} 14 15The compiler and most build tools are exposed at the top level: 16 17* `ghc` is the default version of GHC 18* Language specific tools: `cabal-install`, `stack`, `hpack`, … 19 20Many “normal” user facing packages written in Haskell, like `niv` or `cachix`, 21are also exposed at the top level, and there is nothing Haskell specific to 22installing and using them. 23 24All of these packages are originally defined in the `haskellPackages` package 25set and are re-exposed with a reduced dependency closure for convenience. 26(see `justStaticExecutables` below) 27 28The `haskellPackages` set includes at least one version of every package from 29Hackage as well as some manually injected packages. This amounts to a lot of 30packages, so it is hidden from `nix-env -qa` by default for performance reasons. 31You can still list all packages in the set like this: 32 33```console 34$ nix-env -f '<nixpkgs>' -qaP -A haskellPackages 35haskellPackages.a50 a50-0.5 36haskellPackages.AAI AAI-0.2.0.1 37haskellPackages.aasam aasam-0.2.0.0 38haskellPackages.abacate abacate-0.0.0.0 39haskellPackages.abc-puzzle abc-puzzle-0.2.1 40 41``` 42Also, the `haskellPackages` set is included on [search.nixos.org]. 43 44The attribute names in `haskellPackages` always correspond with their name on 45Hackage. Since Hackage allows names that are not valid Nix without escaping, 46you need to take care when handling attribute names like `3dmodels`. 47 48For packages that are part of [Stackage], we use the version prescribed by a 49Stackage solver (usually the current LTS one) as the default version. For all 50other packages we use the latest version from Hackage. See 51[below](#haskell-available-versions) to learn which versions are provided 52exactly. 53 54Roughly half of the 16K packages contained in `haskellPackages` don't actually 55build and are marked as broken semi-automatically. Most of those packages are 56deprecated or unmaintained, but sometimes packages that should build, do not 57build. Very often fixing them is not a lot of work. 58 59<!-- 60TODO(@sternenseemann): 61How you can help with that is 62described in [Fixing a broken package](#haskell-fixing-a-broken-package). 63--> 64 65`haskellPackages` is built with our default compiler, but we also provide other 66releases of GHC and package sets built with them. You can list all available 67compilers like this: 68 69```console 70$ nix-env -f '<nixpkgs>' -qaP -A haskell.compiler 71haskell.compiler.ghc810 ghc-8.10.7 72haskell.compiler.ghc88 ghc-8.8.4 73haskell.compiler.ghc90 ghc-9.0.2 74haskell.compiler.ghc924 ghc-9.2.4 75haskell.compiler.ghc925 ghc-9.2.5 76haskell.compiler.ghc926 ghc-9.2.6 77haskell.compiler.ghc92 ghc-9.2.7 78haskell.compiler.ghc942 ghc-9.4.2 79haskell.compiler.ghc943 ghc-9.4.3 80haskell.compiler.ghc94 ghc-9.4.4 81haskell.compiler.ghcHEAD ghc-9.7.20221224 82haskell.compiler.ghc8102Binary ghc-binary-8.10.2 83haskell.compiler.ghc8102BinaryMinimal ghc-binary-8.10.2 84haskell.compiler.ghc8107BinaryMinimal ghc-binary-8.10.7 85haskell.compiler.ghc8107Binary ghc-binary-8.10.7 86haskell.compiler.ghc865Binary ghc-binary-8.6.5 87haskell.compiler.ghc924Binary ghc-binary-9.2.4 88haskell.compiler.ghc924BinaryMinimal ghc-binary-9.2.4 89haskell.compiler.integer-simple.ghc810 ghc-integer-simple-8.10.7 90haskell.compiler.integer-simple.ghc8107 ghc-integer-simple-8.10.7 91haskell.compiler.integer-simple.ghc88 ghc-integer-simple-8.8.4 92haskell.compiler.integer-simple.ghc884 ghc-integer-simple-8.8.4 93haskell.compiler.native-bignum.ghc90 ghc-native-bignum-9.0.2 94haskell.compiler.native-bignum.ghc902 ghc-native-bignum-9.0.2 95haskell.compiler.native-bignum.ghc924 ghc-native-bignum-9.2.4 96haskell.compiler.native-bignum.ghc925 ghc-native-bignum-9.2.5 97haskell.compiler.native-bignum.ghc926 ghc-native-bignum-9.2.6 98haskell.compiler.native-bignum.ghc92 ghc-native-bignum-9.2.7 99haskell.compiler.native-bignum.ghc927 ghc-native-bignum-9.2.7 100haskell.compiler.native-bignum.ghc942 ghc-native-bignum-9.4.2 101haskell.compiler.native-bignum.ghc943 ghc-native-bignum-9.4.3 102haskell.compiler.native-bignum.ghc94 ghc-native-bignum-9.4.4 103haskell.compiler.native-bignum.ghc944 ghc-native-bignum-9.4.4 104haskell.compiler.native-bignum.ghcHEAD ghc-native-bignum-9.7.20221224 105haskell.compiler.ghcjs ghcjs-8.10.7 106``` 107 108Each of those compiler versions has a corresponding attribute set built using 109it. However, the non-standard package sets are not tested regularly and, as a 110result, contain fewer working packages. The corresponding package set for GHC 1119.4.5 is `haskell.packages.ghc945`. In fact `haskellPackages` is just an alias 112for `haskell.packages.ghc927`: 113 114```console 115$ nix-env -f '<nixpkgs>' -qaP -A haskell.packages.ghc927 116haskell.packages.ghc927.a50 a50-0.5 117haskell.packages.ghc927.AAI AAI-0.2.0.1 118haskell.packages.ghc927.aasam aasam-0.2.0.0 119haskell.packages.ghc927.abacate abacate-0.0.0.0 120haskell.packages.ghc927.abc-puzzle abc-puzzle-0.2.1 121 122``` 123 124Every package set also re-exposes the GHC used to build its packages as `haskell.packages.*.ghc`. 125 126### Available package versions {#haskell-available-versions} 127 128We aim for a “blessed” package set which only contains one version of each 129package, like Stackage (and based on it) but with more packages. Normally in 130nixpkgs the number of building Haskell packages is roughly two to three times 131the size of Stackage. For choosing the version to use for a certain package we 132use the following rules: 133 1341. By default, for every package `haskellPackages.foo` is the newest version 135found on Hackage (at the time of the last update of our package set). 1362. If the Stackage snapshot that we use (usually the newest LTS snapshot) 137contains a package, we use the Stackage version as default version for that 138package. 1393. For some packages, which are not on Stackage, we have manual overrides to 140set the default version to a version older than the newest on Hackage. We do 141this to get them or their reverse dependencies to compile in our package set. 1424. For all packages, for which the newest Hackage version is not the default 143version, there will also be a `haskellPackages.foo_x_y_z` package with the 144newest version. The `x_y_z` part encodes the version with dots replaced by 145underscores. When the newest version changes by a new release to Hackage the 146old package will disappear under that name and be replaced by a newer one under 147the name with the new version. The package name including the version will 148also disappear when the default version e.g. from Stackage catches up with the 149newest version from Hackage. 1505. For some packages, we also manually add other `haskellPackages.foo_x_y_z` 151versions, if they are required for a certain build. 152 153Relying on `haskellPackages.foo_x_y_z` attributes in derivations outside 154nixpkgs is discouraged because they may change or disappear with every package 155set update. 156<!-- TODO(@maralorn) We should add a link to callHackage, etc. once we added 157them to the docs. --> 158 159All `haskell.packages.*` package sets use the same package descriptions and the same sets 160of versions by default. There are however GHC version specific override `.nix` 161files to loosen this a bit. 162 163### Dependency resolution {#haskell-dependency-resolution} 164 165Normally when you build Haskell packages with `cabal-install`, `cabal-install` 166does dependency resolution. It will look at all Haskell package versions known 167on Hackage and tries to pick for every (transitive) dependency of your build 168exactly one version. Those versions need to satisfy all the version constraints 169given in the `.cabal` file of your package and all its dependencies. 170 171The [Haskell builder in nixpkgs](#haskell-mkderivation) does no such thing. 172It will simply take as input packages with names off the desired dependencies 173and just check whether they fulfill the version bounds and fail if they don’t 174(by default, see `jailbreak` to circumvent this). 175 176The `haskellPackages.callPackage` function does the package resolution. 177It will, e.g., use `haskellPackages.aeson`which has the default version as 178described above for a package input of name `aeson`. (More general: 179`<packages>.callPackage f` will call `f` with named inputs provided from the 180package set `<packages>`.) 181While this is the default behavior, it is possible to override the dependencies 182for a specific package, see 183[`override` and `overrideScope`](#haskell-overriding-haskell-packages). 184 185### Limitations {#haskell-limitations} 186 187Our main objective with `haskellPackages` is to package Haskell software in 188nixpkgs. This entails some limitations, partially due to self-imposed 189restrictions of nixpkgs, partially in the name of maintainability: 190 191* Only the packages built with the default compiler see extensive testing of the 192 whole package set. For other GHC versions only a few essential packages are 193 tested and cached. 194* As described above we only build one version of most packages. 195 196The experience using an older or newer packaged compiler or using different 197versions may be worse, because builds will not be cached on `cache.nixos.org` 198or may fail. 199 200Thus, to get the best experience, make sure that your project can be compiled 201using the default compiler of nixpkgs and recent versions of its dependencies. 202 203A result of this setup is, that getting a valid build plan for a given 204package can sometimes be quite painful, and in fact this is where most of the 205maintenance work for `haskellPackages` is required. Besides that, it is not 206possible to get the dependencies of a legacy project from nixpkgs or to use a 207specific stack solver for compiling a project. 208 209Even though we couldn’t use them directly in nixpkgs, it would be desirable 210to have tooling to generate working Nix package sets from build plans generated 211by `cabal-install` or a specific Stackage snapshot via import-from-derivation. 212Sadly we currently don’t have tooling for this. For this you might be 213interested in the alternative [haskell.nix] framework, which, be warned, is 214completely incompatible with packages from `haskellPackages`. 215 216<!-- TODO(@maralorn) Link to package set generation docs in the contributers guide below. --> 217 218## `haskellPackages.mkDerivation` {#haskell-mkderivation} 219 220Every haskell package set has its own haskell-aware `mkDerivation` which is used 221to build its packages. Generally you won't have to interact with this builder 222since [cabal2nix][cabal2nix] can generate packages 223using it for an arbitrary cabal package definition. Still it is useful to know 224the parameters it takes when you need to 225[override](#haskell-overriding-haskell-packages) a generated Nix expression. 226 227`haskellPackages.mkDerivation` is a wrapper around `stdenv.mkDerivation` which 228re-defines the default phases to be haskell aware and handles dependency 229specification, test suites, benchmarks etc. by compiling and invoking the 230package's `Setup.hs`. It does *not* use or invoke the `cabal-install` binary, 231but uses the underlying `Cabal` library instead. 232 233### General arguments {#haskell-derivation-args} 234 235`pname` 236: Package name, assumed to be the same as on Hackage (if applicable) 237 238`version` 239: Packaged version, assumed to be the same as on Hackage (if applicable) 240 241`src` 242: Source of the package. If omitted, fetch package corresponding to `pname` 243and `version` from Hackage. 244 245`sha256` 246: Hash to use for the default case of `src`. 247 248`revision` 249: Revision number of the updated cabal file to fetch from Hackage. 250If `null` (which is the default value), the one included in `src` is used. 251 252`editedCabalFile` 253: `sha256` hash of the cabal file identified by `revision` or `null`. 254 255`configureFlags` 256: Extra flags passed when executing the `configure` command of `Setup.hs`. 257 258`buildFlags` 259: Extra flags passed when executing the `build` command of `Setup.hs`. 260 261`haddockFlags` 262: Extra flags passed to `Setup.hs haddock` when building the documentation. 263 264`doCheck` 265: Whether to execute the package's test suite if it has one. Defaults to `true` unless cross-compiling. 266 267`doBenchmark` 268: Whether to execute the package's benchmark if it has one. Defaults to `false`. 269 270`doHoogle` 271: Whether to generate an index file for [hoogle][hoogle] as part of 272`haddockPhase` by passing the [`--hoogle` option][haddock-hoogle-option]. 273Defaults to `true`. 274 275`doHaddockQuickjump` 276: Whether to generate an index for interactive navigation of the HTML documentation. 277Defaults to `true` if supported. 278 279`doInstallIntermediates` 280: Whether to install intermediate build products (files written to `dist/build` 281by GHC during the build process). With `enableSeparateIntermediatesOutput`, 282these files are instead installed to [a separate `intermediates` 283output.][multiple-outputs] The output can then be passed into a future build of 284the same package with the `previousIntermediates` argument to support 285incremental builds. See [“Incremental builds”](#haskell-incremental-builds) for 286more information. Defaults to `false`. 287 288`enableLibraryProfiling` 289: Whether to enable [profiling][profiling] for libraries contained in the 290package. Enabled by default if supported. 291 292`enableExecutableProfiling` 293: Whether to enable [profiling][profiling] for executables contained in the 294package. Disabled by default. 295 296`profilingDetail` 297: [Profiling detail level][profiling-detail] to set. Defaults to `exported-functions`. 298 299`enableSharedExecutables` 300: Whether to link executables dynamically. By default, executables are linked statically. 301 302`enableSharedLibraries` 303: Whether to build shared Haskell libraries. This is enabled by default unless we are using 304`pkgsStatic` or shared libraries have been disabled in GHC. 305 306`enableStaticLibraries` 307: Whether to build static libraries. Enabled by default if supported. 308 309`enableDeadCodeElimination` 310: Whether to enable linker based dead code elimination in GHC. 311Enabled by default if supported. 312 313`enableHsc2hsViaAsm` 314: Whether to pass `--via-asm` to `hsc2hs`. Enabled by default only on Windows. 315 316`hyperlinkSource` 317: Whether to render the source as well as part of the haddock documentation 318by passing the [`--hyperlinked-source` flag][haddock-hyperlinked-source-option]. 319Defaults to `true`. 320 321`isExecutable` 322: Whether the package contains an executable. 323 324`isLibrary` 325: Whether the package contains a library. 326 327`jailbreak` 328: Whether to execute [jailbreak-cabal][jailbreak-cabal] before `configurePhase` 329to lift any version constraints in the cabal file. Note that this can't 330lift version bounds if they are conditional, i.e. if a dependency is hidden 331behind a flag. 332 333`enableParallelBuilding` 334: Whether to use the `-j` flag to make GHC/Cabal start multiple jobs in parallel. 335 336`maxBuildCores` 337: Upper limit of jobs to use in parallel for compilation regardless of 338`$NIX_BUILD_CORES`. Defaults to 16 as Haskell compilation with GHC currently 339sees a [performance regression](https://gitlab.haskell.org/ghc/ghc/-/issues/9221) 340if too many parallel jobs are used. 341 342`doCoverage` 343: Whether to generate and install files needed for [HPC][haskell-program-coverage]. 344Defaults to `false`. 345 346`doHaddock` 347: Whether to build (HTML) documentation using [haddock][haddock]. 348Defaults to `true` if supported. 349 350`testTarget` 351: Name of the test suite to build and run. If unset, all test suites will be executed. 352 353`preCompileBuildDriver` 354: Shell code to run before compiling `Setup.hs`. 355 356`postCompileBuildDriver` 357: Shell code to run after compiling `Setup.hs`. 358 359`preHaddock` 360: Shell code to run before building documentation using haddock. 361 362`postHaddock` 363: Shell code to run after building documentation using haddock. 364 365`coreSetup` 366: Whether to only allow core libraries to be used while building `Setup.hs`. 367Defaults to `false`. 368 369`useCpphs` 370: Whether to enable the [cpphs][cpphs] preprocessor. Defaults to `false`. 371 372`enableSeparateBinOutput` 373: Whether to install executables to a separate `bin` output. Defaults to `false`. 374 375`enableSeparateDataOutput` 376: Whether to install data files shipped with the package to a separate `data` output. 377Defaults to `false`. 378 379`enableSeparateDocOutput` 380: Whether to install documentation to a separate `doc` output. 381Is automatically enabled if `doHaddock` is `true`. 382 383`enableSeparateIntermediatesOutput` 384: When `doInstallIntermediates` is true, whether to install intermediate build 385products to a separate `intermediates` output. See [“Incremental 386builds”](#haskell-incremental-builds) for more information. Defaults to 387`false`. 388 389`allowInconsistentDependencies` 390: If enabled, allow multiple versions of the same Haskell package in the 391dependency tree at configure time. Often in such a situation compilation would 392later fail because of type mismatches. Defaults to `false`. 393 394`enableLibraryForGhci` 395: Build and install a special object file for GHCi. This improves performance 396when loading the library in the REPL, but requires extra build time and 397disk space. Defaults to `false`. 398 399`previousIntermediates` 400: If non-null, intermediate build artifacts are copied from this input to 401`dist/build` before performing compiling. See [“Incremental 402builds”](#haskell-incremental-builds) for more information. Defaults to `null`. 403 404`buildTarget` 405: Name of the executable or library to build and install. 406If unset, all available targets are built and installed. 407 408### Specifying dependencies {#haskell-derivation-deps} 409 410Since `haskellPackages.mkDerivation` is intended to be generated from cabal 411files, it reflects cabal's way of specifying dependencies. For one, dependencies 412are grouped by what part of the package they belong to. This helps to reduce the 413dependency closure of a derivation, for example benchmark dependencies are not 414included if `doBenchmark == false`. 415 416`setup*Depends` 417: dependencies necessary to compile `Setup.hs` 418 419`library*Depends` 420: dependencies of a library contained in the package 421 422`executable*Depends` 423: dependencies of an executable contained in the package 424 425`test*Depends` 426: dependencies of a test suite contained in the package 427 428`benchmark*Depends` 429: dependencies of a benchmark contained in the package 430 431The other categorization relates to the way the package depends on the dependency: 432 433`*ToolDepends` 434: Tools we need to run as part of the build process. 435They are added to the derivation's `nativeBuildInputs`. 436 437`*HaskellDepends` 438: Haskell libraries the package depends on. 439They are added to `propagatedBuildInputs`. 440 441`*SystemDepends` 442: Non-Haskell libraries the package depends on. 443They are added to `buildInputs` 444 445`*PkgconfigDepends` 446: `*SystemDepends` which are discovered using `pkg-config`. 447They are added to `buildInputs` and it is additionally 448ensured that `pkg-config` is available at build time. 449 450`*FrameworkDepends` 451: Apple SDK Framework which the package depends on when compiling it on Darwin. 452 453Using these two distinctions, you should be able to categorize most of the dependency 454specifications that are available: 455`benchmarkFrameworkDepends`, 456`benchmarkHaskellDepends`, 457`benchmarkPkgconfigDepends`, 458`benchmarkSystemDepends`, 459`benchmarkToolDepends`, 460`executableFrameworkDepends`, 461`executableHaskellDepends`, 462`executablePkgconfigDepends`, 463`executableSystemDepends`, 464`executableToolDepends`, 465`libraryFrameworkDepends`, 466`libraryHaskellDepends`, 467`libraryPkgconfigDepends`, 468`librarySystemDepends`, 469`libraryToolDepends`, 470`setupHaskellDepends`, 471`testFrameworkDepends`, 472`testHaskellDepends`, 473`testPkgconfigDepends`, 474`testSystemDepends` and 475`testToolDepends`. 476 477That only leaves the following extra ways for specifying dependencies: 478 479`buildDepends` 480: Allows specifying Haskell dependencies which are added to `propagatedBuildInputs` unconditionally. 481 482`buildTools` 483: Like `*ToolDepends`, but are added to `nativeBuildInputs` unconditionally. 484 485`extraLibraries` 486: Like `*SystemDepends`, but are added to `buildInputs` unconditionally. 487 488`pkg-configDepends` 489: Like `*PkgconfigDepends`, but are added to `buildInputs` unconditionally. 490 491`testDepends` 492: Deprecated, use either `testHaskellDepends` or `testSystemDepends`. 493 494`benchmarkDepends` 495: Deprecated, use either `benchmarkHaskellDepends` or `benchmarkSystemDepends`. 496 497The dependency specification methods in this list which are unconditional 498are especially useful when writing [overrides](#haskell-overriding-haskell-packages) 499when you want to make sure that they are definitely included. However, it is 500recommended to use the more accurate ones listed above when possible. 501 502### Meta attributes {#haskell-derivation-meta} 503 504`haskellPackages.mkDerivation` accepts the following attributes as direct 505arguments which are transparently set in `meta` of the resulting derivation. See 506the [Meta-attributes section](#chap-meta) for their documentation. 507 508* These attributes are populated with a default value if omitted: 509 * `homepage`: defaults to the Hackage page for `pname`. 510 * `platforms`: defaults to `lib.platforms.all` (since GHC can cross-compile) 511* These attributes are only set if given: 512 * `description` 513 * `license` 514 * `changelog` 515 * `maintainers` 516 * `broken` 517 * `hydraPlatforms` 518 519### Incremental builds {#haskell-incremental-builds} 520 521`haskellPackages.mkDerivation` supports incremental builds for GHC 9.4 and 522newer with the `doInstallIntermediates`, `enableSeparateIntermediatesOutput`, 523and `previousIntermediates` arguments. 524 525The basic idea is to first perform a full build of the package in question, 526save its intermediate build products for later, and then copy those build 527products into the build directory of an incremental build performed later. 528Then, GHC will use those build artifacts to avoid recompiling unchanged 529modules. 530 531For more detail on how to store and use incremental build products, see 532[Gabriella Gonzalez’ blog post “Nixpkgs support for incremental Haskell 533builds”.][incremental-builds] motivation behind this feature. 534 535An incremental build for [the `turtle` package][turtle] can be performed like 536so: 537 538```nix 539let 540 pkgs = import <nixpkgs> {}; 541 inherit (pkgs) haskell; 542 inherit (haskell.lib.compose) overrideCabal; 543 544 # Incremental builds work with GHC >=9.4. 545 turtle = haskell.packages.ghc944.turtle; 546 547 # This will do a full build of `turtle`, while writing the intermediate build products 548 # (compiled modules, etc.) to the `intermediates` output. 549 turtle-full-build-with-incremental-output = overrideCabal (drv: { 550 doInstallIntermediates = true; 551 enableSeparateIntermediatesOutput = true; 552 }) turtle; 553 554 # This will do an incremental build of `turtle` by copying the previously 555 # compiled modules and intermediate build products into the source tree 556 # before running the build. 557 # 558 # GHC will then naturally pick up and reuse these products, making this build 559 # complete much more quickly than the previous one. 560 turtle-incremental-build = overrideCabal (drv: { 561 previousIntermediates = turtle-full-build-with-incremental-output.intermediates; 562 }) turtle; 563in 564 turtle-incremental-build 565``` 566 567## Development environments {#haskell-development-environments} 568 569In addition to building and installing Haskell software, nixpkgs can also 570provide development environments for Haskell projects. This has the obvious 571advantage that you benefit from `cache.nixos.org` and no longer need to compile 572all project dependencies yourself. While it is often very useful, this is not 573the primary use case of our package set. Have a look at the section 574[available package versions](#haskell-available-versions) to learn which 575versions of packages we provide and the section 576[limitations](#haskell-limitations), to judge whether a `haskellPackages` 577based development environment for your project is feasible. 578 579By default, every derivation built using 580[`haskellPackages.mkDerivation`](#haskell-mkderivation) exposes an environment 581suitable for building it interactively as the `env` attribute. For example, if 582you have a local checkout of `random`, you can enter a development environment 583for it like this (if the dependencies in the development and packaged version 584match): 585 586```console 587$ cd ~/src/random 588$ nix-shell -A haskellPackages.random.env '<nixpkgs>' 589[nix-shell:~/src/random]$ ghc-pkg list 590/nix/store/a8hhl54xlzfizrhcf03c1l3f6l9l8qwv-ghc-9.2.4-with-packages/lib/ghc-9.2.4/package.conf.d 591 Cabal-3.6.3.0 592 array-0.5.4.0 593 base-4.16.3.0 594 binary-0.8.9.0 595 596 ghc-9.2.4 597 598``` 599 600As you can see, the environment contains a GHC which is set up so it finds all 601dependencies of `random`. Note that this environment does not mirror 602the environment used to build the package, but is intended as a convenient 603tool for development and simple debugging. `env` relies on the `ghcWithPackages` 604wrapper which automatically injects a pre-populated package-db into every 605GHC invocation. In contrast, using `nix-shell -A haskellPackages.random` will 606not result in an environment in which the dependencies are in GHCs package 607database. Instead, the Haskell builder will pass in all dependencies explicitly 608via configure flags. 609 610`env` mirrors the normal derivation environment in one aspect: It does not include 611familiar development tools like `cabal-install`, since we rely on plain `Setup.hs` 612to build all packages. However, `cabal-install` will work as expected if in 613`PATH` (e.g. when installed globally and using a `nix-shell` without `--pure`). 614A declarative and pure way of adding arbitrary development tools is provided 615via [`shellFor`](#haskell-shellFor). 616 617When using `cabal-install` for dependency resolution you need to be a bit 618careful to achieve build purity. `cabal-install` will find and use all 619dependencies installed from the packages `env` via Nix, but it will also 620consult Hackage to potentially download and compile dependencies if it can’t 621find a valid build plan locally. To prevent this you can either never run 622`cabal update`, remove the cabal database from your `~/.cabal` folder or run 623`cabal` with `--offline`. Note though, that for some usecases `cabal2nix` needs 624the local Hackage db. 625 626Often you won't work on a package that is already part of `haskellPackages` or 627Hackage, so we first need to write a Nix expression to obtain the development 628environment from. Luckily, we can generate one very easily from an already 629existing cabal file using `cabal2nix`: 630 631```console 632$ ls 633my-project.cabal src … 634$ cabal2nix ./. > my-project.nix 635``` 636 637The generated Nix expression evaluates to a function ready to be 638`callPackage`-ed. For now, we can add a minimal `default.nix` which does just 639that: 640 641```nix 642# Retrieve nixpkgs impurely from NIX_PATH for now, you can pin it instead, of course. 643{ pkgs ? import <nixpkgs> {} }: 644 645# use the nixpkgs default haskell package set 646pkgs.haskellPackages.callPackage ./my-project.nix { } 647``` 648 649Using `nix-build default.nix` we can now build our project, but we can also 650enter a shell with all the package's dependencies available using `nix-shell 651-A env default.nix`. If you have `cabal-install` installed globally, it'll work 652inside the shell as expected. 653 654### shellFor {#haskell-shellFor} 655 656Having to install tools globally is obviously not great, especially if you want 657to provide a batteries-included `shell.nix` with your project. Luckily there's a 658proper tool for making development environments out of packages' build 659environments: `shellFor`, a function exposed by every haskell package set. It 660takes the following arguments and returns a derivation which is suitable as a 661development environment inside `nix-shell`: 662 663`packages` 664: This argument is used to select the packages for which to build the 665development environment. This should be a function which takes a haskell package 666set and returns a list of packages. `shellFor` will pass the used package set to 667this function and include all dependencies of the returned package in the build 668environment. This means you can reuse Nix expressions of packages included in 669nixpkgs, but also use local Nix expressions like this: `hpkgs: [ 670(hpkgs.callPackage ./my-project.nix { }) ]`. 671 672`nativeBuildInputs` 673: Expects a list of derivations to add as build tools to the build environment. 674This is the place to add packages like `cabal-install`, `doctest` or `hlint`. 675Defaults to `[]`. 676 677`buildInputs` 678: Expects a list of derivations to add as library dependencies, like `openssl`. 679This is rarely necessary as the haskell package expressions usually track system 680dependencies as well. Defaults to `[]`. (see also 681[derivation dependencies](#haskell-derivation-deps)) 682 683`withHoogle` 684: If this is true, `hoogle` will be added to `nativeBuildInputs`. 685Additionally, its database will be populated with all included dependencies, 686so you'll be able search through the documentation of your dependencies. 687Defaults to `false`. 688 689`genericBuilderArgsModifier` 690: This argument accepts a function allowing you to modify the arguments passed 691to `mkDerivation` in order to create the development environment. For example, 692`args: { doCheck = false; }` would cause the environment to not include any test 693dependencies. Defaults to `lib.id`. 694 695`doBenchmark` 696: This is a shortcut for enabling `doBenchmark` via `genericBuilderArgsModifier`. 697Setting it to `true` will cause the development environment to include all 698benchmark dependencies which would be excluded by default. Defaults to `false`. 699 700One neat property of `shellFor` is that it allows you to work on multiple 701packages using the same environment in conjunction with 702[cabal.project files][cabal-project-files]. 703Say our example above depends on `distribution-nixpkgs` and we have a project 704file set up for both, we can add the following `shell.nix` expression: 705 706```nix 707{ pkgs ? import <nixpkgs> {} }: 708 709pkgs.haskellPackages.shellFor { 710 packages = hpkgs: [ 711 # reuse the nixpkgs for this package 712 hpkgs.distribution-nixpkgs 713 # call our generated Nix expression manually 714 (hpkgs.callPackage ./my-project/my-project.nix { }) 715 ]; 716 717 # development tools we use 718 nativeBuildInputs = [ 719 pkgs.cabal-install 720 pkgs.haskellPackages.doctest 721 pkgs.cabal2nix 722 ]; 723 724 # Extra arguments are added to mkDerivation's arguments as-is. 725 # Since it adds all passed arguments to the shell environment, 726 # we can use this to set the environment variable the `Paths_` 727 # module of distribution-nixpkgs uses to search for bundled 728 # files. 729 # See also: https://cabal.readthedocs.io/en/latest/cabal-package.html#accessing-data-files-from-package-code 730 distribution_nixpkgs_datadir = toString ./distribution-nixpkgs; 731} 732``` 733 734<!-- TODO(@sternenseemann): deps are not included if not selected --> 735 736### haskell-language-server {#haskell-language-server} 737 738To use HLS in short: Install `pkgs.haskell-language-server` e.g. in 739`nativeBuildInputs` in `shellFor` and use the `haskell-language-server-wrapper` 740command to run it. See the [HLS user guide] on how to configure your text 741editor to use HLS and how to test your setup. 742 743HLS needs to be compiled with the GHC version of the project you use it 744on. 745 746``pkgs.haskell-language-server`` provides 747``haskell-language-server-wrapper``, ``haskell-language-server`` 748and ``haskell-language-server-x.x.x`` 749binaries, where ``x.x.x`` is the GHC version for which it is compiled. By 750default, it only includes binaries for the current GHC version, to reduce 751closure size. The closure size is large, because HLS needs to be dynamically 752linked to work reliably. You can override the list of supported GHC versions 753with e.g. 754 755```nix 756pkgs.haskell-language-server.override { supportedGhcVersions = [ "90" "94" ]; } 757``` 758Where all strings `version` are allowed such that 759`haskell.packages.ghc${version}` is an existing package set. 760 761When you run `haskell-language-server-wrapper` it will detect the GHC 762version used by the project you are working on (by asking e.g. cabal or 763stack) and pick the appropriate versioned binary from your path. 764 765Be careful when installing HLS globally and using a pinned nixpkgs for a 766Haskell project in a `nix-shell`. If the nixpkgs versions deviate to much 767(e.g., use different `glibc` versions) the `haskell-language-server-?.?.?` 768executable will try to detect these situations and refuse to start. It is 769recommended to obtain HLS via `nix-shell` from the nixpkgs version pinned in 770there instead. 771 772The top level `pkgs.haskell-language-server` attribute is just a convenience 773wrapper to make it possible to install HLS for multiple GHC versions at the 774same time. If you know, that you only use one GHC version, e.g., in a project 775specific `nix-shell` you can simply use 776`pkgs.haskellPackages.haskell-language-server` or 777`pkgs.haskell.packages.*.haskell-language-server` from the package set you use. 778 779If you use `nix-shell` for your development environments remember to start your 780editor in that environment. You may want to use something like `direnv` and/or an 781editor plugin to achieve this. 782 783## Overriding Haskell packages {#haskell-overriding-haskell-packages} 784 785### Overriding a single package {#haskell-overriding-a-single-package} 786 787<!-- TODO(@sternenseemann): we should document /somewhere/ that base == null etc. --> 788 789Like many language specific subsystems in nixpkgs, the Haskell infrastructure 790also has its own quirks when it comes to overriding. Overriding of the *inputs* 791to a package at least follows the standard procedure. For example, imagine you 792need to build `nix-tree` with a more recent version of `brick` than the default 793one provided by `haskellPackages`: 794 795```nix 796haskellPackages.nix-tree.override { 797 brick = haskellPackages.brick_0_67; 798} 799``` 800 801<!-- TODO(@sternenseemann): This belongs in the next section 802One common problem you may run into with such an override is the build failing 803with “abort because of serious configure-time warning from Cabal”. When scrolling 804up, you'll usually notice that Cabal noticed that more than one versions of the same 805package was present in the dependency graph. This typically causes a later compilation 806failure (the error message `haskellPackages.mkDerivation` produces tries to save 807you the time of finding this out yourself, but if you wish to do so, you can 808disable it using `allowInconsistentDependencies`). Luckily, `haskellPackages` provides 809you with a tool to deal with this. `overrideScope` creates a new `haskellPackages` 810instance with the override applied *globally* for this package, so the dependency 811closure automatically uses a consistent version of the overridden package. E. g. 812if `haskell-ci` needs a recent version of `Cabal`, but also uses other packages 813that depend on that library, you may want to use: 814 815```nix 816haskellPackages.haskell-ci.overrideScope (self: super: { 817 Cabal = self.Cabal_3_6_2_0; 818}) 819``` 820 821--> 822 823The custom interface comes into play when you want to override the arguments 824passed to `haskellPackages.mkDerivation`. For this, the function `overrideCabal` 825from `haskell.lib.compose` is used. E.g., if you want to install a man page 826that is distributed with the package, you can do something like this: 827 828```nix 829haskell.lib.compose.overrideCabal (drv: { 830 postInstall = '' 831 ${drv.postInstall or ""} 832 install -Dm644 man/pnbackup.1 -t $out/share/man/man1 833 ''; 834}) haskellPackages.pnbackup 835``` 836 837`overrideCabal` takes two arguments: 838 8391. A function which receives all arguments passed to `haskellPackages.mkDerivation` 840 before and returns a set of arguments to replace (or add) with a new value. 8412. The Haskell derivation to override. 842 843The arguments are ordered so that you can easily create helper functions by making 844use of currying: 845 846```nix 847let 848 installManPage = haskell.lib.compose.overrideCabal (drv: { 849 postInstall = '' 850 ${drv.postInstall or ""} 851 install -Dm644 man/${drv.pname}.1 -t "$out/share/man/man1" 852 ''; 853 }); 854in 855 856installManPage haskellPackages.pnbackup 857``` 858 859In fact, `haskell.lib.compose` already provides lots of useful helpers for common 860tasks, detailed in the next section. They are also structured in such a way that 861they can be combined using `lib.pipe`: 862 863```nix 864lib.pipe my-haskell-package [ 865 # lift version bounds on dependencies 866 haskell.lib.compose.doJailbreak 867 # disable building the haddock documentation 868 haskell.lib.compose.dontHaddock 869 # pass extra package flag to Cabal's configure step 870 (haskell.lib.compose.enableCabalFlag "myflag") 871] 872``` 873 874#### `haskell.lib.compose` {#haskell-haskell.lib.compose} 875 876The base interface for all overriding is the following function: 877 878`overrideCabal f drv` 879: Takes the arguments passed to obtain `drv` to `f` and uses the resulting 880attribute set to update the argument set. Then a recomputed version of `drv` 881using the new argument set is returned. 882 883<!-- 884TODO(@sternenseemann): ideally we want to be more detailed here as well, but 885I want to avoid the documentation having to be kept in sync in too many places. 886We already document this stuff in the mkDerivation section and lib/compose.nix. 887Ideally this section would be generated from the latter in the future. 888--> 889 890All other helper functions are implemented in terms of `overrideCabal` and make 891common overrides shorter and more complicate ones trivial. The simple overrides 892which only change a single argument are only described very briefly in the 893following overview. Refer to the 894[documentation of `haskellPackages.mkDerivation`](#haskell-mkderivation) 895for a more detailed description of the effects of the respective arguments. 896 897##### Packaging Helpers {#haskell-packaging-helpers} 898 899`overrideSrc { src, version } drv` 900: Replace the source used for building `drv` with the path or derivation given 901as `src`. The `version` attribute is optional. Prefer this function over 902overriding `src` via `overrideCabal`, since it also automatically takes care of 903removing any Hackage revisions. 904 905<!-- TODO(@sternenseemann): deprecated 906 907`generateOptparseApplicativeCompletions list drv` 908: Generate and install shell completion files for the installed executables whose 909names are given via `list`. The executables need to be using `optparse-applicative` 910for this to work. 911--> 912 913`justStaticExecutables drv` 914: Only build and install the executables produced by `drv`, removing everything 915that may refer to other Haskell packages' store paths (like libraries and 916documentation). This dramatically reduces the closure size of the resulting 917derivation. Note that the executables are only statically linked against their 918Haskell dependencies, but will still link dynamically against libc, GMP and 919other system library dependencies. If dependencies use their Cabal-generated 920`Paths_*` module, this may not work as well if GHC's dead code elimination 921is unable to remove the references to the dependency's store path that module 922contains. 923 924`enableSeparateBinOutput drv` 925: Install executables produced by `drv` to a separate `bin` output. This 926has a similar effect as `justStaticExecutables`, but preserves the libraries 927and documentation in the `out` output alongside the `bin` output with a 928much smaller closure size. 929 930`markBroken drv` 931: Sets the `broken` flag to `true` for `drv`. 932 933`markUnbroken drv`, `unmarkBroken drv` 934: Set the `broken` flag to `false` for `drv`. 935 936`doDistribute drv` 937: Updates `hydraPlatforms` so that Hydra will build `drv`. This is 938sometimes necessary when working with versioned packages in 939`haskellPackages` which are not built by default. 940 941`dontDistribute drv` 942: Sets `hydraPlatforms` to `[]`, causing Hydra to skip this package 943altogether. Useful if it fails to evaluate cleanly and is causing 944noise in the evaluation errors tab on Hydra. 945 946##### Development Helpers {#haskell-development-helpers} 947 948`sdistTarball drv` 949: Create a source distribution tarball like those found on Hackage 950instead of building the package `drv`. 951 952`documentationTarball drv` 953: Create a documentation tarball suitable for uploading to Hackage 954instead of building the package `drv`. 955 956`buildFromSdist drv` 957: Uses `sdistTarball drv` as the source to compile `drv`. This helps to catch 958packaging bugs when building from a local directory, e.g. when required files 959are missing from `extra-source-files`. 960 961`failOnAllWarnings drv` 962: Enables all warnings GHC supports and makes it fail the build if any of them 963are emitted. 964 965<!-- TODO(@sternenseemann): 966`checkUnusedPackages opts drv` 967: Adds an extra check to `postBuild` which fails the build if any dependency 968taken as an input is not used. The `opts` attribute set allows relaxing this 969check. 970--> 971 972`enableDWARFDebugging drv` 973: Compiles the package with additional debug symbols enabled, useful 974for debugging with e.g. `gdb`. 975 976`doStrip drv` 977: Sets `doStrip` to `true` for `drv`. 978 979`dontStrip drv` 980: Sets `doStrip` to `false` for `drv`. 981 982<!-- TODO(@sternenseemann): shellAware --> 983 984##### Trivial Helpers {#haskell-trivial-helpers} 985 986`doJailbreak drv` 987: Sets the `jailbreak` argument to `true` for `drv`. 988 989`dontJailbreak drv` 990: Sets the `jailbreak` argument to `false` for `drv`. 991 992`doHaddock drv` 993: Sets `doHaddock` to `true` for `drv`. 994 995`dontHaddock drv` 996: Sets `doHaddock` to `false` for `drv`. Useful if the build of a package is 997failing because of e.g. a syntax error in the Haddock documentation. 998 999`doHyperlinkSource drv` 1000: Sets `hyperlinkSource` to `true` for `drv`. 1001 1002`dontHyperlinkSource drv` 1003: Sets `hyperlinkSource` to `false` for `drv`. 1004 1005`doCheck drv` 1006: Sets `doCheck` to `true` for `drv`. 1007 1008`dontCheck drv` 1009: Sets `doCheck` to `false` for `drv`. Useful if a package has a broken, 1010flaky or otherwise problematic test suite breaking the build. 1011 1012<!-- Purposefully omitting the non-list variants here. They are a bit 1013ugly, and we may want to deprecate them at some point. --> 1014 1015`appendConfigureFlags list drv` 1016: Adds the strings in `list` to the `configureFlags` argument for `drv`. 1017 1018`enableCabalFlag flag drv` 1019: Makes sure that the Cabal flag `flag` is enabled in Cabal's configure step. 1020 1021`disableCabalFlag flag drv` 1022: Makes sure that the Cabal flag `flag` is disabled in Cabal's configure step. 1023 1024`appendBuildflags list drv` 1025: Adds the strings in `list` to the `buildFlags` argument for `drv`. 1026 1027<!-- TODO(@sternenseemann): removeConfigureFlag --> 1028 1029`appendPatches list drv` 1030: Adds the `list` of derivations or paths to the `patches` argument for `drv`. 1031 1032<!-- TODO(@sternenseemann): link dep section --> 1033 1034`addBuildTools list drv` 1035: Adds the `list` of derivations to the `buildTools` argument for `drv`. 1036 1037`addExtraLibraries list drv` 1038: Adds the `list` of derivations to the `extraLibraries` argument for `drv`. 1039 1040`addBuildDepends list drv` 1041: Adds the `list` of derivations to the `buildDepends` argument for `drv`. 1042 1043`addTestToolDepends list drv` 1044: Adds the `list` of derivations to the `testToolDepends` argument for `drv`. 1045 1046`addPkgconfigDepends list drv` 1047: Adds the `list` of derivations to the `pkg-configDepends` argument for `drv`. 1048 1049`addSetupDepends list drv` 1050: Adds the `list` of derivations to the `setupHaskellDepends` argument for `drv`. 1051 1052`doBenchmark drv` 1053: Set `doBenchmark` to `true` for `drv`. Useful if your development 1054environment is missing the dependencies necessary for compiling the 1055benchmark component. 1056 1057`dontBenchmark drv` 1058: Set `doBenchmark` to `false` for `drv`. 1059 1060`setBuildTargets list drv` 1061: Sets the `buildTarget` argument for `drv` so that the targets specified in `list` are built. 1062 1063`doCoverage drv` 1064: Sets the `doCoverage` argument to `true` for `drv`. 1065 1066`dontCoverage drv` 1067: Sets the `doCoverage` argument to `false` for `drv`. 1068 1069#### Library functions in the Haskell package sets {#haskell-package-set-lib-functions} 1070 1071Some library functions depend on packages from the Haskell package sets. Thus they are 1072exposed from those instead of from `haskell.lib.compose` which can only access what is 1073passed directly to it. When using the functions below, make sure that you are obtaining them 1074from the same package set (`haskellPackages`, `haskell.packages.ghc944` etc.) as the packages 1075you are working with or – even better – from the `self`/`final` fix point of your overlay to 1076`haskellPackages`. 1077 1078Note: Some functions like `shellFor` that are not intended for overriding per se, are omitted 1079in this section. <!-- TODO(@sternenseemann): note about ifd section --> 1080 1081`cabalSdist { src, name ? ... }` 1082: Generates the Cabal sdist tarball for `src`, suitable for uploading to Hackage. 1083Contrary to `haskell.lib.compose.sdistTarball`, it uses `cabal-install` over `Setup.hs`, 1084so it is usually faster: No build dependencies need to be downloaded, and we can 1085skip compiling `Setup.hs`. 1086 1087`buildFromCabalSdist drv` 1088: Build `drv`, but run its `src` attribute through `cabalSdist` first. Useful for catching 1089files necessary for compilation that are missing from the sdist. 1090 1091`generateOptparseApplicativeCompletions list drv` 1092: Generate and install shell completion files for the installed executables whose 1093names are given via `list`. The executables need to be using `optparse-applicative` 1094for [this to work][optparse-applicative-completions]. 1095Note that this feature is automatically disabled when cross-compiling, since it 1096requires executing the binaries in question. 1097 1098<!-- 1099 1100TODO(@NixOS/haskell): finish these planned sections 1101### Overriding the entire package set 1102 1103 1104## Import-from-Derivation helpers 1105 1106* `callCabal2nix` 1107* `callHackage`, `callHackageDirect` 1108* `developPackage` 1109 1110## Contributing {#haskell-contributing} 1111 1112### Fixing a broken package {#haskell-fixing-a-broken-package} 1113 1114### Package set generation {#haskell-package-set-generation} 1115 1116### Packaging a Haskell project 1117 1118### Backporting {#haskell-backporting} 1119 1120Backporting changes to a stable NixOS version in general is covered 1121in nixpkgs' `CONTRIBUTING.md` in general. In particular refer to the 1122[backporting policy](https://github.com/NixOS/nixpkgs/blob/master/CONTRIBUTING.md#criteria-for-backporting-changes) 1123to check if the change you have in mind may be backported. 1124 1125This section focuses on how to backport a package update (e.g. a 1126bug fix or security release). Fixing a broken package works like 1127it does for the unstable branches. 1128 1129--> 1130 1131## F.A.Q. {#haskell-faq} 1132 1133### Why is topic X not covered in this section? Why is section Y missing? {#haskell-why-not-covered} 1134 1135We have been working on [moving the nixpkgs Haskell documentation back into the 1136nixpkgs manual](https://github.com/NixOS/nixpkgs/issues/121403). Since this 1137process has not been completed yet, you may find some topics missing here 1138covered in the old [haskell4nix docs](https://haskell4nix.readthedocs.io/). 1139 1140If you feel any important topic is not documented at all, feel free to comment 1141on the issue linked above. 1142 1143[Stackage]: https://www.stackage.org 1144[cabal-project-files]: https://cabal.readthedocs.io/en/latest/cabal-project.html 1145[cabal2nix]: https://github.com/nixos/cabal2nix 1146[cpphs]: https://Hackage.haskell.org/package/cpphs 1147[haddock-hoogle-option]: https://haskell-haddock.readthedocs.io/en/latest/invoking.html#cmdoption-hoogle 1148[haddock-hyperlinked-source-option]: https://haskell-haddock.readthedocs.io/en/latest/invoking.html#cmdoption-hyperlinked-source 1149[haddock]: https://www.haskell.org/haddock/ 1150[haskell-program-coverage]: https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/profiling.html#observing-code-coverage 1151[haskell.nix]: https://input-output-hk.github.io/haskell.nix/index.html 1152[HLS user guide]: https://haskell-language-server.readthedocs.io/en/latest/configuration.html#configuring-your-editor 1153[hoogle]: https://wiki.haskell.org/Hoogle 1154[incremental-builds]: https://www.haskellforall.com/2022/12/nixpkgs-support-for-incremental-haskell.html 1155[jailbreak-cabal]: https://github.com/NixOS/jailbreak-cabal/ 1156[multiple-outputs]: https://nixos.org/manual/nixpkgs/stable/#chap-multiple-output 1157[optparse-applicative-completions]: https://github.com/pcapriotti/optparse-applicative/blob/7726b63796aa5d0df82e926d467f039b78ca09e2/README.md#bash-zsh-and-fish-completions 1158[profiling-detail]: https://cabal.readthedocs.io/en/latest/cabal-project.html#cfg-field-profiling-detail 1159[profiling]: https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/profiling.html 1160[search.nixos.org]: https://search.nixos.org 1161[turtle]: https://hackage.haskell.org/package/turtle