diff --git a/haskell.nix/.buildkite/pipeline.yml b/haskell.nix/.buildkite/pipeline.yml new file mode 100644 index 0000000..0a2d6ee --- /dev/null +++ b/haskell.nix/.buildkite/pipeline.yml @@ -0,0 +1,28 @@ +steps: + +# build haskell components +- label: build + commands: + - nix-build -A checks.x86_64-linux.build-all + +# don't run tests until all components are built +- wait + +# run the tests +- label: test + commands: + - nix-build -A checks.x86_64-linux.test + +# run hlint +##- label: hlint +## commands: +## - nix run -f. legacyPackages.x86_64-linux.haskellPackages.hlint +## -c hlint -j --hint .hlint.yaml */ + +# run weeder +##- label: weeder +## commands: +## - nix-build -A weeder-script +## # weeder needs .cabal file: +## - nix run -f. legacyPackages.x86_64-linux.haskellPackages.hpack -c hpack +## - ./result diff --git a/haskell.nix/.gitlab-ci.yml b/haskell.nix/.gitlab-ci.yml new file mode 100644 index 0000000..8809dbe --- /dev/null +++ b/haskell.nix/.gitlab-ci.yml @@ -0,0 +1,35 @@ +default: + # specify nix tag to select runner + tags: [nix] + +stages: + - build + - test + +# build haskell components +build-all: + stage: build + script: + - nix-build -A checks.x86_64-linux.build-all + +# run the tests +test: + stage: test + script: + - nix-build -A checks.x86_64-linux.test + +# run hlint +##hlint: +## stage: test +## script: +## - nix run -f. legacyPackages.x86_64-linux.haskellPackages.hlint +## -c hlint --hint .hlint.yaml */ + +# run weeder +##weeder: +## stage: test +## script: +## - nix-build -A weeder-script +## # weeder needs .cabal file: +## - nix run -f. legacyPackages.x86_64-linux.haskellPackages.hpack -c hpack +## - ./result diff --git a/haskell.nix/README.md b/haskell.nix/README.md new file mode 100644 index 0000000..76f666e --- /dev/null +++ b/haskell.nix/README.md @@ -0,0 +1,17 @@ +Template for haskell.nix configuration for Buildkite or Gitlab CI + +## How to use this template + +1. Copy `flake.nix` file to you repository, it contains all nix dependencies and definitions. + +2. Copy `default.nix` and `shell.nix` files which provide compatibility with non-flake nix interface. + +3. Copy pipeline configuration: `.gitlab-ci.yml` for Gitlab, or `.buildkite/pipeline.yml` for Buildkite. + +4. Replace `pataq-package` in `flake.nix` with your haskell package name (usually specified in `package.yaml`). + +5. If you want to run weeder or hlint in your CI, uncomment related lines in `flake.nix` and in the pipeline configuration. If you don't need weeder or hlint, remove those lines. + +6. Get unstable nix with `nix-command flakes` experimental features enabled, and run `nix flake update` to generate `flake.lock` file. This file pins latest versions of the nix dependencies. + +7. Enjoy working CI. diff --git a/haskell.nix/default.nix b/haskell.nix/default.nix new file mode 100644 index 0000000..873ece4 --- /dev/null +++ b/haskell.nix/default.nix @@ -0,0 +1,13 @@ +(import + ( + let + lock = builtins.fromJSON (builtins.readFile ./flake.lock); + in + fetchTarball { + url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; + sha256 = lock.nodes.flake-compat.locked.narHash; + } + ) + { + src = ./.; + }).defaultNix diff --git a/haskell.nix/flake.nix b/haskell.nix/flake.nix new file mode 100644 index 0000000..e33cd5d --- /dev/null +++ b/haskell.nix/flake.nix @@ -0,0 +1,116 @@ +{ + inputs = { + flake-compat = { + url = "github:edolstra/flake-compat"; + flake = false; + }; + nixpkgs.url = "github:serokell/nixpkgs"; + haskell-nix.url = "github:serokell/haskell.nix/serokell-latest"; + hackage-nix = { + url = "github:input-output-hk/hackage.nix"; + flake = false; + }; + stackage-nix = { + url = "github:input-output-hk/stackage.nix"; + flake = false; + }; + + # if you want to run weeder: + ##haskell-nix-weeder = { + ## url = "github:serokell/haskell-nix-weeder"; + ## flake = false; + ##}; + }; + + outputs = { self, nixpkgs, haskell-nix, hackage-nix, stackage-nix, ... }: + + # for weeder: + ##outputs = { self, nixpkgs, haskell-nix, hackage-nix, stackage-nix, haskell-nix-weeder, ... }: + + let + haskellNix = import haskell-nix { + sourcesOverride = { hackage = hackage-nix; stackage = stackage-nix; }; + }; + + pkgs = import nixpkgs haskellNix.nixpkgsArgs; + lib = pkgs.lib; + + # invoke haskell.nix + hs-pkgs = pkgs.haskell-nix.stackProject { + src = pkgs.haskell-nix.haskellLib.cleanGit { + name = "pataq-package"; + src = ./.; + }; + + # haskell.nix configuration + modules = [{ + packages.pataq-package = { + ghcOptions = [ + # fail on warnings + "-Werror" + # disable optimisations to speed up builds + "-O0" + + # for weeder: produce *.dump-hi files + ##"-ddump-to-file" "-ddump-hi" + ]; + + # for weeder: collect all *.dump-hi files + ##postInstall = weeder-hacks.collect-dump-hi-files; + }; + + }]; + }; + + hs-pkg = hs-pkgs.pataq-package; + + # returns the list of all components for a package + get-package-components = pkg: + # library + lib.optional (pkg ? library) pkg.library + # haddock + ++ lib.optional (pkg ? library) pkg.library.haddock + # exes, tests and benchmarks + ++ lib.attrValues pkg.exes + ++ lib.attrValues pkg.tests + ++ lib.attrValues pkg.benchmarks; + + # all components for the current haskell package + all-components = get-package-components hs-pkg.components; + + # for weeder: + ##weeder-hacks = import haskell-nix-weeder { inherit pkgs; }; + + # nixpkgs has weeder 2, but we use weeder 1 + ##weeder-legacy = pkgs.haskellPackages.callHackageDirect { + ## pkg = "weeder"; + ## ver = "1.0.9"; + ## sha256 = "0gfvhw7n8g2274k74g8gnv1y19alr1yig618capiyaix6i9wnmpa"; + ##} {}; + + # a derivation which generates a script for running weeder + ##weeder-script = weeder-hacks.weeder-script { + ## weeder = weeder-legacy; + ## hs-pkgs = hs-pkgs; + ## local-packages = [ + ## { name = "pataq-package"; subdirectory = "."; } + ## ]; + ##}; + + in { + # nixpkgs revision pinned by this flake + legacyPackages.x86_64-linux = pkgs; + + # derivations that we can run from CI + checks.x86_64-linux = { + # builds all haskell components + build-all = all-components; + + # runs the test + test = hs-pkg.checks.pataq-test; + }; + + # script for running weeder + ##weeder-script = weeder-script; + }; +} diff --git a/haskell.nix/shell.nix b/haskell.nix/shell.nix new file mode 100644 index 0000000..8a7465a --- /dev/null +++ b/haskell.nix/shell.nix @@ -0,0 +1,13 @@ +(import + ( + let + lock = builtins.fromJSON (builtins.readFile ./flake.lock); + in + fetchTarball { + url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; + sha256 = lock.nodes.flake-compat.locked.narHash; + } + ) + { + src = ./.; + }).shellNix.default