Posted on January 19, 2018

by Dmitry Bushev

# Stackage nixpkgs Overlays

Nixpkgs overlay with Stackage LTS releases

Yesterday we published a [typeable/nixpkgs-stackage](https://github.com/typeable/nixpkgs-stackage) Stackage Nixpkgs overlay plus a [typeable/stackage2nix](https://github.com/typeable/stackage2nix) update with overlay support.

Here’s a quick overview of new features.

You can find more about `stackage2nix` in our [previous blog post](/content/blog/2017-08-24-stackage2nix/index.html).

## Stackage nixpkgs overlays

Overlay adds Stackage LTS releases to Nixpkgs at the `pkgs.haskell.packages.stackage`.

You can use these Stackage packages the same way as original Hackage packages maintained by Nix. Below you will find some examples from Nixpkgs [Haskell manual](https://nixos.org/nixpkgs/manual/#users-guide-to-the-haskell-infrastructure) with `haskellPackages` replaced with LTS packages.

List packages in _lts-10.0_ release:

```bash
nix-env -f "<nixpkgs>" -qaP -A haskell.packages.stackage.lts-100
```

Create a development environment from LTS package set:

```bash
nix-shell -p "haskell.packages.stackage.lts-100.ghcWithPackages (pkgs: [pkgs.mtl])"
```

Override and enable profiling:

```nix
with import <nixpkgs> {};
haskell.packages.stackage.lts-100.override {
  overrides = self: super: {
    mkDerivation = args: super.mkDerivation (args // {
      enableLibraryProfiling = true;
    });
  };
}
```

Stackage packages are generated with _stackage2nix_ and will be updated on a regular basis.

### stackage2nix build support

Overlay also adds build utils at `pkgs.haskell.packages.stackage.lib`. You can create a build derivation from your project’s _stack.yaml_ using `callStackage2nix` helper function.

```nix
{ nixpkgs ? import <nixpkgs> {} }:

nixpkgs.haskell.packages.stackage.lib.callStackage2nix "my-package" ./. {
  inherit nixpkgs;
};
```

More complex example with pinned Nixpkgs, _stackage2nix_ source pulled from GitHub, and overriding of the resulting Haskell packages:

```nix
let
  nixpkgs = import ./nixpkgs-pinned.nix {};
  inherit (nixpkgs) pkgs lib haskell;

stackage2nixSrc = pkgs.fetchFromGitHub (lib.importJSON ./stackage2nix.json);

stackagePackages = haskell.packages.stackage.lib.callStackage2nix "stackage2nix" stackage2nixSrc { inherit nixpkgs; };
  stackage = stackagePackages.override {
    overrides = self: super: {
      stackage2nix = haskell.lib.disableSharedExecutables super.stackage2nix;
    };
  };
in
  stackage
```

`callStackage2nix` function calls _stackage2nix_ on provided path and produces a derivation with Haskell packages for the project. [nixpkgs-stackage/examples](https://github.com/typeable/nixpkgs-stackage/tree/master/examples) directory contains full source code of the derivations listed above.

## stackage2nix

_stackage2nix_ was also updated to support the Stackage overlay. Now the default is to create a single _default.nix_ with an override of existing Stackage packages. The advantage of this approach is that it does not require additional `--all-cabal-hashes` and `--lts-haskell` flags (they are only needed for Stackage generation). But it is required to have _typeable/nixpkgs-stackage_ overlay installed in the system.

To enable the old behavior and generate all packages required for the build, pass `--with-stackage` or `--with-stackage-closure` flags.
