8

I'm experiencing a problem with GL on NixOS: 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)

This happens even though I have manually installed the 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 question is relevant but it doesn't help: nixos + haskell + opengl (prerequisites)

Solution: After switching to stack everything works.

Ronny Brendel
  • 4,777
  • 5
  • 35
  • 55
samsergey
  • 228
  • 1
  • 8
  • Can you post a repository so I can reproduce? – iElectric Jan 08 '17 at 13:28
  • @iElectric, It could be any Gloss MWE (just display a circle, for example), or something more interesting, like RosettaCode [snake game](http://rosettacode.org/wiki/Snake). – samsergey Jan 08 '17 at 21:19
  • 1
    After having read this page carefully, I'm still facing the described problem when using stack/nixos/gloss . I'd highly appreciate if someone could upload a minimal working example to a github repo. This would be really nice. – Anton Harald Feb 10 '17 at 11:17
  • @AntonHarald, for a repository that doesn't have those linker errors, have a look at https://github.com/turion/rhine/tree/develop/rhine-gloss. But it has another puzzling error, this time related to GLUT. – Turion Sep 27 '18 at 08:45

2 Answers2

5

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.

Soham Chowdhury
  • 2,125
  • 2
  • 18
  • 29
  • uh, he's using cabal, not stack. – vek Jan 11 '17 at 16:02
  • @Vektorweg thanks for pointing that out! I've modified the instructions; hopefully they'll work in their case (although I don't use `cabal` and can't be sure). – Soham Chowdhury Jan 11 '17 at 17:05
  • 1
    Thanks for your effort to help, @Soham Chowdhury! Alas, I've tried all combinations of `mesa`, `mesa+freeglut`, `mesa_glu+freeglut` etc. neither works. I believe, the problem is not with `stack` or `cabal`, probably, something is not set properly in my OS environments or configuarions. Does anyone know whether there is something like `glxgears` to see if OpenGL works at all? I couldn't find it in NixOs packages list. – samsergey Jan 11 '17 at 21:30
  • Yeah, the `stack` really did solve the problem! Farewell, `cabal`, I will miss you :) – samsergey Jan 12 '17 at 08:24
  • Yeah! *small jump of joy* I haven't used Haskell long enough to have known `cabal` nontrivially, so ... welcome to the dark side? ;) – Soham Chowdhury Jan 12 '17 at 08:26
  • 2
    Any idea why `nix-shell -p mesa mesa_glu freeglut` doesn't work? – Turion Dec 02 '17 at 19:46
  • I think now you have to change those packages to `[libGL libGLU freeglut]`. – Turion Sep 27 '18 at 08:46
1

The following helped me:

with import <nixpkgs> {};
pkgs.stdenv.mkDerivation rec {
  name = "dev-env";
  buildInputs = with pkgs; [
    mesa
    freeglut
  ];
  LD_LIBRARY_PATH = with pkgs; "${freeglut}/lib";
}

Copy this to shell.nix, enter the environment with nix-shell and run compiled executable inside the env.

NCrashed
  • 63
  • 3
  • Thank you! For some reason having freeglut in buildInputs is not enough and setting LD_LIBRARY_PATH is needed. – AleXoundOS Jun 27 '21 at 21:15