on stack-cache-native help sign in
haskell.nix/docs/user-guide/development.md
175 lines 5.8 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`][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 ];
72 # Prevents cabal from choosing alternate plans, so that
73 # *all* dependencies are provided by Nix.
74 exactDeps = true;
75 }
76```
78See also: [Haskell.nix Library Reference: `shellFor`][shellFor]
80[shellFor]: ../reference/library.md#shellfor
82## How to get a local Hoogle index
84If you need a local Hoogle for all the dependencies of your project create this file
86```nix
87# shell-hoogle.nix
88let
89 hsPkgs = import ./default.nix {};
90in
91 hsPkgs.shellFor {
92 packages = ps: [ps.my-package];
93 withHoogle = true;
94 }
95```
97and run `nix-shell shell-hoogle.nix --run "hoogle server --local"`.
98This will open a local Hoogle server at `http://127.0.0.1:8080`.
101## How to get an ad-hoc development shell including certain packages
103This creates a development environment with the given packages
104registered in the package database. The `ghcWithPackages` function
105operates on a Haskell.nix package set, and accepts an argument that
106selects packages from the larger package set.
108```nix
109# shell.nix
110let
111 src = builtins.fetchTarball https://github.com/input-output-hk/haskell.nix/archive/master.tar.gz;
112 nixpkgs = import (src + "/nixpkgs") (import src);
113 haskell = nixpkgs.haskell-nix;
114in
115 haskell.haskellPackages.ghcWithPackages (ps: with ps;
116 [ lens conduit conduit-extra ])
117```
119If you need a Hoogle documentation index, use `ghcWithHoogle` in place
120of `ghcWithPackages`.
122## How to get packages from a certain Stackage snapshot
124Haskell.nix knows about every released Stackage snapshot. You can use
125it to build packages from a given snapshot, without setting up a full
126project.
128```nix
129let
130 src = builtins.fetchTarball https://github.com/input-output-hk/haskell.nix/archive/master.tar.gz;
131 nixpkgs = import (src + "/nixpkgs") (import src);
132 haskell = nixpkgs.haskell-nix;
133in
134 haskell.snapshots."lts-13.18".alex.components.exes.alex
135```
137There are Haskell.nix package sets for every Stackage snaphot under
138`haskell.snapshots`.
140The alias `haskell.haskellPackages` corresponds to the package set for
141a recent LTS Haskell version.
143You can use `ghcWithPackages` on any of these package sets to quickly
144get a shell with some packages.
146!!! warning
147 The build will not work if your Nixpkgs does not contain the version
148 of GHC specified in the snapshot. Nixpkgs only carries the
149 latest version of each recent release series, so many snapshots
150 can't be built.
153## Emacs IDE support
155Once you have a development shell, then you can begin configuring
156Emacs to use it. The way I do it is:
1581. Run [lorri watch](https://github.com/target/lorri) to continously
159 build the shell environment and maintain GC roots.
1612. Use [emacs‑direnv](https://github.com/wbolster/emacs-direnv) to
162 push the development environment into Emacs.
1643. Use [Dante](https://github.com/jyp/dante) for highlighting errors
165 and auto-completion. You must customize Dante to prevent it from
166 automatically using `nix‑shell` or `stack`. Trim `dante‑methods` to
167 just `new‑build` and `bare‑ghci`.
169 You can also use [`.dir‑locals.el`](https://www.gnu.org/software/emacs/manual/html_node/emacs/Directory-Variables.html)
170 for this. If your project has multiple targets, set `dante‑target`
171 per-directory.
1734. For [`haskell‑mode`](https://github.com/haskell/haskell-mode)
174 interactive Haskell, set `haskell‑process‑type` to
175 `cabal‑new‑repl`.