Haskell: Difference between revisions
m Use lang=haskell for syntax highlight |
m →Using shellFor (multiple packages): formatting |
||
| Line 147: | Line 147: | ||
=== Using shellFor (multiple packages) === | === Using shellFor (multiple packages) === | ||
[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: | ||
< | {{file|cabal.project|nix| | ||
<nowiki> | |||
packages: | packages: | ||
frontend/ | frontend/ | ||
backend/ | backend/ | ||
</ | </nowiki> | ||
}} | |||
Finally | Finally you define a nix shell in <code>shell.nix</code> containing: | ||
< | {{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 ]; | ||
} | } | ||
</ | </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 | $ 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>: | ||
< | |||
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 | ||
</ | </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. | ||