Haskell: Difference between revisions
imported>Tobias.bora No edit summary |
imported>Tobias.bora No edit summary |
||
Line 33: | Line 33: | ||
=== 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 slack 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 slack 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> | ||
$ 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 40: | Line 40: | ||
Up to date | Up to date | ||
Hello, Haskell! | Hello, Haskell! | ||
</ | </pre> | ||
=== Using Stack (no nix caching) === | === Using Stack (no nix caching) === | ||
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> | ||
$ 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 51: | Line 51: | ||
$ stack build | $ stack build | ||
$ stack exec my-project-exe | $ stack exec my-project-exe | ||
</ | </pre> | ||
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 58: | Line 58: | ||
You can use also nix in place of stack to keep track of the dependencies in a reproducible way. 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. Additionally you can benefit from the caching system offered by Nix. To that end, first create a cabal repository (nix also uses cabal internally): | ||
< | <pre> | ||
$ 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" | ||
… | … | ||
</ | </pre> | ||
And create a file <code>default.nix</code> containing: | And create a file <code>default.nix</code> containing: | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
let | let | ||
Line 70: | Line 71: | ||
root = ./.; | root = ./.; | ||
} | } | ||
<syntaxhighlight> | </syntaxhighlight> | ||
Then you can build and run the program using: | Then you can build and run the program using: | ||
< | <pre> | ||
$ nix-build | $ nix-build | ||
$ ./result/bin/yourprogram | $ ./result/bin/yourprogram | ||
</ | </pre> | ||
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> | ||
$ nix-build | $ nix-build | ||
$ ./result/bin/yourprogram | $ ./result/bin/yourprogram | ||
</ | </pre> | ||
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 haskell packages configured in 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. | 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 haskell packages configured in 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. |