Skip to content

Instantly share code, notes, and snippets.

@chris-martin
Last active December 31, 2019 23:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-martin/5080d513dd206e0ea43aded795788743 to your computer and use it in GitHub Desktop.
Save chris-martin/5080d513dd206e0ea43aded795788743 to your computer and use it in GitHub Desktop.

This gist demonstrates a ghcide setup with nix.

No project file (.cabal, package.yaml, stack.yaml, etc.) is necessary.

Before attempting editor integration, test ghcide in the terminal by running this command:

nix-shell --pure --run ghcide

This will search for Haskell source files and print any errors encountered, which can help you determine whether e.g. ghcide is seeing all of your project dependencies correctly.

For Visual Studio Code integration, install the ghcide extension and use the following settings:

  • Executable path: ./ghcide.sh
  • Arguments: (empty)
#! /usr/bin/env bash
set -eufo pipefail
nix-shell --pure --run 'ghcide --lsp'
cradle:
direct:
arguments:
- -no-user-package-db
let
pkgs = import <nixpkgs> {};
ghcVersion = "ghc865";
haskellPackages = pkgs.haskell.packages.${ghcVersion}.override {
overrides = self: super: {
# If you need a particular version of a package instead
# of the default that nixpkgs provides, specify it here.
};
};
haskell = haskellPackages.ghcWithPackages (p: map (name: p.${name}) [
"aeson"
"async"
"bytestring"
# etc., the rest of the Hackage packages you use
]);
ghcide =
let
url = "https://github.com/hercules-ci/ghcide-nix/tarball/master";
in
(import (builtins.fetchTarball url) {})."ghcide-${ghcVersion}";
in
pkgs.mkShell {
buildInputs = [
haskell
haskellPackages.ghcid
ghcide
];
LC_ALL = "en_US.UTF-8";
LOCALE_ARCHIVE = "${pkgs.glibcLocales}/lib/locale/locale-archive";
shellHook = ''
export NIX_GHC="${haskell}/bin/ghc"
export NIX_GHCPKG="${haskell}/bin/ghc-pkg"
export NIX_GHC_DOCDIR="${haskell}/share/doc/ghc/html"
export NIX_GHC_LIBDIR=$( $NIX_GHC --print-libdir )
'';
}
@willisplummer
Copy link

what is the purpose of hie.yml? I was able to get this working without it, but wondering if I missed something?

also the LOCAL_ARCHIVE was consistently causing this to fail for me so I removed it. Is there a reason that it's necessary? The error was:

error: cannot coerce null to a string, at /Users/willisplummer/code/nix-haskell-starter/shell.nix:29:19

@chris-martin
Copy link
Author

@willisplummer I don't remember the purpose of hie.yml and I no longer use it, so I suspect you can safely ignore it!

I typically set LC_ALL and LOCALE_ARCHIVE in any shell hook because when nix-shell strips environment variables, it unsets the locale and causes encoding issues, and setting these tends to get everything reliably back into UTF-8. I'm not sure why it wouldn't be working for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment