diff --git a/haskell-project/Main.hs b/haskell-project/Main.hs new file mode 100644 index 0000000..584b0d7 --- /dev/null +++ b/haskell-project/Main.hs @@ -0,0 +1,8 @@ +module Main where + +import HaskellSay (haskellSay) +import ExampleLibrary + +main :: IO () +main = haskellSay $ "If inverted, False is " ++ writeBool (invert False) + diff --git a/haskell-project/README.md b/haskell-project/README.md new file mode 100644 index 0000000..756d11e --- /dev/null +++ b/haskell-project/README.md @@ -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`. + diff --git a/haskell-project/flake.lock b/haskell-project/flake.lock new file mode 100644 index 0000000..da66aa8 --- /dev/null +++ b/haskell-project/flake.lock @@ -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 +} diff --git a/haskell-project/flake.nix b/haskell-project/flake.nix new file mode 100644 index 0000000..6606b2c --- /dev/null +++ b/haskell-project/flake.nix @@ -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"; + }; + } + ); +} diff --git a/haskell-project/package.yaml b/haskell-project/package.yaml new file mode 100644 index 0000000..05c5c6a --- /dev/null +++ b/haskell-project/package.yaml @@ -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 diff --git a/haskell-project/src/ExampleLibrary.hs b/haskell-project/src/ExampleLibrary.hs new file mode 100644 index 0000000..3371af7 --- /dev/null +++ b/haskell-project/src/ExampleLibrary.hs @@ -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" diff --git a/haskell-project/test/Spec.hs b/haskell-project/test/Spec.hs new file mode 100644 index 0000000..4aba858 --- /dev/null +++ b/haskell-project/test/Spec.hs @@ -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 +