use cabal2nix to create local nix environment for packages that aren't in nixpkgs

2.4k views Asked by At

I currently have a Yesod web application that I am developing on NixOS. I am using a default.nix file like described in O'Charles' blog post:

{ haskellPackages ? (import <nixpkgs> {}).haskellPackages }:

haskellPackages.cabal.mkDerivation (self: {
  pname = "myblog";
  version = "0.0.0";
  src = ./.;
  isLibrary = true;
  isExecutable = true;
  buildDepends = with haskellPackages; [
    aeson bson conduit dataDefault fastLogger hjsmin
    httpConduit monadControl monadLogger mongoDB persistent
    #persistentMongoDB
    persistentTemplate poolConduit shakespeare text
    waiExtra waiLogger warp yaml yesod yesodAuth
    yesodCore yesodDefault yesodForm yesodStatic
  ];
  testDepends = with haskellPackages; [
    hspec monadLogger persistent
    #persistentMongoDB
    resourcet transformers yesod yesodCore yesodTest
  ];
  buildTools = with haskellPackages; [
    cabalInstall ghcMod yesodBin
  ];
  jailbreak = true;
})

This is working very well. I can use the yesod binary and cabal like normal. However, I am running into one problem. When running cabal haddock, I am getting the following error:

Running Haddock for mycoolblog-0.0.0...
Preprocessing library mycoolblog-0.0.0...
Warning: The documentation for the following packages are not installed. No
links will be generated to these packages: persistent-mongoDB-2.1.1
<command line>: cannot satisfy -package-id aeson-0.8.0.2-9e93bdd6f8e68379314dd5f31414f87f
    (use -v for more information)

Googling about this brings up the following link: github: cabal haddock 'cannot satisfy -package-id' #2468. It seems like it is saying that ghcWithPackages should be used.

Googling about ghcWithPackages brings up O'Charles' (personal?) wiki on Nix. It has an example of using ghcWithPackages to develop a package that is already in nixpkgs, but I can't get it to work with a package that is not already in nixpkgs (my cms/blog).

Setting it up like described on O'Charles' wiki and running nix-shell -v, I get the following error:

evaluating file `/nix/store/an4sg7cxi1ii3vids8417kajl3ss13vd-nix-1.7/share/nix/corepkgs/derivation.nix'
error: cannot auto-call a function that has an argument without a default value (`cabal')

Why am I getting the above error? How can I write a default.nix and shell.nix for a Haskell project that's not already in nixpkgs? Ideally it would be using ghcWithPackages so that cabal haddock works.

1

There are 1 answers

1
illabout On BEST ANSWER

I was able to get this working after using the method on O'Charles' Nix wiki. I have not determined why this method was not working before.

Basically, first run cabal2nix. Then create a generic shell.nix (that can be shared between all Haskell packages).

$ cabal2nix --jailbreak --sha256=X ./$cabal > default.nix
$ sed -i 's#sha = .*#src = builtins.filterSource (path: type: type != "unknown") ./.;#' default.nix
$ cat - > shell.nix <<EOF
let
  pkgs = import <nixpkgs> {};
  haskellPackages = pkgs.haskellPackages.override {
    extension = self: super: {
      thispackage = self.callPackage ./. {};
    };
  };
in pkgs.myEnvFun {
     name = haskellPackages.thispackage.name;
     buildInputs = [
       (haskellPackages.ghcWithPackages (hs: ([
         hs.cabalInstall
         hs.hscolour
         hs.ghcMod
       ] ++ hs.thispackage.propagatedNativeBuildInputs)))
     ];
   }      
EOF
$ nix-shell