How does one obtain Stack 1.8.*
on NixOS? In general, how does one ensure that the version of stack
running on NixOS is the equivalent of the version that would run with stack update --git
on a non-NixOS machine?

- 6,927
- 4
- 34
- 67
1 Answers
In Nix, if you want to have a specific version of a package, you create a derivation for it.
Derivation
To create the derivation for Stack, you can get one of the pre-built Stack releases using the nix-prefetch-scripts
package and stdenv.mkDerivation
as it is shown here.
Stack 1.8 seems not released yet, so you need to build it from sources. One way to do it is cabal2nix
:
cabal2nix --shell 'https://github.com/commercialhaskell/stack.git' > stack.nix
There are more examples in the manual
nixpkgs
Now you have the derivation with the right version of the software. If you want it to be the system-default, you override your Nixpkgs.
Here is the example from the docs:
{
packageOverrides = pkgs: rec {
stack = ./path/to/stack.nix {};
};
}
Update
We've found that the derivation produced by cabal2nix
doesn't work in the case of stack because haskellPackages
are missing some of the dependencies.
Here I've created gist with the (slightly adapted) result of running stackage2nix
on the stack repo. The result of default.nix
is not a single derivation but a set of packages required to build the stack. So the override for your system should look like:
{
packageOverrides = pkgs: rec {
stack = (import ./default.nix {}).stack;
};
}

- 10,696
- 4
- 52
- 62
-
I successfully generated the `stack.nix`, but it's still opaque to me how to link to it in my `configuration.nix` via a package override (I read your link). Can you give me a hint? – George Jul 13 '18 at 14:35
-
Here is the docs https://nixos.org/nixpkgs/manual/#sec-modify-via-packageOverrides. You need to override stack attribute: `stack = import /path/to/stack.nix {};`. Relative path should also work – 4e6 Jul 13 '18 at 14:53
-
I'm getting this error: `error: 'f' at /home/george/.dotfiles/nixos/stack.nix:7:7 called without required argument 'githash', at /nix/store/a55hr4k99fdn5lxym8ba8545lndabwfk-nixos-18.03.132847.aec217852f2/nixos/pkgs/development/haskell-modules/make-package-set.nix:88:27 (use '--show-trace' to show detailed location information)` – George Jul 13 '18 at 15:05
-
It is the issue with `cabal2nix`, I'm getting the same error. I'm looking into it – 4e6 Jul 13 '18 at 15:11
-
Oh, there's no `githash` package in the `haskellPackages` of nixpkgs. So we have the problem with derivation. I'll see what I can do. – 4e6 Jul 13 '18 at 15:23
-
@George, I've made a custom derivation for Stack, check the updated answer – 4e6 Jul 13 '18 at 16:01
-
@436: I now get "error: attribute 'stackage' missing" on line 409. – George Jul 13 '18 at 18:07
-
Oh, it requires the stackage overlay installed. Sorry, completely forgot to mention. This one https://github.com/typeable/nixpkgs-stackage – 4e6 Jul 13 '18 at 19:21
-
The installation instructions on `nixpkgs-stackage` are somewhat opaque. In this context, do you know how I would install it in my NixOS machine? – George Jul 13 '18 at 19:30
-
I updated the gist. Now you don't need to install the overlay, derivation will do it for you. – 4e6 Jul 13 '18 at 19:35
-
@436: Thanks for the updated gist. I now get error "Could not find module ‘Conduit’" after lots of compiling. – George Jul 13 '18 at 19:48
-
Can you please provide the full error message, I need some context. We can also move this conversation to the GitHub. Gist comments should be a better place to post the code snippets. – 4e6 Jul 13 '18 at 20:45
-
I'm in need of something like this again for Stack 1.10. Do you know how one can generalize your solution into a script or something that can make it reproducible for all new future stack builds? – George Dec 16 '18 at 16:50
-
@George you can open an issue on typeable/stackage2nix repo, but I can't promise that I will have time to look into it soon. – 4e6 Dec 19 '18 at 06:03