Search code examples
haskellghchaskell-stacknixnix-shell

How to use already installed ghc with stack inside nix-shell?


I have already installed ghc using stack build tool. It compiles my project. But when I enter nix-shell inside project directory and want to compile my project from nix-shell I see error like this:

No compiler found, expected minor version match with ghc-8.0.2

This can be fixed by running stack setup command. But I worry that this will install ghc once more time consuming more space on my machine and etc. Is there a way to use already installed ghc for nix-shell without running stack setup command?


Solution

  • I'm not sure how you set up your project, but probably the easiest (but limited) way is to use stack's nix integration which will use GHC and native dependencies from nix.

    Unfortunately, the stack nix integration is rather backwards. It doesn't use nix to build your package, just to create an environment with GHC and native dependencies. You have to use stack itself to build the project, and since stack will run cabal, it should still work with the flag. The stack nix integration will not generate a .nix file with the dependencies resolved by stack, so you can't build your project with nix-build and there's no point in entering nix-shell. You would have to use a tool like stack2nix or stackage2nix for that.

    So: build with stack --nix build and stack should use nix to set up GHC and native dependencies.

    When you're ready for serious business (like automated build and deploy), look into stack2nix or stackage2nix, use them to freeze the dependencies into a nix file, then build and deploy with nix-build, nix-copy-closure etc.