You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+44-14Lines changed: 44 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,12 +4,14 @@ This is an example Haskell project using Nix to setup a development environment.
4
4
5
5
This uses Nix and Cabal without Stack. This is because when using Nix, you don't need Stack, as Nix provides harmonious snapshots of Haskell packages. However Stack users can still develop on this project, they just have to generate an appropriate `stack.yaml` from the Cabal file.
6
6
7
-
The first step is that we have to acquire `cabal2nix`, which we use to generate a `default.nix` file from the `package.yaml`. Note that the usage of `package.yaml` means we are using the [hpack format](https://github.com/sol/hpack). This format is transformed to a cabal file via the `hpack` command.
7
+
The first step is that we have to acquire `cabal2nix`, which we use to generate a `cabal.nix` file from the `package.yaml`. Our custom `default.nix` then imports the `cabal.nix` and adds extra custom build steps like encoding environment variables.
8
+
9
+
Note that the usage of `package.yaml` means we are using the [hpack format](https://github.com/sol/hpack). This format is transformed to a cabal file via the `hpack` command.
8
10
9
11
```sh
10
12
nix-shell -p cabal2nix
11
13
# using --hpack ensures that we always use package.yaml
12
-
cabal2nix --hpack .>./default.nix
14
+
cabal2nix --hpack .>./cabal.nix
13
15
```
14
16
15
17
The above command is also executed at the beginning of the `shellHook`.
@@ -88,6 +90,14 @@ Once you have finished developing, you can build the package using:
88
90
nix-build
89
91
```
90
92
93
+
Note that if you want to create a quick and dirty `nix-shell` with GHC and a few packages, just use:
Any module that is meant to be consumed as a library needs to be listed in the `exposed-modules`. Any module that is not listed there are considered to be private modules.
@@ -106,6 +116,38 @@ Note that Haskell dependency constraints and versions when using `cabal2nix` is
106
116
107
117
Remember that Haskell package versions conventionally use `Major.Major.Minor.Patch`. For more information see: https://pvp.haskell.org/
108
118
119
+
## Non-Haskell Dependencies
120
+
121
+
For non-Haskell dependencies that are CLI executables, if you want them to be made available to the build and final output, you need to add these dependencies to:
122
+
123
+
```yaml
124
+
system-build-tools:
125
+
- hello
126
+
```
127
+
128
+
This will put it into the generated `cabal.nix` as a function parameter.
129
+
130
+
To ensure that these dependency names do not conflict with Haskell dependencies with the same name, it's important to specify them when using the `callPackage`.
For non-Haskell dependencies that are compiled libraries that are expected to be linked against, you need to add these dependencies to:
137
+
138
+
```yaml
139
+
extra-libraries:
140
+
- mnl
141
+
```
142
+
143
+
These C libraries will be made available to `nix-build` and `nix-shell`. The `cabal configure` will automatically find them and link them during compilation.
144
+
145
+
However in Nixpkgs, these libraries will have different names. You should then explicitly specify them when using the `callPackage`:
The `cabal v2-repl` only works against the build targets specified in the `package.yaml`. You have to specify the target name:
@@ -143,15 +185,3 @@ The `include-dirs` is a list of directories containing C headers to be included.
143
185
The `install-includes` will ensure that these headers (relative to the include-dirs) are also exported to any downstream package that depends on this package. So they can make use of those same headers, if they were also writing their own C code.
144
186
145
187
Finally you just need to write code like `FFI.hs`, and everything just works normally.
146
-
147
-
---
148
-
149
-
Because Haskell is a compiled language, most building tools are `nativeBuildInputs`. However for the `shell.nix` this distinction doesn't matter, because it just puts you into an environment that has all the dependencies.
150
-
151
-
Note that if you want to create a quick and dirty `nix-shell` with GHC and a few packages, just use:
0 commit comments