I wanted to build from source digest, a Haskell package that depends on the usual zlib system library. I used cabal executable installed via nix-env _(from cabal-install package)_. I also installed zlib in the same fashion, which made the headers and binary library objects available somewhere in /nix/store.
As advised in NixOS wiki, I set export CPATH=~/.nix-profile/include; export LIBRARY_PATH=~/.nix-profile/lib, but nevertheless cabal errored out:
% cd ~/code/haskell-src/digest
% cabal new-build
Build profile: -w ghc-8.2.2 -O1
In order, the following will be built (use -v for more details):
- digest-0.0.1.2 (lib:digest) (configuration changed)
Configuring digest-0.0.1.2...
cabal: Missing dependency on a foreign library:
* Missing (or bad) header file: zlib.h
This problem can usually be solved by installing the system package that
provides this library (you may need the "-dev" version). If the library is
already installed but in a non-standard location then you can use the flags
--extra-include-dirs= and --extra-lib-dirs= to specify where it is.If the
library file does exist, it may contain errors that are caught by the C
compiler at the preprocessing stage. In this case you can re-run configure
with the verbosity flag -v3 to see the error messages.
If the header file does exist, it may contain errors that are caught by the C
compiler at the preprocessing stage. In this case you can re-run configure
with the verbosity flag -v3 to see the error messages.
I dug in and discovered that there is no directory ~/.nix-profile/include at all. I created it and manually simlinked zlib headers in. _(To be specific, there are two headers in zlib: zlib.h & zconf.h, and I linked both of them.)_ It solved the problem.
I wonder if these headers could be put in place by default.
Install ghc, cabal-install, zlib. Download the source for digest. Enter the digest source directory and say cabal new-build.
"x86_64-linux"Linux 4.14.56-1-lts, Arch Linux, noversionyesnonix-env (Nix) 2.0"nixpkgs-18.09pre137268.63a865c403c"/nix/var/nix/profiles/per-user/root/channels/nixpkgszlib is used by many libraries already without any problem. zlib library is a multioutput library (see the doc for more details) so your dependency should include zlib.dev :
$ nix-build -A zlib.dev ~/nixpkgs
As a general tip, you can install nix-locate via nix-env and then run nix-locate zlib.h to find the correct output.
@teto There must be a misunderstanding. I am not defining a package or derivation myself. What I am doing is manually building stuff using cabal that happens to be installed via Nix. So I do not think your advices are applicable.
By the way, when I say nix-env --query --available '.*zlib.*', nothing similar to zlib.dev would appear.
Is there a particular reason to refrain from symlinking those files?
You should be able to run this in a nix-shell:
nix-shell '<nixpkgs>' -A haskellPackages.digest
dev outputs are not installed in a profile.
The way things are designed, nix-env is suitable for set of programs to be used and nix-shell is a much better fit for "development environments" (getting headers, various env-vars set up, etc.)
For Haskell in particular, I suppose you'd best start by trying what's in the manual: https://nixos.org/nixpkgs/manual/#how-to-create-a-development-environment
@vcunat Thank you for clarifying. I think I was misled by the following passage from the NixOS wiki _(also linked to in my initial post)_:
With Nix, you can install the needed development tools into your profile (gcc-wrapper, gnumake), and the dependencies for the source you want to build (libpng, qt, ...). Once that done, set the environment variables (for gcc):
CPATH=~/.nix-profile/include; LIBRARY_PATH=~/.nix-profile/lib; QTDIR=~/nix-profile... And then you should get your build running.
From reading this section, I had had the impression that I could install zlib and cabal, set the environment variables as advised and everything will be prepared for building things. _(Otherwise, what is the point of setting the environment variable CPATH?)_ I do not think this problem is specific to Haskell, since the headers missing are C headers from zlib _(and not, for example, ghc's type checker's output)_ and the error is reported by ld _(and Cabal faithfully passes it to the terminal if set to be verbose)_. In fact, Haskell packages that do not require C libraries build just fine with this set-up.
So, is it possible that there is inconsistency between what is written in the wiki and what is actually happening?
Yeah, that wiki page is misleading. It should also mention steam-run (a FHS-compatible env via Linux mount namespaces).
Thank you for your contributions.
This has been automatically marked as stale because it has had no activity for 180 days.
If this is still important to you, we ask that you leave a comment below. Your comment can be as simple as "still important to me". This lets people see that at least one person still cares about this. Someone will have to do this at most twice a year if there is no other activity.
Here are suggestions that might help resolve this more quickly: