on 38afdfd help sign in
haskell.nix/docs/user-guide/development.md
163 lines 5.4 kB history raw
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`, and they will have all dependencies
8available.
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](projects.md) and
15it exists in a file called `default.nix`.
17!!! note
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.
26## How to get a development shell for a package
28Run a `nix‑shell` on `components.all` of your package. `all` is a
29synthetic component whose dependencies are the union of the
30dependencies of all components in the package. Therefore, you will be
31able to build the test suites because their dependencies will be
32included.
34```nix
35# shell.nix
36let
37 hsPkgs = import ./default.nix {};
38in
39 hsPkgs.my-package.components.all
40```
42## How to get a development shell for a multi-package project
44If you have a Cabal or Stack project with multiple packages
45(i.e. multiple `.cabal` files, not a single package with multiple
46components), then you will need a development environment that
47contains the _dependencies_ of your packages, but not the packages
48themselves. This is what the `shellFor` function does.
50```nix
51# shell.nix
52{ pkgs ? import <nixpkgs> {} }:
54let
55 hsPkgs = import ./default.nix { inherit pkgs; };
56in
57 hsPkgs.shellFor {
58 # Include only the *local* packages of your project.
59 packages = ps: with ps; [
60 pkga
61 pkgb
62 ];
64 # Builds a Hoogle documentation index of all dependencies,
65 # and provides a "hoogle" command to search the index.
66 withHoogle = true;
68 # You might want some extra tools in the shell (optional).
69 buildInputs = with pkgs.haskellPackages;
70 [ hlint stylish-haskell ghcid ];
71 }
72```
74## How to get a local Hoogle index
76If you need a local Hoogle for all the dependencies of your project create this file
78```nix
79# shell-hoogle.nix
80let
81 hsPkgs = import ./default.nix {};
82in
83 hsPkgs.shellFor {
84 packages = ps: [ps.my-package];
85 withHoogle = true;
86 }
87```
89and run `nix-shell shell-hoogle.nix --run "hoogle server --local"`.
90This will open a local Hoogle server at `http://127.0.0.1:8080`.
93## How to get an ad-hoc development shell including certain packages
95This creates a development environment with the given packages
96registered in the package database. The `ghcWithPackages` function
97operates on a Haskell.nix package set, and accepts an argument that
98selects packages from the larger package set.
100```nix
101# shell.nix
102let
103 haskell = import (builtins.fetchTarball https://github.com/input-output-hk/haskell.nix/archive/master.tar.gz) {};
104in
105 haskell.haskellPackages.ghcWithPackages (ps: with ps;
106 [ lens conduit conduit-extra ])
107```
109If you need a Hoogle documentation index, use `ghcWithHoogle` in place
110of `ghcWithPackages`.
112## How to get packages from a certain Stackage snapshot
114Haskell.nix knows about every released Stackage snapshot. You can use
115it to build packages from a given snapshot, without setting up a full
116project.
118```nix
119let
120 haskell = import (builtins.fetchTarball https://github.com/input-output-hk/haskell.nix/archive/master.tar.gz) {};
121in
122 haskell.snapshots."lts-13.18".alex.components.exes.alex
123```
125There are Haskell.nix package sets for every Stackage snaphot under
126`haskell.snapshots`.
128The alias `haskell.haskellPackages` corresponds to the package set for
129a recent LTS Haskell version.
131You can use `ghcWithPackages` on any of these package sets to quickly
132get a shell with some packages.
134!!! warning
135 The build will not work if your Nixpkgs does not contain the version
136 of GHC specified in the snapshot. Nixpkgs only carries the
137 latest version of each recent release series, so many snapshots
138 can't be built.
141## Emacs IDE support
143Once you have a development shell, then you can begin configuring
144Emacs to use it. The way I do it is:
1461. Run [lorri watch](https://github.com/target/lorri) to continously
147 build the shell environment and maintain GC roots.
1492. Use [emacs‑direnv](https://github.com/wbolster/emacs-direnv) to
150 push the development environment into Emacs.
1523. Use [Dante](https://github.com/jyp/dante) for highlighting errors
153 and auto-completion. You must customize Dante to prevent it from
154 automatically using `nix‑shell` or `stack`. Trim `dante‑methods` to
155 just `new‑build` and `bare‑ghci`.
157 You can also use [`.dir‑locals.el`](https://www.gnu.org/software/emacs/manual/html_node/emacs/Directory-Variables.html)
158 for this. If your project has multiple targets, set `dante‑target`
159 per-directory.
1614. For [`haskell‑mode`](https://github.com/haskell/haskell-mode)
162 interactive Haskell, set `haskell‑process‑type` to
163 `cabal‑new‑repl`.