New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
NickAger opened this Issue Feb 17, 2015 · 34 comments

Comments

Projects
None yet
@NickAger

NickAger commented Feb 17, 2015

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!

  1. 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

  1. 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.

  1. 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.
)
@domenkozar

This comment has been minimized.

Member

domenkozar commented Feb 17, 2015

cc @peti

@peti

This comment has been minimized.

Member

peti commented Feb 17, 2015

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?

@copumpkin

This comment has been minimized.

Member

copumpkin commented Feb 17, 2015

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

@NickAger

This comment has been minimized.

NickAger commented Feb 17, 2015

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.

@gridaphobe

This comment has been minimized.

Contributor

gridaphobe commented Feb 19, 2015

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.

@pikajude

This comment has been minimized.

Contributor

pikajude commented Feb 19, 2015

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 notifications@github.com 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.

@copumpkin

This comment has been minimized.

Member

copumpkin commented Feb 19, 2015

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.

@gridaphobe

This comment has been minimized.

Contributor

gridaphobe commented Feb 19, 2015

@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 😄

@gridaphobe

This comment has been minimized.

Contributor

gridaphobe commented Feb 19, 2015

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

@copumpkin

This comment has been minimized.

Member

copumpkin commented Feb 19, 2015

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

@pikajude

This comment has been minimized.

Contributor

pikajude commented Feb 19, 2015

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

@peti

This comment has been minimized.

Member

peti commented Feb 20, 2015

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.

@copumpkin

This comment has been minimized.

Member

copumpkin commented Feb 21, 2015

@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

@jwiegley

This comment has been minimized.

Contributor

jwiegley commented Feb 21, 2015

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.

@jml

This comment has been minimized.

Contributor

jml commented Feb 28, 2015

@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
@giogadi

This comment has been minimized.

Contributor

giogadi commented Mar 13, 2015

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 ?

@gridaphobe

This comment has been minimized.

Contributor

gridaphobe commented Mar 16, 2015

@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.

@jmatsushita

This comment has been minimized.

jmatsushita commented Mar 27, 2015

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)
@peti

This comment has been minimized.

Member

peti commented May 29, 2015

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?

@giogadi

This comment has been minimized.

Contributor

giogadi commented May 29, 2015

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

@nrolland

This comment has been minimized.

nrolland commented May 31, 2015

@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

@dmjio

This comment has been minimized.

Contributor

dmjio commented Jun 3, 2015

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"  
@peti

This comment has been minimized.

Member

peti commented Jun 4, 2015

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?

@peti peti changed the title from installed ghc-7.8.4 on MacOSX Yosemite but then cabal install fails with "fatal error: 'math.h' file not found" to GHC on MacOSX Yosemite can't include <math.h> because it lives in /usr/include, which isn't searched Jun 4, 2015

@copumpkin

This comment has been minimized.

Member

copumpkin commented Jun 4, 2015

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!

@Fuuzetsu

This comment has been minimized.

Member

Fuuzetsu commented Jun 4, 2015

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

@peti

This comment has been minimized.

Member

peti commented Jun 4, 2015

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

@jwiegley

This comment has been minimized.

Contributor

jwiegley commented Jun 10, 2015

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.

@peti

This comment has been minimized.

Member

peti commented Aug 9, 2015

@peti peti added the 0.kind: bug label Aug 9, 2015

@Hodapp87

This comment has been minimized.

Contributor

Hodapp87 commented Jan 11, 2016

@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?

@copumpkin

This comment has been minimized.

Member

copumpkin commented Jan 11, 2016

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.

@Hodapp87

This comment has been minimized.

Contributor

Hodapp87 commented Jan 11, 2016

@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.

@peti

This comment has been minimized.

Member

peti commented Jan 11, 2016

@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.

@Profpatsch

This comment has been minimized.

Member

Profpatsch commented Aug 16, 2016

(triage) any updates?

@matthewbauer

This comment has been minimized.

Member

matthewbauer commented May 20, 2017

@domenkozar I think this can be closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment