Nixpkgs: GHC on MacOSX Yosemite can't include <math.h> because it lives in /usr/include, which isn't searched

Created on 17 Feb 2015  Â·  36Comments  Â·  Source: NixOS/nixpkgs

I'm trying to install ghc-7.8.4 through NIX and then work with cabal to install Haskell packages. It looks like the installation doesn't leave cabal in a usable state.

1) followed the instructions at:
https://nixos.org/wiki/Nix_on_OS_X#Using_Nix_on_10.9_and_10.10

$ nix-env -i hello
...
$ hello
Hello, world!

-- all good so far!

2) then install Haskell:

$ nix-env -iA nixpkgs.haskellPackages.ghc nixpkgs.haskellPackages.cabalInstall_1_20_0_6
$ nix-env -iA nixpkgs.haskellPackages.haddock 

again no problem

3) Then followed the instructions here:
https://nixos.org/wiki/Haskell#Using_cabal_in_the_direct_installation_scenario

Install the standard environment into the profile that contains cabal. This is probably prefered for a profile whose main purpose is for cabal usage

$  nix-env -f "<nixpkgs>" -iA pkgs.stdenv

appeared to work OK.

4) then tried to install some packages using cabal

$ cabal install aeson haskell-src-exts
Resolving dependencies...
Configuring dlist-0.7.1...
Configuring mtl-2.1.3.1...
Configuring primitive-0.5.4.0...
Configuring syb-0.4.4...
Configuring text-1.2.0.4...
Failed to install dlist-0.7.1
Build log ( /Users/nickager/.cabal/logs/dlist-0.7.1.log ):
Failed to install mtl-2.1.3.1
Build log ( /Users/nickager/.cabal/logs/mtl-2.1.3.1.log ):
Failed to install primitive-0.5.4.0
Build log ( /Users/nickager/.cabal/logs/primitive-0.5.4.0.log ):
Failed to install syb-0.4.4
Build log ( /Users/nickager/.cabal/logs/syb-0.4.4.log ):
Failed to install text-1.2.0.4
Build log ( /Users/nickager/.cabal/logs/text-1.2.0.4.log ):
cabal: Error: some packages failed to install:
aeson-0.8.0.2 depends on text-1.2.0.4 which failed to install.
attoparsec-0.12.1.2 depends on text-1.2.0.4 which failed to install.
cpphs-1.18.8 depends on text-1.2.0.4 which failed to install.
dlist-0.7.1 failed during the configure step. The exception was:
user error (
In file included from
/var/folders/lj/13hckkrj5c9bjygql9tcsn940000gn/T/ghc45306_0/ghc45306_4.c:1:0:


In file included from
/nix/store/sg1y8i67c2vrqc2irydi5kyyxsf95wi2-ghc-7.8.4/lib/ghc-7.8.4/include/Rts.h:30:0:


/nix/store/sg1y8i67c2vrqc2irydi5kyyxsf95wi2-ghc-7.8.4/lib/ghc-7.8.4/include/Stg.h:65:10:
fatal error: 'math.h' file not found
#include <math.h>
^
1 error generated.
)
hashable-1.2.3.1 depends on text-1.2.0.4 which failed to install.
haskell-src-exts-1.16.0.1 depends on text-1.2.0.4 which failed to install.
mtl-2.1.3.1 failed during the configure step. The exception was:
user error (
In file included from
/var/folders/lj/13hckkrj5c9bjygql9tcsn940000gn/T/ghc45315_0/ghc45315_4.c:1:0:


In file included from
/nix/store/sg1y8i67c2vrqc2irydi5kyyxsf95wi2-ghc-7.8.4/lib/ghc-7.8.4/include/Rts.h:30:0:


/nix/store/sg1y8i67c2vrqc2irydi5kyyxsf95wi2-ghc-7.8.4/lib/ghc-7.8.4/include/Stg.h:65:10:
fatal error: 'math.h' file not found
#include <math.h>
^
1 error generated.
)
bug darwin haskell

Most helpful comment

I don't think anyone wants (or should want) /usr/include in Nix builds on macOS.

All 36 comments

cc @peti

The file math.h is typically provided by the system. I'm not sure why it's not found, and I'm not much of a Darwin whiz either. Maybe @joelteon, @copumpkin, or @jwiegley can help?

Do you have the command-line tools installed? Until we merge in pure-darwin, standard headers like that come from Apple's package.

I don't think it's a lack of command-line tools:

$ xcode-select --install
xcode-select: error: command line tools are already installed, use "Software Update" to install updates

and I can install packages from nix without errors e.g.:

$ nix-env -i hello
...
$ hello
Hello, world!

the problem is that the haskell and cabal that I've installed using nix appear to install fine but then fail with "fatal error: 'math.h' file not found" when I try to use cabal install <package> e.g.

$ cabal install aeson haskell-src-exts
.
.
fatal error: 'math.h' file not found

However if I install haskell from http://ghcformacosx.github.io then cabal install <package> works without producing header file errors. However I'd like to install Haskell using nix so I keep in sync with releases.

I just ran into this issue today, the problem is that ghc is hardcoded to use the nix-built clang-wrapper, which is patched to not look in /usr/include, but ghc doesn't tell clang where to find math.h.

As a temporary workaround you can export NIX_CFLAGS_COMPILE="-idirafter /usr/include", but this is very un-nixlike.

@peti @copumpkin I think the long-term solution is to add darwin.libsystem as a propagatedBuildInput (and perhaps propagatedUserEnvPkg?) to ghc.

To clarify, this only happens if you invoke GHC on its own which afaik is not really intended behavior because you’re always supposed to be using it in nix-shell. It’s kind of like how npm2nix won’t work on its own because it calls git but doesn’t depend on it, or how you have to use ghcWithPackages to actually get Haskell packages in scope when you’re using GHC.

On Feb 18, 2015, at 8:07 PM, Eric Seidel [email protected] wrote:

I just ran into this issue today, the problem is that ghc is hardcoded to use the nix-built clang-wrapper, which is patched to not look in /usr/include, but ghc doesn't tell clang where to find math.h.

As a temporary workaround you can export NIX_CFLAGS_COMPILE="-idirafter /usr/include", but this is very un-nixlike.

@peti @copumpkin I think the long-term solution is to add darwin.libsystem as a propagatedBuildInput (and perhaps propagatedUserEnvPkg?) to ghc.

—
Reply to this email directly or view it on GitHub.

But if it's calling clang-wrapper, clang-wrapper should take care of passing in the appropriate libSystem. The issue, I think, is that the (impure) darwin stdenv doesn't use the "official" libc-passing mechanism of the cc-wrapper, instead going for NIX_CFLAGS_COMPILE as a prehook in the stdenv. If someone wants to lift the proper libc passing from pure-darwin into master, I'd expect that to fix it.

@joelteon this seems an odd stance to take. If ghc is made available as a package you can install in your user-env, then it should work in your user-env :smile:

@copumpkin are you talking about the libc-cflags stuff?

Talking about nativeLibc = true in the call to cc-wrapper.

@gridaphobe Just saying there's a precedent for stuff that doesn't work in a user-env being installable

Expecting every Haskell developer on Nix to work inside of nix-shell all the time seems like a rather severe restriction, IMHO. I don't really understand the intricacies of the Darwin stdenv, so maybe there are compelling technical reasons for this choice, but as far as the Linux side of things is concern, we certainly don't have this limitation; ghc is intended to "just work" regardless of the environment in which you call it.

@peti nope, no good reasons, and it'll get fixed as soon as we merge the pure stdenv or someone takes the time to carry the fix over to the intermediate one

Having ghc "just work" on Darwin is mandatory. Otherwise, newcomers to Nix will think that GHC is just broken (they don't start out by creating projects to nix-shell into), and that would reflect badly both on Haskell and on Nix.

@gridaphobe Setting NIX_CFLAGS_COMPILE as you suggest doesn't seem to workaround for me. After I try that, I get:

$ ghc -o foo foo.hs
Linking foo ...
ld: library not found for -lm
clang-3.5: error: linker command failed with exit code 1 (use -v to see invocation)

Presumably I also need to update some flag that sets the LD_LIBRARY_PATH (or whatever the Darwin equivalent is).

Update:

This works:

NIX_CFLAGS_COMPILE="-idirafter /usr/include" NIX_LDFLAGS="-L/usr/lib" ghc -o foo foo.hs

I'm also having issues with Haskell and OSX.

I installed nix just fine. I then followed these steps to install haskellPackageNg:

http://stackoverflow.com/questions/27968909/how-to-get-cabal-and-nix-work-together/27970684#27970684

And when I run cabal init, I get the following error:

cabal: The program 'ghc' version >=6.4 is required but it could not be found.

Moreover, if I run cabal build in a directory where I already have a cabal file (no cabal init necessary), building the project fails at linking with the following error:

ld: library not found for -lSystem

Does this latter error have something to do with what you're saying about libSystem, @gridaphobe ?

@giogadi I think those steps are mistaken as they won't place ghc in your user environment, which seems to be required by cabal. I suggest using haskellngPackages.ghcWithPackages instead, which will pull in ghc as well as any other haskell packages you want. The simple version would look something like

my-haskell-env = haskellngPackages.ghcWithPackages (pkgs: [ pkgs.cabal-install pkgs.cabal2nix ]);

which will give you ghc, cabal-install, and cabal2nix.

I use a slightly more involved version (https://github.com/gridaphobe/dotfiles/blob/master/nixpkgs/config.nix#L111), feel free to borrow anything.

As to the libSystem issue, it might be related, but I haven't been able to reproduce it.

Hi there, I also bumped into this when trying to run cabal from the user environment using haskellngPackages.ghcWithPackages. Things work using nix-shell. I exported these flags as suggested by @jml

NIX_CFLAGS_COMPILE="-idirafter /usr/include"
NIX_LDFLAGS="-L/usr/lib"

But I get this error

$ cabal build 
...
Linking dist/build/site/site ...
ld: framework not found Cocoa
clang-3.6: error: linker command failed with exit code 1 (use -v to see invocation)

I wonder what the state of this issue is? Would it be possible to document the problem (and the current solution) on https://nixos.org/wiki/Haskell?

Does anyone feel strongly about keeping this ticket open?

I think it's okay to close it, especially if the Haskell workflow is still changing on Nix.

@jmatsushita just ran into the same.

$cabal build          
..
Linking dist/build/site/site ...
ld: framework not found Cocoa
clang-3.6: error: linker command failed with exit code 1 (use -v to see invocation)

so you used cabal2nix to create a shell and compiled from there ?

edit : it seems to be the case from what you documented here. works for me too

I'm having the OP issue as well (today). math.h not found, when using cabal and ghc from the nix store in a standard user env (not in a nix-shell).

λ Davids-MacBook-Pro fooba → cabal install -j byteable
Resolving dependencies...
Notice: installing into a sandbox located at
/Users/dmj/Desktop/fooba/.cabal-sandbox
Configuring byteable-0.1.1...
Failed to install byteable-0.1.1
Build log ( /Users/dmj/Desktop/fooba/.cabal-sandbox/logs/byteable-0.1.1.log ):
cabal: Error: some packages failed to install:
byteable-0.1.1 failed during the configure step. The exception was:
user error ('/Users/dmj/.nix-profile/bin/ghc' exited with an error:

In file included from
/var/folders/m_/z586qg2d7gg1flh7y3nvw5600000gn/T/ghc86060_0/ghc86060_4.c:1:0:


In file included from
/nix/store/rwka3g6157dj636zsxvggzrkkh33pwil-ghc-7.10.1/lib/ghc-7.10.1/include/Rts.h:30:0:


/nix/store/rwka3g6157dj636zsxvggzrkkh33pwil-ghc-7.10.1/lib/ghc-7.10.1/include/Stg.h:69:10:
fatal error: 'math.h' file not found
#include <math.h>
^
1 error generated.
)

Is the recommended solution still (since February) to export this environment variable?

export NIX_CFLAGS_COMPILE="-idirafter /usr/include

Update, this fixed it (in my bashrc):

export NIX_CFLAGS_COMPILE="-idirafter /usr/include"                                                                                                                             
export NIX_CFLAGS_LINK="-L/usr/lib"  

I documented the problem and the work-around at https://github.com/NixOS/cabal2nix/blob/master/doc/user-guide.md#builds-on-darwin-fail-with-mathh-not-found.

My understanding of this issue so far is that ghc will not find header files located in /usr/include on Darwin --- which is good ---, but unfortunately the system header math.h resides in /usr/include, which is bad. Now, the work-around simply sledgehammers the impure location into the global search path to avoid the problem. This is not exactly pretty, but we don't have a better solution until the time there is a pure stdenv for Darwin.

@cstrahan, @copumpkin, does that summary sound about right?

I'd check with @gridaphobe, @spwhitt, and @pikajude, as I think they have far more recent exposure to the darwin stdenv here than I do!

Might be better to put that blurb on the NixOS wiki page where it's more likely to be discovered.

The Wiki page is going to be replaced by the "user-guide" document soon.

Status of the issue: If you simply install GHC and use it, it fails to find math.h, meaning you can't compile anything outside of a project that uses shell.nix or some such. Now, perhaps I see this because I installed my global GHC using myEnvFun, I'm not sure.

Here is an up-to-date link to the documentation of this issue: http://hydra.nixos.org/job/nixpkgs/trunk/manual/latest/download-by-type/doc/manual#builds-on-darwin-fail-with-math.h-not-found.

@nrolland @jmatsushita I am still running into this same error:

Linking dist/build/site/site ...
ld: framework not found Cocoa
clang-3.7: error: linker command failed with exit code 1 (use -v to see invocation)

Did you ever solve it in your case?

You need to add frameworks.Cocoa to your buildInputs. I don't know which expression it needs to be added to because I don't know the Nix+Haskell ecosystem very well.

@copumpkin Thanks, LnL in the #nixos channel gave me similar advice. I remedied it in my case by adding pkgs.darwin.apple_sdk.frameworks.Cocoa to executableHaskellDepends.

@Hodapp87, executableHaskellDepends is not the right place to do this: that attribute is for dependencies that are written in _Haskell_. System dependencies that contain libraries should go into executableSystemDepends and system dependencies that contain executables should go into executableToolDepends.

(triage) any updates?

@domenkozar I think this can be closed

to get /usr/include on OSX, one should how install additional package: /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg

similar and related issue was posted here: https://forums.developer.apple.com/thread/104296

and release notes: https://developer.apple.com/documentation/xcode_release_notes/xcode_10_release_notes

I don't think anyone wants (or should want) /usr/include in Nix builds on macOS.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tomberek picture tomberek  Â·  3Comments

copumpkin picture copumpkin  Â·  3Comments

domenkozar picture domenkozar  Â·  3Comments

edolstra picture edolstra  Â·  3Comments

rzetterberg picture rzetterberg  Â·  3Comments