Haskell: Difference between revisions

Crasm (talk | contribs)
m Scripting: Add shh-extras for interactive usage of shh
DHCP (talk | contribs)
m Further reading: clean the link text
 
(3 intermediate revisions by 2 users not shown)
Line 4: Line 4:


== How to develop with Haskell and Nix ==
== How to develop with Haskell and Nix ==
There are multiples ways to develop in Haskell on Nix depending on the simplicity of the project and on whether one want to benefit from the reproducibility offered by nix or not. Below is an image to help you in your choice:
[[File:haskell_choice.png]]


{{Note|{{nixos:package|haskellPackages}} is a synonym of <code>haskell.packages.ghcXYZ</code> where <code>XYZ</code> is the current default version of GHC in nixpkgs. However you can use a different version by replacing <code>haskellPackages</code> with the wanted package, for instance use <code>haskell.compiler.ghc884</code> to use GHC 8.8.4. You can get the full list of available GHC versions using:
{{Note|{{nixos:package|haskellPackages}} is a synonym of <code>haskell.packages.ghcXYZ</code> where <code>XYZ</code> is the current default version of GHC in nixpkgs. However you can use a different version by replacing <code>haskellPackages</code> with the wanted package, for instance use <code>haskell.compiler.ghc884</code> to use GHC 8.8.4. You can get the full list of available GHC versions using:
Line 27: Line 23:
For redistributable Haskell scripts on any Nix system, you can use a nix-shell shebang.
For redistributable Haskell scripts on any Nix system, you can use a nix-shell shebang.
<syntaxhighlight lang="haskell">#!/usr/bin/env nix-shell
<syntaxhighlight lang="haskell">#!/usr/bin/env nix-shell
#! nix-shell --pure -i runghc -p "ghc.withPackages (pkgs: [ pkgs.turtle ])"
#! nix-shell --pure -i runghc -p "ghc.withPackages (pkgs: [ pkgs.turtle ])"
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE OverloadedStrings #-}
import Turtle
import Turtle
Line 65: Line 61:
[https://www.haskell.org/cabal/ Cabal] is the basic Haskell tool used to configure builds and is internally used by all the Haskell's packaging methods (including stack and nix). If one does not care about the reproducibility/caching offered by nix, it is always possible to use cabal like in a normal system:
[https://www.haskell.org/cabal/ Cabal] is the basic Haskell tool used to configure builds and is internally used by all the Haskell's packaging methods (including stack and nix). If one does not care about the reproducibility/caching offered by nix, it is always possible to use cabal like in a normal system:
<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
$ nix-shell -p "ghc.withPackages (pkgs: [ pkgs.cabal-install ])"
$ nix-shell -p "ghc.withPackages (pkgs: [ pkgs.cabal-install ])"
$ cabal init
$ cabal init
Line 230: Line 226:
=== Using haskell-flake (flake-parts) ===
=== Using haskell-flake (flake-parts) ===


[https://community.flake.parts/haskell-flake haskell-flake] is a project that aims to simplify writing Nix for Haskell development through use of [[Flake Parts|flake-parts module system]]. It uses <code>callCabal2nix</code> and <code>shellFor</code> under the hood while exposing friendly module options API. For an overview of Flakes, see the [[Flakes]] wiki page.
[https://haskell.nixos.asia/ haskell-flake] is a project that aims to simplify writing Nix for Haskell development through use of [[Flake Parts|flake-parts module system]]. It uses <code>callCabal2nix</code> and <code>shellFor</code> under the hood while exposing friendly module options API. For an overview of Flakes, see the [[Flakes]] wiki page.


* For existing Haskell projects, initialize with:
* For existing Haskell projects, initialize with:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
nix flake init -t github:srid/haskell-flake
$ nix flake init -t github:srid/haskell-flake
</syntaxhighlight>
</syntaxhighlight>


* For new Haskell projects, use the example template:
* For new Haskell projects, use the example template:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
mkdir example && cd ./example
$ mkdir example && cd ./example
nix flake init -t github:srid/haskell-flake#example
$ nix flake init -t github:srid/haskell-flake#example
</syntaxhighlight>
</syntaxhighlight>


Line 304: Line 300:
==== Further reading ====
==== Further reading ====


* [https://github.com/srid/haskell-template/tree/master Example Haskell project with a development environment]
* [https://github.com/srid/haskell-template Example Haskell project with a development environment]


* [https://github.com/srid/haskell-multi-nix/tree/master Example cabal.project multi-package Haskell project]
* [https://github.com/srid/haskell-multi-nix Example cabal.project multi-package Haskell project]


* [https://community.flake.parts/haskell-flake/start Getting started with haskell-flake].
* [https://haskell.nixos.asia/start Getting started with haskell-flake].


* [https://community.flake.parts/haskell-flake/dependency Overriding dependencies in a haskell-flake]
* [https://haskell.nixos.asia/dependency Overriding dependencies in a haskell-flake]


* [https://flake.parts/options/haskell-flake haskell-flake haskell-flake options reference]
* [https://flake.parts/options/haskell-flake haskell-flake options reference]


== Overrides ==
== Overrides ==