Simpler Haskell development with Nix

Datetime:2016-08-23 04:41:42         Topic:         Share        Original >>
Here to See The Original Article!!!

If you use Nix , here's a super simple way to do rapid development in Haskell. This approach simplifies those in Haskell development with Nix , which rely on tedious one-off Nix configuration files and Cabal.

Create a function nix-haskell in your shell configuration file:


function nix-haskell() {
  nix-shell -p "haskellPackages.ghcWithPackages (pkgs: with pkgs; [$@])"

This will start a nix-shell instance with GHC, and an environment configured with the list of packages that you supply as arguments:

$ nix-haskell random

Now you can write library-dependent Haskell code without needing Cabal, Stack, etc.

Random.hs :

{-# LANGUAGE OverloadedStrings #-}

import Control.Monad (replicateM)
import System.Random (randomRIO)

alphabet :: String
alphabet = ['a'..'z'] ++ ['0'..'9']

randomElem :: String -> IO Char
randomElem l = randomRIO (0, ((length l) - 1)) >>= \d -> return (l !! d)

main :: IO ()
main = do
  slug <- replicateM 12 (randomElem alphabet)
  putStrLn $ "Random slug: " ++ slug
[nix-shell]$ runhaskell Random.hs
Random slug: 2tm997e9ko1p

This article is literate Haskell. Try running it with codedown :

$ nix-haskell random
$ curl -s | \
  codedown haskell | \
Random slug: m6n9t3xm9pvy