···11Nixpkgs distributes build instructions for all Haskell packages registered on
12[Hackage](http://hackage.haskell.org/), but strangely enough normal Nix package
13lookups 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
01920The Haskell package set is not registered in the top-level namespace because it
21is *huge*. If all Haskell packages were visible to these commands, then
22name-based search/install operations would be much slower than they are now. We
23avoided that by keeping all Haskell-related packages in a separate attribute
24set 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 ...]
03536To install any of those packages into your profile, refer to them by their
37attribute path (first column):
38-39- $ nix-env -f "<nixpkgs>" -iA haskellPackages.Allure ...
04041The attribute path of any Haskell packages corresponds to the name of that
42particular package on Hackage: the package `cabal-install` has the attribute
···58reach 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`
60without 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
06566On NixOS, for example, Nixpkgs does *not* exist in the top-level namespace by
67default. To figure out the proper attribute path, it's easiest to query for the
68path of a well-known Nixpkgs package, i.e.:
69-70- $ nix-env -qaP coreutils
71- nixos.coreutils coreutils-8.23
07273If your system responds like that (most NixOS installations will), then the
74attribute path to `haskellPackages` is `nixos.haskellPackages`. Thus, if you
75want to use `nix-env` without giving an explicit `-f` flag, then that's the way
76to do it:
77-78- $ nix-env -qaP -A nixos.haskellPackages
79- $ nix-env -iA nixos.haskellPackages.cabal-install
08081Our current default compiler is GHC 7.10.x and the `haskellPackages` set
82contains packages built with that particular version. Nixpkgs contains the
83latest major release of every GHC since 6.10.4, however, and there is a whole
84family of package sets available that defines Hackage packages built with each
85of those compilers, too:
86-87- $ nix-env -f "<nixpkgs>" -qaP -A haskell.packages.ghc6123
88- $ nix-env -f "<nixpkgs>" -qaP -A haskell.packages.ghc763
08990The name `haskellPackages` is really just a synonym for
91`haskell.packages.ghc7102`, because we prefer that package set internally and
92recommend it to our users as their default choice, but ultimately you are free
93to compile your Haskell packages with any GHC version you please. The following
94command 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
0110111We have no package sets for `jhc` or `uhc` yet, unfortunately, but for every
112version of GHC listed above, there exists a package set based on that compiler.
···121of the tools `cabal-install` and `stack`. We saw in section
122[How to install Haskell packages] how you can install those programs into your
123user profile:
124-125- $ nix-env -f "<nixpkgs>" -iA haskellPackages.ghc haskellPackages.cabal-install
0126127Instead of the default package set `haskellPackages`, you can also use the more
128precise name `haskell.compiler.ghc7102`, which has the advantage that it refers
···131132Once you've made those tools available in `$PATH`, it's possible to build
133Hackage 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
0139140If you enjoy working with Cabal sandboxes, then that's entirely possible too:
141just execute the command
142-143- $ cabal sandbox init
144-145before installing the required dependencies.
146147The `nix-shell` utility makes it easy to switch to a different compiler
148version; just enter the Nix shell environment with the command
149-150- $ nix-shell -p haskell.compiler.ghc784
151-152to 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
154per-project. It uses `nix-shell` under the hood when Nix support is turned on.
···159a project that doesn't depend on any additional system libraries outside of GHC,
160then it's even sufficient to just run the `cabal configure` command inside of
161the shell:
162-163- $ nix-shell -p haskell.compiler.ghc784 --command "cabal configure"
0164165Afterwards, all other commands like `cabal build` work just fine in any shell
166environment, because the configure phase recorded the absolute paths to all
···187GHC. For example, the Nix expression `ghcWithPackages (pkgs: [pkgs.mtl])`
188generates a copy of GHC that has the `mtl` library registered in addition to
189its normal core packages:
00190191- $ 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
196197This function allows users to define their own development environment by means
198of 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-213it's possible to install that compiler with `nix-env -f "<nixpkgs>" -iA
214myHaskellEnv`. If you'd like to switch that development environment to a
215different version of GHC, just replace the `ghc7102` bit in the previous
···221The generated `ghc` program is a wrapper script that re-directs the real
222GHC executable to use a new `lib` directory --- one that we specifically
223constructed 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" "$@"
0232233The variables `$NIX_GHC`, `$NIX_GHCPKG`, etc. point to the *new* store path
234`ghcWithPackages` constructed specifically for this environment. The last line
···248To make sure that mechanism works properly all the time, we recommend that you
249set those variables to meaningful values in your shell environment, too, i.e.
250by 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
0255256If you are certain that you'll use only one GHC environment which is located in
257your user profile, then you can use the following code, too, which has the
258advantage that it doesn't contain any paths from the Nix store, i.e. those
259settings always remain valid even if a `nix-env -u` operation updates the GHC
260environment 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
0268269### How to install a compiler with libraries, hoogle and documentation indexes
270···280long and scary.
281282For 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-297allows one to browse module documentation index [not too dissimilar to
298this](https://downloads.haskell.org/~ghc/latest/docs/html/libraries/index.html)
299for all the specified packages and their dependencies by directing a browser of
···303304After 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-310and test it by typing into `ghci`:
311-312- :hoogle a -> a
313- :doc a -> a
0314315Be sure to note the links to `haddock` files in the output. With any modern and
316properly configured terminal emulator you can just click those links to
317navigate there.
318319Finally, you can run
320-321- hoogle server -p 8080
322-323and navigate to http://localhost:8080/ for your own local
324[Hoogle](https://www.haskell.org/hoogle/). Note, however, that Firefox and
325possibly other browsers disallow navigation from `http:` to `file:` URIs for
···334automatically select the right version of GHC and other build tools to build,
335test and execute apps in an existing project downloaded from somewhere on the
336Internet. 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
0341342If 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
344example:
345-346- nix:
347- enable: true
348- packages: [pkgconfig zeromq zlib]
0349350The example configuration snippet above tells Stack to create an ad hoc
351environment for `nix-shell` as in the below section, in which the `pkgconfig`,
···356environments might need to expose Nixpkgs packages compiled in a certain way, or
357with extra environment variables. In these cases, you'll need a `shell` field
358instead of `packages`:
359-360- nix:
361- enable: true
362- shell-file: shell.nix
0363364For more on how to write a `shell.nix` file see the below section. You'll need
365to express a derivation. Note that Nixpkgs ships with a convenience wrapper
···368as `mkDerivation` can be provided. For example, to build a Stack project that
369including packages that link against a version of the R library compiled with
370special options turned on:
00371372- 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- }
380381You can select a particular GHC version to compile with by setting the
382`ghc` attribute as an argument to `buildStackProject`. Better yet, let
383Stack choose what GHC version it wants based on the snapshot specified
384in `stack.yaml` (only works with Stack >= 1.1.3):
00385386- {nixpkgs ? import <nixpkgs> { }, ghc ? nixpkgs.ghc}:
387388- 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- }
397398[stack-nix-doc]: http://docs.haskellstack.org/en/stable/nix_integration.html
399···401402The 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])"
0406407For more sophisticated use-cases, however, it's more convenient to save the
408desired 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- }
0422423Now run `nix-shell` --- or even `nix-shell --pure` --- to enter a shell
424environment that has the appropriate compiler in `$PATH`. If you use `--pure`,
···434environment suitable for compiling that particular package. If you'd like to
435hack the `lens` library, for example, then you just have to check out the
436source code and enter the appropriate environment:
0000437438- $ 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]$
444445At point, you can run `cabal configure`, `cabal build`, and all the other
446development commands. Note that you need `cabal-install` installed in your
···459For 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
461top-level directory and run the command:
462-463- $ cabal2nix . >foo.nix
464-465Then 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 { }
0469470Finally, 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
0474475At this point, you can run `nix-build` to have Nix compile your project and
476install it into a Nix store path. The local directory will contain a symlink
···486487If your package does not depend on any system-level libraries, then it's
488sufficient to run
489-490- $ nix-shell --command "cabal configure"
491-492once to set up your build. `cabal-install` determines the absolute paths to all
493resources 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
···502up 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-509is usually enough to set up a build environment for any given Haskell package.
510You can even use that generated file to run `nix-build`, too:
511-512- $ nix-build shell.nix
0513514### How to build projects that depend on each other
515···518for the dependency resolution performed by `callPackage`. First of all, change
519into each of your projects top-level directories and generate a `default.nix`
520file with `cabal2nix`:
521-522- $ cd ~/src/foo && cabal2nix . >default.nix
523- $ cd ~/src/bar && cabal2nix . >default.nix
524-525Then edit your `~/.config/nixpkgs/config.nix` file to register those builds in the
526default 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-540Once that's accomplished, `nix-env -f "<nixpkgs>" -qA haskellPackages` will
541show your packages like any other package from Hackage, and you can build them
542-543- $ nix-build "<nixpkgs>" -A haskellPackages.foo
544-545or enter an interactive shell environment suitable for building them:
546-547- $ nix-shell "<nixpkgs>" -A haskellPackages.bar.env
0548549## Miscellaneous Topics
550···555feature is to replace the default `mkDerivation` function with one that enables
556library profiling for all packages. To accomplish that, add configure the
557following 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-00572Then, replace instances of `haskellPackages` in the `cabal2nix`-generated
573`default.nix` or `shell.nix` files with `profiledHaskellPackages`.
574···5807.8.4 cannot compile that binary. Now, one way to solve that problem is to
581register an older version of `ghc-events` in the 7.8.x-specific package set.
582The 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-586Then 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- }
000602603This code is a little crazy, no doubt, but it's necessary because the intuitive
604version
00605606- 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-00612doesn't do what we want it to: that code replaces the `haskell` package set in
613Nixpkgs with one that contains only one entry,`packages`, which contains only
614one entry `ghc784`. This override loses the `haskell.compiler` set, and it
···618619Once it's accomplished, however, we can install a variant of `ghc-events`
620that's compiled with GHC 7.8.4:
621-622- nix-env -f "<nixpkgs>" -iA haskell.packages.ghc784.ghc-events
623-624Unfortunately, it turns out that this build fails again while executing the
625test suite! Apparently, the release archive on Hackage is missing some data
626files that the test suite requires, so we cannot run it. We accomplish that by
627re-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-631Now the builds succeeds.
632633Of course, in the concrete example of `ghc-events` this whole exercise is not
···642643GHC and distributed build farms don't get along well:
644645- https://ghc.haskell.org/trac/ghc/ticket/4012
646647When 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-652then you have to download and re-install `foo` and all its dependents from
653scratch:
654-655- # nix-store -q --referrers /nix/store/*-haskell-text-1.2.0.4 \
656- | xargs -L 1 nix-store --repair-path
0657658If you're using additional Hydra servers other than `hydra.nixos.org`, then it
659might be necessary to purge the local caches that store data from those
660machines to disable these binary channels for the duration of the previous
661command, 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/*
0666667### How to use the Haste Haskell-to-Javascript transpiler
668669Open 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-00674You may not need the following step but if `haste-boot` fails to compile all the
675packages 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
680transpiled programs:
681-682- $ haste-boot
683-684Transpile 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
0690691### Builds on Darwin fail with `math.h` not found
692693Users of GHC on Darwin have occasionally reported that builds fail, because the
694compiler complains about a missing include file:
695-696- fatal error: 'math.h' file not found
697-698The issue has been discussed at length in [ticket
6996390](https://github.com/NixOS/nixpkgs/issues/6390), and so far no good
700solution has been proposed. As a work-around, users who run into this problem
701can configure the environment variables
702-703- export NIX_CFLAGS_COMPILE="-idirafter /usr/include"
704- export NIX_CFLAGS_LINK="-L/usr/lib"
705-706in their `~/.bashrc` file to avoid the compiler error.
707708### Builds using Stack complain about missing system libraries
709710- -- 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
0714715- 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.
0725726When 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
728and 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
730that the package works fine inside of nix-shell, but once you leave the
731shell the shared object can no longer be found. That issue is by no
732means specific to Stack: you'll have that problem with any other
···735736You can remedy this issue in several ways. The easiest is to add a `nix` section
737to the `stack.yaml` like the following:
00000738739- 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-750to find out the store path of the system's zlib library. Now, you can
751752-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.
755756-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:
763764- $ stack --extra-lib-dirs=/nix/store/alsvwzkiw4b7ip38l4nlfjijdvg3fvzn-zlib-1.2.8/lib build
0000765766- 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.
771772- The same thing applies to `cabal configure`, of course, if you're
773- building with `cabal-install` instead of Stack.
774775### Creating statically linked binaries
776777There are two levels of static linking. The first option is to configure the
778build with the Cabal flag `--disable-executable-dynamic`. In Nix expressions,
779this can be achieved by setting the attribute:
780-781- enableSharedExecutables = false;
782-783That gives you a binary with statically linked Haskell libraries and
784dynamically linked system libraries.
785786To link both Haskell libraries and system libraries statically, the additional
787flags `--ghc-option=-optl=-static --ghc-option=-optl=-pthread` need to be used.
788In Nix, this is accomplished with:
000789790- 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!
795796### Building GHC with integer-simple
797···801[integer-gmp](http://hackage.haskell.org/package/integer-gmp) package.
802803A 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),
805a kind of "copyleft" license. According to the terms of the LGPL, paragraph 5,
806you may distribute a program that is designed to be compiled and dynamically
807linked with the library under the terms of your choice (i.e., commercially) but
···814programs compiled with GHC because most distributions (and builds) of GHC use
815static libraries. (Dynamic libraries are currently distributed only for OS X.)
816The LGPL licensing situation may be worse: even though
817-[The Glasgow Haskell Compiler License](https://www.haskell.org/ghc/license)
818is essentially a "free software" license (BSD3), according to
819paragraph 2 of the LGPL, GHC must be distributed under the terms of the LGPL!
820···825To get a GHC compiler build with `integer-simple` instead of `integer-gmp` use
826the attribute: `haskell.compiler.integer-simple."${ghcVersion}"`.
827For 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-834The 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
0846847To get a package set supporting `integer-simple` use the attribute:
848`haskell.packages.integer-simple."${ghcVersion}"`. For example
849use the following to get the `scientific` package build with `integer-simple`:
850-851- $ nix-build -A haskell.packages.integer-simple.ghc802.scientific
852-853854## Other resources
855856-- 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.
861862-- 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.
866867-- Oliver Charles wrote a very nice [Tutorial how to develop Haskell packages with Nix](http://wiki.ocharles.org.uk/Nix).
868869-- The *Journey into the Haskell NG infrastructure* series of postings
870- describe the new Haskell infrastructure in great detail:
871872- - [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.
875876- - [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.
879880- - [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.
···11Nixpkgs distributes build instructions for all Haskell packages registered on
12[Hackage](http://hackage.haskell.org/), but strangely enough normal Nix package
13lookups 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+```
2021The Haskell package set is not registered in the top-level namespace because it
22is *huge*. If all Haskell packages were visible to these commands, then
23name-based search/install operations would be much slower than they are now. We
24avoided that by keeping all Haskell-related packages in a separate attribute
25set 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+```
3738To install any of those packages into your profile, refer to them by their
39attribute path (first column):
40+```shell
41+nix-env -f "<nixpkgs>" -iA haskellPackages.Allure ...
42+```
4344The attribute path of any Haskell packages corresponds to the name of that
45particular package on Hackage: the package `cabal-install` has the attribute
···61reach 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`
63without 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+```
6970On NixOS, for example, Nixpkgs does *not* exist in the top-level namespace by
71default. To figure out the proper attribute path, it's easiest to query for the
72path of a well-known Nixpkgs package, i.e.:
73+```
74+$ nix-env -qaP coreutils
75+nixos.coreutils coreutils-8.23
76+```
7778If your system responds like that (most NixOS installations will), then the
79attribute path to `haskellPackages` is `nixos.haskellPackages`. Thus, if you
80want to use `nix-env` without giving an explicit `-f` flag, then that's the way
81to do it:
82+```shell
83+nix-env -qaP -A nixos.haskellPackages
84+nix-env -iA nixos.haskellPackages.cabal-install
85+```
8687Our current default compiler is GHC 7.10.x and the `haskellPackages` set
88contains packages built with that particular version. Nixpkgs contains the
89latest major release of every GHC since 6.10.4, however, and there is a whole
90family of package sets available that defines Hackage packages built with each
91of 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+```
9697The name `haskellPackages` is really just a synonym for
98`haskell.packages.ghc7102`, because we prefer that package set internally and
99recommend it to our users as their default choice, but ultimately you are free
100to compile your Haskell packages with any GHC version you please. The following
101command 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+```
118119We have no package sets for `jhc` or `uhc` yet, unfortunately, but for every
120version of GHC listed above, there exists a package set based on that compiler.
···129of the tools `cabal-install` and `stack`. We saw in section
130[How to install Haskell packages] how you can install those programs into your
131user profile:
132+```shell
133+nix-env -f "<nixpkgs>" -iA haskellPackages.ghc haskellPackages.cabal-install
134+```
135136Instead of the default package set `haskellPackages`, you can also use the more
137precise name `haskell.compiler.ghc7102`, which has the advantage that it refers
···140141Once you've made those tools available in `$PATH`, it's possible to build
142Hackage 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+```
149150If you enjoy working with Cabal sandboxes, then that's entirely possible too:
151just execute the command
152+```shell
153+cabal sandbox init
154+```
155before installing the required dependencies.
156157The `nix-shell` utility makes it easy to switch to a different compiler
158version; just enter the Nix shell environment with the command
159+```shell
160+nix-shell -p haskell.compiler.ghc784
161+```
162to 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
164per-project. It uses `nix-shell` under the hood when Nix support is turned on.
···169a project that doesn't depend on any additional system libraries outside of GHC,
170then it's even sufficient to just run the `cabal configure` command inside of
171the shell:
172+```shell
173+nix-shell -p haskell.compiler.ghc784 --command "cabal configure"
174+```
175176Afterwards, all other commands like `cabal build` work just fine in any shell
177environment, because the configure phase recorded the absolute paths to all
···198GHC. For example, the Nix expression `ghcWithPackages (pkgs: [pkgs.mtl])`
199generates a copy of GHC that has the `mtl` library registered in addition to
200its normal core packages:
201+```
202+$ nix-shell -p "haskellPackages.ghcWithPackages (pkgs: [pkgs.mtl])"
203204+[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+```
0208209This function allows users to define their own development environment by means
210of 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+```
225it's possible to install that compiler with `nix-env -f "<nixpkgs>" -iA
226myHaskellEnv`. If you'd like to switch that development environment to a
227different version of GHC, just replace the `ghc7102` bit in the previous
···233The generated `ghc` program is a wrapper script that re-directs the real
234GHC executable to use a new `lib` directory --- one that we specifically
235constructed 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+```
245246The variables `$NIX_GHC`, `$NIX_GHCPKG`, etc. point to the *new* store path
247`ghcWithPackages` constructed specifically for this environment. The last line
···261To make sure that mechanism works properly all the time, we recommend that you
262set those variables to meaningful values in your shell environment, too, i.e.
263by 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+```
269270If you are certain that you'll use only one GHC environment which is located in
271your user profile, then you can use the following code, too, which has the
272advantage that it doesn't contain any paths from the Nix store, i.e. those
273settings always remain valid even if a `nix-env -u` operation updates the GHC
274environment 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+```
283284### How to install a compiler with libraries, hoogle and documentation indexes
285···295long and scary.
296297For 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+```
312allows one to browse module documentation index [not too dissimilar to
313this](https://downloads.haskell.org/~ghc/latest/docs/html/libraries/index.html)
314for all the specified packages and their dependencies by directing a browser of
···318319After 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+```
325and test it by typing into `ghci`:
326+```
327+:hoogle a -> a
328+:doc a -> a
329+```
330331Be sure to note the links to `haddock` files in the output. With any modern and
332properly configured terminal emulator you can just click those links to
333navigate there.
334335Finally, you can run
336+```shell
337+hoogle server -p 8080
338+```
339and navigate to http://localhost:8080/ for your own local
340[Hoogle](https://www.haskell.org/hoogle/). Note, however, that Firefox and
341possibly other browsers disallow navigation from `http:` to `file:` URIs for
···350automatically select the right version of GHC and other build tools to build,
351test and execute apps in an existing project downloaded from somewhere on the
352Internet. 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+```
358359If 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
361example:
362+```yaml
363+nix:
364+ enable: true
365+ packages: [pkgconfig zeromq zlib]
366+```
367368The example configuration snippet above tells Stack to create an ad hoc
369environment for `nix-shell` as in the below section, in which the `pkgconfig`,
···374environments might need to expose Nixpkgs packages compiled in a certain way, or
375with extra environment variables. In these cases, you'll need a `shell` field
376instead of `packages`:
377+```yaml
378+nix:
379+ enable: true
380+ shell-file: shell.nix
381+```
382383For more on how to write a `shell.nix` file see the below section. You'll need
384to express a derivation. Note that Nixpkgs ships with a convenience wrapper
···387as `mkDerivation` can be provided. For example, to build a Stack project that
388including packages that link against a version of the R library compiled with
389special options turned on:
390+```nix
391+with (import <nixpkgs> { });
392393+let R = pkgs.R.override { enableStrictBarrier = true; };
394+in
395+haskell.lib.buildStackProject {
396+ name = "HaskellR";
397+ buildInputs = [ R zeromq zlib ];
398+}
399+```
0400401You can select a particular GHC version to compile with by setting the
402`ghc` attribute as an argument to `buildStackProject`. Better yet, let
403Stack choose what GHC version it wants based on the snapshot specified
404in `stack.yaml` (only works with Stack >= 1.1.3):
405+```nix
406+{nixpkgs ? import <nixpkgs> { }, ghc ? nixpkgs.ghc}:
407408+with nixpkgs;
409410+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+```
0418419[stack-nix-doc]: http://docs.haskellstack.org/en/stable/nix_integration.html
420···422423The 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+```
428429For more sophisticated use-cases, however, it's more convenient to save the
430desired 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+```
445446Now run `nix-shell` --- or even `nix-shell --pure` --- to enter a shell
447environment that has the appropriate compiler in `$PATH`. If you use `--pure`,
···457environment suitable for compiling that particular package. If you'd like to
458hack the `lens` library, for example, then you just have to check out the
459source 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/
464465+$ nix-shell "<nixpkgs>" -A haskellPackages.lens.env
466+[nix-shell:/tmp/lens-4.11]$
467+```
000468469At point, you can run `cabal configure`, `cabal build`, and all the other
470development commands. Note that you need `cabal-install` installed in your
···483For 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
485top-level directory and run the command:
486+```shell
487+cabal2nix . > foo.nix
488+```
489Then 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+```
494495Finally, 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+```
500501At this point, you can run `nix-build` to have Nix compile your project and
502install it into a Nix store path. The local directory will contain a symlink
···512513If your package does not depend on any system-level libraries, then it's
514sufficient to run
515+```shell
516+nix-shell --command "cabal configure"
517+```
518once to set up your build. `cabal-install` determines the absolute paths to all
519resources 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
···528up 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+```
535is usually enough to set up a build environment for any given Haskell package.
536You can even use that generated file to run `nix-build`, too:
537+```shell
538+nix-build shell.nix
539+```
540541### How to build projects that depend on each other
542···545for the dependency resolution performed by `callPackage`. First of all, change
546into each of your projects top-level directories and generate a `default.nix`
547file with `cabal2nix`:
548+```shell
549+cd ~/src/foo && cabal2nix . > default.nix
550+cd ~/src/bar && cabal2nix . > default.nix
551+```
552Then edit your `~/.config/nixpkgs/config.nix` file to register those builds in the
553default 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+```
567Once that's accomplished, `nix-env -f "<nixpkgs>" -qA haskellPackages` will
568show your packages like any other package from Hackage, and you can build them
569+```shell
570+nix-build "<nixpkgs>" -A haskellPackages.foo
571+```
572or enter an interactive shell environment suitable for building them:
573+```shell
574+nix-shell "<nixpkgs>" -A haskellPackages.bar.env
575+```
576577## Miscellaneous Topics
578···583feature is to replace the default `mkDerivation` function with one that enables
584library profiling for all packages. To accomplish that, add configure the
585following 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+ });
00595 };
596+ };
597+ };
598+}
599+```
600Then, replace instances of `haskellPackages` in the `cabal2nix`-generated
601`default.nix` or `shell.nix` files with `profiledHaskellPackages`.
602···6087.8.4 cannot compile that binary. Now, one way to solve that problem is to
609register an older version of `ghc-events` in the 7.8.x-specific package set.
610The 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+```
614Then 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 {};
00624 };
625 };
626 };
627+ };
628+ };
629+}
630+```
631632This code is a little crazy, no doubt, but it's necessary because the intuitive
633version
634+```nix
635+{ # ...
636637+ haskell.packages.ghc784 = super.haskell.packages.ghc784.override {
638+ overrides = self: super: {
639+ ghc-events = self.callPackage ./ghc-events-0.4.3.0.nix {};
0640 };
641+ };
642+}
643+```
644doesn't do what we want it to: that code replaces the `haskell` package set in
645Nixpkgs with one that contains only one entry,`packages`, which contains only
646one entry `ghc784`. This override loses the `haskell.compiler` set, and it
···650651Once it's accomplished, however, we can install a variant of `ghc-events`
652that's compiled with GHC 7.8.4:
653+```shell
654+nix-env -f "<nixpkgs>" -iA haskell.packages.ghc784.ghc-events
655+```
656Unfortunately, it turns out that this build fails again while executing the
657test suite! Apparently, the release archive on Hackage is missing some data
658files that the test suite requires, so we cannot run it. We accomplish that by
659re-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+```
663Now the builds succeeds.
664665Of course, in the concrete example of `ghc-events` this whole exercise is not
···674675GHC and distributed build farms don't get along well:
676677+ - https://ghc.haskell.org/trac/ghc/ticket/4012
678679When 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+```
684then you have to download and re-install `foo` and all its dependents from
685scratch:
686+```shell
687+nix-store -q --referrers /nix/store/*-haskell-text-1.2.0.4 \
688+ | xargs -L 1 nix-store --repair-path
689+```
690691If you're using additional Hydra servers other than `hydra.nixos.org`, then it
692might be necessary to purge the local caches that store data from those
693machines to disable these binary channels for the duration of the previous
694command, 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+```
700701### How to use the Haste Haskell-to-Javascript transpiler
702703Open 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+```
710You may not need the following step but if `haste-boot` fails to compile all the
711packages 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
716transpiled programs:
717+```shell
718+haste-boot
719+```
720Transpile 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+```
727728### Builds on Darwin fail with `math.h` not found
729730Users of GHC on Darwin have occasionally reported that builds fail, because the
731compiler complains about a missing include file:
732+```
733+fatal error: 'math.h' file not found
734+```
735The issue has been discussed at length in [ticket
7366390](https://github.com/NixOS/nixpkgs/issues/6390), and so far no good
737solution has been proposed. As a work-around, users who run into this problem
738can configure the environment variables
739+```shell
740+export NIX_CFLAGS_COMPILE="-idirafter /usr/include"
741+export NIX_CFLAGS_LINK="-L/usr/lib"
742+```
743in their `~/.bashrc` file to avoid the compiler error.
744745### Builds using Stack complain about missing system libraries
746747+```
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
752753+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+```
764765When 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
767and 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
769that the package works fine inside of nix-shell, but once you leave the
770shell the shared object can no longer be found. That issue is by no
771means specific to Stack: you'll have that problem with any other
···774775You can remedy this issue in several ways. The easiest is to add a `nix` section
776to the `stack.yaml` like the following:
777+```yaml
778+nix:
779+ enable: true
780+ packages: [ zlib ]
781+```
782783+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+```
0000790to find out the store path of the system's zlib library. Now, you can
791792+ 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.
795796+ 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.
000800801+ 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+ ```
806807+ 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.
812813+ The same thing applies to `cabal configure`, of course, if you're
814+ building with `cabal-install` instead of Stack.
815816### Creating statically linked binaries
817818There are two levels of static linking. The first option is to configure the
819build with the Cabal flag `--disable-executable-dynamic`. In Nix expressions,
820this can be achieved by setting the attribute:
821+```
822+enableSharedExecutables = false;
823+```
824That gives you a binary with statically linked Haskell libraries and
825dynamically linked system libraries.
826827To link both Haskell libraries and system libraries statically, the additional
828flags `--ghc-option=-optl=-static --ghc-option=-optl=-pthread` need to be used.
829In Nix, this is accomplished with:
830+```
831+configureFlags = [ "--ghc-option=-optl=-static" "--ghc-option=-optl=-pthread" ];
832+```
833834+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!
00837838### Building GHC with integer-simple
839···843[integer-gmp](http://hackage.haskell.org/package/integer-gmp) package.
844845A 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),
847a kind of "copyleft" license. According to the terms of the LGPL, paragraph 5,
848you may distribute a program that is designed to be compiled and dynamically
849linked with the library under the terms of your choice (i.e., commercially) but
···856programs compiled with GHC because most distributions (and builds) of GHC use
857static libraries. (Dynamic libraries are currently distributed only for OS X.)
858The LGPL licensing situation may be worse: even though
859+[The Glasgow Haskell Compiler License](https://www.haskell.org/ghc/license)
860is essentially a "free software" license (BSD3), according to
861paragraph 2 of the LGPL, GHC must be distributed under the terms of the LGPL!
862···867To get a GHC compiler build with `integer-simple` instead of `integer-gmp` use
868the attribute: `haskell.compiler.integer-simple."${ghcVersion}"`.
869For 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+```
876The 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+```
889890To get a package set supporting `integer-simple` use the attribute:
891`haskell.packages.integer-simple."${ghcVersion}"`. For example
892use the following to get the `scientific` package build with `integer-simple`:
893+```shell
894+nix-build -A haskell.packages.integer-simple.ghc802.scientific
895+```
896897## Other resources
898899+ - 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.
904905+ - 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.
909910+ - Oliver Charles wrote a very nice [Tutorial how to develop Haskell packages with Nix](http://wiki.ocharles.org.uk/Nix).
911912+ - The *Journey into the Haskell NG infrastructure* series of postings
913+ describe the new Haskell infrastructure in great detail:
914915+ - [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.
918919+ - [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.
922923+ - [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.