• Stars
    star
    1,135
  • Rank 41,026 (Top 0.9 %)
  • Language
    Nix
  • Created almost 8 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Nix and Haskell in production

Nix and Haskell in production

This guide documents how I use Nix for Haskell development. Feel free to open issues or pull requests if you would like to contribute or suggest improvements

The purpose of this project is to support two Haskell workflows:

  • Workflow #1: Nix provisions the development environment
    • Nix provides all dependencies and the Haskell toolchain
    • You still build the root project using cabal (overview, user's guide)
    • This approach is ideal for development as it supports incremental builds
  • Workflow #2: Nix builds the root project for you
    • This approach is ideal for continuous integration (especially Hydra)

The emphasis of this guide is to be as robust as possible and gracefully handle writing Haskell projects at scale. Some of the suggestions in this guide might be overkill for a small Haskell project but are essential when managing multiple private Haskell projects across a team of developers.

This guide is based partly on the Haskell section of the nixpkgs manual and partly on experience using Nix and Haskell in production at Awake Security.

Background

Nix is not a cabal replacement and Nix actually complements cabal quite well. Nix is much more analogous to a stack replacement. stack does provide some support for Nix integration, but this document does not cover that. Instead, this document describes how to use Nix in conjunction with cabal for Haskell development

The main benefits of using Nix over stack are:

  • Binary caches

    Nix lets you download precompiled Hackage packages whereas stack compiles them on your computer the first time you depend on them

  • Space efficiency

    stack creates a copy of each package for each resolver. This means that if you have two projects with different resolvers then they will not use the same copy of shared dependencies

  • Generality

    Nix is a language-independent build tool. This means you can use Nix to also build and customize non-Haskell dependencies (like gtk). This uniform language simplifies build tooling and infrastructure.

  • Larger ecosystem

    Nix provides a large ecosystem of tools that integrate with anything that Nix can build, such as Hydra (continuous integration), NixOS (an operating system), and NixOps (a deploy tool)

  • Flexibility

    Nix is a powerful tool in the hands of advanced users. You can make very deep and sweeping changes to your toolchain, such as recompiling everything with security hardening

The main disadvantage of using Nix over stack are:

  • Verbosity

    Nix derivations for Haskell projects are significantly more complex than their corresponding stack.yaml files. The release.nix files in this repository are the Nix analog of a stack.yaml file and you can see for yourself the increase in complexity as the examples progress in difficulty.

  • Poor error messages

    Nix is an untyped language with no special Haskell integration, so error messages are unhelpful

  • Nix cannot incrementally compile Haskell libraries

    Note that you can still use Nix to provision a development environment and incrementally compile a Haskell package using cabal. However, if you use Nix to build the package then Nix will build the package from scratch for every minor change. In theory, this could be fixed to have Nix directly support incremental Haskell builds but this has not been done yet.

  • Worse user experience

    Nix does not provide many conveniences that stack does such as bootstrapping new projects or "file watch"

Both Nix and stack use curated package sets instead of version bounds for dependency management. stack calls these package sets "resolvers" whereas Nix calls these package sets "channels". Nix provides stable channels with names like NixOS-18.09 (analogous to stack's LTS releases) and then an unstable channel named nixpkgs-unstable (analogous to stack's nightly releases)

Related guides

  • Nix Haskell Monorepo Tutorial - Guide on how to scale Nix development to a larger repository containing all of a company's internally-developed Haskell packages

Related tools

Before continuing, I'd like to mention some other tools for mixing Haskell with Nix:

  • tinc - this uses cabal's solver to select which Haskell packages to use instead of the curated Haskell package set from nixpkgs
  • styx - This tool provides a stack-like interface to managing Haskell dependencies using Nix
  • haskell-overridez - Tool that automates dependency management as described in this guide

Setup

Before you begin, you must install Nix if you haven't already:

$ curl -L https://nixos.org/nix/install | sh

You must also install cabal2nix and nix-prefetch-git:

$ nix-env --install cabal2nix
$ nix-env --install nix-prefetch-git

You also need to install cabal if you haven't done so already. You can either use your installed cabal or you can use nix to install cabal for you:

$ nix-env --install cabal-install

Make sure that you have a fairly recent version of cabal installed since these examples will use GHC 8 which requires version 1.24 or later of cabal. You can check what version you have installed by running:

$ cabal --version

Finally, run cabal update if you haven't done so already

Organization

This tutorial is split into several tutorial projects in the project*/ subdirectories. Read the README.md file in each subdirectory in order to follow the tutorial:

More Repositories

1

post-rfc

Blog post previews in need of peer review
2,189
star
2

turtle

Shell programming, Haskell style
Haskell
942
star
3

bench

Command-line benchmark tool
Haskell
867
star
4

simple-twitter

A bare-bones Twitter clone implemented in a single file
Nix
720
star
5

pipes

Compositional pipelines
Haskell
478
star
6

nixos-in-production

Source files for the book "NixOS in Production"
HCL
461
star
7

grace

A ready-to-fork interpreted functional language with type inference
JavaScript
385
star
8

Haskell-Morte-Library

A bare-bones calculus-of-constructions
Haskell
367
star
9

nix-diff

Explain why two Nix derivations differ
Haskell
328
star
10

slides

Slides from talks that I give
Haskell
294
star
11

optparse-generic

Auto-generate a command-line parser for your datatype
Haskell
207
star
12

Haskell-Typed-Spreadsheet-Library

Typed and composable spreadsheets
Haskell
187
star
13

foldl

Composable, streaming, and efficient left folds
Haskell
158
star
14

terraform-nixos-ng

Terraform modules for NixOS and spiritual successor to the terraform-nixos project
HCL
122
star
15

Haskell-Annah-Library

Distributed programming language that desugars to Morte
Haskell
92
star
16

Haskell-Lens-Tutorial-Library

The missing tutorial module for the lens library
Haskell
81
star
17

macos-builder

Bootstrap a Linux build VM on macOS
Nix
78
star
18

haskell-in-the-large

A book about large-scale design in Haskell
75
star
19

Haskell-Errors-Library

Type-safe error handling
Haskell
64
star
20

Haskell-MVC-Library

Model-view-controller
Haskell
62
star
21

list-transformer

List monad transformer
Haskell
60
star
22

HasCal

Haskell embedding of PlusCal
Haskell
59
star
23

nixfmt

Automatic code formatter for the Nix programming language
Haskell
52
star
24

managed

A monad for managed values
Haskell
49
star
25

Haskell-MMorph-Library

Monad morphisms
Haskell
47
star
26

dhall-manual

The Dhall Configuration Language Manual
46
star
27

pipes-concurrency

Concurrency for the pipes ecosystem
Haskell
43
star
28

Purescript-to-Python

A compiler from Purescript to Python
Haskell
43
star
29

dhall-dot

Dhall support for the DOT language for graphviz
Dhall
38
star
30

suns-search

Fast all-atom protein structural search engine
Haskell
37
star
31

Haskell-Nix-Derivation-Library

Parse and render *.drv files
Haskell
35
star
32

servant-crud

Example CRUD web server+client using Servant
Haskell
34
star
33

Haskell-Total-Library

Exhaustive pattern matching using lenses, traversals, and prisms
Haskell
31
star
34

Haskell-Server-Generic-Library

Auto-generate a server for your datatype
Haskell
30
star
35

Haskell-Pipes-Parse-Library

Parsing for the pipes ecosystem
Haskell
28
star
36

Haskell-Pipes-Safe-Library

Safety for the pipes ecosystem
Haskell
26
star
37

Haskell-Bears-Library

Relational algebra
Haskell
25
star
38

Haskell-Succinct-Vector-Library

Succinct vectors
Haskell
20
star
39

graph

Dhall support for directed graphs with labeled vertices
Dhall
19
star
40

spire

Algorithmically solve Slay the Spire
Haskell
19
star
41

Haskell-Pipes-HTTP-Library

HTTP client with pipes interface
Haskell
18
star
42

Haskell-Optional-Args-Library

Optional function arguments
Haskell
16
star
43

defaultable-map

Applicative maps
Haskell
15
star
44

Haskell-Pipes-ByteString-Library

ByteString support for pipes
Haskell
14
star
45

dhall-semver

Dhall support for semantic version numbers
Dhall
14
star
46

dhall-kubernetes-charts

This repository is superseded by https://github.com/EarnestResearch/dhall-packages
13
star
47

Haskell-Index-Core-Library

Indexed Types
Haskell
13
star
48

Haskell-Pipes-Extras-Library

Miscellaneous utilities for pipes
Haskell
11
star
49

composable-path

Typed paths that are composable
Haskell
11
star
50

political-notes

Markdown
11
star
51

Haskell-MVC-Updates-Library

Concurrent and combinable updates
Haskell
10
star
52

Haskell-Event-Source-Library

Event sourcing library
Haskell
10
star
53

Haskell-RCPL-Library

Concurrent console input and output
Haskell
10
star
54

sig

Blazing fast signature detection
Haskell
10
star
55

Haskell-Break-Library

Break from a loop
Haskell
9
star
56

ghcjs-demo

Example GHCJS demo
Haskell
8
star
57

Haskell-Nordom-Library

A variation on Morte that is close to the machine (obsoleted in favor of Dhall)
Haskell
7
star
58

Haskell-Dhall-Edit-Library

Autogenerate a curses editor for a Dhall configuration file
Haskell
7
star
59

Haskell-MVC-Updates-Examples-Library

Examples for the mvc-updates library
Haskell
7
star
60

Haskell-DirStream-Library

Easily stream directory contents in constant memory
Haskell
7
star
61

Haskell-Free-Monads-Library

Free monads
Haskell
6
star
62

pipes-web

Deploy pipes over the web
Haskell
5
star
63

Haskell-Transformers-Free-Library

Free monad transformers
Haskell
5
star
64

suns-cmd

Command line client for the Suns search engine
Haskell
5
star
65

tiktoken

Haskell
5
star
66

pipes-platform

The full ecosystem of pipes libraries
Haskell
4
star
67

pipes-group

Group streams into substreams
Haskell
4
star
68

delta-lambda

test language for an automath style type system
Haskell
3
star
69

holepunch

NixOS configuration for tunneling an inbound SSH connection over an outbound HTTPS connection
Nix
3
star
70

Haskell-Pipes-Foldl-Library

Folding utilities for pipes
Haskell
3
star
71

Haskell-Crunch-Library

Fast serialization and deserialization
Haskell
3
star
72

Haskell-Aurora-Library

Haskell-Aurora-Library
Haskell
2
star
73

pipes-ecosystem

testing the pipes suite of libraries
Haskell
2
star
74

Haskell-Record-Library

Command line utility to record command environments and output
Haskell
2
star
75

pipes-process

Shell support for pipes
Haskell
2
star
76

Haskell-Pipes-Handle-Library

Model traditional handles using pipes
Haskell
1
star
77

twill

Twilio API interaction for Haskell
Haskell
1
star
78

min-standalone

Minimal reproducing case for standalone-haddock issue
Haskell
1
star
79

emitter

haskell pipes example
Haskell
1
star
80

code-kata-439

Haskell
1
star