Jump to content

Haskell: Difference between revisions

210 bytes added ,  30 September 2023
m
Fixed syntax highlighting
imported>Kintrix
No edit summary
imported>IgorM
m (Fixed syntax highlighting)
Line 21: Line 21:
Note that in the following, <code>haskellPackages</code> 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 that in the following, <code>haskellPackages</code> 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:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
$ nix-env -f "<nixpkgs>" -qaP -A haskell.compiler
$ nix-env -f "<nixpkgs>" -qaP -A haskell.compiler
haskell.compiler.ghc8107                ghc-8.10.7
haskell.compiler.ghc8107                ghc-8.10.7
Line 35: Line 35:


For simple scripts, you can directly use nix-shell to get a redistributable Haskell script that you can run on any Nix system with <code>./my-script.hs</code>:
For simple scripts, you can directly use nix-shell to get a redistributable Haskell script that you can run on any Nix system with <code>./my-script.hs</code>:
<pre>
<syntaxhighlight lang=bash>
#!/usr/bin/env nix-shell
#!/usr/bin/env nix-shell
#!nix-shell --pure -i runghc -p "haskellPackages.ghcWithPackages (pkgs: [ pkgs.turtle ])"
#!nix-shell --pure -i runghc -p "haskellPackages.ghcWithPackages (pkgs: [ pkgs.turtle ])"
Line 42: Line 42:
   # do stuff
   # do stuff
   putStrLn "Hello world from a distributable Haskell script!"
   putStrLn "Hello world from a distributable Haskell script!"
</pre>
</syntaxhighlight>


Read below if some packages are broken.
Read below if some packages are broken.
Line 48: Line 48:
=== Directly using cabal (no nix caching/reproducibility) ===
=== Directly using cabal (no nix caching/reproducibility) ===
Note that 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:
Note that 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:
<pre>
<syntaxhighlight lang="console">
$  nix-shell -p "haskellPackages.ghcWithPackages (pkgs: with pkgs; [ cabal-install ])"
$  nix-shell -p "haskellPackages.ghcWithPackages (pkgs: with pkgs; [ cabal-install ])"
$ cabal init
$ cabal init
Line 55: Line 55:
Up to date
Up to date
Hello, Haskell!
Hello, Haskell!
</pre>
</syntaxhighlight>


Notes:
Notes:
Line 64: Line 64:


Similarly you can use stack that let you find the appropriate version of the libraries for you if you do not want the caching offered by nix (stack will build all the dependencies):
Similarly you can use stack that let you find the appropriate version of the libraries for you if you do not want the caching offered by nix (stack will build all the dependencies):
<pre>
<syntaxhighlight lang="console">
$ nix-shell -p "haskellPackages.ghcWithPackages (pkgs: with pkgs; [ stack ])"
$ nix-shell -p "haskellPackages.ghcWithPackages (pkgs: with pkgs; [ stack ])"
$ stack new my-project
$ stack new my-project
Line 70: Line 70:
$ stack build
$ stack build
$ stack exec my-project-exe
$ stack exec my-project-exe
</pre>
</syntaxhighlight>


You can also use the features offered by stack to enable nix integration in order to use nix to install the non-haskell dependencies. You can read more [https://docs.haskellstack.org/en/stable/nix_integration/ here].
You can also use the features offered by stack to enable nix integration in order to use nix to install the non-haskell dependencies. You can read more [https://docs.haskellstack.org/en/stable/nix_integration/ here].
Line 91: Line 91:


You can use also nix in place of stack to keep track of the dependencies in a reproducible way (note that while stack uses a solver to find a working set of dependencies, nix uses a fixed set of packages). Additionally you can benefit from the caching system offered by Nix. To that end, first create a cabal repository (nix also uses cabal internally):
You can use also nix in place of stack to keep track of the dependencies in a reproducible way (note that while stack uses a solver to find a working set of dependencies, nix uses a fixed set of packages). Additionally you can benefit from the caching system offered by Nix. To that end, first create a cabal repository (nix also uses cabal internally):
<syntaxhighlight lang="nix">
<syntaxhighlight lang="console">
$ nix-shell -p "haskellPackages.ghcWithPackages (pkgs: with pkgs; [ cabal-install ])" --run "cabal init"
$ nix-shell -p "haskellPackages.ghcWithPackages (pkgs: with pkgs; [ cabal-install ])" --run "cabal init"
…  
…  
Line 109: Line 109:


Then you can build and run the program using:
Then you can build and run the program using:
<pre>
<syntaxhighlight lang="console">
$ nix-build
$ nix-build
$ ./result/bin/yourprogram  
$ ./result/bin/yourprogram  
</pre>
</syntaxhighlight>
or run a nix-shell to use the standard development tools provided by cabal:
or run a nix-shell to use the standard development tools provided by cabal:
<pre>
<syntaxhighlight lang="console">
$ nix-build
$ nix-build
$ ./result/bin/yourprogram  
$ ./result/bin/yourprogram  
</pre>
</syntaxhighlight>


Nix will automatically read the <code>build-depends</code> field in the <code>*.cabal</code> file to get the name of the dependencies and use the haskell packages provided in the configured package set provided by nix. Note that some of the packages present in the nix repository are broken (for instance because a package requires an older version of a library while nix only provides a recent version). For this reason it may be necessary to override some packages present in the nix package set as described below using the <code>overrides</code> and <code>source-overrides</code> attribute. Note that the <code>source-overrides</code> attribute can also turn out to be useful to load local libraries:
Nix will automatically read the <code>build-depends</code> field in the <code>*.cabal</code> file to get the name of the dependencies and use the haskell packages provided in the configured package set provided by nix. Note that some of the packages present in the nix repository are broken (for instance because a package requires an older version of a library while nix only provides a recent version). For this reason it may be necessary to override some packages present in the nix package set as described below using the <code>overrides</code> and <code>source-overrides</code> attribute. Note that the <code>source-overrides</code> attribute can also turn out to be useful to load local libraries:
Line 161: Line 161:
For instance you can define your various projects in subfolders <code>./frontend</code> and <code>./backend</code> (you can use cabal init to create the content in each folder), then create a file <code>cabal.project</code> containing:
For instance you can define your various projects in subfolders <code>./frontend</code> and <code>./backend</code> (you can use cabal init to create the content in each folder), then create a file <code>cabal.project</code> containing:


<syntaxhighlight>
<syntaxhighlight lang=text>
packages:
packages:
   frontend/
   frontend/
Line 186: Line 186:
then you can use cabal to develop incrementally your projects using for instance:
then you can use cabal to develop incrementally your projects using for instance:


<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
$ nix-shell
$ nix-shell
$ cabal new-build all
$ cabal new-build all
Anonymous user