Introduction
Given the following haskell script generate-random-samples.hs
that requires mwc-random
1 |
|
… how do you run it without having to globally install the package or having to build a whole cabal project ?
Initial approach
One of the simplest approaches using nix is the following:
1 |
|
In order to reuse the command so other people can run the script, you can add the following shell.nix
:
1 |
|
So now you only need to call $ nix-shell
to enter into a pure shell with a specific GHC version that includes all your dependencies:
1 |
|
Improving the first approach
The only issue is that you must be aware of how nix work in order to be able to run the script.
But these could be solved using bash shebangs in your haskell script:
1 |
|
So now, you can run your haskell script in an environment without ghc:
1 |
|
Update: 11-03-2020
After reading the following comment in reddit, I think it is worth mentioning that you can achieve better modularity in exchange of maintainability, by having everything on the haskell script, without having to depend on a shell.nix
file:
1 |
|