Skip to content

Commit 06d787e

Browse files
committed
Fixed up non-haskell dependencies, fixes #7
1 parent 3ab6e19 commit 06d787e

4 files changed

Lines changed: 33 additions & 16 deletions

File tree

README.md

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@ This is an example Haskell project using Nix to setup a development environment.
44

55
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.
66

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.
7+
The first step is that we have to acquire `cabal2nix`, which we use to generate a `cabal.nix` file from the `package.yaml`.
8+
9+
We also write a custom `default.nix` then imports the `cabal.nix` and adds extra custom build steps like encoding environment variables.
10+
11+
Both the `cabal.nix` and `default.nix` remain as Haskell `callPackage` derivations.
12+
13+
This means the function parameter names may conflict with non-Haskell package names.
14+
15+
If you get a conflict, make sure to do explicit overrides when using `callPackage`.
816

917
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.
1018

@@ -118,34 +126,36 @@ Remember that Haskell package versions conventionally use `Major.Major.Minor.Pat
118126

119127
## Non-Haskell Dependencies
120128

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:
129+
For non-Haskell dependencies that are CLI executables, add them to:
122130

123131
```yaml
124132
system-build-tools:
125133
- hello
126134
```
127135

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`.
136+
They will be available during `nix-build` and `nix-shell`.
131137

132-
```nix
133-
(haskellPackages.callPackage ./cabal.nix { hello = pkgs.hello; });
134-
```
138+
For them to be available for the output, you must use `makeWrapper`.
135139

136-
For non-Haskell dependencies that are compiled libraries that are expected to be linked against, you need to add these dependencies to:
140+
For non-Haskell dependencies that are linkable libraries, add them to:
137141

138142
```yaml
139143
extra-libraries:
140144
- mnl
141145
```
142146

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.
147+
The name of these libraries is the suffix of the C linking option `-lmnl` giving you `mnl`.
148+
149+
They will be will available to `nix-build` and `nix-shell`. The `cabal configure` will automatically find them and link them during compilation.
150+
151+
Sometimes these non-Haskell dependencies have names that conflict with Haskell dependencies of the same name.
152+
153+
To resolve this, override explicitly when using `callPackage`. This is done in both `shell.nix` and `release.nix`.
144154

145-
However in Nixpkgs, these libraries will have different names. You should then explicitly specify them when using the `callPackage`:
155+
For example:
146156

147157
```nix
148-
haskellPackages.callPackage (import ./cabal.nix) { mnl = pkgs.libmnl; };
158+
haskellPackages.callPackage ./cabal.nix { hello = pkgs.hello; mnl = pkgs.libmnl; };
149159
```
150160

151161
## Using GHCi (or `cabal repl` or `stack ghci`)

default.nix

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# this is still a haskell callPackage-based derivation
2+
# the names here can clash with non-haskell dependencies
3+
# make sure to specify the names the explicitly that may be clashing
14
{ callPackage
25
, nix-gitignore
36
, makeWrapper
@@ -6,10 +9,10 @@
69
}:
710

811
let
9-
drv = (callPackage ./cabal.nix { hello = hello; });
12+
drv = (callPackage ./cabal.nix { inherit hello; });
1013
in
1114
drv.overrideAttrs (attrs: {
12-
src = nix-gitignore.gitignoreSource [] ./.;
15+
src = nix-gitignore.gitignoreSource [] attrs.src;
1316
nativeBuildInputs = attrs.nativeBuildInputs ++ [ makeWrapper ];
1417
postFixup = ''
1518
wrapProgram $out/bin/haskell-demo-deps-exe \

release.nix

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ with pkgs;
44
let
55
haskellPackages = haskell.packages.ghc865;
66
strict = drv: haskell.lib.buildStrictly drv;
7-
drv = haskellPackages.callPackage ./default.nix {};
7+
drv = haskellPackages.callPackage ./default.nix {
8+
hello = hello;
9+
};
810
in
911
rec {
1012
library = drv;

shell.nix

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
with pkgs;
44
let
55
haskellPackages = haskell.packages.ghc865;
6-
drv = (haskellPackages.callPackage ./default.nix {}).env;
6+
drv = (haskellPackages.callPackage ./default.nix {
7+
hello = hello;
8+
}).env;
79
in
810
drv.overrideAttrs (attrs: {
911
src = null;

0 commit comments

Comments
 (0)