Is it possible to get Haskell stack to use Haskell packages provided by nix?

Viewed 181

I'm developing a Haskell program that will eventually have some non-Haskell dependencies, and I heard that the nix integration in stack is one way to accomplish this. However, I'm now wondering if it's possible to use nix to install Haskell dependencies too since all the libraries I'm using are on nix, and currently I have only Haskell dependencies. I tried to make a custom "shell.nix" file that describes all the Haskell dependencies I have as follows:

with (import <nixpkgs> {});

stdenv.mkDerivation {
  name = "simple";
  buildInputs = [
    haskellPackages.array haskellPackages.base haskellPackages.bound
    haskellPackages.containers haskellPackages.deriving-compat 
    haskellPackages.haskeline haskellPackages.logict haskellPackages.mtl
    haskellPackages.text haskellPackages.unification-fd haskellPackages.alex 
    haskellPackages.happy haskellPackages.BNFC ghc
];
buildTools = [ haskellPackages.alex haskellPackages.happy ];
libraryHaskellDepends = [
    haskellPackages.array haskellPackages.base haskellPackages.bound  
    haskellPackages.containers haskellPackages.deriving-compat 
    haskellPackages.haskeline haskellPackages.logict haskellPackages.mtl
    haskellPackages.text haskellPackages.unification-fd haskellPackages.alex 
    haskellPackages.happy haskellPackages.BNFC
];
libraryToolDepends = [ haskellPackages.alex haskellPackages.happy haskellPackages.hpack ];
executableHaskellDepends = [
    haskellPackages.array haskellPackages.base haskellPackages.bound 
    haskellPackages.containers haskellPackages.deriving-compat 
    haskellPackages.haskeline haskellPackages.logict haskellPackages.mtl
    haskellPackages.text haskellPackages.unification-fd haskellPackages.alex 
    haskellPackages.happy haskellPackages.BNFC
];
executableToolDepends = [ haskellPackages.alex haskellPackages.happy ];
testHaskellDepends = [
    haskellPackages.array haskellPackages.base haskellPackages.bound 
    haskellPackages.containers haskellPackages.deriving-compat 
    haskellPackages.haskeline haskellPackages.logict haskellPackages.mtl
    haskellPackages.text haskellPackages.unification-fd haskellPackages.alex 
    haskellPackages.happy haskellPackages.BNFC
];
testToolDepends = [ haskellPackages.alex haskellPackages.happy ];
}

And in stack.yaml, set

nix:
    enable: true
    shell-file: shell.nix

However, when I type "stack build," alex, etc are not found. I can add all the package requirements manually to the stack.yaml file (the dependency requirements are already set in the package.yaml/package.cabal file). However, the library versions on nixpkgs satisfy the dependency requirements in the package.yaml file, so I'm curious, is it supported to tell stack to just use the versions of (Haskell) libraries provided by nixpkgs? If not, are there any other options for building a Haskell project using dependencies provided by nixpkgs whenever they are available?

1 Answers

This is not a real answer to the question, but it's a start of a workaround which I'll post a separate question about.

In Nix, you can use haskellPackages.ghcWithPackages (package-name), to obtain a ghc together with packages that ghc knows about. E.g.

{ pkgs ? import <nixpkgs> {}}:
let 
  # get ghc with libraries bundled
  ghc = pkgs.haskellPackages.ghcWithPackages (p:[
    p.array
    p.base
    ...]);
...

If you then run nix-shell shell.nix and then cabal build, it will build the package using the dependencies pulled in from Nix, and everything seems to work as I was hoping. With two exceptions: alex and happy. On v1-build, the nix-installed alex and happy are used, but on v2-build they are not (but this is out of scope for this question).

Related