Skip to content

Commit

Permalink
Add build / develop environment in Nix
Browse files Browse the repository at this point in the history
  • Loading branch information
Nymphium committed Jan 25, 2022
1 parent 94ea5d6 commit 47bd5e3
Show file tree
Hide file tree
Showing 12 changed files with 5,583 additions and 1 deletion.
47 changes: 47 additions & 0 deletions .github/workflows/test-in-nix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: test in Nix

on:
- push
- pull_request

jobs:
tests-in-nix:
name: Tests in Nix
strategy:
fail-fast: false
matrix:
os:
- ubuntu-latest
- macos-latest
ocaml-version:
- 4_11
- 4_12
- 4_13

runs-on: ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup Nix
uses: cachix/install-nix-action@v15
with:
nix_path: nixpkgs=channel:nixos-21.11
extra_nix_config: |
trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=
substituters = https://cache.nixos.org/
max-jobs = auto
cores = 0
- name: Build
run: |
nix-shell --argstr ocamlVersion ${{ matrix.ocaml-version }} --run "dune build"
- name: Test
run: |
nix-shell --argstr ocamlVersion ${{ matrix.ocaml-version }} --run "dune runtest"
- name: Examples
run: |
nix-shell --argstr ocamlVersion ${{ matrix.ocaml-version }} --run "dune examples"
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: build
name: test

on:
- push
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ lib_test/tests_
*.docdir
.merlin
*.install

/result
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ opam pin add -n httpaf .
opam install --deps-only httpaf
```

If you use [Nix](https://nixos.org/), you can enter `nix-shell`, which contains dev dependencies including ocaml-lsp.

```bash
nix-shell shell.nix
```

After this, you may install a development version of the library using the
install command as usual.

Expand Down
10 changes: 10 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{ pkgs ? import <nixpkgs> {}
, ocamlVersion ? import ./nix/ocamlDefaultVersion.nix
, opam2nix ?
pkgs.callPackage ./nix/opam2nix.nix {
inherit pkgs;
ocamlPackagesOverride = pkgs.ocaml-ng."ocamlPackages_${ocamlVersion}";

} }:

pkgs.callPackage ./nix { inherit ocamlVersion opam2nix; }
36 changes: 36 additions & 0 deletions nix/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{ pkgs, lib, stdenv, ocamlVersion, opam2nix }:
let
inherit (lib) strings;
args = {
inherit (pkgs.ocaml-ng."ocamlPackages_${ocamlVersion}") ocaml;
src =
let ignores = pkgs.lib.strings.fileContents ../.gitignore;
in pkgs.nix-gitignore.gitignoreSourcePure ignores ../.;
};

opam-selection = opam2nix.build (args // {
selection = "${./opam-selection_${ocamlVersion}.nix}";
});

localPackages =
let contents = builtins.attrNames (builtins.readDir ../.);
in builtins.filter (strings.hasSuffix ".opam") contents;

# list of dependencies with "with-test" flag manually
testPackageNames = [ "alcotest" ];
testPackages =
builtins.map (name: builtins.getAttr name opam-selection) testPackageNames;

resolve = opam2nix.resolve (args // {
selection = "./nix/opam-selection_${ocamlVersion}.nix";
}) (localPackages ++ testPackageNames);


in (builtins.listToAttrs (builtins.map (fname:
let packageName = strings.removeSuffix ".opam" fname;
in {
name = packageName;
value = builtins.getAttr packageName opam-selection;
}) localPackages)) // {
inherit resolve opam-selection testPackages;
}
1 change: 1 addition & 0 deletions nix/ocamlDefaultVersion.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"4_13"
Loading

0 comments on commit 47bd5e3

Please sign in to comment.