Haskell: Difference between revisions

Pigs (talk | contribs)
m Use lang=haskell for syntax highlight
Pigs (talk | contribs)
Line 147: Line 147:
===  Using shellFor (multiple packages) ===
===  Using shellFor (multiple packages) ===


<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.
[https://nixos.org/manual/nixpkgs/stable/#haskell-shellFor shellFor] is similar to <code>developPackage</code> but (slightly) more complicated to also allow you to develop multiples packages at the same time (it can work in conjuction with [https://cabal.readthedocs.io/en/stable/cabal-project-description-file.html cabal.project]). 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 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.
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 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.
Line 153: Line 153:
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 lang=text>
{{file|cabal.project|nix|
<nowiki>
packages:
packages:
   frontend/
   frontend/
   backend/
   backend/
</syntaxhighlight>
</nowiki>
}}


Finally create a file <code>shell.nix</code> containing:
Finally you define a nix shell in <code>shell.nix</code> containing:


<syntaxhighlight lang="nix">
{{file|shell.nix|nix|
with import <nixpkgs> {};
<nowiki>
with import </nowiki><<nowiki>nixpkgs</nowiki>><nowiki> {};
# We add our packages to the haskell package set
# We add our packages to the haskell package set
(haskellPackages.extend (haskell.lib.compose.packageSourceOverrides {
(haskellPackages.extend (haskell.lib.compose.packageSourceOverrides {
Line 174: Line 177:
     buildInputs = [ pkgs.python pkgs.cabal-install ];
     buildInputs = [ pkgs.python pkgs.cabal-install ];
   }
   }
</syntaxhighlight>
</nowiki>
}}


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:
Line 180: Line 184:
<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
$ nix-shell
$ nix-shell
$ cabal new-build all
$ cabal build all
</syntaxhighlight>
</syntaxhighlight>


If you want to be able to compile a project non-incrementally with <code>nix-build</code> (say the backend in the above example) you can put in <code>default.nix</code>:
If you want to be able to compile a project non-incrementally with <code>nix-build</code> (say the backend in the above example) you can put in <code>default.nix</code>:
<syntaxhighlight lang="nix">
 
with import <nixpkgs> {};
{{file|default.nix|nix|
<nowiki>
with import </nowiki><<nowiki>nixpkgs</nowiki>><nowiki> {};
# We add our packages to the haskell package set
# We add our packages to the haskell package set
(haskellPackages.extend (haskell.lib.compose.packageSourceOverrides {
(haskellPackages.extend (haskell.lib.compose.packageSourceOverrides {
Line 191: Line 197:
   backend = ./backend;
   backend = ./backend;
})).backend
})).backend
</syntaxhighlight>
</nowiki>
}}
 
or if you want to create a single derivation file, you can use <code>if pkgs.lib.inNixShell then … else …</code> to output the shell when we start a shell and the packages when we want to build them. You can find [https://github.com/kowainik/summoner/blob/60de4f2f087e5bd2beaad9253e7eded731cfbaaf/default.nix here] an example.
or if you want to create a single derivation file, you can use <code>if pkgs.lib.inNixShell then … else …</code> to output the shell when we start a shell and the packages when we want to build them. You can find [https://github.com/kowainik/summoner/blob/60de4f2f087e5bd2beaad9253e7eded731cfbaaf/default.nix here] an example.