Haskell: Difference between revisions

Srid (talk | contribs)
Add https://nixos.asia/en/nixify-haskell-nixpkgs
m update cabal docs link for index-state
 
(5 intermediate revisions by 3 users not shown)
Line 4: Line 4:


* [https://www.srid.ca/haskell-nix '''Nix recipes for Haskellers'''] aims to get a beginner comfortable managing simple Haskell programs and projects using Nix.  
* [https://www.srid.ca/haskell-nix '''Nix recipes for Haskellers'''] aims to get a beginner comfortable managing simple Haskell programs and projects using Nix.  
* [https://nixos.asia/en/nixify-haskell-nixpkgs Nixifying a Haskell project using nixpkgs] explains how to use Nix to package and develop Haskell projects using nothing but nixpkgs.  
* [https://nixos.asia/en/nixify-haskell-nixpkgs '''Nixifying a Haskell project using nixpkgs'''] explains how to use Nix to package and develop Haskell projects using nothing but nixpkgs.  


* [https://github.com/mhwombat/nix-for-numbskulls/blob/78bcc186f79931c0e4a1e445e2f6b1f12f6d46be/Haskell/ss-haskell-dev.md '''Super-Simple Haskell Development with Nix'''] (and [https://discourse.nixos.org/t/super-simple-haskell-development-with-nix/14287/2 discussion] that provides interesting alternative methods together with there pro and cons)
* [https://github.com/mhwombat/nix-for-numbskulls/blob/78bcc186f79931c0e4a1e445e2f6b1f12f6d46be/Haskell/ss-haskell-dev.md '''Super-Simple Haskell Development with Nix'''] (and [https://discourse.nixos.org/t/super-simple-haskell-development-with-nix/14287/2 discussion] that provides interesting alternative methods together with there pro and cons)
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/stable/cabal-project.html#cfg-field-index-state] for more information.
* 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 `haskellPackages.shellFor {packages= p: [p.myproject1 p.myproject2]}` to create a shell with all wanted packages.
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://haskell.flake.page/ 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.
[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 ==