Add haskell-project

This commit is contained in:
harris-chris 2022-10-08 14:12:40 +09:00
parent 8eed91f4cd
commit 3d1f7a9685
7 changed files with 144 additions and 0 deletions

8
haskell-project/Main.hs Normal file
View file

@ -0,0 +1,8 @@
module Main where
import HaskellSay (haskellSay)
import ExampleLibrary
main :: IO ()
main = haskellSay $ "If inverted, False is " ++ writeBool (invert False)

16
haskell-project/README.md Normal file
View file

@ -0,0 +1,16 @@
**Nix flake for an example haskell package**
To get started:
- In the `package.yaml`, set any names beginning with `example` to your chosen name - for example, change the `name` setting on the first line from `example-package` to the nameof your new package.
- Run `hpack` from the terminal; this will generate a cabal file for your project.
- Run `cabal test` or `cabal run ${executable-name}`, where `${executable-name}` is the name of your executable, defined under `executables` in the `package.yaml`.
**Overview of what's here**
This flake has a simple executable, a library that that executable depends on, and a test suite which tests the library.
It uses cabal to build, hpack as the format for the cabal project specification, and hspec to run tests.
Remember to run hpack after any changes to the `package.yaml`, in order to re-create the `${project-name}.cabal` file, as well as the first time you try to use this.
The executable can be run via cabal using `cabal run example-executable`, whereas tests can be run via `cabal test`.

40
haskell-project/flake.lock generated Normal file
View file

@ -0,0 +1,40 @@
{
"nodes": {
"flake-utils": {
"locked": {
"lastModified": 1659877975,
"narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1628427351,
"narHash": "sha256-WuZUIQ07AvRw+T9wvQ3qFf8MXmKZ+ktZz9drNgWXDbs=",
"path": "/nix/store/aqinic6h77nrsrzwdsq2mxihw0kd87ml-source",
"rev": "348bc5de8bca09c624f5c4975f538684da4713d2",
"type": "path"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

36
haskell-project/flake.nix Normal file
View file

@ -0,0 +1,36 @@
{
description = "Example haskell package with library, executable and tests";
inputs = {
nixpkgs.url = "nixpkgs";
flake-utils.url = github:numtide/flake-utils;
};
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
overlay = (final: prev: {
inherit example-package;
});
pkgs = import nixpkgs {
inherit system;
overlays = [ overlay ];
};
example-package = pkgs.haskellPackages.callCabal2nix "example-package" ./. {};
in
{
packages = { inherit example-package; };
defaultPackage = example-package;
devShell = pkgs.haskellPackages.shellFor {
name = "example-package";
packages = p: [ example-package ];
withHoogle = true;
buildInputs = with pkgs; with pkgs.haskellPackages; [
haskell-language-server
ghcid
cabal-install
hpack
];
shellHook = "command -v fish &> /dev/null && fish";
};
}
);
}

View file

@ -0,0 +1,26 @@
name: example-package
dependencies:
- base == 4.*
- haskell-say
library:
source-dirs: src
exposed-modules: ExampleLibrary
executables:
example-executable:
main: Main.hs
dependencies:
- example-package
tests:
spec:
main: Spec.hs
source-dirs:
- test
- src
dependencies:
- hspec == 2.*
- HUnit >= 1.6.0.0
build-tools: hspec-discover

View file

@ -0,0 +1,9 @@
module ExampleLibrary where
invert :: Bool -> Bool
invert True = False
invert False = True
writeBool :: Bool -> String
writeBool True = "True"
writeBool False = "False"

View file

@ -0,0 +1,9 @@
import Test.Hspec
import ExampleLibrary(invert)
main :: IO ()
main = hspec $ do
describe "test simple functions" $ do
it "ensures invert True is False" $ do
invert True `shouldBe` False