2020-12-07 07:45:13 +00:00
# OCaml {#sec-language-ocaml}
OCaml libraries should be installed in `$(out)/lib/ocaml/${ocaml.version}/site-lib/` . Such directories are automatically added to the `$OCAMLPATH` environment variable when building another package that depends on them or when opening a `nix-shell` .
Given that most of the OCaml ecosystem is now built with dune, nixpkgs includes a convenience build support function called `buildDunePackage` that will build an OCaml package using dune, OCaml and findlib and any additional dependencies provided as `buildInputs` or `propagatedBuildInputs` .
2021-06-28 23:13:55 +00:00
Here is a simple package example.
- It defines an (optional) attribute `minimalOCamlVersion` that will be used to
throw a descriptive evaluation error if building with an older OCaml is
attempted.
- It uses the `fetchFromGitHub` fetcher to get its source.
- `useDune2 = true` ensures that the latest version of Dune is used for the
build (this may become the default value in a future release).
- It sets the optional `doCheck` attribute such that tests will be run with
`dune runtest -p angstrom` after the build (`dune build -p angstrom`) is
complete, but only if the Ocaml version is at at least `"4.05"` .
- It uses the package `ocaml-syntax-shims` as a build input, `alcotest` and
`ppx_let` as check inputs (because they are needed to run the tests), and
`bigstringaf` and `result` as propagated build inputs (thus they will also be
available to libraries depending on this library).
- The library will be installed using the `angstrom.install` file that dune
generates.
2020-12-07 07:45:13 +00:00
```nix
2021-06-28 23:13:55 +00:00
{ lib,
fetchFromGitHub,
buildDunePackage,
ocaml,
ocaml-syntax-shims,
alcotest,
result,
bigstringaf,
ppx_let }:
2020-12-07 07:45:13 +00:00
buildDunePackage rec {
pname = "angstrom";
2021-06-28 23:13:55 +00:00
version = "0.15.0";
useDune2 = true;
2020-12-07 07:45:13 +00:00
2021-06-28 23:13:55 +00:00
minimalOCamlVersion = "4.04";
2020-12-07 07:45:13 +00:00
src = fetchFromGitHub {
owner = "inhabitedtype";
repo = pname;
rev = version;
2021-06-28 23:13:55 +00:00
sha256 = "1hmrkdcdlkwy7rxhngf3cv3sa61cznnd9p5lmqhx20664gx2ibrh";
2020-12-07 07:45:13 +00:00
};
2021-06-28 23:13:55 +00:00
checkInputs = [ alcotest ppx_let ];
buildInputs = [ ocaml-syntax-shims ];
2020-12-07 07:45:13 +00:00
propagatedBuildInputs = [ bigstringaf result ];
2021-06-28 23:13:55 +00:00
doCheck = lib.versionAtLeast ocaml.version "4.05";
2020-12-07 07:45:13 +00:00
2021-06-28 23:13:55 +00:00
meta = {
2020-12-07 07:45:13 +00:00
homepage = "https://github.com/inhabitedtype/angstrom";
description = "OCaml parser combinators built for speed and memory efficiency";
2021-06-28 23:13:55 +00:00
license = lib.licenses.bsd3;
maintainers = with lib.maintainers; [ sternenseemann ];
2020-12-07 07:45:13 +00:00
};
```
Here is a second example, this time using a source archive generated with `dune-release` . It is a good idea to use this archive when it is available as it will usually contain substituted variables such as a `%%VERSION%%` field. This library does not depend on any other OCaml library and no tests are run after building it.
```nix
2021-06-28 23:13:55 +00:00
{ lib, fetchurl, buildDunePackage }:
2020-12-07 07:45:13 +00:00
buildDunePackage rec {
pname = "wtf8";
2021-06-28 23:13:55 +00:00
version = "1.0.2";
useDune2 = true;
2020-12-07 07:45:13 +00:00
2021-06-28 23:13:55 +00:00
minimalOCamlVersion = "4.02";
2020-12-07 07:45:13 +00:00
src = fetchurl {
2021-06-28 23:13:55 +00:00
url = "https://github.com/flowtype/ocaml-${pname}/releases/download/v${version}/${pname}-v${version}.tbz";
sha256 = "09ygcxxd5warkdzz17rgpidrd0pg14cy2svvnvy1hna080lzg7vp";
2020-12-07 07:45:13 +00:00
};
2021-01-15 22:18:51 +00:00
meta = with lib; {
2020-12-07 07:45:13 +00:00
homepage = "https://github.com/flowtype/ocaml-wtf8";
description = "WTF-8 is a superset of UTF-8 that allows unpaired surrogates.";
license = licenses.mit;
maintainers = [ maintainers.eqyiel ];
};
}
```