NixOs, Haskell, opengl : problems with building and running openGL programs

Question

I have just tried NixOS a week ago and I'm really happy with it, being 8-ears old Arch-user. If only I could manage this GL problem...

The problem seems to be old, but there is still no straightforward solution yet in 2017!

I'm trying to build a Haskell program using the Gloss library. I have installed gloss and everything it needs using the nix-shell -p mesa and it seems to be properly build and istalled (using cabal install). However if I build the program in the same nix-shell it doesn't work:

$ nix-shell -p mesa_glu

[nix-shell:]$ ghc --make -O2 SnakePar.hs
Linking SnakePar ...

[nix-shell:]$ ./SnakePar 
SnakePar: user error (unknown GLUT entry glutInit)

While working outside of a nix-shell the linking stage failes:

$ ghc --make -O2 SnakePar.hs
[1 of 1] Compiling Main             ( SnakePar.hs, SnakePar.o )
Linking SnakePar ...
/nix/store/<hash>-binutils-2.27/bin/ld: cannot find -lGLU
/nix/store/<hash>-binutils-2.27/bin/ld: cannot find -lGL
collect2: error: ld returned 1 exit status
`cc' failed in phase `Linker'. (Exit code: 1)

Even though I have manually installed glu library via nix-env -iA.

$ nix-env -q
cabal-install-1.24.0.0
ghc-8.0.1
glu-9.0.0

I have tried using freeglut or mesa in the same manner, but none of these (or even all together) didn't work.

What am I missing?

This QA is relevant but it doesn't help: nixos + haskell + opengl (prerequisites)


Show source
| haskell   | opengl   | nixos   2017-01-07 23:01 1 Answers

Answers ( 1 )

  1. 2017-01-11 11:01

    I just fixed this, after three days of, well, being another not-happy Arch-user-until-last-week: try adding freeglut to the nix-shell environment, i.e. use nix-shell -p mesa freeglut.

    For stack users stumbling upon this answer, add this to ~/.stack/stack.yaml:

    nix:
      enable: true
      packages: [mesa freeglut]
    

    The nix-shell solution doesn't work in this case -- the problem, as I'm guessing, is that Stack always works inside a pure environment even if run from inside a nix-shell. I did try pure: false in the nix section of stack.yaml, but that didn't work. This does, for now.

    • mesa is required to provide the C headers for OpenGLRaw and other libraries (and this is why linking fails in your case, I believe).
    • freeglut provides the GLUT bindings (you'll get the "unknown GLUT entry" error otherwise) required at runtime.

    You may need to change the mesa there to something else, like mesa_glu.

◀ Go back