This haskell project template contains a basic haskell setup using nix flakes and IOHK’s haskell.nix.
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
'';
};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"
];
};- 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
- (optional) If using
lorrianddirenv:$ lorri init $ direnv allow
3a. If using nix flakes: Enter the shell
$ nix developIf 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-shellIf using lorri a gc root will be maintained for you.
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).
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:
- install according to github:nix-community/lorri
- in the project
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