|
1 | 1 | # Getting started with Hix
|
2 | 2 |
|
3 |
| -The `hix` tools are wrappers for the various `nix` tools that |
4 |
| -use `haskell.nix` without the need to add any `.nix` files. |
| 3 | +Hix is a command line tool that provides an easy way to add haskell.nix |
| 4 | +support to existing haskell projects. |
5 | 5 |
|
6 |
| -This is useful for: |
| 6 | +You will need `nix` installed and in you `PATH` with nix in PATH with |
| 7 | +`experimental-features = [ "nix-command" "flakes" ];` configured. |
| 8 | +See https://nixos.wiki/wiki/Flakes for details. |
7 | 9 |
|
8 |
| -* A quick way to try out haskell.nix for new users. |
| 10 | +## Using `hix init` and `nix` |
9 | 11 |
|
10 |
| -* Using haskell.nix to work on projects that do not have |
11 |
| - `.nix` files. |
| 12 | +The `hix init` command adds a `flake.nix` and `nix/hix.nix` file. |
| 13 | +After that the project can be used with regular `nix` tools. |
12 | 14 |
|
13 |
| -* Testing to see if `haskell.nix` can build a project. |
| 15 | +For instance to run `cabal build` on the `hello` package from hackage: |
14 | 16 |
|
15 |
| -* Making `flake` and `non flake` configurations to check `haskell.nix` |
16 |
| - treats them the same. |
17 |
| - |
18 |
| -## Installing Nix |
19 |
| - |
20 |
| -To use Hix you will need to install [Nix](https://nixos.org/download.html). |
21 |
| - |
22 |
| -## Setting up the binary cache |
23 |
| - |
24 |
| -IMPORTANT: you *must* do this or you *will* build several copies of GHC! |
25 |
| - |
26 |
| -You can configure Nix to use our binary cache, which is pushed to by CI, so should contain the artifacts that you need. |
| 17 | +```bash |
| 18 | +cabal unpack hello |
| 19 | +cd hello-1.0.0.2 |
| 20 | +nix run "github:input-output-hk/haskell.nix#hix" -- init |
| 21 | +nix develop |
| 22 | +cabal build |
| 23 | +``` |
27 | 24 |
|
28 |
| -You 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). |
| 25 | +To view the contents of the flake run: |
29 | 26 |
|
30 | 27 | ```
|
31 |
| -trusted-public-keys = [...] hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ= [...] |
32 |
| -substituters = [...] https://cache.iog.io [...] |
| 28 | +nix flake show |
33 | 29 | ```
|
34 | 30 |
|
35 |
| -If you're running NixOS, you need to add/update the following in your `/etc/nixos/configuration.nix` files instead. |
| 31 | +To build a component with nix: |
36 | 32 |
|
37 | 33 | ```
|
38 |
| -# Binary Cache for Haskell.nix |
39 |
| -nix.settings.trusted-public-keys = [ |
40 |
| - "hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ=" |
41 |
| -]; |
42 |
| -nix.settings.substituters = [ |
43 |
| - "https://cache.iog.io" |
44 |
| -]; |
| 34 | +nix build .#hello:exe:hello |
45 | 35 | ```
|
46 | 36 |
|
47 |
| -NixOS-21.11 and older use slightly different settings. |
| 37 | +To build and run a component: |
48 | 38 |
|
49 | 39 | ```
|
50 |
| -# Binary Cache for Haskell.nix |
51 |
| -nix.binaryCachePublicKeys = [ |
52 |
| - "hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ=" |
53 |
| -]; |
54 |
| -nix.binaryCaches = [ |
55 |
| - "https://cache.iog.io" |
56 |
| -]; |
| 40 | +nix run .#hello:exe:hello |
57 | 41 | ```
|
58 | 42 |
|
59 |
| -This can be tricky to get setup properly. If you're still having trouble getting cache hits, consult the corresponding [troubleshooting section](../troubleshooting.md#why-am-i-building-ghc). |
60 |
| - |
61 | 43 | ## Installing Hix
|
62 | 44 |
|
| 45 | +To use the other Hix features first install Hix with: |
| 46 | + |
63 | 47 | ```
|
64 | 48 | nix-env -iA hix -f https://github.com/input-output-hk/haskell.nix/tarball/master
|
65 | 49 | ```
|
66 | 50 |
|
67 |
| -## Updating Hix (also updates hackage) |
| 51 | +To update run to the latest version run: |
68 | 52 |
|
69 | 53 | ```
|
70 | 54 | hix update
|
71 | 55 | ```
|
72 | 56 |
|
73 |
| -This is also necessary to make the latest nightly snapshot of hackage |
74 |
| -avaiable to nix. |
75 |
| - |
76 |
| -## Building with Hix |
77 |
| - |
78 |
| -To run `cabal build` in a nix-shell with all the dependencies required: |
79 |
| - |
80 |
| -``` |
81 |
| -cabal unpack hello |
82 |
| -cd hello-1.0.0.2 |
83 |
| -hix-shell --run 'cabal build' |
84 |
| -``` |
85 |
| - |
86 |
| -Build with nix: |
87 |
| - |
88 |
| -``` |
89 |
| -hix-build -A hsPkgs.hello.components.exes.hello |
90 |
| -``` |
91 |
| - |
92 |
| -Cross compile to JavaScript: |
93 |
| - |
94 |
| -``` |
95 |
| -hix-build -A projectCross.ghcjs.hsPkgs.hello.components.exes.hello |
96 |
| -``` |
97 |
| - |
98 |
| -## Configuring Hix |
99 |
| - |
100 |
| -The configuration arguments for `Hix` can be (from highest precedence to lowest): |
101 |
| - |
102 |
| -* Passed on the command line with `--arg` (or `--argstr` for string args). |
103 |
| - |
104 |
| -* Placed in `nix/hix.nix` file in the project dir. |
105 |
| - |
106 |
| -* Placed in `~/.config/hix/hix.conf` |
107 |
| - |
108 |
| -For example to build with GHC 8.10.7: |
109 |
| - |
110 |
| -``` |
111 |
| -hix-shell --argstr compiler-nix-name ghc8107 --run 'cabal build' |
112 |
| -``` |
113 |
| - |
114 |
| -or add a `nix/hix.nix` or `~/.config/hix/hix.conf` file: |
115 |
| - |
116 |
| -```nix |
117 |
| -{ compiler-nix-name = "ghc8107"; } |
118 |
| -``` |
119 |
| - |
120 |
| -Here are just a few of the other configuration arguments you could use |
121 |
| -in the files or on the command line (they are all optional): |
122 |
| - |
123 |
| -```nix |
124 |
| -{ name = "hello"; # for better error messages and derivation names |
125 |
| - nixpkgsPin = "nixpkgs-unstable"; # or nixpkgs-2111 or nixpkgs-2105 |
126 |
| - nixpkgs = <nixpkgs>; # use this instead of nixpkgsPin |
127 |
| - subDir = "some/sub/dir"; # sub dir containing the haskell project |
128 |
| - projectFileName = "stack.yaml"; # use this project file |
129 |
| - tools.haskell-language-server = "latest"; |
130 |
| - tools.hlint = "latest"; # Include the latest hls and hlint in the shell |
131 |
| - index-state = "2021-02-22T00:00:00Z"; # It is normally best to put this in `cabal.project` (not here) |
132 |
| -
|
133 |
| -# PLUS MANY MORE! Almost any argument you can pass to the project functions |
134 |
| -# or to `shellFor` can be used in as a Hix configuration argument. |
135 |
| -
|
136 |
| -} |
137 |
| -``` |
138 |
| - |
139 |
| -## Adding Nix Support with Niv |
| 57 | +## Using `hix develop`, `hix flake`, `hix build` and `hix run` |
140 | 58 |
|
141 |
| -If you have a `nix/hix.nix` file with suitable configuration that |
142 |
| -you want to make available to users with Nix (without having to |
143 |
| -install Hix). |
| 59 | +These commands work the same as the `nix` versions |
| 60 | +without using the `flake.nix`. Instead a boiler |
| 61 | +plate haskell.nix flake.nix file is added to |
| 62 | +`.hix-flake/flake.nix` and used from there. |
144 | 63 |
|
145 |
| -[Niv](https://github.com/nmattia/niv) is a command line tool for keeping track of Nix project dependencies. |
| 64 | +The is can be useful if the project already includes a |
| 65 | +`flake.nix` or if you do not intend to maintain one. |
146 | 66 |
|
147 |
| -After installing niv you can initialize niv and pin the latest haskell.nix |
148 |
| -commit by running the following in the root directory of the project: |
149 | 67 |
|
150 |
| -``` |
151 |
| -niv init |
152 |
| -niv add input-output-hk/haskell.nix -n haskellNix |
153 |
| -``` |
| 68 | +Then all of these should work without the need to |
| 69 | +run `hix init`: |
154 | 70 |
|
155 |
| -Add `default.nix`: |
156 |
| - |
157 |
| -```nix |
158 |
| -(import (import nix/sources.nix).haskellNix {}).hix.project { src = ./.; } |
159 | 71 | ```
|
160 |
| - |
161 |
| -If you want to also pin `nixpkgs` with Niv use: |
162 |
| - |
163 |
| -```nix |
164 |
| -let |
165 |
| - sources = import nix/sources.nix; |
166 |
| -in |
167 |
| - (import sources.haskellNix {}).hix.project { |
168 |
| - inherit (sources) nixpkgs; |
169 |
| - src = ./.; |
170 |
| - } |
| 72 | +hix develop |
| 73 | +hix flake show |
| 74 | +hix build .#hello:exe:hello |
| 75 | +hix run .#hello:exe:hello |
171 | 76 | ```
|
172 | 77 |
|
173 |
| -Add `shell.nix`: |
| 78 | +## Using `hix-shell` and `hix-build` |
174 | 79 |
|
175 |
| -```nix |
176 |
| -(import ./.).shell |
177 |
| -``` |
178 |
| - |
179 |
| -When you want to update to the latest version of haskell.nix use: |
| 80 | +These commands behave like `nix-build` and `hix-shell` |
| 81 | +would if a boiler plate `default.nix` and `shell.nix` |
| 82 | +we present. |
180 | 83 |
|
181 | 84 | ```
|
182 |
| -niv update haskellNix |
183 |
| -``` |
184 |
| - |
185 |
| -## Adding Nix Flake Support |
186 |
| - |
187 |
| -To add flake support that uses the `nix/hix.nix` configuration in your |
188 |
| -follow the [Getting started with flakes](getting-started.md) guide, but |
189 |
| -use `haskell-nix.hix.project` instead of `haskell-nix.project'` |
190 |
| - |
191 |
| -The `nixpkgs` used will need to be selected as a flake input (any selection |
192 |
| -made in `nix/hix.nix` will be ignored). |
193 |
| - |
194 |
| -Example `flake.nix` file: |
195 |
| - |
196 |
| -```nix |
197 |
| -{ |
198 |
| - description = "A very basic flake"; |
199 |
| - inputs.haskellNix.url = "github:input-output-hk/haskell.nix"; |
200 |
| - inputs.nixpkgs.follows = "haskellNix/nixpkgs-unstable"; |
201 |
| - inputs.flake-utils.url = "github:numtide/flake-utils"; |
202 |
| - outputs = { self, nixpkgs, flake-utils, haskellNix }: |
203 |
| - flake-utils.lib.eachSystem [ "x86_64-linux" "x86_64-darwin" ] (system: |
204 |
| - let |
205 |
| - overlays = [ haskellNix.overlay |
206 |
| - (final: prev: { |
207 |
| - # This overlay adds our project to pkgs |
208 |
| - helloProject = |
209 |
| - final.haskell-nix.hix.project { |
210 |
| - src = ./.; |
211 |
| - # Other project options can be put in `nix/hix.nix` |
212 |
| - }; |
213 |
| - }) |
214 |
| - ]; |
215 |
| - pkgs = import nixpkgs { inherit system overlays; inherit (haskellNix) config; }; |
216 |
| - flake = pkgs.helloProject.flake { |
217 |
| - # This adds support for `nix build .#js-unknown-ghcjs:hello:exe:hello` |
218 |
| - crossPlatforms = p: [p.ghcjs]; |
219 |
| - }; |
220 |
| - in flake // { |
221 |
| - # Built by `nix build .` |
222 |
| - defaultPackage = flake.packages."hello:exe:hello"; |
223 |
| - }); |
224 |
| -} |
| 85 | +hix-shell --run 'cabal build all' |
| 86 | +hix-build -A hsPkgs.hello.components.exes.hello |
225 | 87 | ```
|
226 |
| - |
227 |
| - |
0 commit comments