OpenGL in a pure nix-shell on OSX 10.14.6

I’m trying to run a pure nix-shell on osx and then build a haskell project with Stack. The project uses using OpenGL via GLFW-b and gloss and i can successfully build it outside of nix-shell but in the shell, i consistently get the following error:

Building all executables for `haskell-game' once. After a successful build of all of them, only specified executables will be rebuilt.
haskell-game-0.1.0.0: configure (lib + exe)
Configuring haskell-game-0.1.0.0...
haskell-game-0.1.0.0: build (lib + exe)
Preprocessing library for haskell-game-0.1.0.0..
Building library for haskell-game-0.1.0.0..
[1 of 3] Compiling Paths_haskell_game ( .stack-work/dist/x86_64-osx/Cabal-2.4.0.1/build/autogen/Paths_haskell_game.hs, .stack-work/dist/x86_
64-osx/Cabal-2.4.0.1/build/Paths_haskell_game.o )
[2 of 3] Compiling WithWindow       ( src/WithWindow.hs, .stack-work/dist/x86_64-osx/Cabal-2.4.0.1/build/WithWindow.o )
[3 of 3] Compiling Lib              ( src/Lib.hs, .stack-work/dist/x86_64-osx/Cabal-2.4.0.1/build/Lib.o )
ld: framework not found OpenGL
collect2: error: ld returned 1 exit status
`gcc' failed in phase `Linker'. (Exit code: 1)

--  While building package haskell-game-0.1.0.0 using:
      /Users/willisplummer/.stack/setup-exe-cache/x86_64-osx/Cabal-simple_mPHDZzAJ_2.4.0.1_ghc-8.6.5 --builddir=.stack-work/dist/x86_64-osx/
Cabal-2.4.0.1 build lib:haskell-game exe:haskell-game-exe --ghc-options " -ddump-hi -ddump-to-file -fdiagnostics-color=always"

I’ve been poking around a bit and it seems like this may be related to xcode stuff being unavailable to the shell. Curious if there’s a solution or workaround?

here’s the github project for reference: https://github.com/willisplummer/haskell-game

Thanks!

Maybe https://github.com/guibou/nixGL helps ?

You can add darwin.apple_sdk.frameworks.OpenGL to buildInputs. I think this will be found by cabal.

@matthewbauer – i get the same error when i include OpenGL in the build inputs. is it possible that i need to somehow make stack/cabal aware of it?

You may need to add NIX_LDFLAGS = "-F${darwin.apple_sdk.frameworks.OpenGL}/Library/Frameworks" as well.

Awesome! That seems to have worked. I got a new error about AGL not found.

edit:

I was able to get a working build with the following shell, but the actual game ends up crashing after about a second of runtime. very mysterious, but i appreciate the advice!

with import <nixpkgs> {};
let
  stack-1_9_3 =
    (import (builtins.fetchTarball {
      url = "https://github.com/NixOS/nixpkgs/archive/bc94dcf500286495e3c478a9f9322debc94c4304.tar.gz";
      sha256 = "1siqklf863181fqk19d0x5cd0xzxf1w0zh08lv0l0dmjc8xic64a";
    }) { }).stack;

in
  mkShell {
      buildInputs = [
          darwin.apple_sdk.frameworks.OpenGL
          darwin.apple_sdk.frameworks.AGL
          darwin.apple_sdk.frameworks.Cocoa
          darwin.apple_sdk.frameworks.IOKit
          darwin.apple_sdk.frameworks.CoreFoundation
          darwin.apple_sdk.frameworks.CoreVideo
          clang
          glfw
          ftgl
          freealut
          gcc
          stack-1_9_3
          (haskell.packages.ghc865.ghcWithPackages (pkgs: [
              pkgs.gloss
              pkgs.GLFW-b
              pkgs.gloss-rendering
              pkgs.mtl
          ]))
      ];
    NIX_LDFLAGS = ''
      -F${darwin.apple_sdk.frameworks.OpenGL}/Library/Frameworks
      -F${darwin.apple_sdk.frameworks.AGL}/Library/Frameworks
      -F${darwin.apple_sdk.frameworks.Cocoa}/Library/Frameworks
      -F${darwin.apple_sdk.frameworks.IOKit}/Library/Frameworks
      -F${darwin.apple_sdk.frameworks.CoreFoundation}/Library/Frameworks
      -F${darwin.apple_sdk.frameworks.CoreVideo}/Library/Frameworks
    '';
  }
1 Like