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).
- install dependencies:
sudo apt install libicu-dev libncurses-dev libgmp-dev libtinfo-dev
- install Stack:
curl -sSL https://get.haskellstack.org/ | sh
- download VSCode and install it:
sudo apt install ./<file>.deb
- compile and install HIE:
git clone https://github.com/haskell/haskell-ide-engine --recurse-submodules
cd haskell-ide-engine
./install.hs build
run VSCode and install the
Haskell Language Server
extensionconfigure the extension or the
PATH
environment variable (using$HOME/.local/bin
and$(stack path --compiler-bin)
)
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.
- install some Debian dependencies:
sudo apt install curl rsync build-essential libgmp-dev
- install and configure Nix:
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
- allow non-free software, in
~/.config/nixpkgs/config.nix
:
{ allowUnfree = true; }
- install cachix and activate the HIE cache:
nix-env -iA nixpkgs.cachix
cachix use all-hies
- add an overlay for HIE, in
~/.config/nixpkgs/overlays/hies.nix
:
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; }; };
}
- install Stack, VSCode and HIE:
nix-env -iA nixpkgs.stack nixpkgs.vscode nixpkgs.hies
- run VSCode and install the
Haskell Language Server
extension
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.
- allow non-free software, in
~/.config/nixpkgs/config.nix
:
{ allowUnfree = true; }
- install cachix and activate the HIE cache:
nix-env -iA nixpkgs.cachix
cachix use all-hies
- add an overlay for HIE, in
~/.config/nixpkgs/overlays/hies.nix
:
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; }; };
}
- activate VSCode and HIE, in
~/.config/nixpkgs/home.nix
:
{
programs.vscode = enable = true;
haskell = {
enable = true;
hie = {
enable = true;
executablePath = "${pkgs.hies}/bin/hie-wrapper";
};
};
};
- update the user configuration:
home-manager switch
And that’s all. HIE integration in VSCode is automatic.
Test with a Haskell project
- create a Haskell project and run VSCode:
stack new myproject --resolver=lts-14
cd myproject
code .
when VSCode is launched, try code navigation, code completion, compilation with stack inside the VSCode console…
if HIE gives an error in VSCode, delete the
.cache/cabal-helper
folder