git.iko.soy/typeable/cabal2nix
on master help sign in
doc: remember some text that describes how to use Stack with Nix
commit1d38968572db20c9e260adb0d3fe7e94b3c9abe2 authorPeter Simons <simons@cryp.to> dateThu 3 Sep 2015, 13:20 (11 years ago) parent249c4d2
1 file changed+64
cabal2nix/doc/user-guide.md+64
cabal2nix/doc/user-guide.md+64
@@ -538,6 +538,70 @@ can configure the environment variables
538538
539539in their `~/.bashrc` file to avoid the compiler error.
540540
541+## Using Stack together with Nix
542+
543+> -- While building package zlib-0.5.4.2 using:
544+> /home/simons/.nix-profile/bin/runhaskell -package=Cabal-1.22.4.0 -clear-package-db -global-package-db -package-db=/home/simons/.stack/snapshots/x86_64-linux/nightly-2015-08-06/7.10.2/pkgdb/ /tmp/stack14237/Setup.hs --builddir=.stack-work/dist/x86_64-linux/Cabal-1.22.4.0/ configure --user --package-db=clear --package-db=global --package-db=/home/simons/.stack/snapshots/x86_64-linux/nightly-2015-08-06/7.10.2/pkgdb/ --dependency=base=base-4.8.1.0-4f7206fd964c629946bb89db72c80011 --dependency=bytestring=bytestring-0.10.6.0-18c05887c1aaac7adb3350f6a4c6c8ed --libdir=/home/simons/.stack/snapshots/x86_64-linux/nightly-2015-08-06/7.10.2/lib --bindir=/home/simons/.stack/snapshots/x86_64-linux/nightly-2015-08-06/7.10.2/bin --datadir=/home/simons/.stack/snapshots/x86_64-linux/nightly-2015-08-06/7.10.2/share --docdir=/home/simons/.stack/snapshots/x86_64-linux/nightly-2015-08-06/7.10.2/doc/zlib-0.5.4.2 --htmldir=/home/simons/.stack/snapshots/x86_64-linux/nightly-2015-08-06/7.10.2/doc/zlib-0.5.4.2 --haddockdir=/home/simons/.stack/snapshots/x86_64-linux/nightly-2015-08-06/7.10.2/doc/zlib-0.5.4.2 --extra-lib-dirs=/nix/store/alsvwzkiw4b7ip38l4nlfjijdvg3fvzn-zlib-1.2.8/lib
545+> Process exited with code: ExitFailure 1
546+> Logs have been written to: /home/simons/src/stack-ide/.stack-work/logs/zlib-0.5.4.2.log
547+
548+> Configuring zlib-0.5.4.2...
549+> Setup.hs: Missing dependency on a foreign library:
550+> * Missing (or bad) header file: zlib.h
551+> This problem can usually be solved by installing the system package that
552+> provides this library (you may need the "-dev" version). If the library is
553+> already installed but in a non-standard location then you can use the flags
554+> --extra-include-dirs= and --extra-lib-dirs= to specify where it is.
555+> If the header file does exist, it may contain errors that are caught by the C
556+> compiler at the preprocessing stage. In this case you can re-run configure
557+> with the verbosity flag -v3 to see the error messages.
558+
559+When you run the build inside of the nix-shell environment, the system
560+is configured to find libz.so without any special flags -- the compiler
561+and linker "just know" how to find it. Consequently, Cabal won't record
562+any search paths for libz.so in the package description, which means
563+that the package works fine inside of nix-shell, but once you leave the
564+shell the shared object can no longer be found. That issue is by no
565+means specific to "stack": you'll have that problem with any other
566+Haskell package that's built inside of nix-shell but run outside of that
567+environment.
568+
569+I suppose we could try to remedy the issue by wrapping "stack" or
570+"cabal" with a script that tries to find those kind of implicit search
571+paths and makes them explicit on the "cabal configure" command line. I
572+don't think anyone is working on that subject yet, though, because the
573+problem doesn't seem so bad in practice.
574+
575+You can remedy that issue in several ways. First of all, run
576+
577+ $ nix-build --no-out-link "<nixpkgs>" -A zlib
578+ /nix/store/alsvwzkiw4b7ip38l4nlfjijdvg3fvzn-zlib-1.2.8
579+
580+to find out the store path of the system's zlib library. Now, you can
581+
582+ 1) add that path (plus a "/lib" suffix) to your $LD_LIBRARY_PATH
583+ environment variable to make sure your system linker finds libz.so
584+ automatically. It's no pretty solution, but it will work.
585+
586+ 2) As a variant of (1), you can also install any number of system
587+ libraries into your user's profile (or some other profile) and point
588+ $LD_LIBRARY_PATH to that profile instead, so that you don't have to
589+ list dozens of those store paths all over the place.
590+
591+ 3) The solution I prefer is to call stack with an appropriate
592+ --extra-lib-dirs flag like so:
593+
594+ $ stack --extra-lib-dirs=/nix/store/alsvwzkiw4b7ip38l4nlfjijdvg3fvzn-zlib-1.2.8/lib build
595+
596+ Typically, you'll need --extra-include-dirs as well. It's possible
597+ to add those flag to the project's "stack.yaml" or your user's
598+ global "~/.stack/global/stack.yaml" file so that you don't have to
599+ specify them manually every time.
600+
601+ The same thing applies to "cabal configure", of course, if you're
602+ building with cabal-install instead of Stack.
603+
604+
541605# Other resources
542606
543607- The Youtube video [Nix Loves Haskell](https://www.youtube.com/watch?v=BsBhi_r-OeE)