Skip to content

Instantly share code, notes, and snippets.

@aaronlevin
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save aaronlevin/a45747c69659fbf352a2 to your computer and use it in GitHub Desktop.

Select an option

Save aaronlevin/a45747c69659fbf352a2 to your computer and use it in GitHub Desktop.
nix learnings

Recently had some trouble with nix. I was trying to write a Haskell ffi wrap around the micro-ecc lib. After doing this successfully, I wanted my library to work if micro-ecc was correctly installed somewhere on your system. I had the following two directories:

$ ls
micro-ecc
hs-micro-ecc

In micro-ecc I had a fairly vanilla nix expression that looked like:

stdenv.mkDerivation {

  name = "micro-ecc";
  
  buildPhase = ''gcc stuff'';
  installPhase = ''
    mkdir -p $out/micro-ecc/{lib,include}
    cp uECC.h $out/micro-ecc/include
    cp libmicro-ecc.so $out/micro-ecc/include
  '';

}

In hs-micro-ecc had a standard haskell ffi setup. I was getting all kinds of issues. Turns out it was the following:

  1. "installing" a library on your system means having the .so files in a /lib subdirectory in your package, in addition to any .h files in a /include sub directory. This is standard faire and not necessarily nix related.
  2. For haskell ffi setups that assume a library is installed somewhere, you need to put the name of the library in the extra-libraries directive of your .cabal file. The name of this library should correspond to the name of the .so file with lib prepended. So, for the micro-ecc library you need libmicro-ecc.so installed somewhere.
  3. Nix will ensure libmicro-ecc.so is installed and provided to cabal if you use the cabal.mkDerivation and in there specify micro-ecc ala extraLibraries = [ micro-ecc ];. By doing this the /include and /lib directories will be included during linking via -I and -L.

With the above, I was able to write my ffi wrappers with #include <micro-ecc/uECC.h> and foreign import ccall "uECC.h uECC_make_key" and everything worked fine.

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