Configure VSCode for Haskell (Debian/Nix/NixOS)

See also: youtube video (french) - peertube video (french) - journal linuxfr (french) - version française

Like many programming languages, Haskell has no official IDE. However, there are some classic tools: Emacs + Intero/Dante, Vim + Ghcid, IntelliJ-Haskell…

Recently, VSCode (combined with HIE and Stack) is gaining more and more attention. It has many of the features that we can expect in an IDE: syntax highlighting, code navigation, documentation, completion… This post explains how to install and configure VSCode for Haskell, using Debian, Debian + Nix and NixOS.

Overview

Visual Studio Code is a text editor, supported by Microsoft. VSCode is an open-source project but we generally install a non-free binary (alternatively, we can install VS Codium which has a free license, in the same manner as Chrome/Chromium).

The Language Server Protocol (LSP)), also supported by Microsoft, is a protocol for implementing development tools more easily.

Haskell IDE Engine (HIE) is a backend implementing LSP for Haskell.

Haskell Language Server Client is an extension for using HIE inside VSCode.

Finally, Stack is a classic tool for packaging and building Haskell projects. Stack can be used in VSCode and is needed by HIE.

Using Debian

The toolchain “VS code + HIE + HIE client + Stack” is not very easy to install on Debian. The tools are not present in the package manager (or only old versions), so we have to install them manually and even to compile some of them (HIE requires at least 4 GB RAM and 1h compilation time).

sudo apt install libicu-dev libncurses-dev libgmp-dev libtinfo-dev 
curl -sSL https://get.haskellstack.org/ | sh
sudo apt install ./<file>.deb
git clone https://github.com/haskell/haskell-ide-engine --recurse-submodules
cd haskell-ide-engine
./install.hs build

And then, the toolchain is supposed to work.

Using Debian and Nix

Nix is a package manager which can be used on any Linux distribution and on macOS. Nix also has the Cachix tool, which let us install our Haskell/VSCode toolchain in a few minutes only.

sudo apt install curl rsync build-essential libgmp-dev
curl https://nixos.org/nix/install | sh
nix-channel --add https://nixos.org/channels/nixos-19.09 nixpkgs
nix-channel --update
echo ". $HOME/.nix-profile/etc/profile.d/nix.sh" >> ~/.bashrc
source ~/.bashrc
{ allowUnfree = true; }
nix-env -iA nixpkgs.cachix
cachix use all-hies
self: super: 
let 
  all-hies = import (fetchTarball "https://github.com/infinisil/all-hies/tarball/master") {};
in {
  hies = all-hies.selection { selector = p: { inherit (p) ghc865 ghc844; }; };
}
nix-env -iA nixpkgs.stack nixpkgs.vscode nixpkgs.hies

Using NixOS and home-manager

NixOS is a Linux distribution based on Nix. Home-manager is a user environment manager, also based on Nix, which makes the installation even simpler.

{ allowUnfree = true; }
nix-env -iA nixpkgs.cachix
cachix use all-hies
self: super: 
let 
  all-hies = import (fetchTarball "https://github.com/infinisil/all-hies/tarball/master") {};
in {
  hies = all-hies.selection { selector = p: { inherit (p) ghc865 ghc844; }; };
}
  programs.vscode = {
    enable = true;
    haskell = {
      enable = true;
      hie = {
         enable = true;
         executablePath = "${pkgs.hies}/bin/hie-wrapper";
      };
    };
  };
home-manager switch

And that’s all. HIE integration in VSCode is automatic.

Test with a Haskell project

stack new myproject --resolver=lts-14
cd myproject
code .