Haskell: Difference between revisions
m Formatting for consistency in the list |
Phanirithvij (talk | contribs) m update cabal docs link for index-state |
||
(4 intermediate revisions by 3 users not shown) | |||
Line 60: | Line 60: | ||
Notes: | Notes: | ||
* some packages may need additional libraries/programs, notably <code>zlib</code>, you should be able to add them as additional programs in the nix-shell option | * some packages may need additional libraries/programs, notably <code>zlib</code>, you should be able to add them as additional programs in the nix-shell option | ||
* since Cabal 2.0, cabal has acquired caching similar to nix (but not as powerful) and reproducibility (via the cabal.project file and the index-state option). See [https://cabal.readthedocs.io/en/ | * since Cabal 2.0, cabal has acquired caching similar to nix (but not as powerful) and reproducibility (via the cabal.project file and the index-state option). See [https://cabal.readthedocs.io/en/latest/cabal-project-description-file.html#cfg-field-index-state] for more information. | ||
=== Using Stack (no nix caching) === | === Using Stack (no nix caching) === | ||
Line 73: | Line 73: | ||
</syntaxhighlight> | </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/topics/nix_integration/ here]. | ||
If you want to package your program using stack in nix, you can actually use <code>haskell.lib.buildStackProject</code> that is a wrapper around <code>stdenv.mkDerivation</code> that will [https://github.com/NixOS/nixpkgs/blob/350fd0044447ae8712392c6b212a18bdf2433e71/pkgs/development/haskell-modules/generic-stack-builder.nix#L56 call <code>stack build</code> for you]… However because stack needs to download stuff you need to disable the sandbox using <code>nix-build --option sandbox false</code>. For instance if you want to compile a stack project that needs R, zeromq and zlib you can put the following into <code>default.nix</code>: | If you want to package your program using stack in nix, you can actually use <code>haskell.lib.buildStackProject</code> that is a wrapper around <code>stdenv.mkDerivation</code> that will [https://github.com/NixOS/nixpkgs/blob/350fd0044447ae8712392c6b212a18bdf2433e71/pkgs/development/haskell-modules/generic-stack-builder.nix#L56 call <code>stack build</code> for you]… However because stack needs to download stuff you need to disable the sandbox using <code>nix-build --option sandbox false</code>. For instance if you want to compile a stack project that needs R, zeromq and zlib you can put the following into <code>default.nix</code>: | ||
Line 158: | Line 158: | ||
<code>shellFor</code> is similar to <code>developPackage</code> but (slightly) more complicated to also allow you to develop multiples packages at the same time (similar to <code>cabal.project</code>). Note that contrary to <code>developPackage</code> I don't think that <code>shellFor</code> can output a derivation. | <code>shellFor</code> is similar to <code>developPackage</code> but (slightly) more complicated to also allow you to develop multiples packages at the same time (similar to <code>cabal.project</code>). Note that contrary to <code>developPackage</code> I don't think that <code>shellFor</code> can output a derivation. | ||
The idea is to first extend/override the set of haskell packages in order to add your projects as additional haskell packages (for instance using <code>haskellPackages.extend</code> and <code>packageSourceOverrides</code> that just need a the path of the project to compile it), and then to use | The idea is to first extend/override the set of haskell packages in order to add your projects as additional haskell packages (for instance using <code>haskellPackages.extend</code> and <code>packageSourceOverrides</code> that just need a the path of the project to compile it), and then to use <code>haskellPackages.shellFor {packages= p: [p.myproject1 p.myproject2]}</code> to create a shell with all wanted packages. | ||
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: | ||
Line 209: | Line 209: | ||
=== Using haskell-flake (flake-parts) === | === Using haskell-flake (flake-parts) === | ||
[https:// | [https://flake.parts/options/haskell-flake haskell-flake] aims to simplify writing Nix for Haskell development through use of flake-parts module system. It uses <code>callCabal2nix</code> and <code>shellFor</code> under the hood while exposing friendly module options API. | ||
== Overrides == | == Overrides == |