Buffer Yard
Language setup

Emacs Haskell Setup: HLS, GHCup and an Interactive GHCi

Updated

The first time you open a .hs file in a fresh Emacs, nothing much happens: fundamental-mode, no colours, no types. That is the right starting point, because the order in which you fix it matters. A Haskell setup breaks most often at the toolchain level, long before any Emacs Lisp is involved, so this guide starts outside Emacs and works inward.

Get the toolchain from GHCup

GHCup is the installer the Haskell community points newcomers to, and it is the least painful way to keep GHC, cabal, Stack and haskell-language-server in versions that agree with each other. That agreement is the whole point. HLS has to be built for the exact GHC version your project uses, and GHCup knows which combinations exist.

curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh
ghcup tui

The text interface lets you install a GHC, set it as the default and pick a matching HLS. A tick next to an HLS version means it supports the GHC you selected. If you only remember one thing from this page, make it that check: a mismatched HLS starts, prints a warning nobody reads, and then gives you no diagnostics at all.

GHCup puts its binaries in ~/.ghcup/bin. A terminal will see them after the installer edits your shell profile. Emacs started from a desktop launcher often will not, which is the single most common reason a Haskell config “does nothing”.

Teach Emacs where the binaries live

On macOS and on some Linux desktops, the cleanest fix is the exec-path-from-shell package, which copies PATH from your login shell. If you would rather avoid a package for one line, add the directory by hand:

(let ((ghcup (expand-file-name "~/.ghcup/bin")))
  (add-to-list 'exec-path ghcup)
  (setenv "PATH" (concat ghcup path-separator (getenv "PATH"))))

Setting both exec-path and the PATH environment variable matters. Eglot uses exec-path to find the server, but subprocesses started by the server, such as GHC itself, read PATH.

haskell-mode and the language server

haskell-mode is the long-standing major mode for Haskell and it is still the one to install. It handles indentation, highlighting, imports and the interactive REPL integration covered below. Emacs does not ship a built-in Haskell tree-sitter mode in its core releases, so unlike the Rust setup there is no -ts-mode decision to make here.

Eglot already knows about haskell-mode and will launch haskell-language-server-wrapper --lsp when you call it. The wrapper reads your project, works out the GHC version and starts the matching server binary.

(use-package haskell-mode
  :ensure t
  :hook ((haskell-mode . eglot-ensure)
         (haskell-mode . interactive-haskell-mode))
  :config
  (setq haskell-process-type 'auto))

(use-package eglot
  :config
  (setq-default eglot-workspace-configuration
                '(:haskell (:formattingProvider "ormolu"))))

The first start on a large project is slow. HLS has to type-check every module before it can answer anything, and for a few hundred modules that can take minutes. Watch the mode line: Eglot shows progress there, and once it settles, M-x eglot-find-typeDefinition, hover documentation through ElDoc and M-x eglot-code-actions all work.

Code actions are where HLS earns its memory use. It offers to add missing imports, insert type signatures for top-level bindings, apply HLint suggestions and fill in typed holes. Bind eglot-code-actions to something short; Buffer Yard’s sample config puts it on C-c a, and the keybindings guide explains why the C-c plus letter space is yours to use.

GHCi inside Emacs

Diagnostics tell you a program typechecks. They do not tell you what foldr actually does to your list. For that you want GHCi, and interactive-haskell-mode puts it in a buffer that knows about your source file.

With the hook above active, open a module and press C-c C-l. That runs haskell-process-load-file, which starts a session if none exists and loads the current file. C-c C-z jumps to the REPL buffer. Compile errors from the load show up as links you can follow back to the source.

haskell-process-type 'auto makes haskell-mode guess between cabal repl, stack ghci and plain ghci by looking for project files. When the guess is wrong (a directory with both stack.yaml and a cabal.project will confuse it), set the variable per project in a .dir-locals.el instead of fighting it globally.

Running GHCi and HLS together means two copies of your project in memory. On a laptop with 8 GB this gets tight for big codebases. The usual compromise is to keep HLS always on and start GHCi only when you are actually experimenting.

Formatting with ormolu or fourmolu

Haskell formatting is one of those topics where a team decides once and nobody wants to revisit it. Ormolu has no configuration at all by design. Fourmolu started as a fork of it and adds options for indentation width, comma placement and import style.

HLS includes plugins for both, so you do not need a separate Emacs package. The workspace setting in the snippet picks ormolu; change the string to "fourmolu" to switch, and fourmolu will pick up a fourmolu.yaml from the project root. Then format with M-x eglot-format-buffer, or on save:

(add-hook 'haskell-mode-hook
          (lambda ()
            (add-hook 'before-save-hook #'eglot-format-buffer nil t)))

The nil t at the end makes the hook buffer-local. Leave that out and every buffer in Emacs tries to format through Eglot on save, which errors in buffers without a server.

There is also a standalone ormolu package on MELPA that shells out to the binary. It is useful if you edit Haskell without running HLS, for example in a small script directory, but with HLS running it duplicates work.

Cabal files and the rest

haskell-mode includes haskell-cabal-mode for .cabal files, so dependency editing gets highlighting without extra setup. HLS also has some cabal-file support, offering completion for field names in newer releases.

Completion inside Haskell buffers comes from Eglot through completion-at-point, which means whatever UI you chose in the completion guide (Corfu, Company or plain *Completions*) works without Haskell-specific glue. For which packages are worth adding beyond this point, see the package guide; for comparing how other typed languages wire up a server, the Elixir page uses the same Eglot pattern with a very different REPL story, and the full list sits on the languages index.

One detail that saves a confusing afternoon: when you switch GHC versions in GHCup, run M-x eglot-reconnect in an open Haskell buffer. The old server keeps running against the previous compiler until you do.

Questions readers ask

Why does Eglot say it cannot find haskell-language-server?

Emacs looks for haskell-language-server-wrapper on its own exec-path, which may differ from your shell's PATH. Add ~/.ghcup/bin to exec-path in your init file, or start Emacs from a shell, and check with M-x shell-command and which haskell-language-server-wrapper.

Do I need interactive-haskell-mode if I already run HLS?

No, they solve different problems. HLS gives diagnostics, types on hover and code actions while you edit; interactive-haskell-mode gives you a live GHCi session where you load the module and evaluate expressions. Most people end up using both.

Should I format with ormolu or fourmolu?

Use whatever the project already uses, since a formatter change rewrites every file. For a new personal project fourmolu is ormolu with configuration options; ormolu has none on purpose. HLS ships both as plugins, so the choice is a single setting.