Skip to content

Commit 549a7bc

Browse files
committed
Replace NixOS example with one with dev tools
1 parent 769bdf7 commit 549a7bc

File tree

2 files changed

+43
-34
lines changed

2 files changed

+43
-34
lines changed

doc/nix_integration.md

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -289,75 +289,80 @@ The `stack build` command will behave exactly the same as above. Note
289289
that specifying both `packages:` and a `shell-file:` results in an
290290
error. (Comment one out before adding the other.)
291291

292-
## Stack on NixOS
292+
## Stack and developer tools on NixOS
293293

294294
When using Stack on NixOS, you have no choice but to use Stack's Nix integration to
295295
install GHC, because external C libraries in NixOS are not installed in the usual
296296
distro folders. So a GHC compiler installed through Stack (without Nix) can't find
297-
those libraries and therefore can't build most projects. GHC provided through Nix
298-
is patched in a way that it finds the external C libraries listed and provided through Nix.
297+
those libraries and therefore can't build most projects. However, GHC provided through Nix
298+
can be modified to find the external C libraries provided through Nix.
299+
299300
A detailed tutorial on how to configure Stack so that it supports NixOS and non-Nix users
300301
can be found [here](https://www.tweag.io/blog/2022-06-02-haskell-stack-nix-shell/). A corresponding
301302
example project can be found [here](https://github.com/tweag/haskell-stack-nix-example).
302303

303-
If you're already using Nix flakes, here's an adaption of that example:
304+
If you're already using Nix flakes, here's an adaptation of that example extended with typical developer
305+
tools like the [Haskell Language Server](https://haskell-language-server.readthedocs.io/en/latest/what-is-hls.html):
304306
Add the following `flake.nix` file to your project.
305307

306308
```nix
307309
{
310+
description = "my project description";
308311
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
309312
inputs.flake-utils.url = "github:numtide/flake-utils";
310313
311314
outputs = { self, nixpkgs, flake-utils }:
312315
flake-utils.lib.eachDefaultSystem (system:
313316
let
314317
pkgs = nixpkgs.legacyPackages.${system};
315-
# Wrap Stack to configure Nix integration and target the correct Stack-Nix file
316-
#
317-
# - nix: Enable Nix support
318-
# - no-nix-pure: Pass environment variables, like `NIX_PATH`
319-
# - nix-shell-file: Specify the Nix file to use (otherwise it uses `shell.nix` by default)
318+
319+
hPkgs =
320+
pkgs.haskell.packages."ghc8107"; # need to match Stackage LTS version from stack.yaml resolver
321+
322+
myDevTools = [
323+
hPkgs.ghc # GHC compiler in the desired version (will be available on PATH)
324+
hPkgs.ghcid # Continous terminal Haskell compile checker
325+
hPkgs.ormolu # Haskell formatter
326+
hPkgs.hlint # Haskell codestyle checker
327+
hPkgs.hoogle # Lookup Haskell documentation
328+
hPkgs.haskell-language-server # LSP server for editor
329+
hPkgs.implicit-hie # auto generate LSP hie.yaml file from cabal
330+
hPkgs.retrie # Haskell refactoring tool
331+
# hPkgs.cabal-install
332+
stack-wrapped
333+
pkgs.zlib # External C library needed by some Haskell packages
334+
];
335+
336+
# Wrap Stack to work with our Nix integration. We don't want to modify stack.yaml so non-Nix users don't notice anything.
337+
# - no-nix: We don't want Stack's way of integrating Nix.
338+
# --system-ghc # Use the existing GHC on PATH (will come from this Nix file)
339+
# --no-install-ghc # Don't try to install GHC if no matching GHC found on PATH
320340
stack-wrapped = pkgs.symlinkJoin {
321-
name = "stack";
341+
name = "stack"; # will be available as the usual `stack` in terminal
322342
paths = [ pkgs.stack ];
323343
buildInputs = [ pkgs.makeWrapper ];
324344
postBuild = ''
325345
wrapProgram $out/bin/stack \
326346
--add-flags "\
327-
--nix \
328-
--no-nix-pure \
329-
--nix-shell-file=flake-stack-integration.nix \
347+
--no-nix \
348+
--system-ghc \
349+
--no-install-ghc \
330350
"
331351
'';
332352
};
333353
in {
334354
devShells.default = pkgs.mkShell {
335-
buildInputs = [ stack-wrapped ];
355+
buildInputs = myDevTools;
336356
337-
# Configure the Nix path to our own pinned package set, to ensure Stack uses the same one rather than another global <nixpkgs> when looking for the right `ghc` argument to pass in `flake-stack-integration.nix`
338-
# See https://nixos.org/nixos/nix-pills/nix-search-paths.html for more information
339-
NIX_PATH = "nixpkgs=" + pkgs.path;
357+
# Make external Nix c libraries like zlib known to GHC, like pkgs.haskell.lib.buildStackProject does
358+
# https://github.com/NixOS/nixpkgs/blob/d64780ea0e22b5f61cd6012a456869c702a72f20/pkgs/development/haskell-modules/generic-stack-builder.nix#L38
359+
LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath myDevTools;
340360
};
341361
});
342362
}
343-
344-
```
345-
346-
Then also add the following `flake-stack-integration.nix` file to your project:
347-
348-
```nix
349-
{ ghc }:
350-
with (import <nixpkgs> { });
351-
352-
haskell.lib.buildStackProject {
353-
inherit ghc;
354-
name = "haskell-stack-flake-nix";
355-
buildInputs = [ zlib glpk pcre ];
356-
}
357-
358363
```
359364
360-
Commit both files to Git, run `nix develop` (it searches for `flake.nix` by default),
365+
Commit this file to Git, run `nix develop` (it searches for `flake.nix` by default),
361366
and you'll find a new `flake.lock` file that pins the precise nixpkgs package set.
362367
Commit this file to Git as well so that every developer of your project will use precisely
363368
the same package set.

stack.cabal

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 2.0
22

3-
-- This file has been generated from package.yaml by hpack version 0.34.4.
3+
-- This file has been generated from package.yaml by hpack version 0.34.7.
44
--
55
-- see: https://github.com/sol/hpack
66

@@ -462,6 +462,8 @@ executable stack-integration-test
462462
other-modules:
463463
StackTest
464464
Paths_stack
465+
autogen-modules:
466+
Paths_stack
465467
hs-source-dirs:
466468
test/integration
467469
test/integration/lib
@@ -590,6 +592,8 @@ test-suite stack-test
590592
Stack.Types.TemplateNameSpec
591593
Stack.UploadSpec
592594
Paths_stack
595+
autogen-modules:
596+
Paths_stack
593597
hs-source-dirs:
594598
src/test
595599
ghc-options: -Wall -fwarn-tabs -fwarn-incomplete-uni-patterns -fwarn-incomplete-record-updates -optP-Wno-nonportable-include-path -threaded

0 commit comments

Comments
 (0)