nixpkgs mirror (for testing) github.com/NixOS/nixpkgs
nix
at r-updates 1417 lines 58 kB view raw view rendered
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 set. 25The same packages are re-exposed with a reduced dependency closure for convenience (see `justStaticExecutables` or `separateBinOutput` below). 26 27:::{.note} 28See [](#chap-language-support) for techniques to explore package sets. 29::: 30 31The `haskellPackages` set includes at least one version of every package from [Hackage](https://hackage.haskell.org/) as well as some manually injected packages. 32 33The attribute names in `haskellPackages` always correspond with their name on 34Hackage. Since Hackage allows names that are not valid Nix without escaping, 35you need to take care when handling attribute names like `3dmodels`. 36 37For packages that are part of [Stackage] (a curated set of known to be 38compatible packages), we use the version prescribed by a Stackage snapshot 39(usually the current LTS one) as the default version. For all other packages we 40use the latest version from [Hackage](https://hackage.org) (the repository of 41basically all open source Haskell packages). See [below](#haskell-available-versions) for a few more details on this. 42 43Roughly half of the 16K packages contained in `haskellPackages` don’t actually 44build and are [marked as broken semi-automatically](https://github.com/NixOS/nixpkgs/blob/haskell-updates/pkgs/development/haskell-modules/configuration-hackage2nix/broken.yaml). 45Most of those packages are deprecated or unmaintained, but sometimes packages 46that should build, do not build. Very often fixing them is not a lot of work. 47 48<!-- 49TODO(@sternenseemann): 50How you can help with that is 51described in [Fixing a broken package](#haskell-fixing-a-broken-package). 52--> 53 54`haskellPackages` is built with our default compiler, but we also provide other releases of GHC and package sets built with them. 55Available compilers are collected under `haskell.compiler`. 56 57Each of those compiler versions has a corresponding attribute set `packages` built with 58it. However, the non-standard package sets are not tested regularly and, as a 59result, contain fewer working packages. The corresponding package set for GHC 609.4.8 is `haskell.packages.ghc948`. In fact, `haskellPackages` (at the time of writing) is just an alias 61for `haskell.packages.ghc9103`. 62 63Every package set also re-exposes the GHC used to build its packages as `haskell.packages.*.ghc`. 64 65### Available package versions {#haskell-available-versions} 66 67We aim for a “blessed” package set which only contains one version of each 68package, like [Stackage], which is a curated set of known to be compatible 69packages. We use the version information from Stackage snapshots and extend it 70with more packages. Normally in Nixpkgs the number of building Haskell packages 71is roughly two to three times the size of Stackage. For choosing the version to 72use for a certain package we use the following rules: 73 741. By default, for `haskellPackages.foo` is the newest version of the package 75`foo` found on [Hackage](https://hackage.org), which is the central registry 76of all open source Haskell packages. Nixpkgs contains a reference to a pinned 77Hackage snapshot, thus we use the state of Hackage as of the last time we 78updated this pin. 792. If the [Stackage] snapshot that we use (usually the newest LTS snapshot) 80contains a package, [we use instead the version in the Stackage snapshot as 81default version for that package.](https://github.com/NixOS/nixpkgs/blob/haskell-updates/pkgs/development/haskell-modules/configuration-hackage2nix/stackage.yaml) 823. For some packages, which are not on Stackage, we have if necessary [manual 83overrides to set the default version to a version older than the newest on 84Hackage.](https://github.com/NixOS/nixpkgs/blob/haskell-updates/pkgs/development/haskell-modules/configuration-hackage2nix/main.yaml) 854. For all packages, for which the newest Hackage version is not the default 86version, there will also be a `haskellPackages.foo_x_y_z` package with the 87newest version. The `x_y_z` part encodes the version with dots replaced by 88underscores. When the newest version changes by a new release to Hackage the 89old package will disappear under that name and be replaced by a newer one under 90the name with the new version. The package name including the version will 91also disappear when the default version e.g. from Stackage catches up with the 92newest version from Hackage. E.g. if `haskellPackages.foo` gets updated from 931.0.0 to 1.1.0 the package `haskellPackages.foo_1_1_0` becomes obsolete and 94gets dropped. 955. For some packages, we also [manually add other `haskellPackages.foo_x_y_z` 96versions](https://github.com/NixOS/nixpkgs/blob/haskell-updates/pkgs/development/haskell-modules/configuration-hackage2nix/main.yaml), 97if they are required for a certain build. 98 99Relying on `haskellPackages.foo_x_y_z` attributes in derivations outside 100nixpkgs is discouraged because they may change or disappear with every package 101set update. 102<!-- TODO(@maralorn) We should add a link to callHackage, etc. once we added 103them to the docs. --> 104 105All `haskell.packages.*` package sets use the same package descriptions and the same sets 106of versions by default. There are however GHC version specific override `.nix` 107files to loosen this a bit. 108 109### Dependency resolution {#haskell-dependency-resolution} 110 111Normally when you build Haskell packages with `cabal-install`, `cabal-install` 112does dependency resolution. It will look at all Haskell package versions known 113on Hackage and try to pick for every (transitive) dependency of your build 114exactly one version. Those versions need to satisfy all the version constraints 115given in the `.cabal` file of your package and all its dependencies. 116 117The [Haskell builder in nixpkgs](#haskell-mkderivation) does no such thing. 118It will take as input packages with names of the desired dependencies 119and just check whether they fulfill the version bounds and fail if they don’t 120(by default, see `jailbreak` to circumvent this). 121 122The `haskellPackages.callPackage` function does the package resolution. 123It will, e.g., use `haskellPackages.aeson`which has the default version as 124described above for a package input of name `aeson`. (More general: 125`<packages>.callPackage f` will call `f` with named inputs provided from the 126package set `<packages>`.) 127While this is the default behavior, it is possible to override the dependencies 128for a specific package, see 129[`override` and `overrideScope`](#haskell-overriding-haskell-packages). 130 131### Limitations {#haskell-limitations} 132 133Our main objective with `haskellPackages` is to package Haskell software in 134Nixpkgs. This entails some limitations, partially due to self-imposed 135restrictions of Nixpkgs, partially in the name of maintainability: 136 137* Only the packages built with the default compiler see extensive testing of the 138 whole package set. For other GHC versions only a few essential packages are 139 tested and cached. 140* As described above we only build one version of most packages. 141 142The experience using an older or newer packaged compiler or using different 143versions may be worse, because builds will not be cached on `cache.nixos.org` 144or may fail. 145 146Thus, to get the best experience, make sure that your project can be compiled 147using the default compiler of nixpkgs and recent versions of its dependencies. 148 149A result of this setup is, that getting a valid build plan for a given 150package can sometimes be quite painful, and in fact this is where most of the 151maintenance work for `haskellPackages` is required. Besides that, it is not 152possible to get the dependencies of a legacy project from nixpkgs or to use a 153specific stack solver for compiling a project. 154 155Even though we couldn’t use them directly in nixpkgs, it would be desirable 156to have tooling to generate working Nix package sets from build plans generated 157by `cabal-install` or a specific Stackage snapshot via import-from-derivation. 158Sadly we currently don’t have tooling for this. For this you might be 159interested in the alternative [haskell.nix] framework, which, be warned, is 160completely incompatible with packages from `haskellPackages`. 161 162<!-- TODO(@maralorn) Link to package set generation docs in the contributors guide below. --> 163 164### GHC Deprecation Policy {#ghc-deprecation-policy} 165 166We remove GHC versions according to the following policy: 167 168#### Major GHC versions {#major-ghc-deprecation} 169 170We keep the following GHC major versions: 1711. The current Stackage LTS as the default and all later major versions. 1722. The two latest major versions older than our default. 1733. The currently recommended GHCup version and all later major versions. 174 175Older GHC versions might be kept longer, if there are in-tree consumers. We will coordinate with the maintainers of those dependencies to find a way forward. 176 177#### Minor GHC versions {#minor-ghc-deprecation} 178 179Every major version has a default minor version. The default minor version will be updated as soon as viable without breakage. 180 181Older minor versions for a supported major version will only be kept, if they are the last supported version of a major Stackage LTS release. 182 183<!-- Policy introduced here: https://discourse.nixos.org/t/nixpkgs-ghc-deprecation-policy-user-feedback-necessary/64153 --> 184 185## `haskellPackages.mkDerivation` {#haskell-mkderivation} 186 187Every Haskell package set has its own Haskell-aware `mkDerivation` which is used 188to build its packages. Generally you won't have to interact with this builder 189since [cabal2nix](#haskell-cabal2nix) can generate packages 190using it for an arbitrary cabal package definition. Still it is useful to know 191the parameters it takes when you need to 192[override](#haskell-overriding-haskell-packages) a generated Nix expression. 193 194`haskellPackages.mkDerivation` is a wrapper around `stdenv.mkDerivation` which 195re-defines the default phases to be Haskell-aware and handles dependency 196specification, test suites, benchmarks etc. by compiling and invoking the 197package's `Setup.hs`. It does *not* use or invoke the `cabal-install` binary, 198but uses the underlying `Cabal` library instead. 199 200### General arguments {#haskell-derivation-args} 201 202`pname` 203: Package name, assumed to be the same as on Hackage (if applicable) 204 205`version` 206: Packaged version, assumed to be the same as on Hackage (if applicable) 207 208`src` 209: Source of the package. If omitted, fetch package corresponding to `pname` 210and `version` from Hackage. 211 212`sha256` 213: Hash to use for the default case of `src`. 214 215`sourceRoot`, `setSourceRoot` 216: Passed to `stdenv.mkDerivation`; see [“Variables controlling the unpack 217phase”](#variables-controlling-the-unpack-phase). 218 219`revision` 220: Revision number of the updated cabal file to fetch from Hackage. 221If `null` (which is the default value), the one included in `src` is used. 222 223`editedCabalFile` 224: `sha256` hash of the cabal file identified by `revision` or `null`. 225 226`env` 227: Extra environment variables to set during the build. 228These will also be set inside the [development environment defined by the `passthru.env` attribute in the returned derivation](#haskell-development-environments), but will not be set inside a development environment built with [`shellFor`](#haskell-shellFor) that includes this package. 229 230`configureFlags` 231: Extra flags passed when executing the `configure` command of `Setup.hs`. 232 233`buildFlags` 234: Extra flags passed when executing the `build` command of `Setup.hs`. 235 236`haddockFlags` 237: Extra flags passed to `Setup.hs haddock` when building the documentation. 238 239`doCheck` 240: Whether to execute the package's test suite if it has one. Defaults to `true` unless cross-compiling. 241 242`doBenchmark` 243: Whether to execute the package's benchmark if it has one. Defaults to `false`. 244 245`doHoogle` 246: Whether to generate an index file for [hoogle][hoogle] as part of 247`haddockPhase` by passing the [`--hoogle` option][haddock-hoogle-option]. 248Defaults to `true`. 249 250`doHaddockQuickjump` 251: Whether to generate an index for interactive navigation of the HTML documentation. 252Defaults to `true` if supported. 253 254`doInstallIntermediates` 255: Whether to install intermediate build products (files written to `dist/build` 256by GHC during the build process). With `enableSeparateIntermediatesOutput`, 257these files are instead installed to [a separate `intermediates` 258output.][multiple-outputs] The output can then be passed into a future build of 259the same package with the `previousIntermediates` argument to support 260incremental builds. See [“Incremental builds”](#haskell-incremental-builds) for 261more information. Defaults to `false`. 262 263`dontConvertCabalFileToUnix` 264: By default, `haskellPackages.mkDerivation` converts the `.cabal` file of a 265given package to Unix line endings. 266This is intended to work around 267[Hackage converting revised `.cabal` files to DOS line endings](https://github.com/haskell/hackage-server/issues/316) 268which frequently causes patches to stop applying. 269You can pass `true` to disable this behavior. 270 271`enableLibraryProfiling` 272: Whether to enable [profiling][profiling] for libraries contained in the 273package. Enabled by default if supported. 274 275`enableExecutableProfiling` 276: Whether to enable [profiling][profiling] for executables contained in the 277package. Disabled by default. 278 279`profilingDetail` 280: [Profiling detail level][profiling-detail] to set. Defaults to `exported-functions`. 281 282`enableSharedExecutables` 283: Whether to link executables dynamically. By default, executables are linked statically. 284 285`enableSharedLibraries` 286: Whether to build shared Haskell libraries. This is enabled by default unless we are using 287`pkgsStatic` or shared libraries have been disabled in GHC. 288 289`enableStaticLibraries` 290: Whether to build static libraries. Enabled by default if supported. 291 292`enableDeadCodeElimination` 293: Whether to enable linker based dead code elimination in GHC. 294Enabled by default if supported. 295 296`enableHsc2hsViaAsm` 297: Whether to pass `--via-asm` to `hsc2hs`. Enabled by default only on Windows. 298 299`hyperlinkSource` 300: Whether to render the source as well as part of the haddock documentation 301by passing the [`--hyperlinked-source` flag][haddock-hyperlinked-source-option]. 302Defaults to `true`. 303 304`isExecutable` 305: Whether the package contains an executable. 306 307`isLibrary` 308: Whether the package contains a library. 309 310`jailbreak` 311: Whether to execute [jailbreak-cabal][jailbreak-cabal] before `configurePhase` 312to lift any version constraints in the cabal file. Note that this can't 313lift version bounds if they are conditional, i.e. if a dependency is hidden 314behind a flag. 315 316`enableParallelBuilding` 317: Whether to use the `-j` flag to make GHC/Cabal start multiple jobs in parallel. 318 319`maxBuildCores` 320: Upper limit of jobs to use in parallel for compilation regardless of 321`$NIX_BUILD_CORES`. Defaults to 16 as Haskell compilation with GHC currently 322sees a [performance regression](https://gitlab.haskell.org/ghc/ghc/-/issues/9221) 323if too many parallel jobs are used. 324 325`doCoverage` 326: Whether to generate and install files needed for [HPC][haskell-program-coverage]. 327Defaults to `false`. 328 329`doHaddock` 330: Whether to build (HTML) documentation using [haddock][haddock]. 331Defaults to `true` if supported. 332 333`testTargets` 334: Names of the test suites to build and run. If unset, all test suites will be executed. 335 336`preCompileBuildDriver` 337: Shell code to run before compiling `Setup.hs`. 338 339`postCompileBuildDriver` 340: Shell code to run after compiling `Setup.hs`. 341 342`preHaddock` 343: Shell code to run before building documentation using haddock. 344 345`postHaddock` 346: Shell code to run after building documentation using haddock. 347 348`coreSetup` 349: Whether to only allow core libraries to be used while building `Setup.hs`. 350Defaults to `false`. 351 352`useCpphs` 353: Whether to enable the [cpphs][cpphs] preprocessor. Defaults to `false`. 354 355`enableSeparateBinOutput` 356: Whether to install executables to a separate `bin` output. Defaults to `false`. 357 358`enableSeparateDataOutput` 359: Whether to install data files shipped with the package to a separate `data` output. 360Defaults to `false`. 361 362`enableSeparateDocOutput` 363: Whether to install documentation to a separate `doc` output. 364Is automatically enabled if `doHaddock` is `true`. 365 366`enableSeparateIntermediatesOutput` 367: When `doInstallIntermediates` is true, whether to install intermediate build 368products to a separate `intermediates` output. See [“Incremental 369builds”](#haskell-incremental-builds) for more information. Defaults to 370`false`. 371 372`allowInconsistentDependencies` 373: If enabled, allow multiple versions of the same Haskell package in the 374dependency tree at configure time. Often in such a situation compilation would 375later fail because of type mismatches. Defaults to `false`. 376 377`enableLibraryForGhci` 378: Build and install a special object file for GHCi. This improves performance 379when loading the library in the REPL, but requires extra build time and 380disk space. Defaults to `false`. 381 382`previousIntermediates` 383: If non-null, intermediate build artifacts are copied from this input to 384`dist/build` before performing compiling. See [“Incremental 385builds”](#haskell-incremental-builds) for more information. Defaults to `null`. 386 387`buildTarget` 388: Name of the executable or library to build and install. 389If unset, all available targets are built and installed. 390 391### Specifying dependencies {#haskell-derivation-deps} 392 393Since `haskellPackages.mkDerivation` is intended to be generated from cabal 394files, it reflects cabal's way of specifying dependencies. For one, dependencies 395are grouped by what part of the package they belong to. This helps to reduce the 396dependency closure of a derivation, for example benchmark dependencies are not 397included if `doBenchmark == false`. 398 399`setup*Depends` 400: dependencies necessary to compile `Setup.hs` 401 402`library*Depends` 403: dependencies of a library contained in the package 404 405`executable*Depends` 406: dependencies of an executable contained in the package 407 408`test*Depends` 409: dependencies of a test suite contained in the package 410 411`benchmark*Depends` 412: dependencies of a benchmark contained in the package 413 414The other categorization relates to the way the package depends on the dependency: 415 416`*ToolDepends` 417: Tools we need to run as part of the build process. 418They are added to the derivation's `nativeBuildInputs`. 419 420`*HaskellDepends` 421: Haskell libraries the package depends on. 422They are added to `propagatedBuildInputs`. 423 424`*SystemDepends` 425: Non-Haskell libraries the package depends on. 426They are added to `buildInputs` 427 428`*PkgconfigDepends` 429: `*SystemDepends` which are discovered using `pkg-config`. 430They are added to `buildInputs` and it is additionally 431ensured that `pkg-config` is available at build time. 432 433`*FrameworkDepends` 434: Apple SDK Framework which the package depends on when compiling it on Darwin. 435 436Using these two distinctions, you should be able to categorize most of the dependency 437specifications that are available: 438`benchmarkFrameworkDepends`, 439`benchmarkHaskellDepends`, 440`benchmarkPkgconfigDepends`, 441`benchmarkSystemDepends`, 442`benchmarkToolDepends`, 443`executableFrameworkDepends`, 444`executableHaskellDepends`, 445`executablePkgconfigDepends`, 446`executableSystemDepends`, 447`executableToolDepends`, 448`libraryFrameworkDepends`, 449`libraryHaskellDepends`, 450`libraryPkgconfigDepends`, 451`librarySystemDepends`, 452`libraryToolDepends`, 453`setupHaskellDepends`, 454`testFrameworkDepends`, 455`testHaskellDepends`, 456`testPkgconfigDepends`, 457`testSystemDepends` and 458`testToolDepends`. 459 460That only leaves the following extra ways for specifying dependencies: 461 462`buildDepends` 463: Allows specifying Haskell dependencies which are added to `propagatedBuildInputs` unconditionally. 464 465`buildTools` 466: Like `*ToolDepends`, but are added to `nativeBuildInputs` unconditionally. 467 468`extraLibraries` 469: Like `*SystemDepends`, but are added to `buildInputs` unconditionally. 470 471`pkg-configDepends` 472: Like `*PkgconfigDepends`, but are added to `buildInputs` unconditionally. 473 474`testDepends` 475: Deprecated, use either `testHaskellDepends` or `testSystemDepends`. 476 477`benchmarkDepends` 478: Deprecated, use either `benchmarkHaskellDepends` or `benchmarkSystemDepends`. 479 480The dependency specification methods in this list which are unconditional 481are especially useful when writing [overrides](#haskell-overriding-haskell-packages) 482when you want to make sure that they are definitely included. However, it is 483recommended to use the more accurate ones listed above when possible. 484 485### Meta attributes {#haskell-derivation-meta} 486 487`haskellPackages.mkDerivation` accepts the following attributes as direct 488arguments which are transparently set in `meta` of the resulting derivation. See 489the [Meta-attributes section](#chap-meta) for their documentation. 490 491* These attributes are populated with a default value if omitted: 492 * `homepage`: defaults to the Hackage page for `pname`. 493 * `platforms`: defaults to `lib.platforms.all` (since GHC can cross-compile) 494* These attributes are only set if given: 495 * `description` 496 * `license` 497 * `changelog` 498 * `maintainers` 499 * `broken` 500 * `hydraPlatforms` 501 502### Incremental builds {#haskell-incremental-builds} 503 504`haskellPackages.mkDerivation` supports incremental builds for GHC 9.4 and 505newer with the `doInstallIntermediates`, `enableSeparateIntermediatesOutput`, 506and `previousIntermediates` arguments. 507 508The basic idea is to first perform a full build of the package in question, 509save its intermediate build products for later, and then copy those build 510products into the build directory of an incremental build performed later. 511Then, GHC will use those build artifacts to avoid recompiling unchanged 512modules. 513 514For more detail on how to store and use incremental build products, see 515[Gabriella Gonzalez’ blog post “Nixpkgs support for incremental Haskell 516builds”.][incremental-builds] motivation behind this feature. 517 518An incremental build for [the `turtle` package][turtle] can be performed like 519so: 520 521```nix 522let 523 pkgs = import <nixpkgs> { }; 524 inherit (pkgs) haskell; 525 inherit (haskell.lib.compose) overrideCabal; 526 527 # Incremental builds work with GHC >=9.4. 528 turtle = haskell.packages.ghc944.turtle; 529 530 # This will do a full build of `turtle`, while writing the intermediate build products 531 # (compiled modules, etc.) to the `intermediates` output. 532 turtle-full-build-with-incremental-output = overrideCabal (drv: { 533 doInstallIntermediates = true; 534 enableSeparateIntermediatesOutput = true; 535 }) turtle; 536 537 # This will do an incremental build of `turtle` by copying the previously 538 # compiled modules and intermediate build products into the source tree 539 # before running the build. 540 # 541 # GHC will then naturally pick up and reuse these products, making this build 542 # complete much more quickly than the previous one. 543 turtle-incremental-build = overrideCabal (drv: { 544 previousIntermediates = turtle-full-build-with-incremental-output.intermediates; 545 }) turtle; 546in 547turtle-incremental-build 548``` 549 550## Development environments {#haskell-development-environments} 551 552In addition to building and installing Haskell software, Nixpkgs can also 553provide development environments for Haskell projects. This has the obvious 554advantage that you benefit from `cache.nixos.org` and no longer need to compile 555all project dependencies yourself. While it is often very useful, this is not 556the primary use case of our package set. Have a look at the section 557[available package versions](#haskell-available-versions) to learn which 558versions of packages we provide and the section 559[limitations](#haskell-limitations), to judge whether a `haskellPackages` 560based development environment for your project is feasible. 561 562By default, every derivation built using 563[`haskellPackages.mkDerivation`](#haskell-mkderivation) exposes an environment 564suitable for building it interactively as the `env` attribute. For example, if 565you have a local checkout of `random`, you can enter a development environment 566for it like this (if the dependencies in the development and packaged version 567match): 568 569```console 570$ cd ~/src/random 571$ nix-shell -A haskellPackages.random.env '<nixpkgs>' 572[nix-shell:~/src/random]$ ghc-pkg list 573/nix/store/a8hhl54xlzfizrhcf03c1l3f6l9l8qwv-ghc-9.2.4-with-packages/lib/ghc-9.2.4/package.conf.d 574 Cabal-3.6.3.0 575 array-0.5.4.0 576 base-4.16.3.0 577 binary-0.8.9.0 578 579 ghc-9.2.4 580 581``` 582 583As you can see, the environment contains a GHC which is set up so it finds all 584dependencies of `random`. Note that this environment does not mirror 585the environment used to build the package, but is intended as a convenient 586tool for development and simple debugging. `env` relies on the `ghcWithPackages` 587wrapper which automatically injects a pre-populated package-db into every 588GHC invocation. In contrast, using `nix-shell -A haskellPackages.random` will 589not result in an environment in which the dependencies are in GHCs package 590database. Instead, the Haskell builder will pass in all dependencies explicitly 591via configure flags. 592 593`env` mirrors the normal derivation environment in one aspect: It does not include 594familiar development tools like `cabal-install`, since we rely on plain `Setup.hs` 595to build all packages. However, `cabal-install` will work as expected if in 596`PATH` (e.g. when installed globally and using a `nix-shell` without `--pure`). 597A declarative and pure way of adding arbitrary development tools is provided 598via [`shellFor`](#haskell-shellFor). 599 600When using `cabal-install` for dependency resolution you need to be a bit 601careful to achieve build purity. `cabal-install` will find and use all 602dependencies installed from the packages `env` via Nix, but it will also 603consult Hackage to potentially download and compile dependencies if it can’t 604find a valid build plan locally. To prevent this you can either never run 605`cabal update`, remove the cabal database from your `~/.cabal` folder or run 606`cabal` with `--offline`. Note though, that for some usecases `cabal2nix` needs 607the local Hackage db. 608 609Often you won't work on a package that is already part of `haskellPackages` or 610Hackage, so we first need to write a Nix expression to obtain the development 611environment from. Luckily, we can generate one very easily from an already 612existing cabal file using `cabal2nix`: 613 614```console 615$ ls 616my-project.cabal src … 617$ cabal2nix ./. > my-project.nix 618``` 619 620The generated Nix expression evaluates to a function ready to be 621`callPackage`-ed. For now, we can add a minimal `default.nix` which does just 622that: 623 624```nix 625# Retrieve nixpkgs impurely from NIX_PATH for now, you can pin it instead, of course. 626{ 627 pkgs ? import <nixpkgs> { }, 628}: 629 630# use the nixpkgs default haskell package set 631pkgs.haskellPackages.callPackage ./my-project.nix { } 632``` 633 634Using `nix-build default.nix` we can now build our project, but we can also 635enter a shell with all the package's dependencies available using `nix-shell 636-A env default.nix`. If you have `cabal-install` installed globally, it'll work 637inside the shell as expected. 638 639### shellFor {#haskell-shellFor} 640 641Having to install tools globally is obviously not great, especially if you want 642to provide a batteries-included `shell.nix` with your project. Luckily there's a 643proper tool for making development environments out of packages' build 644environments: `shellFor`, a function exposed by every haskell package set. It 645takes the following arguments and returns a derivation which is suitable as a 646development environment inside `nix-shell`: 647 648`packages` 649: This argument is used to select the packages for which to build the 650development environment. This should be a function which takes a haskell package 651set and returns a list of packages. `shellFor` will pass the used package set to 652this function and include all dependencies of the returned package in the build 653environment. This means you can reuse Nix expressions of packages included in 654nixpkgs, but also use local Nix expressions like this: `hpkgs: [ 655(hpkgs.callPackage ./my-project.nix { }) ]`. 656 657`extraDependencies` 658: Extra dependencies, in the form of cabal2nix build attributes. An example use 659case is when you have Haskell scripts that use libraries that don't occur in 660your packages' dependencies. Example: `hpkgs: {libraryHaskellDepends = 661[ hpkgs.releaser ]}`. Defaults to `hpkgs: { }`. 662 663`nativeBuildInputs` 664: Expects a list of derivations to add as build tools to the build environment. 665This is the place to add packages like `cabal-install`, `doctest` or `hlint`. 666Defaults to `[]`. 667 668`buildInputs` 669: Expects a list of derivations to add as library dependencies, like `openssl`. 670This is rarely necessary as the haskell package expressions usually track system 671dependencies as well. Defaults to `[]`. (see also 672[derivation dependencies](#haskell-derivation-deps)) 673 674`withHoogle` 675: If this is true, `hoogle` will be added to `nativeBuildInputs`. 676Additionally, its database will be populated with all included dependencies, 677so you'll be able search through the documentation of your dependencies. 678Defaults to `false`. 679 680`genericBuilderArgsModifier` 681: This argument accepts a function allowing you to modify the arguments passed 682to `mkDerivation` in order to create the development environment. For example, 683`args: { doCheck = false; }` would cause the environment to not include any test 684dependencies. Defaults to `lib.id`. 685 686`doBenchmark` 687: This is a shortcut for enabling `doBenchmark` via `genericBuilderArgsModifier`. 688Setting it to `true` will cause the development environment to include all 689benchmark dependencies which would be excluded by default. Defaults to `false`. 690 691One neat property of `shellFor` is that it allows you to work on multiple 692packages using the same environment in conjunction with 693[cabal.project files][cabal-project-files]. 694Say our example above depends on `distribution-nixpkgs` and we have a project 695file set up for both, we can add the following `shell.nix` expression: 696 697```nix 698{ 699 pkgs ? import <nixpkgs> { }, 700}: 701 702pkgs.haskellPackages.shellFor { 703 packages = hpkgs: [ 704 # reuse the nixpkgs for this package 705 hpkgs.distribution-nixpkgs 706 # call our generated Nix expression manually 707 (hpkgs.callPackage ./my-project/my-project.nix { }) 708 ]; 709 710 # development tools we use 711 nativeBuildInputs = [ 712 pkgs.cabal-install 713 pkgs.haskellPackages.doctest 714 pkgs.cabal2nix 715 ]; 716 717 # Extra arguments are added to mkDerivation's arguments as-is. 718 # Since it adds all passed arguments to the shell environment, 719 # we can use this to set the environment variable the `Paths_` 720 # module of distribution-nixpkgs uses to search for bundled 721 # files. 722 # See also: https://cabal.readthedocs.io/en/latest/cabal-package.html#accessing-data-files-from-package-code 723 distribution_nixpkgs_datadir = toString ./distribution-nixpkgs; 724} 725``` 726 727<!-- TODO(@sternenseemann): deps are not included if not selected --> 728 729### haskell-language-server {#haskell-language-server} 730 731To use HLS in short: Install `pkgs.haskell-language-server`, e.g. in 732`nativeBuildInputs` in `shellFor` and use the `haskell-language-server-wrapper` 733command to run it. See the [HLS user guide] on how to configure your text 734editor to use HLS and how to test your setup. 735 736HLS needs to be compiled with the GHC version of the project you use it 737on. 738 739``pkgs.haskell-language-server`` provides 740``haskell-language-server-wrapper``, ``haskell-language-server`` 741and ``haskell-language-server-x.x.x`` 742binaries, where ``x.x.x`` is the GHC version for which it is compiled. By 743default, it only includes binaries for the current GHC version, to reduce 744closure size. The closure size is large, because HLS needs to be dynamically 745linked to work reliably. You can override the list of supported GHC versions 746with e.g. 747 748```nix 749pkgs.haskell-language-server.override { 750 supportedGhcVersions = [ 751 "90" 752 "94" 753 ]; 754} 755``` 756Where all strings `version` are allowed such that 757`haskell.packages.ghc${version}` is an existing package set. 758 759When you run `haskell-language-server-wrapper` it will detect the GHC 760version used by the project you are working on (by asking e.g. cabal or 761stack) and pick the appropriate versioned binary from your path. 762 763Be careful when installing HLS globally and using a pinned nixpkgs for a 764Haskell project in a `nix-shell`. If the nixpkgs versions deviate to much 765(e.g., use different `glibc` versions) the `haskell-language-server-?.?.?` 766executable will try to detect these situations and refuse to start. It is 767recommended to obtain HLS via `nix-shell` from the nixpkgs version pinned in 768there instead. 769 770The top level `pkgs.haskell-language-server` attribute is just a convenience 771wrapper to make it possible to install HLS for multiple GHC versions at the 772same time. If you know, that you only use one GHC version, e.g., in a project 773specific `nix-shell` you can use 774`pkgs.haskellPackages.haskell-language-server` or 775`pkgs.haskell.packages.*.haskell-language-server` from the package set you use. 776 777If you use `nix-shell` for your development environments remember to start your 778editor in that environment. You may want to use something like `direnv` and/or an 779editor plugin to achieve this. 780 781## Overriding Haskell packages {#haskell-overriding-haskell-packages} 782 783### Overriding a single package {#haskell-overriding-a-single-package} 784 785<!-- TODO(@sternenseemann): we should document /somewhere/ that base == null etc. --> 786 787Like many language specific subsystems in nixpkgs, the Haskell infrastructure 788also has its own quirks when it comes to overriding. Overriding of the *inputs* 789to a package at least follows the standard procedure. For example, imagine you 790need to build `nix-tree` with a more recent version of `brick` than the default 791one provided by `haskellPackages`: 792 793```nix 794haskellPackages.nix-tree.override { brick = haskellPackages.brick_0_67; } 795``` 796 797<!-- TODO(@sternenseemann): This belongs in the next section 798One common problem you may run into with such an override is the build failing 799with “abort because of serious configure-time warning from Cabal”. When scrolling 800up, you'll usually notice that Cabal noticed that more than one versions of the same 801package was present in the dependency graph. This typically causes a later compilation 802failure (the error message `haskellPackages.mkDerivation` produces tries to save 803you the time of finding this out yourself, but if you wish to do so, you can 804disable it using `allowInconsistentDependencies`). Luckily, `haskellPackages` provides 805you with a tool to deal with this. `overrideScope` creates a new `haskellPackages` 806instance with the override applied *globally* for this package, so the dependency 807closure automatically uses a consistent version of the overridden package. E. g. 808if `haskell-ci` needs a recent version of `Cabal`, but also uses other packages 809that depend on that library, you may want to use: 810 811```nix 812haskellPackages.haskell-ci.overrideScope (self: super: { 813 Cabal = self.Cabal_3_14_2_0; 814}) 815``` 816 817--> 818 819The custom interface comes into play when you want to override the arguments 820passed to `haskellPackages.mkDerivation`. For this, the function `overrideCabal` 821from `haskell.lib.compose` is used. E.g., if you want to install a man page 822that is distributed with the package, you can do something like this: 823 824```nix 825haskell.lib.compose.overrideCabal (drv: { 826 postInstall = '' 827 ${drv.postInstall or ""} 828 install -Dm644 man/pnbackup.1 -t $out/share/man/man1 829 ''; 830}) haskellPackages.pnbackup 831``` 832 833`overrideCabal` takes two arguments: 834 8351. A function which receives all arguments passed to `haskellPackages.mkDerivation` 836 before and returns a set of arguments to replace (or add) with a new value. 8372. The Haskell derivation to override. 838 839The arguments are ordered so that you can easily create helper functions by making 840use of currying: 841 842```nix 843let 844 installManPage = haskell.lib.compose.overrideCabal (drv: { 845 postInstall = '' 846 ${drv.postInstall or ""} 847 install -Dm644 man/${drv.pname}.1 -t "$out/share/man/man1" 848 ''; 849 }); 850 851in 852installManPage haskellPackages.pnbackup 853``` 854 855In fact, `haskell.lib.compose` already provides lots of useful helpers for common 856tasks, detailed in the next section. They are also structured in such a way that 857they can be combined using `lib.pipe`: 858 859```nix 860lib.pipe my-haskell-package [ 861 # lift version bounds on dependencies 862 haskell.lib.compose.doJailbreak 863 # disable building the haddock documentation 864 haskell.lib.compose.dontHaddock 865 # pass extra package flag to Cabal's configure step 866 (haskell.lib.compose.enableCabalFlag "myflag") 867] 868``` 869 870#### `haskell.lib.compose` {#haskell-haskell.lib.compose} 871 872The base interface for all overriding is the following function: 873 874`overrideCabal f drv` 875: Takes the arguments passed to obtain `drv` to `f` and uses the resulting 876attribute set to update the argument set. Then a recomputed version of `drv` 877using the new argument set is returned. 878 879<!-- 880TODO(@sternenseemann): ideally we want to be more detailed here as well, but 881I want to avoid the documentation having to be kept in sync in too many places. 882We already document this stuff in the mkDerivation section and lib/compose.nix. 883Ideally this section would be generated from the latter in the future. 884--> 885 886All other helper functions are implemented in terms of `overrideCabal` and make 887common overrides shorter and more complicate ones trivial. The simple overrides 888which only change a single argument are only described very briefly in the 889following overview. Refer to the 890[documentation of `haskellPackages.mkDerivation`](#haskell-mkderivation) 891for a more detailed description of the effects of the respective arguments. 892 893##### Packaging Helpers {#haskell-packaging-helpers} 894 895`overrideSrc { src, version } drv` 896: Replace the source used for building `drv` with the path or derivation given 897as `src`. The `version` attribute is optional. Prefer this function over 898overriding `src` via `overrideCabal`, since it also automatically takes care of 899removing any Hackage revisions. 900 901<!-- TODO(@sternenseemann): deprecated 902 903`generateOptparseApplicativeCompletions list drv` 904: Generate and install shell completion files for the installed executables whose 905names are given via `list`. The executables need to be using `optparse-applicative` 906for this to work. 907--> 908 909`justStaticExecutables drv` 910: Only build and install the executables produced by `drv`, removing everything 911 that may refer to other Haskell packages' store paths (like libraries and 912 documentation). This dramatically reduces the closure size of the resulting 913 derivation. Note that the executables are only statically linked against their 914 Haskell dependencies, but will still link dynamically against libc, GMP and 915 other system library dependencies. 916 917 If a library or its dependencies use their Cabal-generated 918 `Paths_*` module, this may not work as well if GHC's dead code elimination is 919 unable to remove the references to the dependency's store path that module 920 contains. 921 As a consequence, an unused reference may be created from the static binary to such a _library_ store path. 922 (See [nixpkgs#164630][164630] for more information.) 923 924 Importing the `Paths_*` module may cause builds to fail with this message: 925 926 ``` 927 error: output '/nix/store/64k8iw0ryz76qpijsnl9v87fb26v28z8-my-haskell-package-1.0.0.0' is not allowed to refer to the following paths: 928 /nix/store/5q5s4a07gaz50h04zpfbda8xjs8wrnhg-ghc-9.6.3 929 ``` 930 931 If that happens, first disable the check for GHC references and rebuild the 932 derivation: 933 934 ```nix 935 pkgs.haskell.lib.overrideCabal (pkgs.haskell.lib.justStaticExecutables my-haskell-package) (drv: { 936 disallowGhcReference = false; 937 }) 938 ``` 939 940 Then use `strings` to determine which libraries are responsible: 941 942 ``` 943 $ nix-build ... 944 $ strings result/bin/my-haskell-binary | grep /nix/store/ 945 ... 946 /nix/store/n7ciwdlg8yyxdhbrgd6yc2d8ypnwpmgq-hs-opentelemetry-sdk-0.0.3.6/bin 947 ... 948 ``` 949 950 Finally, use `remove-references-to` to delete those store paths from the produced output: 951 952 ```nix 953 pkgs.haskell.lib.overrideCabal (pkgs.haskell.lib.justStaticExecutables my-haskell-package) (drv: { 954 postInstall = '' 955 ${drv.postInstall or ""} 956 remove-references-to -t ${pkgs.haskellPackages.hs-opentelemetry-sdk} 957 ''; 958 }) 959 ``` 960 961[164630]: https://github.com/NixOS/nixpkgs/issues/164630 962 963`enableSeparateBinOutput drv` 964: Install executables produced by `drv` to a separate `bin` output. This 965has a similar effect as `justStaticExecutables`, but preserves the libraries 966and documentation in the `out` output alongside the `bin` output with a 967much smaller closure size. 968 969`markBroken drv` 970: Sets the `broken` flag to `true` for `drv`. 971 972`markUnbroken drv`, `unmarkBroken drv` 973: Set the `broken` flag to `false` for `drv`. 974 975`doDistribute drv` 976: Updates `hydraPlatforms` so that Hydra will build `drv`. This is 977sometimes necessary when working with versioned packages in 978`haskellPackages` which are not built by default. 979 980`dontDistribute drv` 981: Sets `hydraPlatforms` to `[]`, causing Hydra to skip this package 982altogether. Useful if it fails to evaluate cleanly and is causing 983noise in the evaluation errors tab on Hydra. 984 985##### Development Helpers {#haskell-development-helpers} 986 987`sdistTarball drv` 988: Create a source distribution tarball like those found on Hackage 989instead of building the package `drv`. 990 991`documentationTarball drv` 992: Create a documentation tarball suitable for uploading to Hackage 993instead of building the package `drv`. 994 995`buildFromSdist drv` 996: Uses `sdistTarball drv` as the source to compile `drv`. This helps to catch 997packaging bugs when building from a local directory, e.g. when required files 998are missing from `extra-source-files`. 999 1000`failOnAllWarnings drv` 1001: Enables all warnings GHC supports and makes it fail the build if any of them 1002are emitted. 1003 1004<!-- TODO(@sternenseemann): 1005`checkUnusedPackages opts drv` 1006: Adds an extra check to `postBuild` which fails the build if any dependency 1007taken as an input is not used. The `opts` attribute set allows relaxing this 1008check. 1009--> 1010 1011`enableDWARFDebugging drv` 1012: Compiles the package with additional debug symbols enabled, useful 1013for debugging with e.g. `gdb`. 1014 1015`doStrip drv` 1016: Sets `doStrip` to `true` for `drv`. 1017 1018`dontStrip drv` 1019: Sets `doStrip` to `false` for `drv`. 1020 1021<!-- TODO(@sternenseemann): shellAware --> 1022 1023##### Trivial Helpers {#haskell-trivial-helpers} 1024 1025`doJailbreak drv` 1026: Sets the `jailbreak` argument to `true` for `drv`. 1027 1028`dontJailbreak drv` 1029: Sets the `jailbreak` argument to `false` for `drv`. 1030 1031`doHaddock drv` 1032: Sets `doHaddock` to `true` for `drv`. 1033 1034`dontHaddock drv` 1035: Sets `doHaddock` to `false` for `drv`. Useful if the build of a package is 1036failing because of e.g. a syntax error in the Haddock documentation. 1037 1038`doHyperlinkSource drv` 1039: Sets `hyperlinkSource` to `true` for `drv`. 1040 1041`dontHyperlinkSource drv` 1042: Sets `hyperlinkSource` to `false` for `drv`. 1043 1044`doCheck drv` 1045: Sets `doCheck` to `true` for `drv`. 1046 1047`dontCheck drv` 1048: Sets `doCheck` to `false` for `drv`. Useful if a package has a broken, 1049flaky or otherwise problematic test suite breaking the build. 1050 1051`dontCheckIf condition drv` 1052: Sets `doCheck` to `false` for `drv`, but only if `condition` applies. 1053Otherwise it's a no-op. Useful to conditionally disable tests for a package 1054without interfering with previous overrides or default values. 1055 1056<!-- Purposefully omitting the non-list variants here. They are a bit 1057ugly, and we may want to deprecate them at some point. --> 1058 1059`appendConfigureFlags list drv` 1060: Adds the strings in `list` to the `configureFlags` argument for `drv`. 1061 1062`enableCabalFlag flag drv` 1063: Makes sure that the Cabal flag `flag` is enabled in Cabal's configure step. 1064 1065`disableCabalFlag flag drv` 1066: Makes sure that the Cabal flag `flag` is disabled in Cabal's configure step. 1067 1068`appendBuildFlags list drv` 1069: Adds the strings in `list` to the `buildFlags` argument for `drv`. 1070 1071<!-- TODO(@sternenseemann): removeConfigureFlag --> 1072 1073`appendPatches list drv` 1074: Adds the `list` of derivations or paths to the `patches` argument for `drv`. 1075 1076<!-- TODO(@sternenseemann): link dep section --> 1077 1078`addBuildTools list drv` 1079: Adds the `list` of derivations to the `buildTools` argument for `drv`. 1080 1081`addExtraLibraries list drv` 1082: Adds the `list` of derivations to the `extraLibraries` argument for `drv`. 1083 1084`addBuildDepends list drv` 1085: Adds the `list` of derivations to the `buildDepends` argument for `drv`. 1086 1087`addTestToolDepends list drv` 1088: Adds the `list` of derivations to the `testToolDepends` argument for `drv`. 1089 1090`addPkgconfigDepends list drv` 1091: Adds the `list` of derivations to the `pkg-configDepends` argument for `drv`. 1092 1093`addSetupDepends list drv` 1094: Adds the `list` of derivations to the `setupHaskellDepends` argument for `drv`. 1095 1096`doBenchmark drv` 1097: Set `doBenchmark` to `true` for `drv`. Useful if your development 1098environment is missing the dependencies necessary for compiling the 1099benchmark component. 1100 1101`dontBenchmark drv` 1102: Set `doBenchmark` to `false` for `drv`. 1103 1104`setBuildTargets drv list` 1105: Sets the `buildTarget` argument for `drv` so that the targets specified in `list` are built. 1106 1107`doCoverage drv` 1108: Sets the `doCoverage` argument to `true` for `drv`. 1109 1110`dontCoverage drv` 1111: Sets the `doCoverage` argument to `false` for `drv`. 1112 1113`enableExecutableProfiling drv` 1114: Sets the `enableExecutableProfiling` argument to `true` for `drv`. 1115 1116`disableExecutableProfiling drv` 1117: Sets the `enableExecutableProfiling` argument to `false` for `drv`. 1118 1119`enableLibraryProfiling drv` 1120: Sets the `enableLibraryProfiling` argument to `true` for `drv`. 1121 1122`disableLibraryProfiling drv` 1123: Sets the `enableLibraryProfiling` argument to `false` for `drv`. 1124 1125`disableParallelBuilding drv` 1126: Sets the `enableParallelBuilding` argument to `false` for `drv`. 1127 1128#### Library functions in the Haskell package sets {#haskell-package-set-lib-functions} 1129 1130Some library functions depend on packages from the Haskell package sets. Thus they are 1131exposed from those instead of from `haskell.lib.compose` which can only access what is 1132passed directly to it. When using the functions below, make sure that you are obtaining them 1133from the same package set (`haskellPackages`, `haskell.packages.ghc944` etc.) as the packages 1134you are working with or – even better – from the `self`/`final` fix point of your overlay to 1135`haskellPackages`. 1136 1137Note: Some functions like `shellFor` that are not intended for overriding per se, are omitted 1138in this section. <!-- TODO(@sternenseemann): note about ifd section --> 1139 1140`cabalSdist { src, name ? ... }` 1141: Generates the Cabal sdist tarball for `src`, suitable for uploading to Hackage. 1142Contrary to `haskell.lib.compose.sdistTarball`, it uses `cabal-install` over `Setup.hs`, 1143so it is usually faster: No build dependencies need to be downloaded, and we can 1144skip compiling `Setup.hs`. 1145 1146`buildFromCabalSdist drv` 1147: Build `drv`, but run its `src` attribute through `cabalSdist` first. Useful for catching 1148files necessary for compilation that are missing from the sdist. 1149 1150`generateOptparseApplicativeCompletions list drv` 1151: Generate and install shell completion files for the installed executables whose 1152names are given via `list`. The executables need to be using `optparse-applicative` 1153for [this to work][optparse-applicative-completions]. 1154Note that this feature is automatically disabled when cross-compiling, since it 1155requires executing the binaries in question. 1156 1157## Import-from-Derivation helpers {#haskell-import-from-derivation} 1158 1159### cabal2nix {#haskell-cabal2nix} 1160 1161[`cabal2nix`][cabal2nix] can generate Nix package definitions for arbitrary 1162Haskell packages using [import from derivation][import-from-derivation]. 1163`cabal2nix` will generate Nix expressions that look like this: 1164 1165```nix 1166# cabal get mtl-2.2.1 && cd mtl-2.2.1 && cabal2nix . 1167{ 1168 mkDerivation, 1169 base, 1170 lib, 1171 transformers, 1172}: 1173mkDerivation { 1174 pname = "mtl"; 1175 version = "2.2.1"; 1176 src = ./.; 1177 libraryHaskellDepends = [ 1178 base 1179 transformers 1180 ]; 1181 homepage = "http://github.com/ekmett/mtl"; 1182 description = "Monad classes, using functional dependencies"; 1183 license = lib.licenses.bsd3; 1184} 1185``` 1186 1187This expression should be called with `haskellPackages.callPackage`, which will 1188supply [`haskellPackages.mkDerivation`](#haskell-mkderivation) and the Haskell 1189dependencies as arguments. 1190 1191`callCabal2nix name src args` 1192: Create a package named `name` from the source derivation `src` using 1193 `cabal2nix`. 1194 1195 `args` are extra arguments provided to `haskellPackages.callPackage`. 1196 1197`callCabal2nixWithOptions name src opts args` 1198: Create a package named `name` from the source derivation `src` using 1199 `cabal2nix`. 1200 1201 `opts` are extra options for calling `cabal2nix`. If `opts` is a string, it 1202 will be used as extra command line arguments for `cabal2nix`, e.g. `--subpath 1203 path/to/dir/containing/cabal-file`. Otherwise, `opts` should be an AttrSet 1204 which can contain the following attributes: 1205 1206 `extraCabal2nixOptions` 1207 : Extra command line arguments for `cabal2nix`. 1208 1209 `srcModifier` 1210 : A function which is used to modify the given `src` instead of the default 1211 filter. 1212 1213 The default source filter will remove all files from `src` except for 1214 `.cabal` files and `package.yaml` files. 1215 1216<!-- 1217 1218`callHackage` 1219: TODO 1220 1221`callHackageDirect` 1222: TODO 1223 1224`developPackage` 1225: TODO 1226 1227--> 1228 1229<!-- 1230 1231TODO(@NixOS/haskell): finish these planned sections 1232### Overriding the entire package set 1233 1234## Contributing {#haskell-contributing} 1235 1236### Fixing a broken package {#haskell-fixing-a-broken-package} 1237 1238### Package set generation {#haskell-package-set-generation} 1239 1240### Packaging a Haskell project 1241 1242### Backporting {#haskell-backporting} 1243 1244Backporting changes to a stable NixOS version in general is covered 1245in nixpkgs' `CONTRIBUTING.md` in general. In particular refer to the 1246[backporting policy](https://github.com/NixOS/nixpkgs/blob/master/CONTRIBUTING.md#criteria-for-backporting-changes) 1247to check if the change you have in mind may be backported. 1248 1249This section focuses on how to backport a package update (e.g. a 1250bug fix or security release). Fixing a broken package works like 1251it does for the unstable branches. 1252 1253--> 1254 1255## F.A.Q. {#haskell-faq} 1256 1257### Why is topic X not covered in this section? Why is section Y missing? {#haskell-why-not-covered} 1258 1259We have been working on [moving the nixpkgs Haskell documentation back into the 1260nixpkgs manual](https://github.com/NixOS/nixpkgs/issues/121403). <!-- krank:ignore-line --> 1261Since this process has not been completed yet, you may find some topics missing here 1262covered in the old [haskell4nix docs](https://haskell4nix.readthedocs.io/). 1263 1264If you feel any important topic is not documented at all, feel free to comment 1265on the issue linked above. 1266 1267### How to enable or disable profiling builds globally? {#haskell-faq-override-profiling} 1268 1269By default, Nixpkgs builds a profiling version of each Haskell library. The 1270exception to this rule are some platforms where it is disabled due to concerns 1271over output size. You may want to… 1272 1273* …enable profiling globally so that you can build a project you are working on 1274 with profiling ability giving you insight in the time spent across your code 1275 and code you depend on using [GHC's profiling feature][profiling]. 1276 1277* …disable profiling (globally) to reduce the time spent building the profiling 1278 versions of libraries which a significant amount of build time is spent on 1279 (although they are not as expensive as the “normal” build of a Haskell library). 1280 1281::: {.note} 1282The method described below affects the build of all libraries in the 1283respective Haskell package set as well as GHC. If your choices differ from 1284Nixpkgs' default for your (host) platform, you will lose the ability to 1285substitute from the official binary cache. 1286 1287If you are concerned about build times and thus want to disable profiling, it 1288probably makes sense to use `haskell.lib.compose.disableLibraryProfiling` (see 1289[](#haskell-trivial-helpers)) on the packages you are building locally while 1290continuing to substitute their dependencies and GHC. 1291::: 1292 1293Since we need to change the profiling settings for the desired Haskell package 1294set _and_ GHC (as the core libraries like `base`, `filepath` etc. are bundled 1295with GHC), it is recommended to use overlays for Nixpkgs to change them. 1296Since the interrelated parts, i.e. the package set and GHC, are connected 1297via the Nixpkgs fixpoint, we need to modify them both in a way that preserves 1298their connection (or else we'd have to wire it up again manually). This is 1299achieved by changing GHC and the package set in separate overlays to prevent 1300the package set from pulling in GHC from `prev`. 1301 1302The result is two overlays like the ones shown below. Adjustable parts are 1303annotated with comments, as are any optional or alternative ways to achieve 1304the desired profiling settings without causing too many rebuilds. 1305 1306<!-- TODO(@sternenseemann): buildHaskellPackages != haskellPackages with this overlay, 1307affected by https://github.com/NixOS/nixpkgs/issues/235960 which needs to be fixed 1308properly still. 1309--> 1310 1311```nix 1312let 1313 # Name of the compiler and package set you want to change. If you are using 1314 # the default package set `haskellPackages`, you need to look up what version 1315 # of GHC it currently uses (note that this is subject to change). 1316 ghcName = "ghc910"; 1317 # Desired new setting 1318 enableProfiling = true; 1319 1320in 1321[ 1322 # The first overlay modifies the GHC derivation so that it does or does not 1323 # build profiling versions of the core libraries bundled with it. It is 1324 # recommended to only use such an overlay if you are enabling profiling on a 1325 # platform that doesn't by default, because compiling GHC from scratch is 1326 # quite expensive. 1327 ( 1328 final: prev: 1329 let 1330 inherit (final) lib; 1331 1332 in 1333 { 1334 haskell = prev.haskell // { 1335 compiler = prev.haskell.compiler // { 1336 ${ghcName} = prev.haskell.compiler.${ghcName}.override { 1337 # Unfortunately, the GHC setting is named differently for historical reasons 1338 enableProfiledLibs = enableProfiling; 1339 }; 1340 }; 1341 }; 1342 } 1343 ) 1344 1345 ( 1346 final: prev: 1347 let 1348 inherit (final) lib; 1349 haskellLib = final.haskell.lib.compose; 1350 1351 in 1352 { 1353 haskell = prev.haskell // { 1354 packages = prev.haskell.packages // { 1355 ${ghcName} = prev.haskell.packages.${ghcName}.override { 1356 overrides = hfinal: hprev: { 1357 mkDerivation = 1358 args: 1359 hprev.mkDerivation ( 1360 args 1361 // { 1362 # Since we are forcing our ideas upon mkDerivation, this change will 1363 # affect every package in the package set. 1364 enableLibraryProfiling = enableProfiling; 1365 1366 # To actually use profiling on an executable, executable profiling 1367 # needs to be enabled for the executable you want to profile. You 1368 # can either do this globally or… 1369 enableExecutableProfiling = enableProfiling; 1370 } 1371 ); 1372 1373 # …only for the package that contains an executable you want to profile. 1374 # That saves on unnecessary rebuilds for packages that you only depend 1375 # on for their library, but also contain executables (e.g. pandoc). 1376 my-executable = haskellLib.enableExecutableProfiling hprev.my-executable; 1377 1378 # If you are disabling profiling to save on build time, but want to 1379 # retain the ability to substitute from the binary cache. Drop the 1380 # override for mkDerivation above and instead have an override like 1381 # this for the specific packages you are building locally and want 1382 # to make cheaper to build. 1383 my-library = haskellLib.disableLibraryProfiling hprev.my-library; 1384 }; 1385 }; 1386 }; 1387 }; 1388 } 1389 ) 1390] 1391``` 1392 1393<!-- TODO(@sternenseemann): write overriding mkDerivation, overriding GHC, and 1394overriding the entire package set sections and link to them from here where 1395relevant. 1396--> 1397 1398[Stackage]: https://www.stackage.org 1399[cabal-project-files]: https://cabal.readthedocs.io/en/latest/cabal-project.html 1400[cabal2nix]: https://github.com/nixos/cabal2nix 1401[cpphs]: https://Hackage.haskell.org/package/cpphs 1402[haddock-hoogle-option]: https://haskell-haddock.readthedocs.io/en/latest/invoking.html#cmdoption-hoogle 1403[haddock-hyperlinked-source-option]: https://haskell-haddock.readthedocs.io/en/latest/invoking.html#cmdoption-hyperlinked-source 1404[haddock]: https://www.haskell.org/haddock/ 1405[haskell-program-coverage]: https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/profiling.html#observing-code-coverage 1406[haskell.nix]: https://input-output-hk.github.io/haskell.nix/index.html 1407[HLS user guide]: https://haskell-language-server.readthedocs.io/en/latest/configuration.html#configuring-your-editor 1408[hoogle]: https://wiki.haskell.org/Hoogle 1409[incremental-builds]: https://www.haskellforall.com/2022/12/nixpkgs-support-for-incremental-haskell.html 1410[jailbreak-cabal]: https://github.com/NixOS/jailbreak-cabal/ 1411[multiple-outputs]: https://nixos.org/manual/nixpkgs/stable/#chap-multiple-output 1412[optparse-applicative-completions]: https://github.com/pcapriotti/optparse-applicative/blob/7726b63796aa5d0df82e926d467f039b78ca09e2/README.md#bash-zsh-and-fish-completions 1413[profiling-detail]: https://cabal.readthedocs.io/en/latest/cabal-project.html#cfg-field-profiling-detail 1414[profiling]: https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/profiling.html 1415[search.nixos.org]: https://search.nixos.org 1416[turtle]: https://hackage.haskell.org/package/turtle 1417[import-from-derivation]: https://nixos.org/manual/nix/stable/language/import-from-derivation