Skip to content

Commit

Permalink
[OPS-958] Add template for a haskell.nix project
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenyavinogradov committed Jun 1, 2021
1 parent 0c5be7d commit 1e3dd8b
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 0 deletions.
28 changes: 28 additions & 0 deletions haskell.nix/.buildkite/pipeline.yml
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions haskell.nix/.gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions haskell.nix/README.md
Original file line number Diff line number Diff line change
@@ -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.
13 changes: 13 additions & 0 deletions haskell.nix/default.nix
Original file line number Diff line number Diff line change
@@ -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
116 changes: 116 additions & 0 deletions haskell.nix/flake.nix
Original file line number Diff line number Diff line change
@@ -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;
};
}
13 changes: 13 additions & 0 deletions haskell.nix/shell.nix
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1e3dd8b

Please sign in to comment.