/haskell-project-template

Haskell project template using nix, cabal and haskell.nix.

Primary LanguageNix

Haskell Project Template

This haskell project template contains a basic haskell setup using nix flakes and IOHK’s haskell.nix.

Preconditions

For using nix flakes:

Optional: you can skip this step in case you simply want to use nix-shell without flakes, see troubleshooting section.

Nix Flakes are still experimental. To use them, you’ll have to enable them in your /etc/nixos/configuration.nix:

nix = {
  package = pkgs.nixFlakes;
  extraOptions = ''
    experimental-features = nix-command flakes
  '';
};

Use IOHK binary cache

To avoid compiling GHC, add IOHK’s binary cache to /etc/nixos/configuration.nix:

nix = {
  binaryCachePublicKeys = [
    "hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ="
  ];
  binaryCaches = [
    "https://hydra.iohk.io"
  ];
};

Usage

  1. Create new repository from template
    $ export REPO="somerepo" 
    $ git init "${REPO}" && cd "${REPO}" \
        && git commit --allow-empty -m "initial" \
        && git pull --allow-unrelated-histories --squash git@github.com:j0xaf/haskell-project-template.git \
        && rm README.org \
        && nix-shell --run 'nix --experimental-features "nix-command flakes" flake lock' -p nixFlakes \
        && nix-shell --run "cabal init --minimal --non-interactive --package-name=${REPO}" -p cabal-install ghc \
        && git add -A \
        && git commit --amend --no-edit
        
  2. (optional) If using lorri and direnv:
    $ lorri init
    $ direnv allow
        

3a. If using nix flakes: Enter the shell

$ nix develop

If you are not using lorri, please look into troubleshooting section for how to retail garbage collection root. If you are using lorri, the provided shell.nix will be monitored by lorri and it will also maintain a gc root for you.

3b. If using nix-shell: Enter the shell

$ nix-shell

If using lorri a gc root will be maintained for you.

Troubleshooting

Error “git tree is dirty”

If you see an error like “git tree is dirty” when entering nix develop (or nix-shell), like

warning: Git tree ‘/home/j0xaf/git/github.com/j0xaf/haskell-lsp-example’ is dirty

then you need to add your .cabal file to the git index (git add <projectname>.cabal should be sufficient).

Using lorri

Unfortunately, github:nix-community/lorri does not yet support nix flakes. For details see github:target/lorri#460. Thankfully, there is a workaround using github:edolstra/flake-compat.

A shell.nix is provided for this compatibility shim. This makes it also possible to use lorri, which keeps garbage collection roots for your projects.

To use lorri:

  1. install according to github:nix-community/lorri
  2. in the project

Garbage collection without lorri

To prevent garbage collection while using nix develop there is unfortunately not yet the perfect solution.

github:NixOS/nix#4250 suggests using profiles to manage gc roots:

$ nix develop --profile .nixprofile/foo # create profile
$ nix develop .nixprofile/foo # re-enter previously created profile