on 5f11b11 help sign in
haskell.nix/docs/tutorials/development.md
205 lines 6.7 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` (`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`.
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. 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
29If you have a Cabal or Stack project with one or more packages
30(i.e. multiple `.cabal` files, not a single package with multiple
31components), then you will need a development environment that
32contains the _dependencies_ of your packages, but not the packages
33themselves. This is what the [`shellFor`][shellFor] function does.
35```nix
36# shell.nix
37let
38 project = import ./default.nix;
39in
40 project.shellFor {
41 # ALL of these arguments are optional.
43 # List of packages from the project you want to work on in
44 # the shell (default is all the projects local packages).
45 packages = ps: with ps; [
46 pkga
47 pkgb
48 ];
50 # Builds a Hoogle documentation index of all dependencies,
51 # and provides a "hoogle" command to search the index.
52 withHoogle = true;
54 # Some common tools can be added with the `tools` argument
55 tools = {
56 cabal = "3.2.0.0";
57 hlint = "latest"; # Selects the latest version in the hackage.nix snapshot
58 haskell-language-server = "latest";
59 };
60 # See overlays/tools.nix for more details
62 # Some you may need to get some other way.
63 buildInputs = [ (import <nixpkgs> {}).git ];
65 # Prevents cabal from choosing alternate plans, so that
66 # *all* dependencies are provided by Nix.
67 exactDeps = true;
68 }
69```
71See also: [Haskell.nix Library Reference: `shellFor`][shellFor]
73[shellFor]: ../reference/library.md#shellfor
75## How to get a local Hoogle index
77If you need a local Hoogle for all the dependencies of your project create this file
79```nix
80# shell-hoogle.nix
81let
82 project = import ./default.nix {};
83in
84 project.shellFor {
85 packages = ps: [ps.my-package];
86 withHoogle = true;
87 }
88```
90and run `nix-shell shell-hoogle.nix --run "hoogle server --local"`.
91This will open a local Hoogle server at `http://127.0.0.1:8080`.
94## How to get an ad-hoc development shell including certain packages
96This creates a development environment with the given packages
97registered in the package database. The `ghcWithPackages` function
98operates on a Haskell.nix package set, and accepts an argument that
99selects packages from the larger package set.
101```nix
102# shell.nix
103let
104 haskellNix = import (builtins.fetchTarball https://github.com/input-output-hk/haskell.nix/archive/master.tar.gz) {};
105 nixpkgs = import haskellNix.sources.nixpkgs haskellNix.nixpkgsArgs;
106 haskell = nixpkgs.haskell-nix;
107in
108 haskell.haskellPackages.ghcWithPackages (ps: with ps;
109 [ lens conduit conduit-extra ])
110```
112If you need a Hoogle documentation index, use `ghcWithHoogle` in place
113of `ghcWithPackages`.
115## How to get packages from a certain Stackage snapshot
117Haskell.nix knows about every released Stackage snapshot. You can use
118it to build packages from a given snapshot, without setting up a full
119project.
121```nix
122let
123 haskellNix = import (builtins.fetchTarball https://github.com/input-output-hk/haskell.nix/archive/master.tar.gz) {};
124 nixpkgs = import haskellNix.sources.nixpkgs haskellNix.nixpkgsArgs;
125 haskell = nixpkgs.haskell-nix;
126in
127 haskell.snapshots."lts-13.18".alex.components.exes.alex
128```
130There are Haskell.nix package sets for every Stackage snaphot under
131`haskell.snapshots`.
133The alias `haskell.haskellPackages` corresponds to the package set for
134a recent LTS Haskell version.
136You can use `ghcWithPackages` on any of these package sets to quickly
137get a shell with some packages.
139!!! warning
140 The build will not work if your Nixpkgs does not contain the version
141 of GHC specified in the snapshot. Nixpkgs only carries the
142 latest version of each recent release series, so many snapshots
143 can't be built.
146## Emacs IDE support
148Once you have a development shell, then you can begin configuring
149Emacs to use it. The way I do it is:
1511. Run [lorri watch](https://github.com/target/lorri) to continuously
152 build the shell environment and maintain GC roots.
1542. Use [emacs‑direnv](https://github.com/wbolster/emacs-direnv) to
155 push the development environment into Emacs.
1573. Use [Dante](https://github.com/jyp/dante) for highlighting errors
158 and auto-completion. You must customize Dante to prevent it from
159 automatically using `nix‑shell` or `stack`. Trim `dante‑methods` to
160 just `new‑build` and `bare‑ghci`.
162 You can also use [`.dir‑locals.el`](https://www.gnu.org/software/emacs/manual/html_node/emacs/Directory-Variables.html)
163 for this. If your project has multiple targets, set `dante‑target`
164 per-directory.
1664. For [`haskell‑mode`](https://github.com/haskell/haskell-mode)
167 interactive Haskell, set `haskell‑process‑type` to
168 `cabal‑new‑repl`.
170## Using `nix repl`
172It's sometimes useful to load [Haskell.nix][] in the REPL to explore
173attrsets and try examples.
175```
176# example.nix
177{ nixpkgs ? <nixpkgs> }:
178rec {
179 haskell = import nixpkgs (import (builtins.fetchTarball https://github.com/input-output-hk/haskell.nix/archive/master.tar.gz) {}).nixpkgsArgs;
180 pkgNames = haskell.pkgs.lib.attrNames haskell.haskell-nix.snapshots."lts-13.18";
182```
184Load the example file:
186```
187$ nix repl '<nixpkgs>' example.nix
188Welcome to Nix version 2.3pre6631_e58a7144. Type :? for help.
190Loading '<nixpkgs>'...
191Added 10403 variables.
193Loading 'example.nix'...
194Added 2 variables.
196nix-repl> lib.take 5 pkgNames
197[ "ALUT" "ANum" "Allure" "Boolean" "BoundedChan" ]
199nix-repl>
200```
202Now that you have `nix-tools` and are able to import [Haskell.nix][],
203you can continue to the next chapter.
205[haskell.nix]: https://github.com/input-output-hk/haskell.nix