Confusion getting set up with haskell development

I’m trying to hack on a haskell project at the moment. I don’t need to package it with nix or anything… I’m just trying to hack. The project is built with cabal/ghc. I tried getting started with simply nix-shell -p cabal-install ghc, but I’m getting the following error:

❯ nix-shell -p cabal-install ghc           

[nix-shell:~/dev/graphql-engine/server]$ cabal new-build
cabal: Cannot find the program 'ghc'. User-specified path 'ghc-8.10.2' does
not refer to an executable and the program is not on the system path.

What’s going on here? Why don’t the cabal-install and ghc packages play nicely together?

I can’t say much, but that I have been in a similar situation some time ago. After researching for a while, what helped me set things up was:

Sorry, this does not answer your question.

I agree with @blaggacao and suggest reading The working programmer’s guide to setting up Haskell projects. I just finished a small Haskell game with a Nix setup you could take inspiration from.

Thanks for the pointers, @blaggacao and @DamienCassou. I’m really looking for something more lightweight though… All I want to do is basically get the equivalent of installing the haskell toolchain via ghcup. Is there a way to do that via nix-shell?

You might have to give more details about what your repo looks like, and what exactly is going wrong.

In general, nix-shell -p cabal-install ghc should work (although it becomes more complicated if you need system libraries).

Here’s an example of my doing this on my system:

$ nix-shell -p cabal-install ghc
these paths will be fetched (137.12 MiB download, 1883.55 MiB unpacked):
copying path '/nix/store/3kk0zjpx093r86k0f8ppbdz2lf6adkim-ghc-8.8.4-doc' from ''...
copying path '/nix/store/yn4yvi26bnzng1sn1gzi956s32f186yh-cabal-install-' from ''...
copying path '/nix/store/8b4nbc8x1k710gbhkjwlpx1scasqjd97-ghc-8.8.4' from ''...
$ mkdir temptemptemp  # this is within the nix-shell...
$ cd temptemptemp/
$ cabal init
Warning: The package list for '' is 56 days old.
Run 'cabal update' to get the latest list of available packages.

Guessing dependencies...

Generating LICENSE...
Warning: unknown license type, you must put a copy in LICENSE yourself.
Generating Setup.hs...
Generating Main.hs...
Generating temptemptemp.cabal...
$ cabal build all
Resolving dependencies...
Build profile: -w ghc-8.8.4 -O1
In order, the following will be built (use -v for more details):
 - temptemptemp- (exe:temptemptemp) (first run)
Configuring executable 'temptemptemp' for temptemptemp-
Warning: The 'license-file' field refers to the file 'LICENSE' which does not
Preprocessing executable 'temptemptemp' for temptemptemp-
Building executable 'temptemptemp' for temptemptemp-
[1 of 1] Compiling Main             ( Main.hs, /home/user/temp/temptemptemp/dist-newstyle/build/x86_64-linux/ghc-8.8.4/temptemptemp-
-tmp/Main.o )
Linking /home/user/temp/temptemptemp/dist-newstyle/build/x86_64-linux/ghc-8.8.4/temptemptemp- ...
$ which ghc
$ which cabal

If you’ve already ran builds with a GHC or cabal-install that has been installed through ghcup, then I’d recommend you get rid of all relevant files and directories (like ./dist-newstyle/, .ghc-environment or whatever it is called, ~/.cabal, etc) before trying again with a GHC or cabal-install from nix-shell.

Hmm, weird that doesn’t work for me… I’m tracking nixpkgs-unstable. Could that have something to do with it? Should I report this as a bug?

I actually haven’t installed ghcup on this particular machine. Just trying to recreate that experience in pure Nix :slight_smile:

For reproducibility’s sake, I’m attempting to build on a Ubuntu 20.04.2 LTS machine and

[nix-shell:~/dev/graphql-engine/server]$ nix-instantiate --eval -E '(import <nixpkgs> {}).lib.version'

I cloned graphql-engine, cd'd into server/, and ran the following nix-shell command:

$ nix-shell -I nixpkgs=~/.nix-defexpr/channels/unstable -p haskell.compiler.ghc8102Binary cabal-install  zlib postgresql unixODBC unixODBCDrivers.psql openssl

This gave me a shell with cabal-install- and GHC-8.10.2, along with all the system libraries.

Running cabal build, I was able to compile all dependencies in this environment and start compiling the graphql-engine server package. I didn’t let the server package fully compile since it would have taken a long time.

I am on Nixpkgs unstable commit dcdf30a78a5. I am running on Debian 10. The graphql-engine repo is commit e5b8ae8639

If you actually wanted to use this for a something more than just playing around, I’d suggest you throw the above into a shell.nix file.

The above will also probably not work for cabal repl, so that is something to be wary of.

1 Like

Oh sweet, thanks! I wasn’t aware that individual ghc versions were packaged like that!

1 Like