on f6bc2f7 help sign in
haskell.nix/docs/tutorials/getting-started.md
126 lines 4.7 kB history raw
1# Getting started
3`haskell.nix` can automatically translate your
4[Cabal](https://cabal.readthedocs.io/en/latest/cabal-project.html)
5or [Stack](https://docs.haskellstack.org/en/stable/README/#quick-start-guide)
6project and its dependencies into Nix code.
8Assuming you have [Nix](https://nixos.org/download.html) installed, you can
9start setting up your project.
11## Setting up the binary cache
13IMPORTANT: you *must* do this or you *will* build several copies of GHC!
15You can configure Nix to use our binary cache, which is pushed to by CI, so should contain the artifacts that you need.
17You need to add the following sections to `/etc/nix/nix.conf` or, if you are a trusted user, `~/.config/nix/nix.conf` (if you don't know what a "trusted user" is, you probably want to do the former).
19```
20trusted-public-keys = [...] hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ= [...]
21substituters = [...] https://hydra.iohk.io [...]
22```
24This can be tricky to get setup properly. If you're still having trouble getting cache hits, consult the corresponding [troubleshooting section](../reference/troubleshooting#why-am-i-building-ghc).
26## Scaffolding
28The following work with `stack.yaml` and `cabal.project` based
29projects.
31Add `default.nix`:
33```nix
34{ # Fetch the latest haskell.nix and import its default.nix
35 haskellNix ? import (builtins.fetchTarball "https://github.com/input-output-hk/haskell.nix/archive/master.tar.gz") {}
37# haskell.nix provides access to the nixpkgs pins which are used by our CI,
38# hence you will be more likely to get cache hits when using these.
39# But you can also just use your own, e.g. '<nixpkgs>'.
40, nixpkgsSrc ? haskellNix.sources.nixpkgs-2003
42# haskell.nix provides some arguments to be passed to nixpkgs, including some
43# patches and also the haskell.nix functionality itself as an overlay.
44, nixpkgsArgs ? haskellNix.nixpkgsArgs
46# import nixpkgs with overlays
47, pkgs ? import nixpkgsSrc nixpkgsArgs
48}: pkgs.haskell-nix.project {
49 # 'cleanGit' cleans a source directory based on the files known by git
50 src = pkgs.haskell-nix.haskellLib.cleanGit {
51 name = "haskell-nix-project";
52 src = ./.;
53 };
54 # Specify the GHC version to use.
55 compiler-nix-name = "ghc8102"; # Not required for `stack.yaml` based projects.
57```
59!!! note "git dependencies"
60 If you have git dependencies in your project, you'll need
61 to [calculate sha256 hashes for them](./source-repository-hashes.md).
63### Working with a project
65Top-level attributes are Haskell packages (incl. dependencies) part of your project.
67To build the library component of a package in the project run:
69```shell
70nix-build -A your-package-name.components.library
71```
73There are also other components such as `exes`, `tests`, `benchmarks` and `all`.
74To build an executable:
76```shell
77nix-build -A your-package-name.components.exes.your-exe-name
78```
80To cross compile use the `projectCross` attribute:
82```
83nix-build -A projectCross.ghcjs.hsPkgs.your-package-name.components.exes.your-exe-name
84nix-build -A projectCross.mingwW64.hsPkgs.your-package-name.components.exes.your-exe-name
85```
87To open a shell for use with `cabal` run:
89```shell
90nix-shell -A shellFor
91cabal new-repl your-package-name:library:your-package-name
92cabal new-build your-package-name
93```
95To open a shell for use with `stack` see [the following issue](https://github.com/input-output-hk/haskell.nix/issues/689#issuecomment-643832619).
97### Pinning the [haskell.nix][] version
99For simplicity's sake we will use `fetchTarball` for the examples in
100this documentation. This will always get the latest version, and is
101similar to an auto-updating Nix channel.
103However, in your own project, you may wish to pin [haskell.nix][] (as
104you would pin Nixpkgs). This will make your builds reproducible, more
105predictable, and faster (because the fixed version is cached).
107Straightforward way of doing this is to change the branch name to a revision.
109```nix
110{ # Fetch a specific haskell.nix and import its default.nix
111 haskellNix ? import (builtins.fetchTarball "https://github.com/input-output-hk/haskell.nix/archive/f1a94a4c82a2ab999a67c3b84269da78d89f0075.tar.gz") {}
113...
114```
116There are other possible schemes for pinning. See
117[Bumping Hackage and Stackage snapshots](./hackage-stackage.md) and
118[Nix tutorial on reproducibility using pinning](https://nix.dev/tutorials/towards-reproducibility-pinning-nixpkgs.html).
120## Going forward
122Read through [project](../reference/library.md#project) function reference to see how the API works.
124There are a number of things to explore further in the tutorials section.
126[haskell.nix]: https://github.com/input-output-hk/haskell.nix