Can cabal/stack/nix be used to compile into dynamic library?

Viewed 118

I'm trying to build shared library containing exported ffi code into shared library. I wonder if it is possible to do it with common tools like stack or nix (so basically with cabal)? Is it? Then how?

https://github.com/bennoleslie/haskell-shared-example contains good example of such code, but with manual build instructions.

1 Answers

The .cabal file stanza you're looking for is foreign-library. The documentation describes it very well.

As for nix, if you use haskell.nix, a derivation for the foreign library will be exposed under <binding>.components.foreignlibs.<libname>.

To build with cabal2nix, run

cabal2nix . > default.nix
nix-build -E '(import <nixpkgs> {}).haskellPackages.callPackage ./default.nix {}'

Your shared library will be under result/lib/ghc-<version>/<your-lib>.(so|dll|dylib).

Related