2

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?

Shersh
  • 9,019
  • 3
  • 33
  • 61
  • I know nothing about `nix`, but AFAIK running `stack setup` won't redownload a version of GHC if that version was already downloaded somewhere on your computer. Stack tries as much as possible to share downloads between projects. – Alec Sep 13 '17 at 16:09
  • These days stack wants to use its own ghc unless `--system-ghc` is passed or `system-ghc: true` is specified in config.yaml / stack.yaml. So, I recommend putting `system-ghc: true` in `config.yaml`. – mgsloan Sep 13 '17 at 19:21
  • @mgsloan Thanks for your advice! Probably, this is the solution. Also considering adding `ghc` to `PATH`. – Shersh Sep 14 '17 at 18:36
  • Is stack meant to be installed inside a nix-shell, or is it expected to exist in the user profile? I was under the the impression that I would bring in stack inside the shell via shell.nix then run stack while inside the shell. – CMCDragonkai Feb 21 '18 at 02:54
  • @CMCDragonkai in my case `stack` already exist in my user profile. – Shersh Feb 21 '18 at 07:59

1 Answers1

2

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.

brainrape
  • 326
  • 2
  • 7