Skip to content

Commit 73d99ec

Browse files
committed
nix: restore dev shell
This commit restores a working behavior for `nix-shell` or `nix develop`, for all supported GHC versions. When entering a `nix-shell`, or `nix develop .#haskell-language-server-ghcXxX-dev` you will get an environment with all the dependencies needed to run `cabal build`. Haskell dependencies will be built by cabal instead of nix. This may be more robust compared to a shell where dependencies are built by nix: - It won't try to build any dependency with nix. This mean that it won't benefit from nix caching, but on the other hand, it will perfectly match the cabal plan. - The nix shell may fail to start if an (possibly unneeded) dependency was failing to build. Entering nix-shell also generates an alias command `cabal_project` which runs cabal with the correct project file if needed (e.g. for GHC 9.0 and 9.2). Users are notified with a message when entering the shell. The old behavior (i.e. where dependencies are built with nix) is still available with `nix develop .#haskell-language-server-xxx-dev-nix` (i.e. suffix the name of the shell with `-nix`).
1 parent 7343de6 commit 73d99ec

File tree

1 file changed

+66
-28
lines changed

1 file changed

+66
-28
lines changed

flake.nix

Lines changed: 66 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -236,46 +236,78 @@
236236
dontInstall = true;
237237
};
238238

239+
mkDevShell = hpkgs: cabalProject: with pkgs; mkShell {
240+
# For theses tools packages, we use ghcDefault
241+
# This removes a rebuild with a different GHC version
242+
# Theses programs are tools, used as binary, independently of the
243+
# version of GHC.
244+
# The drawback of this approach is that our shell may pull two GHC
245+
# version in scope (the default one, and the one defined in
246+
# `hpkgs`.)
247+
# The advantage is that we won't have to rebuild theses tools (and
248+
# dependencies) with a recent GHC which may not be supported by
249+
# them.
250+
buildInputs = [
251+
# our compiling toolchain
252+
hpkgs.ghc
253+
pkgs.cabal-install
254+
# @guibou: I'm not sure hie-bios is needed
255+
ghcDefault.hie-bios
256+
# Dependencies needed to build some parts of hackage
257+
gmp zlib ncurses
258+
# Changelog tooling
259+
(gen-hls-changelogs ghcDefault)
260+
# For the documentation
261+
pythonWithPackages
262+
# @guibou: I'm not sure this is needed.
263+
hlint
264+
ghcDefault.opentelemetry-extra
265+
capstone tracy
266+
# ormolu
267+
# stylish-haskell
268+
];
269+
270+
271+
shellHook = ''
272+
# @guibou: I'm not sure theses lines are needed
273+
export LD_LIBRARY_PATH=${gmp}/lib:${zlib}/lib:${ncurses}/lib:${capstone}/lib
274+
export DYLD_LIBRARY_PATH=${gmp}/lib:${zlib}/lib:${ncurses}/lib:${capstone}/lib
275+
export PATH=$PATH:$HOME/.local/bin
276+
277+
# Enable the shell hooks
278+
${(pre-commit-check ghcDefault).shellHook}
279+
280+
# If the cabal project file is not the default one.
281+
# Print a warning and generate an alias.
282+
if [ ${cabalProject} != "cabal.project" ]
283+
then
284+
echo "Cabal won't be able to build your project without using the project file "${cabalProject}", such as:"
285+
echo " cabal --project-file=${cabalProject}"
286+
echo "An alias "cabal_project" is available. Use it like:"
287+
echo " cabal_project build"
288+
289+
alias cabal_project='cabal --project-file=${cabalProject}'
290+
fi
291+
'';
292+
};
293+
239294
# Create a development shell of hls project
240295
# See https://github.com/NixOS/nixpkgs/blob/5d4a430472cafada97888cc80672fab255231f57/pkgs/development/haskell-modules/make-package-set.nix#L319
241-
mkDevShell = hpkgs: cabalProject:
296+
mkDevShellWithNixDeps = hpkgs: cabalProject:
242297
with pkgs;
298+
let simpleShell = mkDevShell hpkgs cabalProject;
299+
in
243300
hpkgs.shellFor {
301+
inherit (simpleShell) shellHook buildInputs;
302+
244303
doBenchmark = true;
245304
packages = p:
246305
with builtins;
247306
map (name: p.${name}) (attrNames
248307
# Disable dependencies should not be part of the shell.
249308
(removeAttrs hlsSources (hpkgs.hlsDisabledPlugins or [])));
250-
# For theses tools packages, we use ghcDefault
251-
# This removes a rebuild with a different GHC version
252-
# Theses programs are tools, used as binary, independently of the
253-
# version of GHC.
254-
# The drawback of this approach is that our shell may pull two GHC
255-
# version in scope (the `ghcDefault` one, and the one defined in
256-
# `hpkgs`.)
257-
# The advantage is that we won't have to rebuild theses tools (and
258-
# dependencies) with a recent GHC which may not be supported by
259-
# them.
260-
buildInputs = [ gmp zlib ncurses capstone tracy (gen-hls-changelogs ghcDefault) pythonWithPackages ]
261-
++ [
262-
pkgs.cabal-install
263-
ghcDefault.hie-bios
264-
hlint
265-
# ormolu
266-
# stylish-haskell
267-
ghcDefault.opentelemetry-extra
268-
];
269309

270310
src = null;
271-
shellHook = ''
272-
export LD_LIBRARY_PATH=${gmp}/lib:${zlib}/lib:${ncurses}/lib:${capstone}/lib
273-
export DYLD_LIBRARY_PATH=${gmp}/lib:${zlib}/lib:${ncurses}/lib:${capstone}/lib
274-
export PATH=$PATH:$HOME/.local/bin
275-
${(pre-commit-check ghcDefault).shellHook}
276-
277-
alias cabal='cabal --project-file=${cabalProject}'
278-
'';
279311
};
280312
# Create a hls executable
281313
# Copied from https://github.com/NixOS/nixpkgs/blob/210784b7c8f3d926b7db73bdad085f4dc5d79418/pkgs/development/tools/haskell/haskell-language-server/withWrapper.nix#L16
@@ -299,6 +331,12 @@
299331
haskell-language-server-8107-dev = mkDevShell ghc8107 "cabal.project";
300332
haskell-language-server-901-dev = mkDevShell ghc901 "cabal-ghc90.project";
301333
haskell-language-server-921-dev = mkDevShell ghc921 "cabal-ghc921.project";
334+
335+
haskell-language-server-dev-nix = mkDevShellWithNixDeps ghcDefault "cabal.project";
336+
haskell-language-server-884-dev-nix = mkDevShellWithNixDeps ghc884 "cabal.project";
337+
haskell-language-server-8107-dev-nix = mkDevShellWithNixDeps ghc8107 "cabal.project";
338+
haskell-language-server-901-dev-nix = mkDevShellWithNixDeps ghc901 "cabal-ghc90.project";
339+
haskell-language-server-921-dev-nix = mkDevShellWithNixDeps ghc921 "cabal-ghc921.project";
302340
};
303341

304342
packages = {

0 commit comments

Comments
 (0)