Search code examples
haskellnixnixpkgs

How do you build an executable from haskell packages (which use stack / cabal) in nix?


I'm looking for something similar to this:

shell.nix

{ pkgs ? import <nixpkgs> {} }:
let
  threadscope = ...
  speedscope = ...
in
  stdenv.mkDerivation {
    name = "haskell-env";
    nativeBuildInputs = [
      # Haskell tools
      ghcid ghc

      # Profiling tools
      threadscope
      speedscope
    ];
  }

Then I can use nix-shell:

% nix-shell
...
nix-shell % threadscope <some-commands>
nix-shell % speedscope <some-commands>

Do you usually just build from source from github / hackage / stackage or use pkgs.haskellPackages.<my-package>?


Solution

  • I usually take tools from haskellPackages.

    If you only want a shell with tools, you can use an expression like this.

    { pkgs ? import <nixpkgs> {} }:
    
    let
      # Select a package set. haskellPackages is recommended, but
      # if you need a different ghc, use pkgs.haskell.packages.ghc8102 for example
      haskellPackages = pkgs.haskellPackages;
    
    in
      pkgs.mkShell {
        nativeBuildInputs = [
          # Haskell tools
          haskellPackages.ghc
          haskellPackages.ghcid
    
          # Profiling tools
          haskellPackages.threadscope
          # pkgs.speedscope (not packaged yet)
        ];
      }
    

    If you want a shell with specific precompiled libraries, you can use the ghcWithPackages function instead of ghc. If you want precompiled dependencies for your local Haskell package(s), you can replace pkgs.mkShell by the haskellPackages.shellFor function.

    If you'd like to package speedscope, the Nixpkgs manual documents the packaging process for node packages