Scaffold a Haskell project w/ IDE support + Nix

Update (May 31, 2021): If you use Flakes, try haskell-template.

I create new Haskell libraries and applications using Nix, along with IDE support in VSCode as follows.

First, install nix-thunk (alternatively, you may use niv or flakes).

# Initialize project layout using cabal
mkdir mypkg && cd mypkg
# Note: Pass --lib, --exe or --libandexe as appropriate
nix-shell -p cabal-install -p ghc --run \
  "cabal init --cabal-version=3.0 -m -l BSD3 --lib -p mypkg"

# Add to git
git init && git add . && git commit -m "Initial commit"

# Remove, or update, version constraint on base to match compiler
vim *.cabal  # and remove version constraint on `base`

# Pin nixpkgs, etc
# You might want to pass --rev, using the value from
nix-thunk create \ dep/nixpkgs
nix-thunk create \ dep/gitignoresrc

# Write template default.nix
cat << EOF > default.nix
{ pkgs ? import ./dep/nixpkgs {} }:
  inherit (import ./dep/gitignoresrc { inherit (pkgs) lib; }) gitignoreSource;
  pkgs.haskellPackages.developPackage {
    name = "mypkg";
    root = gitignoreSource ./.;
    modifier = drv:
      pkgs.haskell.lib.addBuildTools drv (with pkgs.haskellPackages;
        [ cabal-install

# Test your changes
nix-shell --run 'cabal-fmt -i *.cabal'  # Formats your cabal file
nix-shell --run 'ghcid -T :main'

# Create .gitignore and commit
echo -e "dist-newstyle\nresult" > .gitignore
git add . && git commit -m "Nixify"

To enable IDE support,

  • Copy .vscode template to ./.vscode (note: settings.json should point to default.nix instead of shell.nix)
  • Add the appropriate hie.yaml
  • Test your configuration by running nix-shell --run haskell-language-server (it should succeed with module-level reports)
  • Open the folder in VSCode and follow the instructions

Other things you might want to do:


Links to this page