Merge pull request #23627 from alexeymuranov/haskell-doc-markdown

doc: enable code syntax highlighting

authored by Frederik Rietdijk and committed by GitHub 84b1643a d4dbef90

+424 -381
+424 -381
doc/languages-frameworks/haskell.md
··· 11 Nixpkgs distributes build instructions for all Haskell packages registered on 12 [Hackage](http://hackage.haskell.org/), but strangely enough normal Nix package 13 lookups don't seem to discover any of them, except for the default version of ghc, cabal-install, and stack: 14 - 15 - $ nix-env -i alex 16 - error: selector ‘alex’ matches no derivations 17 - $ nix-env -qa ghc 18 - ghc-7.10.2 19 20 The Haskell package set is not registered in the top-level namespace because it 21 is *huge*. If all Haskell packages were visible to these commands, then 22 name-based search/install operations would be much slower than they are now. We 23 avoided that by keeping all Haskell-related packages in a separate attribute 24 set called `haskellPackages`, which the following command will list: 25 - 26 - $ nix-env -f "<nixpkgs>" -qaP -A haskellPackages 27 - haskellPackages.a50 a50-0.5 28 - haskellPackages.abacate haskell-abacate-0.0.0.0 29 - haskellPackages.abcBridge haskell-abcBridge-0.12 30 - haskellPackages.afv afv-0.1.1 31 - haskellPackages.alex alex-3.1.4 32 - haskellPackages.Allure Allure-0.4.101.1 33 - haskellPackages.alms alms-0.6.7 34 - [... some 8000 entries omitted ...] 35 36 To install any of those packages into your profile, refer to them by their 37 attribute path (first column): 38 - 39 - $ nix-env -f "<nixpkgs>" -iA haskellPackages.Allure ... 40 41 The attribute path of any Haskell packages corresponds to the name of that 42 particular package on Hackage: the package `cabal-install` has the attribute ··· 58 reach Nixpkgs varies from system to system. We dodged that problem by giving 59 `nix-env` an explicit `-f "<nixpkgs>"` parameter, but if you call `nix-env` 60 without that flag, then chances are the invocation fails: 61 - 62 - $ nix-env -iA haskellPackages.cabal-install 63 - error: attribute ‘haskellPackages’ in selection path 64 - ‘haskellPackages.cabal-install’ not found 65 66 On NixOS, for example, Nixpkgs does *not* exist in the top-level namespace by 67 default. To figure out the proper attribute path, it's easiest to query for the 68 path of a well-known Nixpkgs package, i.e.: 69 - 70 - $ nix-env -qaP coreutils 71 - nixos.coreutils coreutils-8.23 72 73 If your system responds like that (most NixOS installations will), then the 74 attribute path to `haskellPackages` is `nixos.haskellPackages`. Thus, if you 75 want to use `nix-env` without giving an explicit `-f` flag, then that's the way 76 to do it: 77 - 78 - $ nix-env -qaP -A nixos.haskellPackages 79 - $ nix-env -iA nixos.haskellPackages.cabal-install 80 81 Our current default compiler is GHC 7.10.x and the `haskellPackages` set 82 contains packages built with that particular version. Nixpkgs contains the 83 latest major release of every GHC since 6.10.4, however, and there is a whole 84 family of package sets available that defines Hackage packages built with each 85 of those compilers, too: 86 - 87 - $ nix-env -f "<nixpkgs>" -qaP -A haskell.packages.ghc6123 88 - $ nix-env -f "<nixpkgs>" -qaP -A haskell.packages.ghc763 89 90 The name `haskellPackages` is really just a synonym for 91 `haskell.packages.ghc7102`, because we prefer that package set internally and 92 recommend it to our users as their default choice, but ultimately you are free 93 to compile your Haskell packages with any GHC version you please. The following 94 command displays the complete list of available compilers: 95 - 96 - $ nix-env -f "<nixpkgs>" -qaP -A haskell.compiler 97 - haskell.compiler.ghc6104 ghc-6.10.4 98 - haskell.compiler.ghc6123 ghc-6.12.3 99 - haskell.compiler.ghc704 ghc-7.0.4 100 - haskell.compiler.ghc722 ghc-7.2.2 101 - haskell.compiler.ghc742 ghc-7.4.2 102 - haskell.compiler.ghc763 ghc-7.6.3 103 - haskell.compiler.ghc784 ghc-7.8.4 104 - haskell.compiler.ghc7102 ghc-7.10.2 105 - haskell.compiler.ghcHEAD ghc-7.11.20150402 106 - haskell.compiler.ghcNokinds ghc-nokinds-7.11.20150704 107 - haskell.compiler.ghcjs ghcjs-0.1.0 108 - haskell.compiler.jhc jhc-0.8.2 109 - haskell.compiler.uhc uhc-1.1.9.0 110 111 We have no package sets for `jhc` or `uhc` yet, unfortunately, but for every 112 version of GHC listed above, there exists a package set based on that compiler. ··· 121 of the tools `cabal-install` and `stack`. We saw in section 122 [How to install Haskell packages] how you can install those programs into your 123 user profile: 124 - 125 - $ nix-env -f "<nixpkgs>" -iA haskellPackages.ghc haskellPackages.cabal-install 126 127 Instead of the default package set `haskellPackages`, you can also use the more 128 precise name `haskell.compiler.ghc7102`, which has the advantage that it refers ··· 131 132 Once you've made those tools available in `$PATH`, it's possible to build 133 Hackage packages the same way people without access to Nix do it all the time: 134 - 135 - $ cabal get lens-4.11 && cd lens-4.11 136 - $ cabal install -j --dependencies-only 137 - $ cabal configure 138 - $ cabal build 139 140 If you enjoy working with Cabal sandboxes, then that's entirely possible too: 141 just execute the command 142 - 143 - $ cabal sandbox init 144 - 145 before installing the required dependencies. 146 147 The `nix-shell` utility makes it easy to switch to a different compiler 148 version; just enter the Nix shell environment with the command 149 - 150 - $ nix-shell -p haskell.compiler.ghc784 151 - 152 to bring GHC 7.8.4 into `$PATH`. Alternatively, you can use Stack instead of 153 `nix-shell` directly to select compiler versions and other build tools 154 per-project. It uses `nix-shell` under the hood when Nix support is turned on. ··· 159 a project that doesn't depend on any additional system libraries outside of GHC, 160 then it's even sufficient to just run the `cabal configure` command inside of 161 the shell: 162 - 163 - $ nix-shell -p haskell.compiler.ghc784 --command "cabal configure" 164 165 Afterwards, all other commands like `cabal build` work just fine in any shell 166 environment, because the configure phase recorded the absolute paths to all ··· 187 GHC. For example, the Nix expression `ghcWithPackages (pkgs: [pkgs.mtl])` 188 generates a copy of GHC that has the `mtl` library registered in addition to 189 its normal core packages: 190 191 - $ nix-shell -p "haskellPackages.ghcWithPackages (pkgs: [pkgs.mtl])" 192 - 193 - [nix-shell:~]$ ghc-pkg list mtl 194 - /nix/store/zy79...-ghc-7.10.2/lib/ghc-7.10.2/package.conf.d: 195 - mtl-2.2.1 196 197 This function allows users to define their own development environment by means 198 of an override. After adding the following snippet to `~/.config/nixpkgs/config.nix`, 199 - 200 - { 201 - packageOverrides = super: let self = super.pkgs; in 202 - { 203 - myHaskellEnv = self.haskell.packages.ghc7102.ghcWithPackages 204 - (haskellPackages: with haskellPackages; [ 205 - # libraries 206 - arrows async cgi criterion 207 - # tools 208 - cabal-install haskintex 209 - ]); 210 - }; 211 - } 212 - 213 it's possible to install that compiler with `nix-env -f "<nixpkgs>" -iA 214 myHaskellEnv`. If you'd like to switch that development environment to a 215 different version of GHC, just replace the `ghc7102` bit in the previous ··· 221 The generated `ghc` program is a wrapper script that re-directs the real 222 GHC executable to use a new `lib` directory --- one that we specifically 223 constructed to contain all those packages the user requested: 224 - 225 - $ cat $(type -p ghc) 226 - #! /nix/store/xlxj...-bash-4.3-p33/bin/bash -e 227 - export NIX_GHC=/nix/store/19sm...-ghc-7.10.2/bin/ghc 228 - export NIX_GHCPKG=/nix/store/19sm...-ghc-7.10.2/bin/ghc-pkg 229 - export NIX_GHC_DOCDIR=/nix/store/19sm...-ghc-7.10.2/share/doc/ghc/html 230 - export NIX_GHC_LIBDIR=/nix/store/19sm...-ghc-7.10.2/lib/ghc-7.10.2 231 - exec /nix/store/j50p...-ghc-7.10.2/bin/ghc "-B$NIX_GHC_LIBDIR" "$@" 232 233 The variables `$NIX_GHC`, `$NIX_GHCPKG`, etc. point to the *new* store path 234 `ghcWithPackages` constructed specifically for this environment. The last line ··· 248 To make sure that mechanism works properly all the time, we recommend that you 249 set those variables to meaningful values in your shell environment, too, i.e. 250 by adding the following code to your `~/.bashrc`: 251 - 252 - if type >/dev/null 2>&1 -p ghc; then 253 - eval "$(egrep ^export "$(type -p ghc)")" 254 - fi 255 256 If you are certain that you'll use only one GHC environment which is located in 257 your user profile, then you can use the following code, too, which has the 258 advantage that it doesn't contain any paths from the Nix store, i.e. those 259 settings always remain valid even if a `nix-env -u` operation updates the GHC 260 environment in your profile: 261 - 262 - if [ -e ~/.nix-profile/bin/ghc ]; then 263 - export NIX_GHC="$HOME/.nix-profile/bin/ghc" 264 - export NIX_GHCPKG="$HOME/.nix-profile/bin/ghc-pkg" 265 - export NIX_GHC_DOCDIR="$HOME/.nix-profile/share/doc/ghc/html" 266 - export NIX_GHC_LIBDIR="$HOME/.nix-profile/lib/ghc-$($NIX_GHC --numeric-version)" 267 - fi 268 269 ### How to install a compiler with libraries, hoogle and documentation indexes 270 ··· 280 long and scary. 281 282 For example, installing the following environment 283 - 284 - { 285 - packageOverrides = super: let self = super.pkgs; in 286 - { 287 - myHaskellEnv = self.haskellPackages.ghcWithHoogle 288 - (haskellPackages: with haskellPackages; [ 289 - # libraries 290 - arrows async cgi criterion 291 - # tools 292 - cabal-install haskintex 293 - ]); 294 - }; 295 - } 296 - 297 allows one to browse module documentation index [not too dissimilar to 298 this](https://downloads.haskell.org/~ghc/latest/docs/html/libraries/index.html) 299 for all the specified packages and their dependencies by directing a browser of ··· 303 304 After you've marveled enough at that try adding the following to your 305 `~/.ghc/ghci.conf` 306 - 307 - :def hoogle \s -> return $ ":! hoogle search -cl --count=15 \"" ++ s ++ "\"" 308 - :def doc \s -> return $ ":! hoogle search -cl --info \"" ++ s ++ "\"" 309 - 310 and test it by typing into `ghci`: 311 - 312 - :hoogle a -> a 313 - :doc a -> a 314 315 Be sure to note the links to `haddock` files in the output. With any modern and 316 properly configured terminal emulator you can just click those links to 317 navigate there. 318 319 Finally, you can run 320 - 321 - hoogle server -p 8080 322 - 323 and navigate to http://localhost:8080/ for your own local 324 [Hoogle](https://www.haskell.org/hoogle/). Note, however, that Firefox and 325 possibly other browsers disallow navigation from `http:` to `file:` URIs for ··· 334 automatically select the right version of GHC and other build tools to build, 335 test and execute apps in an existing project downloaded from somewhere on the 336 Internet. Pass the `--nix` flag to any `stack` command to do so, e.g. 337 - 338 - $ git clone --recursive http://github.com/yesodweb/wai 339 - $ cd wai 340 - $ stack --nix build 341 342 If you want `stack` to use Nix by default, you can add a `nix` section to the 343 `stack.yaml` file, as explained in the [Stack documentation][stack-nix-doc]. For 344 example: 345 - 346 - nix: 347 - enable: true 348 - packages: [pkgconfig zeromq zlib] 349 350 The example configuration snippet above tells Stack to create an ad hoc 351 environment for `nix-shell` as in the below section, in which the `pkgconfig`, ··· 356 environments might need to expose Nixpkgs packages compiled in a certain way, or 357 with extra environment variables. In these cases, you'll need a `shell` field 358 instead of `packages`: 359 - 360 - nix: 361 - enable: true 362 - shell-file: shell.nix 363 364 For more on how to write a `shell.nix` file see the below section. You'll need 365 to express a derivation. Note that Nixpkgs ships with a convenience wrapper ··· 368 as `mkDerivation` can be provided. For example, to build a Stack project that 369 including packages that link against a version of the R library compiled with 370 special options turned on: 371 372 - with (import <nixpkgs> { }); 373 - 374 - let R = pkgs.R.override { enableStrictBarrier = true; }; 375 - in 376 - haskell.lib.buildStackProject { 377 - name = "HaskellR"; 378 - buildInputs = [ R zeromq zlib ]; 379 - } 380 381 You can select a particular GHC version to compile with by setting the 382 `ghc` attribute as an argument to `buildStackProject`. Better yet, let 383 Stack choose what GHC version it wants based on the snapshot specified 384 in `stack.yaml` (only works with Stack >= 1.1.3): 385 386 - {nixpkgs ? import <nixpkgs> { }, ghc ? nixpkgs.ghc}: 387 388 - with nixpkgs; 389 - 390 - let R = pkgs.R.override { enableStrictBarrier = true; }; 391 - in 392 - haskell.lib.buildStackProject { 393 - name = "HaskellR"; 394 - buildInputs = [ R zeromq zlib ]; 395 - inherit ghc; 396 - } 397 398 [stack-nix-doc]: http://docs.haskellstack.org/en/stable/nix_integration.html 399 ··· 401 402 The easiest way to create an ad hoc development environment is to run 403 `nix-shell` with the appropriate GHC environment given on the command-line: 404 - 405 - nix-shell -p "haskellPackages.ghcWithPackages (pkgs: with pkgs; [mtl pandoc])" 406 407 For more sophisticated use-cases, however, it's more convenient to save the 408 desired configuration in a file called `shell.nix` that looks like this: 409 - 410 - { nixpkgs ? import <nixpkgs> {}, compiler ? "ghc7102" }: 411 - let 412 - inherit (nixpkgs) pkgs; 413 - ghc = pkgs.haskell.packages.${compiler}.ghcWithPackages (ps: with ps; [ 414 - monad-par mtl 415 - ]); 416 - in 417 - pkgs.stdenv.mkDerivation { 418 - name = "my-haskell-env-0"; 419 - buildInputs = [ ghc ]; 420 - shellHook = "eval $(egrep ^export ${ghc}/bin/ghc)"; 421 - } 422 423 Now run `nix-shell` --- or even `nix-shell --pure` --- to enter a shell 424 environment that has the appropriate compiler in `$PATH`. If you use `--pure`, ··· 434 environment suitable for compiling that particular package. If you'd like to 435 hack the `lens` library, for example, then you just have to check out the 436 source code and enter the appropriate environment: 437 438 - $ cabal get lens-4.11 && cd lens-4.11 439 - Downloading lens-4.11... 440 - Unpacking to lens-4.11/ 441 - 442 - $ nix-shell "<nixpkgs>" -A haskellPackages.lens.env 443 - [nix-shell:/tmp/lens-4.11]$ 444 445 At point, you can run `cabal configure`, `cabal build`, and all the other 446 development commands. Note that you need `cabal-install` installed in your ··· 459 For example, let's assume that you're working on a private project called 460 `foo`. To generate a Nix build expression for it, change into the project's 461 top-level directory and run the command: 462 - 463 - $ cabal2nix . >foo.nix 464 - 465 Then write the following snippet into a file called `default.nix`: 466 - 467 - { nixpkgs ? import <nixpkgs> {}, compiler ? "ghc7102" }: 468 - nixpkgs.pkgs.haskell.packages.${compiler}.callPackage ./foo.nix { } 469 470 Finally, store the following code in a file called `shell.nix`: 471 - 472 - { nixpkgs ? import <nixpkgs> {}, compiler ? "ghc7102" }: 473 - (import ./default.nix { inherit nixpkgs compiler; }).env 474 475 At this point, you can run `nix-build` to have Nix compile your project and 476 install it into a Nix store path. The local directory will contain a symlink ··· 486 487 If your package does not depend on any system-level libraries, then it's 488 sufficient to run 489 - 490 - $ nix-shell --command "cabal configure" 491 - 492 once to set up your build. `cabal-install` determines the absolute paths to all 493 resources required for the build and writes them into a config file in the 494 `dist/` directory. Once that's done, you can run `cabal build` and any other ··· 502 up a `default.nix` and `shell.nix` file manually, then you can use the 503 `--shell` flag offered by `cabal2nix` to have it generate a stand-alone 504 `nix-shell` environment for you. With that feature, running 505 - 506 - $ cabal2nix --shell . >shell.nix 507 - $ nix-shell --command "cabal configure" 508 - 509 is usually enough to set up a build environment for any given Haskell package. 510 You can even use that generated file to run `nix-build`, too: 511 - 512 - $ nix-build shell.nix 513 514 ### How to build projects that depend on each other 515 ··· 518 for the dependency resolution performed by `callPackage`. First of all, change 519 into each of your projects top-level directories and generate a `default.nix` 520 file with `cabal2nix`: 521 - 522 - $ cd ~/src/foo && cabal2nix . >default.nix 523 - $ cd ~/src/bar && cabal2nix . >default.nix 524 - 525 Then edit your `~/.config/nixpkgs/config.nix` file to register those builds in the 526 default Haskell package set: 527 - 528 - { 529 - packageOverrides = super: let self = super.pkgs; in 530 - { 531 - haskellPackages = super.haskellPackages.override { 532 - overrides = self: super: { 533 - foo = self.callPackage ../src/foo {}; 534 - bar = self.callPackage ../src/bar {}; 535 - }; 536 - }; 537 - }; 538 - } 539 - 540 Once that's accomplished, `nix-env -f "<nixpkgs>" -qA haskellPackages` will 541 show your packages like any other package from Hackage, and you can build them 542 - 543 - $ nix-build "<nixpkgs>" -A haskellPackages.foo 544 - 545 or enter an interactive shell environment suitable for building them: 546 - 547 - $ nix-shell "<nixpkgs>" -A haskellPackages.bar.env 548 549 ## Miscellaneous Topics 550 ··· 555 feature is to replace the default `mkDerivation` function with one that enables 556 library profiling for all packages. To accomplish that, add configure the 557 following snippet in your `~/.config/nixpkgs/config.nix` file: 558 - 559 - { 560 - packageOverrides = super: let self = super.pkgs; in 561 - { 562 - profiledHaskellPackages = self.haskellPackages.override { 563 - overrides = self: super: { 564 - mkDerivation = args: super.mkDerivation (args // { 565 - enableLibraryProfiling = true; 566 - }); 567 - }; 568 - }; 569 }; 570 - } 571 - 572 Then, replace instances of `haskellPackages` in the `cabal2nix`-generated 573 `default.nix` or `shell.nix` files with `profiledHaskellPackages`. 574 ··· 580 7.8.4 cannot compile that binary. Now, one way to solve that problem is to 581 register an older version of `ghc-events` in the 7.8.x-specific package set. 582 The first step is to generate Nix build instructions with `cabal2nix`: 583 - 584 - $ cabal2nix cabal://ghc-events-0.4.3.0 >~/.nixpkgs/ghc-events-0.4.3.0.nix 585 - 586 Then add the override in `~/.config/nixpkgs/config.nix`: 587 - 588 - { 589 - packageOverrides = super: let self = super.pkgs; in 590 - { 591 - haskell = super.haskell // { 592 - packages = super.haskell.packages // { 593 - ghc784 = super.haskell.packages.ghc784.override { 594 - overrides = self: super: { 595 - ghc-events = self.callPackage ./ghc-events-0.4.3.0.nix {}; 596 - }; 597 - }; 598 }; 599 }; 600 }; 601 - } 602 603 This code is a little crazy, no doubt, but it's necessary because the intuitive 604 version 605 606 - haskell.packages.ghc784 = super.haskell.packages.ghc784.override { 607 - overrides = self: super: { 608 - ghc-events = self.callPackage ./ghc-events-0.4.3.0.nix {}; 609 - }; 610 }; 611 - 612 doesn't do what we want it to: that code replaces the `haskell` package set in 613 Nixpkgs with one that contains only one entry,`packages`, which contains only 614 one entry `ghc784`. This override loses the `haskell.compiler` set, and it ··· 618 619 Once it's accomplished, however, we can install a variant of `ghc-events` 620 that's compiled with GHC 7.8.4: 621 - 622 - nix-env -f "<nixpkgs>" -iA haskell.packages.ghc784.ghc-events 623 - 624 Unfortunately, it turns out that this build fails again while executing the 625 test suite! Apparently, the release archive on Hackage is missing some data 626 files that the test suite requires, so we cannot run it. We accomplish that by 627 re-generating the Nix expression with the `--no-check` flag: 628 - 629 - $ cabal2nix --no-check cabal://ghc-events-0.4.3.0 >~/.nixpkgs/ghc-events-0.4.3.0.nix 630 - 631 Now the builds succeeds. 632 633 Of course, in the concrete example of `ghc-events` this whole exercise is not ··· 642 643 GHC and distributed build farms don't get along well: 644 645 - https://ghc.haskell.org/trac/ghc/ticket/4012 646 647 When you see an error like this one 648 - 649 - package foo-0.7.1.0 is broken due to missing package 650 - text-1.2.0.4-98506efb1b9ada233bb5c2b2db516d91 651 - 652 then you have to download and re-install `foo` and all its dependents from 653 scratch: 654 - 655 - # nix-store -q --referrers /nix/store/*-haskell-text-1.2.0.4 \ 656 - | xargs -L 1 nix-store --repair-path 657 658 If you're using additional Hydra servers other than `hydra.nixos.org`, then it 659 might be necessary to purge the local caches that store data from those 660 machines to disable these binary channels for the duration of the previous 661 command, i.e. by running: 662 - 663 - rm /nix/var/nix/binary-cache-v3.sqlite 664 - rm /nix/var/nix/manifests/* 665 - rm /nix/var/nix/channel-cache/* 666 667 ### How to use the Haste Haskell-to-Javascript transpiler 668 669 Open a shell with `haste-compiler` and `haste-cabal-install` (you don't actually need 670 `node`, but it can be useful to test stuff): 671 - 672 - $ nix-shell -p "haskellPackages.ghcWithPackages (self: with self; [haste-cabal-install haste-compiler])" -p nodejs 673 - 674 You may not need the following step but if `haste-boot` fails to compile all the 675 packages it needs, this might do the trick 676 - 677 - $ haste-cabal update 678 - 679 `haste-boot` builds a set of core libraries so that they can be used from Javascript 680 transpiled programs: 681 - 682 - $ haste-boot 683 - 684 Transpile and run a "Hello world" program: 685 - 686 - $ echo 'module Main where main = putStrLn "Hello world"' > hello-world.hs 687 - $ hastec --onexec hello-world.hs 688 - $ node hello-world.js 689 - Hello world 690 691 ### Builds on Darwin fail with `math.h` not found 692 693 Users of GHC on Darwin have occasionally reported that builds fail, because the 694 compiler complains about a missing include file: 695 - 696 - fatal error: 'math.h' file not found 697 - 698 The issue has been discussed at length in [ticket 699 6390](https://github.com/NixOS/nixpkgs/issues/6390), and so far no good 700 solution has been proposed. As a work-around, users who run into this problem 701 can configure the environment variables 702 - 703 - export NIX_CFLAGS_COMPILE="-idirafter /usr/include" 704 - export NIX_CFLAGS_LINK="-L/usr/lib" 705 - 706 in their `~/.bashrc` file to avoid the compiler error. 707 708 ### Builds using Stack complain about missing system libraries 709 710 - -- While building package zlib-0.5.4.2 using: 711 - runhaskell -package=Cabal-1.22.4.0 -clear-package-db [... lots of flags ...] 712 - Process exited with code: ExitFailure 1 713 - Logs have been written to: /home/foo/src/stack-ide/.stack-work/logs/zlib-0.5.4.2.log 714 715 - Configuring zlib-0.5.4.2... 716 - Setup.hs: Missing dependency on a foreign library: 717 - * Missing (or bad) header file: zlib.h 718 - This problem can usually be solved by installing the system package that 719 - provides this library (you may need the "-dev" version). If the library is 720 - already installed but in a non-standard location then you can use the flags 721 - --extra-include-dirs= and --extra-lib-dirs= to specify where it is. 722 - If the header file does exist, it may contain errors that are caught by the C 723 - compiler at the preprocessing stage. In this case you can re-run configure 724 - with the verbosity flag -v3 to see the error messages. 725 726 When you run the build inside of the nix-shell environment, the system 727 - is configured to find libz.so without any special flags -- the compiler 728 and linker "just know" how to find it. Consequently, Cabal won't record 729 - any search paths for libz.so in the package description, which means 730 that the package works fine inside of nix-shell, but once you leave the 731 shell the shared object can no longer be found. That issue is by no 732 means specific to Stack: you'll have that problem with any other ··· 735 736 You can remedy this issue in several ways. The easiest is to add a `nix` section 737 to the `stack.yaml` like the following: 738 739 - nix: 740 - enable: true 741 - packages: [ zlib ] 742 - 743 - Stack's Nix support knows to add `${zlib.out}/lib` and `${zlib.dev}/include` as an 744 - `--extra-lib-dirs` and `extra-include-dirs`, respectively. Alternatively, you 745 - can achieve the same effect by hand. First of all, run 746 - 747 - $ nix-build --no-out-link "<nixpkgs>" -A zlib 748 - /nix/store/alsvwzkiw4b7ip38l4nlfjijdvg3fvzn-zlib-1.2.8 749 - 750 to find out the store path of the system's zlib library. Now, you can 751 752 - 1) add that path (plus a "/lib" suffix) to your $LD_LIBRARY_PATH 753 - environment variable to make sure your system linker finds libz.so 754 - automatically. It's no pretty solution, but it will work. 755 756 - 2) As a variant of (1), you can also install any number of system 757 - libraries into your user's profile (or some other profile) and point 758 - $LD_LIBRARY_PATH to that profile instead, so that you don't have to 759 - list dozens of those store paths all over the place. 760 - 761 - 3) The solution I prefer is to call stack with an appropriate 762 - --extra-lib-dirs flag like so: 763 764 - $ stack --extra-lib-dirs=/nix/store/alsvwzkiw4b7ip38l4nlfjijdvg3fvzn-zlib-1.2.8/lib build 765 766 - Typically, you'll need --extra-include-dirs as well. It's possible 767 - to add those flag to the project's "stack.yaml" or your user's 768 - global "~/.stack/global/stack.yaml" file so that you don't have to 769 - specify them manually every time. But again, you're likely better off using 770 - Stack's Nix support instead. 771 772 - The same thing applies to `cabal configure`, of course, if you're 773 - building with `cabal-install` instead of Stack. 774 775 ### Creating statically linked binaries 776 777 There are two levels of static linking. The first option is to configure the 778 build with the Cabal flag `--disable-executable-dynamic`. In Nix expressions, 779 this can be achieved by setting the attribute: 780 - 781 - enableSharedExecutables = false; 782 - 783 That gives you a binary with statically linked Haskell libraries and 784 dynamically linked system libraries. 785 786 To link both Haskell libraries and system libraries statically, the additional 787 flags `--ghc-option=-optl=-static --ghc-option=-optl=-pthread` need to be used. 788 In Nix, this is accomplished with: 789 790 - configureFlags = [ "--ghc-option=-optl=-static" "--ghc-option=-optl=-pthread" ]; 791 - 792 - It's important to realize, however, that most system libraries in Nix are built 793 - as shared libraries only, i.e. there is just no static library available that 794 - Cabal could link! 795 796 ### Building GHC with integer-simple 797 ··· 801 [integer-gmp](http://hackage.haskell.org/package/integer-gmp) package. 802 803 A potential problem with this is that GMP is licensed under the 804 - [​GNU Lesser General Public License (LGPL)](http://www.gnu.org/copyleft/lesser.html), 805 a kind of "copyleft" license. According to the terms of the LGPL, paragraph 5, 806 you may distribute a program that is designed to be compiled and dynamically 807 linked with the library under the terms of your choice (i.e., commercially) but ··· 814 programs compiled with GHC because most distributions (and builds) of GHC use 815 static libraries. (Dynamic libraries are currently distributed only for OS X.) 816 The LGPL licensing situation may be worse: even though 817 - ​[The Glasgow Haskell Compiler License](https://www.haskell.org/ghc/license) 818 is essentially a "free software" license (BSD3), according to 819 paragraph 2 of the LGPL, GHC must be distributed under the terms of the LGPL! 820 ··· 825 To get a GHC compiler build with `integer-simple` instead of `integer-gmp` use 826 the attribute: `haskell.compiler.integer-simple."${ghcVersion}"`. 827 For example: 828 - 829 - $ nix-build -E '(import <nixpkgs> {}).haskell.compiler.integer-simple.ghc802' 830 - ... 831 - $ result/bin/ghc-pkg list | grep integer 832 - integer-simple-0.1.1.1 833 - 834 The following command displays the complete list of GHC compilers build with `integer-simple`: 835 - 836 - $ nix-env -f "<nixpkgs>" -qaP -A haskell.compiler.integer-simple 837 - haskell.compiler.integer-simple.ghc7102 ghc-7.10.2 838 - haskell.compiler.integer-simple.ghc7103 ghc-7.10.3 839 - haskell.compiler.integer-simple.ghc722 ghc-7.2.2 840 - haskell.compiler.integer-simple.ghc742 ghc-7.4.2 841 - haskell.compiler.integer-simple.ghc783 ghc-7.8.3 842 - haskell.compiler.integer-simple.ghc784 ghc-7.8.4 843 - haskell.compiler.integer-simple.ghc801 ghc-8.0.1 844 - haskell.compiler.integer-simple.ghc802 ghc-8.0.2 845 - haskell.compiler.integer-simple.ghcHEAD ghc-8.1.20170106 846 847 To get a package set supporting `integer-simple` use the attribute: 848 `haskell.packages.integer-simple."${ghcVersion}"`. For example 849 use the following to get the `scientific` package build with `integer-simple`: 850 - 851 - $ nix-build -A haskell.packages.integer-simple.ghc802.scientific 852 - 853 854 ## Other resources 855 856 - - The Youtube video [Nix Loves Haskell](https://www.youtube.com/watch?v=BsBhi_r-OeE) 857 - provides an introduction into Haskell NG aimed at beginners. The slides are 858 - available at http://cryp.to/nixos-meetup-3-slides.pdf and also -- in a form 859 - ready for cut & paste -- at 860 - https://github.com/NixOS/cabal2nix/blob/master/doc/nixos-meetup-3-slides.md. 861 862 - - Another Youtube video is [Escaping Cabal Hell with Nix](https://www.youtube.com/watch?v=mQd3s57n_2Y), 863 - which discusses the subject of Haskell development with Nix but also provides 864 - a basic introduction to Nix as well, i.e. it's suitable for viewers with 865 - almost no prior Nix experience. 866 867 - - Oliver Charles wrote a very nice [Tutorial how to develop Haskell packages with Nix](http://wiki.ocharles.org.uk/Nix). 868 869 - - The *Journey into the Haskell NG infrastructure* series of postings 870 - describe the new Haskell infrastructure in great detail: 871 872 - - [Part 1](http://lists.science.uu.nl/pipermail/nix-dev/2015-January/015591.html) 873 - explains the differences between the old and the new code and gives 874 - instructions how to migrate to the new setup. 875 876 - - [Part 2](http://lists.science.uu.nl/pipermail/nix-dev/2015-January/015608.html) 877 - looks in-depth at how to tweak and configure your setup by means of 878 - overrides. 879 880 - - [Part 3](http://lists.science.uu.nl/pipermail/nix-dev/2015-April/016912.html) 881 - describes the infrastructure that keeps the Haskell package set in Nixpkgs 882 - up-to-date.
··· 11 Nixpkgs distributes build instructions for all Haskell packages registered on 12 [Hackage](http://hackage.haskell.org/), but strangely enough normal Nix package 13 lookups don't seem to discover any of them, except for the default version of ghc, cabal-install, and stack: 14 + ``` 15 + $ nix-env -i alex 16 + error: selector ‘alex’ matches no derivations 17 + $ nix-env -qa ghc 18 + ghc-7.10.2 19 + ``` 20 21 The Haskell package set is not registered in the top-level namespace because it 22 is *huge*. If all Haskell packages were visible to these commands, then 23 name-based search/install operations would be much slower than they are now. We 24 avoided that by keeping all Haskell-related packages in a separate attribute 25 set called `haskellPackages`, which the following command will list: 26 + ``` 27 + $ nix-env -f "<nixpkgs>" -qaP -A haskellPackages 28 + haskellPackages.a50 a50-0.5 29 + haskellPackages.abacate haskell-abacate-0.0.0.0 30 + haskellPackages.abcBridge haskell-abcBridge-0.12 31 + haskellPackages.afv afv-0.1.1 32 + haskellPackages.alex alex-3.1.4 33 + haskellPackages.Allure Allure-0.4.101.1 34 + haskellPackages.alms alms-0.6.7 35 + [... some 8000 entries omitted ...] 36 + ``` 37 38 To install any of those packages into your profile, refer to them by their 39 attribute path (first column): 40 + ```shell 41 + nix-env -f "<nixpkgs>" -iA haskellPackages.Allure ... 42 + ``` 43 44 The attribute path of any Haskell packages corresponds to the name of that 45 particular package on Hackage: the package `cabal-install` has the attribute ··· 61 reach Nixpkgs varies from system to system. We dodged that problem by giving 62 `nix-env` an explicit `-f "<nixpkgs>"` parameter, but if you call `nix-env` 63 without that flag, then chances are the invocation fails: 64 + ``` 65 + $ nix-env -iA haskellPackages.cabal-install 66 + error: attribute ‘haskellPackages’ in selection path 67 + ‘haskellPackages.cabal-install’ not found 68 + ``` 69 70 On NixOS, for example, Nixpkgs does *not* exist in the top-level namespace by 71 default. To figure out the proper attribute path, it's easiest to query for the 72 path of a well-known Nixpkgs package, i.e.: 73 + ``` 74 + $ nix-env -qaP coreutils 75 + nixos.coreutils coreutils-8.23 76 + ``` 77 78 If your system responds like that (most NixOS installations will), then the 79 attribute path to `haskellPackages` is `nixos.haskellPackages`. Thus, if you 80 want to use `nix-env` without giving an explicit `-f` flag, then that's the way 81 to do it: 82 + ```shell 83 + nix-env -qaP -A nixos.haskellPackages 84 + nix-env -iA nixos.haskellPackages.cabal-install 85 + ``` 86 87 Our current default compiler is GHC 7.10.x and the `haskellPackages` set 88 contains packages built with that particular version. Nixpkgs contains the 89 latest major release of every GHC since 6.10.4, however, and there is a whole 90 family of package sets available that defines Hackage packages built with each 91 of those compilers, too: 92 + ```shell 93 + nix-env -f "<nixpkgs>" -qaP -A haskell.packages.ghc6123 94 + nix-env -f "<nixpkgs>" -qaP -A haskell.packages.ghc763 95 + ``` 96 97 The name `haskellPackages` is really just a synonym for 98 `haskell.packages.ghc7102`, because we prefer that package set internally and 99 recommend it to our users as their default choice, but ultimately you are free 100 to compile your Haskell packages with any GHC version you please. The following 101 command displays the complete list of available compilers: 102 + ``` 103 + $ nix-env -f "<nixpkgs>" -qaP -A haskell.compiler 104 + haskell.compiler.ghc6104 ghc-6.10.4 105 + haskell.compiler.ghc6123 ghc-6.12.3 106 + haskell.compiler.ghc704 ghc-7.0.4 107 + haskell.compiler.ghc722 ghc-7.2.2 108 + haskell.compiler.ghc742 ghc-7.4.2 109 + haskell.compiler.ghc763 ghc-7.6.3 110 + haskell.compiler.ghc784 ghc-7.8.4 111 + haskell.compiler.ghc7102 ghc-7.10.2 112 + haskell.compiler.ghcHEAD ghc-7.11.20150402 113 + haskell.compiler.ghcNokinds ghc-nokinds-7.11.20150704 114 + haskell.compiler.ghcjs ghcjs-0.1.0 115 + haskell.compiler.jhc jhc-0.8.2 116 + haskell.compiler.uhc uhc-1.1.9.0 117 + ``` 118 119 We have no package sets for `jhc` or `uhc` yet, unfortunately, but for every 120 version of GHC listed above, there exists a package set based on that compiler. ··· 129 of the tools `cabal-install` and `stack`. We saw in section 130 [How to install Haskell packages] how you can install those programs into your 131 user profile: 132 + ```shell 133 + nix-env -f "<nixpkgs>" -iA haskellPackages.ghc haskellPackages.cabal-install 134 + ``` 135 136 Instead of the default package set `haskellPackages`, you can also use the more 137 precise name `haskell.compiler.ghc7102`, which has the advantage that it refers ··· 140 141 Once you've made those tools available in `$PATH`, it's possible to build 142 Hackage packages the same way people without access to Nix do it all the time: 143 + ```shell 144 + cabal get lens-4.11 && cd lens-4.11 145 + cabal install -j --dependencies-only 146 + cabal configure 147 + cabal build 148 + ``` 149 150 If you enjoy working with Cabal sandboxes, then that's entirely possible too: 151 just execute the command 152 + ```shell 153 + cabal sandbox init 154 + ``` 155 before installing the required dependencies. 156 157 The `nix-shell` utility makes it easy to switch to a different compiler 158 version; just enter the Nix shell environment with the command 159 + ```shell 160 + nix-shell -p haskell.compiler.ghc784 161 + ``` 162 to bring GHC 7.8.4 into `$PATH`. Alternatively, you can use Stack instead of 163 `nix-shell` directly to select compiler versions and other build tools 164 per-project. It uses `nix-shell` under the hood when Nix support is turned on. ··· 169 a project that doesn't depend on any additional system libraries outside of GHC, 170 then it's even sufficient to just run the `cabal configure` command inside of 171 the shell: 172 + ```shell 173 + nix-shell -p haskell.compiler.ghc784 --command "cabal configure" 174 + ``` 175 176 Afterwards, all other commands like `cabal build` work just fine in any shell 177 environment, because the configure phase recorded the absolute paths to all ··· 198 GHC. For example, the Nix expression `ghcWithPackages (pkgs: [pkgs.mtl])` 199 generates a copy of GHC that has the `mtl` library registered in addition to 200 its normal core packages: 201 + ``` 202 + $ nix-shell -p "haskellPackages.ghcWithPackages (pkgs: [pkgs.mtl])" 203 204 + [nix-shell:~]$ ghc-pkg list mtl 205 + /nix/store/zy79...-ghc-7.10.2/lib/ghc-7.10.2/package.conf.d: 206 + mtl-2.2.1 207 + ``` 208 209 This function allows users to define their own development environment by means 210 of an override. After adding the following snippet to `~/.config/nixpkgs/config.nix`, 211 + ```nix 212 + { 213 + packageOverrides = super: let self = super.pkgs; in 214 + { 215 + myHaskellEnv = self.haskell.packages.ghc7102.ghcWithPackages 216 + (haskellPackages: with haskellPackages; [ 217 + # libraries 218 + arrows async cgi criterion 219 + # tools 220 + cabal-install haskintex 221 + ]); 222 + }; 223 + } 224 + ``` 225 it's possible to install that compiler with `nix-env -f "<nixpkgs>" -iA 226 myHaskellEnv`. If you'd like to switch that development environment to a 227 different version of GHC, just replace the `ghc7102` bit in the previous ··· 233 The generated `ghc` program is a wrapper script that re-directs the real 234 GHC executable to use a new `lib` directory --- one that we specifically 235 constructed to contain all those packages the user requested: 236 + ``` 237 + $ cat $(type -p ghc) 238 + #! /nix/store/xlxj...-bash-4.3-p33/bin/bash -e 239 + export NIX_GHC=/nix/store/19sm...-ghc-7.10.2/bin/ghc 240 + export NIX_GHCPKG=/nix/store/19sm...-ghc-7.10.2/bin/ghc-pkg 241 + export NIX_GHC_DOCDIR=/nix/store/19sm...-ghc-7.10.2/share/doc/ghc/html 242 + export NIX_GHC_LIBDIR=/nix/store/19sm...-ghc-7.10.2/lib/ghc-7.10.2 243 + exec /nix/store/j50p...-ghc-7.10.2/bin/ghc "-B$NIX_GHC_LIBDIR" "$@" 244 + ``` 245 246 The variables `$NIX_GHC`, `$NIX_GHCPKG`, etc. point to the *new* store path 247 `ghcWithPackages` constructed specifically for this environment. The last line ··· 261 To make sure that mechanism works properly all the time, we recommend that you 262 set those variables to meaningful values in your shell environment, too, i.e. 263 by adding the following code to your `~/.bashrc`: 264 + ```bash 265 + if type >/dev/null 2>&1 -p ghc; then 266 + eval "$(egrep ^export "$(type -p ghc)")" 267 + fi 268 + ``` 269 270 If you are certain that you'll use only one GHC environment which is located in 271 your user profile, then you can use the following code, too, which has the 272 advantage that it doesn't contain any paths from the Nix store, i.e. those 273 settings always remain valid even if a `nix-env -u` operation updates the GHC 274 environment in your profile: 275 + ```bash 276 + if [ -e ~/.nix-profile/bin/ghc ]; then 277 + export NIX_GHC="$HOME/.nix-profile/bin/ghc" 278 + export NIX_GHCPKG="$HOME/.nix-profile/bin/ghc-pkg" 279 + export NIX_GHC_DOCDIR="$HOME/.nix-profile/share/doc/ghc/html" 280 + export NIX_GHC_LIBDIR="$HOME/.nix-profile/lib/ghc-$($NIX_GHC --numeric-version)" 281 + fi 282 + ``` 283 284 ### How to install a compiler with libraries, hoogle and documentation indexes 285 ··· 295 long and scary. 296 297 For example, installing the following environment 298 + ```nix 299 + { 300 + packageOverrides = super: let self = super.pkgs; in 301 + { 302 + myHaskellEnv = self.haskellPackages.ghcWithHoogle 303 + (haskellPackages: with haskellPackages; [ 304 + # libraries 305 + arrows async cgi criterion 306 + # tools 307 + cabal-install haskintex 308 + ]); 309 + }; 310 + } 311 + ``` 312 allows one to browse module documentation index [not too dissimilar to 313 this](https://downloads.haskell.org/~ghc/latest/docs/html/libraries/index.html) 314 for all the specified packages and their dependencies by directing a browser of ··· 318 319 After you've marveled enough at that try adding the following to your 320 `~/.ghc/ghci.conf` 321 + ``` 322 + :def hoogle \s -> return $ ":! hoogle search -cl --count=15 \"" ++ s ++ "\"" 323 + :def doc \s -> return $ ":! hoogle search -cl --info \"" ++ s ++ "\"" 324 + ``` 325 and test it by typing into `ghci`: 326 + ``` 327 + :hoogle a -> a 328 + :doc a -> a 329 + ``` 330 331 Be sure to note the links to `haddock` files in the output. With any modern and 332 properly configured terminal emulator you can just click those links to 333 navigate there. 334 335 Finally, you can run 336 + ```shell 337 + hoogle server -p 8080 338 + ``` 339 and navigate to http://localhost:8080/ for your own local 340 [Hoogle](https://www.haskell.org/hoogle/). Note, however, that Firefox and 341 possibly other browsers disallow navigation from `http:` to `file:` URIs for ··· 350 automatically select the right version of GHC and other build tools to build, 351 test and execute apps in an existing project downloaded from somewhere on the 352 Internet. Pass the `--nix` flag to any `stack` command to do so, e.g. 353 + ```shell 354 + git clone --recursive http://github.com/yesodweb/wai 355 + cd wai 356 + stack --nix build 357 + ``` 358 359 If you want `stack` to use Nix by default, you can add a `nix` section to the 360 `stack.yaml` file, as explained in the [Stack documentation][stack-nix-doc]. For 361 example: 362 + ```yaml 363 + nix: 364 + enable: true 365 + packages: [pkgconfig zeromq zlib] 366 + ``` 367 368 The example configuration snippet above tells Stack to create an ad hoc 369 environment for `nix-shell` as in the below section, in which the `pkgconfig`, ··· 374 environments might need to expose Nixpkgs packages compiled in a certain way, or 375 with extra environment variables. In these cases, you'll need a `shell` field 376 instead of `packages`: 377 + ```yaml 378 + nix: 379 + enable: true 380 + shell-file: shell.nix 381 + ``` 382 383 For more on how to write a `shell.nix` file see the below section. You'll need 384 to express a derivation. Note that Nixpkgs ships with a convenience wrapper ··· 387 as `mkDerivation` can be provided. For example, to build a Stack project that 388 including packages that link against a version of the R library compiled with 389 special options turned on: 390 + ```nix 391 + with (import <nixpkgs> { }); 392 393 + let R = pkgs.R.override { enableStrictBarrier = true; }; 394 + in 395 + haskell.lib.buildStackProject { 396 + name = "HaskellR"; 397 + buildInputs = [ R zeromq zlib ]; 398 + } 399 + ``` 400 401 You can select a particular GHC version to compile with by setting the 402 `ghc` attribute as an argument to `buildStackProject`. Better yet, let 403 Stack choose what GHC version it wants based on the snapshot specified 404 in `stack.yaml` (only works with Stack >= 1.1.3): 405 + ```nix 406 + {nixpkgs ? import <nixpkgs> { }, ghc ? nixpkgs.ghc}: 407 408 + with nixpkgs; 409 410 + let R = pkgs.R.override { enableStrictBarrier = true; }; 411 + in 412 + haskell.lib.buildStackProject { 413 + name = "HaskellR"; 414 + buildInputs = [ R zeromq zlib ]; 415 + inherit ghc; 416 + } 417 + ``` 418 419 [stack-nix-doc]: http://docs.haskellstack.org/en/stable/nix_integration.html 420 ··· 422 423 The easiest way to create an ad hoc development environment is to run 424 `nix-shell` with the appropriate GHC environment given on the command-line: 425 + ```shell 426 + nix-shell -p "haskellPackages.ghcWithPackages (pkgs: with pkgs; [mtl pandoc])" 427 + ``` 428 429 For more sophisticated use-cases, however, it's more convenient to save the 430 desired configuration in a file called `shell.nix` that looks like this: 431 + ```nix 432 + { nixpkgs ? import <nixpkgs> {}, compiler ? "ghc7102" }: 433 + let 434 + inherit (nixpkgs) pkgs; 435 + ghc = pkgs.haskell.packages.${compiler}.ghcWithPackages (ps: with ps; [ 436 + monad-par mtl 437 + ]); 438 + in 439 + pkgs.stdenv.mkDerivation { 440 + name = "my-haskell-env-0"; 441 + buildInputs = [ ghc ]; 442 + shellHook = "eval $(egrep ^export ${ghc}/bin/ghc)"; 443 + } 444 + ``` 445 446 Now run `nix-shell` --- or even `nix-shell --pure` --- to enter a shell 447 environment that has the appropriate compiler in `$PATH`. If you use `--pure`, ··· 457 environment suitable for compiling that particular package. If you'd like to 458 hack the `lens` library, for example, then you just have to check out the 459 source code and enter the appropriate environment: 460 + ``` 461 + $ cabal get lens-4.11 && cd lens-4.11 462 + Downloading lens-4.11... 463 + Unpacking to lens-4.11/ 464 465 + $ nix-shell "<nixpkgs>" -A haskellPackages.lens.env 466 + [nix-shell:/tmp/lens-4.11]$ 467 + ``` 468 469 At point, you can run `cabal configure`, `cabal build`, and all the other 470 development commands. Note that you need `cabal-install` installed in your ··· 483 For example, let's assume that you're working on a private project called 484 `foo`. To generate a Nix build expression for it, change into the project's 485 top-level directory and run the command: 486 + ```shell 487 + cabal2nix . > foo.nix 488 + ``` 489 Then write the following snippet into a file called `default.nix`: 490 + ```nix 491 + { nixpkgs ? import <nixpkgs> {}, compiler ? "ghc7102" }: 492 + nixpkgs.pkgs.haskell.packages.${compiler}.callPackage ./foo.nix { } 493 + ``` 494 495 Finally, store the following code in a file called `shell.nix`: 496 + ```nix 497 + { nixpkgs ? import <nixpkgs> {}, compiler ? "ghc7102" }: 498 + (import ./default.nix { inherit nixpkgs compiler; }).env 499 + ``` 500 501 At this point, you can run `nix-build` to have Nix compile your project and 502 install it into a Nix store path. The local directory will contain a symlink ··· 512 513 If your package does not depend on any system-level libraries, then it's 514 sufficient to run 515 + ```shell 516 + nix-shell --command "cabal configure" 517 + ``` 518 once to set up your build. `cabal-install` determines the absolute paths to all 519 resources required for the build and writes them into a config file in the 520 `dist/` directory. Once that's done, you can run `cabal build` and any other ··· 528 up a `default.nix` and `shell.nix` file manually, then you can use the 529 `--shell` flag offered by `cabal2nix` to have it generate a stand-alone 530 `nix-shell` environment for you. With that feature, running 531 + ```shell 532 + cabal2nix --shell . > shell.nix 533 + nix-shell --command "cabal configure" 534 + ``` 535 is usually enough to set up a build environment for any given Haskell package. 536 You can even use that generated file to run `nix-build`, too: 537 + ```shell 538 + nix-build shell.nix 539 + ``` 540 541 ### How to build projects that depend on each other 542 ··· 545 for the dependency resolution performed by `callPackage`. First of all, change 546 into each of your projects top-level directories and generate a `default.nix` 547 file with `cabal2nix`: 548 + ```shell 549 + cd ~/src/foo && cabal2nix . > default.nix 550 + cd ~/src/bar && cabal2nix . > default.nix 551 + ``` 552 Then edit your `~/.config/nixpkgs/config.nix` file to register those builds in the 553 default Haskell package set: 554 + ```nix 555 + { 556 + packageOverrides = super: let self = super.pkgs; in 557 + { 558 + haskellPackages = super.haskellPackages.override { 559 + overrides = self: super: { 560 + foo = self.callPackage ../src/foo {}; 561 + bar = self.callPackage ../src/bar {}; 562 + }; 563 + }; 564 + }; 565 + } 566 + ``` 567 Once that's accomplished, `nix-env -f "<nixpkgs>" -qA haskellPackages` will 568 show your packages like any other package from Hackage, and you can build them 569 + ```shell 570 + nix-build "<nixpkgs>" -A haskellPackages.foo 571 + ``` 572 or enter an interactive shell environment suitable for building them: 573 + ```shell 574 + nix-shell "<nixpkgs>" -A haskellPackages.bar.env 575 + ``` 576 577 ## Miscellaneous Topics 578 ··· 583 feature is to replace the default `mkDerivation` function with one that enables 584 library profiling for all packages. To accomplish that, add configure the 585 following snippet in your `~/.config/nixpkgs/config.nix` file: 586 + ```nix 587 + { 588 + packageOverrides = super: let self = super.pkgs; in 589 + { 590 + profiledHaskellPackages = self.haskellPackages.override { 591 + overrides = self: super: { 592 + mkDerivation = args: super.mkDerivation (args // { 593 + enableLibraryProfiling = true; 594 + }); 595 }; 596 + }; 597 + }; 598 + } 599 + ``` 600 Then, replace instances of `haskellPackages` in the `cabal2nix`-generated 601 `default.nix` or `shell.nix` files with `profiledHaskellPackages`. 602 ··· 608 7.8.4 cannot compile that binary. Now, one way to solve that problem is to 609 register an older version of `ghc-events` in the 7.8.x-specific package set. 610 The first step is to generate Nix build instructions with `cabal2nix`: 611 + ```shell 612 + cabal2nix cabal://ghc-events-0.4.3.0 > ~/.nixpkgs/ghc-events-0.4.3.0.nix 613 + ``` 614 Then add the override in `~/.config/nixpkgs/config.nix`: 615 + ```nix 616 + { 617 + packageOverrides = super: let self = super.pkgs; in 618 + { 619 + haskell = super.haskell // { 620 + packages = super.haskell.packages // { 621 + ghc784 = super.haskell.packages.ghc784.override { 622 + overrides = self: super: { 623 + ghc-events = self.callPackage ./ghc-events-0.4.3.0.nix {}; 624 }; 625 }; 626 }; 627 + }; 628 + }; 629 + } 630 + ``` 631 632 This code is a little crazy, no doubt, but it's necessary because the intuitive 633 version 634 + ```nix 635 + { # ... 636 637 + haskell.packages.ghc784 = super.haskell.packages.ghc784.override { 638 + overrides = self: super: { 639 + ghc-events = self.callPackage ./ghc-events-0.4.3.0.nix {}; 640 }; 641 + }; 642 + } 643 + ``` 644 doesn't do what we want it to: that code replaces the `haskell` package set in 645 Nixpkgs with one that contains only one entry,`packages`, which contains only 646 one entry `ghc784`. This override loses the `haskell.compiler` set, and it ··· 650 651 Once it's accomplished, however, we can install a variant of `ghc-events` 652 that's compiled with GHC 7.8.4: 653 + ```shell 654 + nix-env -f "<nixpkgs>" -iA haskell.packages.ghc784.ghc-events 655 + ``` 656 Unfortunately, it turns out that this build fails again while executing the 657 test suite! Apparently, the release archive on Hackage is missing some data 658 files that the test suite requires, so we cannot run it. We accomplish that by 659 re-generating the Nix expression with the `--no-check` flag: 660 + ```shell 661 + cabal2nix --no-check cabal://ghc-events-0.4.3.0 > ~/.nixpkgs/ghc-events-0.4.3.0.nix 662 + ``` 663 Now the builds succeeds. 664 665 Of course, in the concrete example of `ghc-events` this whole exercise is not ··· 674 675 GHC and distributed build farms don't get along well: 676 677 + - https://ghc.haskell.org/trac/ghc/ticket/4012 678 679 When you see an error like this one 680 + ``` 681 + package foo-0.7.1.0 is broken due to missing package 682 + text-1.2.0.4-98506efb1b9ada233bb5c2b2db516d91 683 + ``` 684 then you have to download and re-install `foo` and all its dependents from 685 scratch: 686 + ```shell 687 + nix-store -q --referrers /nix/store/*-haskell-text-1.2.0.4 \ 688 + | xargs -L 1 nix-store --repair-path 689 + ``` 690 691 If you're using additional Hydra servers other than `hydra.nixos.org`, then it 692 might be necessary to purge the local caches that store data from those 693 machines to disable these binary channels for the duration of the previous 694 command, i.e. by running: 695 + ```shell 696 + rm /nix/var/nix/binary-cache-v3.sqlite 697 + rm /nix/var/nix/manifests/* 698 + rm /nix/var/nix/channel-cache/* 699 + ``` 700 701 ### How to use the Haste Haskell-to-Javascript transpiler 702 703 Open a shell with `haste-compiler` and `haste-cabal-install` (you don't actually need 704 `node`, but it can be useful to test stuff): 705 + ```shell 706 + nix-shell \ 707 + -p "haskellPackages.ghcWithPackages (self: with self; [haste-cabal-install haste-compiler])" \ 708 + -p nodejs 709 + ``` 710 You may not need the following step but if `haste-boot` fails to compile all the 711 packages it needs, this might do the trick 712 + ```shell 713 + haste-cabal update 714 + ``` 715 `haste-boot` builds a set of core libraries so that they can be used from Javascript 716 transpiled programs: 717 + ```shell 718 + haste-boot 719 + ``` 720 Transpile and run a "Hello world" program: 721 + ``` 722 + $ echo 'module Main where main = putStrLn "Hello world"' > hello-world.hs 723 + $ hastec --onexec hello-world.hs 724 + $ node hello-world.js 725 + Hello world 726 + ``` 727 728 ### Builds on Darwin fail with `math.h` not found 729 730 Users of GHC on Darwin have occasionally reported that builds fail, because the 731 compiler complains about a missing include file: 732 + ``` 733 + fatal error: 'math.h' file not found 734 + ``` 735 The issue has been discussed at length in [ticket 736 6390](https://github.com/NixOS/nixpkgs/issues/6390), and so far no good 737 solution has been proposed. As a work-around, users who run into this problem 738 can configure the environment variables 739 + ```shell 740 + export NIX_CFLAGS_COMPILE="-idirafter /usr/include" 741 + export NIX_CFLAGS_LINK="-L/usr/lib" 742 + ``` 743 in their `~/.bashrc` file to avoid the compiler error. 744 745 ### Builds using Stack complain about missing system libraries 746 747 + ``` 748 + -- While building package zlib-0.5.4.2 using: 749 + runhaskell -package=Cabal-1.22.4.0 -clear-package-db [... lots of flags ...] 750 + Process exited with code: ExitFailure 1 751 + Logs have been written to: /home/foo/src/stack-ide/.stack-work/logs/zlib-0.5.4.2.log 752 753 + Configuring zlib-0.5.4.2... 754 + Setup.hs: Missing dependency on a foreign library: 755 + * Missing (or bad) header file: zlib.h 756 + This problem can usually be solved by installing the system package that 757 + provides this library (you may need the "-dev" version). If the library is 758 + already installed but in a non-standard location then you can use the flags 759 + --extra-include-dirs= and --extra-lib-dirs= to specify where it is. 760 + If the header file does exist, it may contain errors that are caught by the C 761 + compiler at the preprocessing stage. In this case you can re-run configure 762 + with the verbosity flag -v3 to see the error messages. 763 + ``` 764 765 When you run the build inside of the nix-shell environment, the system 766 + is configured to find `libz.so` without any special flags -- the compiler 767 and linker "just know" how to find it. Consequently, Cabal won't record 768 + any search paths for `libz.so` in the package description, which means 769 that the package works fine inside of nix-shell, but once you leave the 770 shell the shared object can no longer be found. That issue is by no 771 means specific to Stack: you'll have that problem with any other ··· 774 775 You can remedy this issue in several ways. The easiest is to add a `nix` section 776 to the `stack.yaml` like the following: 777 + ```yaml 778 + nix: 779 + enable: true 780 + packages: [ zlib ] 781 + ``` 782 783 + Stack's Nix support knows to add `${zlib.out}/lib` and `${zlib.dev}/include` 784 + as an `--extra-lib-dirs` and `extra-include-dirs`, respectively. 785 + Alternatively, you can achieve the same effect by hand. First of all, run 786 + ``` 787 + $ nix-build --no-out-link "<nixpkgs>" -A zlib 788 + /nix/store/alsvwzkiw4b7ip38l4nlfjijdvg3fvzn-zlib-1.2.8 789 + ``` 790 to find out the store path of the system's zlib library. Now, you can 791 792 + 1. add that path (plus a "/lib" suffix) to your `$LD_LIBRARY_PATH` 793 + environment variable to make sure your system linker finds `libz.so` 794 + automatically. It's no pretty solution, but it will work. 795 796 + 2. As a variant of (1), you can also install any number of system 797 + libraries into your user's profile (or some other profile) and point 798 + `$LD_LIBRARY_PATH` to that profile instead, so that you don't have to 799 + list dozens of those store paths all over the place. 800 801 + 3. The solution I prefer is to call stack with an appropriate 802 + --extra-lib-dirs flag like so: 803 + ```shell 804 + stack --extra-lib-dirs=/nix/store/alsvwzkiw4b7ip38l4nlfjijdvg3fvzn-zlib-1.2.8/lib build 805 + ``` 806 807 + Typically, you'll need `--extra-include-dirs` as well. It's possible 808 + to add those flag to the project's `stack.yaml` or your user's 809 + global `~/.stack/global/stack.yaml` file so that you don't have to 810 + specify them manually every time. But again, you're likely better off 811 + using Stack's Nix support instead. 812 813 + The same thing applies to `cabal configure`, of course, if you're 814 + building with `cabal-install` instead of Stack. 815 816 ### Creating statically linked binaries 817 818 There are two levels of static linking. The first option is to configure the 819 build with the Cabal flag `--disable-executable-dynamic`. In Nix expressions, 820 this can be achieved by setting the attribute: 821 + ``` 822 + enableSharedExecutables = false; 823 + ``` 824 That gives you a binary with statically linked Haskell libraries and 825 dynamically linked system libraries. 826 827 To link both Haskell libraries and system libraries statically, the additional 828 flags `--ghc-option=-optl=-static --ghc-option=-optl=-pthread` need to be used. 829 In Nix, this is accomplished with: 830 + ``` 831 + configureFlags = [ "--ghc-option=-optl=-static" "--ghc-option=-optl=-pthread" ]; 832 + ``` 833 834 + It's important to realize, however, that most system libraries in Nix are 835 + built as shared libraries only, i.e. there is just no static library 836 + available that Cabal could link! 837 838 ### Building GHC with integer-simple 839 ··· 843 [integer-gmp](http://hackage.haskell.org/package/integer-gmp) package. 844 845 A potential problem with this is that GMP is licensed under the 846 + [GNU Lesser General Public License (LGPL)](http://www.gnu.org/copyleft/lesser.html), 847 a kind of "copyleft" license. According to the terms of the LGPL, paragraph 5, 848 you may distribute a program that is designed to be compiled and dynamically 849 linked with the library under the terms of your choice (i.e., commercially) but ··· 856 programs compiled with GHC because most distributions (and builds) of GHC use 857 static libraries. (Dynamic libraries are currently distributed only for OS X.) 858 The LGPL licensing situation may be worse: even though 859 + [The Glasgow Haskell Compiler License](https://www.haskell.org/ghc/license) 860 is essentially a "free software" license (BSD3), according to 861 paragraph 2 of the LGPL, GHC must be distributed under the terms of the LGPL! 862 ··· 867 To get a GHC compiler build with `integer-simple` instead of `integer-gmp` use 868 the attribute: `haskell.compiler.integer-simple."${ghcVersion}"`. 869 For example: 870 + ``` 871 + $ nix-build -E '(import <nixpkgs> {}).haskell.compiler.integer-simple.ghc802' 872 + ... 873 + $ result/bin/ghc-pkg list | grep integer 874 + integer-simple-0.1.1.1 875 + ``` 876 The following command displays the complete list of GHC compilers build with `integer-simple`: 877 + ``` 878 + $ nix-env -f "<nixpkgs>" -qaP -A haskell.compiler.integer-simple 879 + haskell.compiler.integer-simple.ghc7102 ghc-7.10.2 880 + haskell.compiler.integer-simple.ghc7103 ghc-7.10.3 881 + haskell.compiler.integer-simple.ghc722 ghc-7.2.2 882 + haskell.compiler.integer-simple.ghc742 ghc-7.4.2 883 + haskell.compiler.integer-simple.ghc783 ghc-7.8.3 884 + haskell.compiler.integer-simple.ghc784 ghc-7.8.4 885 + haskell.compiler.integer-simple.ghc801 ghc-8.0.1 886 + haskell.compiler.integer-simple.ghc802 ghc-8.0.2 887 + haskell.compiler.integer-simple.ghcHEAD ghc-8.1.20170106 888 + ``` 889 890 To get a package set supporting `integer-simple` use the attribute: 891 `haskell.packages.integer-simple."${ghcVersion}"`. For example 892 use the following to get the `scientific` package build with `integer-simple`: 893 + ```shell 894 + nix-build -A haskell.packages.integer-simple.ghc802.scientific 895 + ``` 896 897 ## Other resources 898 899 + - The Youtube video [Nix Loves Haskell](https://www.youtube.com/watch?v=BsBhi_r-OeE) 900 + provides an introduction into Haskell NG aimed at beginners. The slides are 901 + available at http://cryp.to/nixos-meetup-3-slides.pdf and also -- in a form 902 + ready for cut & paste -- at 903 + https://github.com/NixOS/cabal2nix/blob/master/doc/nixos-meetup-3-slides.md. 904 905 + - Another Youtube video is [Escaping Cabal Hell with Nix](https://www.youtube.com/watch?v=mQd3s57n_2Y), 906 + which discusses the subject of Haskell development with Nix but also provides 907 + a basic introduction to Nix as well, i.e. it's suitable for viewers with 908 + almost no prior Nix experience. 909 910 + - Oliver Charles wrote a very nice [Tutorial how to develop Haskell packages with Nix](http://wiki.ocharles.org.uk/Nix). 911 912 + - The *Journey into the Haskell NG infrastructure* series of postings 913 + describe the new Haskell infrastructure in great detail: 914 915 + - [Part 1](http://lists.science.uu.nl/pipermail/nix-dev/2015-January/015591.html) 916 + explains the differences between the old and the new code and gives 917 + instructions how to migrate to the new setup. 918 919 + - [Part 2](http://lists.science.uu.nl/pipermail/nix-dev/2015-January/015608.html) 920 + looks in-depth at how to tweak and configure your setup by means of 921 + overrides. 922 923 + - [Part 3](http://lists.science.uu.nl/pipermail/nix-dev/2015-April/016912.html) 924 + describes the infrastructure that keeps the Haskell package set in Nixpkgs 925 + up-to-date.