1Haskell.nix also provides reproducible development environments for
2your Haskell projects. These environments can contain not only GHC and
3your Haskell package dependencies, but also the required system
4libraries and build tools.
6Inside the development shell, you can run commands such as `ghc`,
7`ghci`, or `cabal new‑build` (`cabal build` on Cabal 3.0),
8and they will have all dependencies available.
10Every dependency will be cached in your Nix store. If you have set up
11Hydra CI, then your team can share pre-built dependencies.
13These examples assume that you have created your package set as
14described in [Creating Nix builds for your projects](getting-started.md) and
15it exists in a file called `default.nix`.
19 Old-style `cabal build` and `stack` builds are not (yet)
20 supported. For example, `stack` will (by design) download and
21 rebuild all dependencies, even though they are available in the
22 shell. However, if you have a Stack project, you can generate the
23 package set with Haskell.nix, then use `cabal new‑build` to work
24 on it. Starting Cabal 3.0 `cabal build` will work out of the box, as
25 new style builds are the default.
27## How to get a development shell for a package
29Run a `nix‑shell` on `components.all` of your package. `all` is a
30synthetic component whose dependencies are the union of the
31dependencies of all components in the package. Therefore, you will be
32able to build the test suites because their dependencies will be
38 hsPkgs = import ./default.nix {};
40 hsPkgs.my-package.components.all
43## How to get a development shell for a multi-package project
45If you have a Cabal or Stack project with multiple packages
46(i.e. multiple `.cabal` files, not a single package with multiple
47components), then you will need a development environment that
48contains the _dependencies_ of your packages, but not the packages
49themselves. This is what the [`shellFor`][shellFor] function does.
53{ pkgs ? import <nixpkgs> {} }:
56 hsPkgs = import ./default.nix { inherit pkgs; };
59 # Include only the *local* packages of your project.
60 packages = ps: with ps; [
65 # Builds a Hoogle documentation index of all dependencies,
66 # and provides a "hoogle" command to search the index.
69 # You might want some extra tools in the shell (optional).
71 # Some common tools can be added with the `tools` argument
72 tools = { cabal = "3.2.0.0"; hlint = "2.2.11"; };
73 # See overlays/tools.nix for more details
75 # Some you may need to get some other way.
76 buildInputs = with pkgs.haskellPackages;
79 # Prevents cabal from choosing alternate plans, so that
80 # *all* dependencies are provided by Nix.
85See also: [Haskell.nix Library Reference: `shellFor`][shellFor]
87[shellFor]: ../reference/library.md#shellfor
89## How to get a local Hoogle index
91If you need a local Hoogle for all the dependencies of your project create this file
96 hsPkgs = import ./default.nix {};
99 packages = ps: [ps.my-package];
104and run `nix-shell shell-hoogle.nix --run "hoogle server --local"`.
105This will open a local Hoogle server at `http://127.0.0.1:8080`.
108## How to get an ad-hoc development shell including certain packages
110This creates a development environment with the given packages
111registered in the package database. The `ghcWithPackages` function
112operates on a Haskell.nix package set, and accepts an argument that
113selects packages from the larger package set.
118 haskellNix = import (builtins.fetchTarball https://github.com/input-output-hk/haskell.nix/archive/master.tar.gz) {};
119 nixpkgs = import haskellNix.sources.nixpkgs-1909 haskellNix.nixpkgsArgs;
120 haskell = nixpkgs.haskell-nix;
122 haskell.haskellPackages.ghcWithPackages (ps: with ps;
123 [ lens conduit conduit-extra ])
126If you need a Hoogle documentation index, use `ghcWithHoogle` in place
129## How to get packages from a certain Stackage snapshot
131Haskell.nix knows about every released Stackage snapshot. You can use
132it to build packages from a given snapshot, without setting up a full
137 haskellNix = import (builtins.fetchTarball https://github.com/input-output-hk/haskell.nix/archive/master.tar.gz) {};
138 nixpkgs = import haskellNix.sources.nixpkgs-1909 haskellNix.nixpkgsArgs;
139 haskell = nixpkgs.haskell-nix;
141 haskell.snapshots."lts-13.18".alex.components.exes.alex
144There are Haskell.nix package sets for every Stackage snaphot under
147The alias `haskell.haskellPackages` corresponds to the package set for
148a recent LTS Haskell version.
150You can use `ghcWithPackages` on any of these package sets to quickly
151get a shell with some packages.
154 The build will not work if your Nixpkgs does not contain the version
155 of GHC specified in the snapshot. Nixpkgs only carries the
156 latest version of each recent release series, so many snapshots
162Once you have a development shell, then you can begin configuring
163Emacs to use it. The way I do it is:
1651. Run [lorri watch](https://github.com/target/lorri) to continously
166 build the shell environment and maintain GC roots.
1682. Use [emacs‑direnv](https://github.com/wbolster/emacs-direnv) to
169 push the development environment into Emacs.
1713. Use [Dante](https://github.com/jyp/dante) for highlighting errors
172 and auto-completion. You must customize Dante to prevent it from
173 automatically using `nix‑shell` or `stack`. Trim `dante‑methods` to
174 just `new‑build` and `bare‑ghci`.
176 You can also use [`.dir‑locals.el`](https://www.gnu.org/software/emacs/manual/html_node/emacs/Directory-Variables.html)
177 for this. If your project has multiple targets, set `dante‑target`
1804. For [`haskell‑mode`](https://github.com/haskell/haskell-mode)
181 interactive Haskell, set `haskell‑process‑type` to
186It's sometimes useful to load [Haskell.nix][] in the REPL to explore
187attrsets and try examples.
191{ nixpkgs ? <nixpkgs> }:
193 haskell = import nixpkgs (import (builtins.fetchTarball https://github.com/input-output-hk/haskell.nix/archive/master.tar.gz) {}).nixpkgsArgs;
194 pkgNames = haskell.pkgs.lib.attrNames haskell.haskell-nix.snapshots."lts-13.18";
198Load the example file:
201$ nix repl '<nixpkgs>' example.nix
202Welcome to Nix version 2.3pre6631_e58a7144. Type :? for help.
204Loading '<nixpkgs>'...
205Added 10403 variables.
207Loading 'example.nix'...
210nix-repl> lib.take 5 pkgNames
211[ "ALUT" "ANum" "Allure" "Boolean" "BoundedChan" ]
216Now that you have `nix-tools` and are able to import [Haskell.nix][],
217you can continue to the next chapter.
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list