Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions haskell-simple-package/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Main where

import HaskellSay (haskellSay)
import ExampleLibrary

main :: IO ()
main = haskellSay $ "If inverted, False is " ++ writeBool (invert False)

16 changes: 16 additions & 0 deletions haskell-simple-package/README.md
Original file line number Diff line number Diff line change
@@ -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 changes: 40 additions & 0 deletions haskell-simple-package/flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions haskell-simple-package/flake.nix
Original file line number Diff line number Diff line change
@@ -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";
};
}
);
}
26 changes: 26 additions & 0 deletions haskell-simple-package/package.yaml
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions haskell-simple-package/src/ExampleLibrary.hs
Original file line number Diff line number Diff line change
@@ -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"
9 changes: 9 additions & 0 deletions haskell-simple-package/test/Spec.hs
Original file line number Diff line number Diff line change
@@ -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