1# OCaml {#sec-language-ocaml}
2
3## User guide {#sec-language-ocaml-user-guide}
4
5OCaml libraries are available in attribute sets of the form `ocaml-ng.ocamlPackages_X_XX` where X is to be replaced with the desired compiler version. For example, ocamlgraph compiled with OCaml 4.12 can be found in `ocaml-ng.ocamlPackages_4_12.ocamlgraph`. The compiler itself is also located in this set, under the name `ocaml`.
6
7If you don't care about the exact compiler version, `ocamlPackages` is a top-level alias pointing to a recent version of OCaml.
8
9OCaml applications are usually available top-level, and not inside `ocamlPackages`. Notable exceptions are build tools that must be built with the same compiler version as the compiler you intend to use like `dune` or `ocaml-lsp`.
10
11To open a shell able to build a typical OCaml project, put the dependencies in `buildInputs` and add `ocamlPackages.ocaml` and `ocamlPackages.findlib` to `nativeBuildInputs` at least.
12For example:
13```nix
14let
15 pkgs = import <nixpkgs> {};
16 # choose the ocaml version you want to use
17 ocamlPackages = pkgs.ocaml-ng.ocamlPackages_4_12;
18in
19pkgs.mkShell {
20 # build tools
21 nativeBuildInputs = with ocamlPackages; [ ocaml findlib dune_2 ocaml-lsp ];
22 # dependencies
23 buildInputs = with ocamlPackages; [ ocamlgraph ];
24}
25```
26
27## Packaging guide {#sec-language-ocaml-packaging}
28
29OCaml 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`.
30
31Given 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`.
32
33Here is a simple package example.
34
35- It defines an (optional) attribute `minimalOCamlVersion` that will be used to
36 throw a descriptive evaluation error if building with an older OCaml is
37 attempted.
38
39- It uses the `fetchFromGitHub` fetcher to get its source.
40
41- `useDune2 = true` ensures that the latest version of Dune is used for the
42 build (this may become the default value in a future release).
43
44- It sets the optional `doCheck` attribute such that tests will be run with
45 `dune runtest -p angstrom` after the build (`dune build -p angstrom`) is
46 complete, but only if the Ocaml version is at at least `"4.05"`.
47
48- It uses the package `ocaml-syntax-shims` as a build input, `alcotest` and
49 `ppx_let` as check inputs (because they are needed to run the tests), and
50 `bigstringaf` and `result` as propagated build inputs (thus they will also be
51 available to libraries depending on this library).
52
53- The library will be installed using the `angstrom.install` file that dune
54 generates.
55
56```nix
57{ lib,
58 fetchFromGitHub,
59 buildDunePackage,
60 ocaml,
61 ocaml-syntax-shims,
62 alcotest,
63 result,
64 bigstringaf,
65 ppx_let }:
66
67buildDunePackage rec {
68 pname = "angstrom";
69 version = "0.15.0";
70 useDune2 = true;
71
72 minimalOCamlVersion = "4.04";
73
74 src = fetchFromGitHub {
75 owner = "inhabitedtype";
76 repo = pname;
77 rev = version;
78 sha256 = "1hmrkdcdlkwy7rxhngf3cv3sa61cznnd9p5lmqhx20664gx2ibrh";
79 };
80
81 checkInputs = [ alcotest ppx_let ];
82 buildInputs = [ ocaml-syntax-shims ];
83 propagatedBuildInputs = [ bigstringaf result ];
84 doCheck = lib.versionAtLeast ocaml.version "4.05";
85
86 meta = {
87 homepage = "https://github.com/inhabitedtype/angstrom";
88 description = "OCaml parser combinators built for speed and memory efficiency";
89 license = lib.licenses.bsd3;
90 maintainers = with lib.maintainers; [ sternenseemann ];
91 };
92```
93
94Here 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.
95
96```nix
97{ lib, fetchurl, buildDunePackage }:
98
99buildDunePackage rec {
100 pname = "wtf8";
101 version = "1.0.2";
102
103 useDune2 = true;
104
105 minimalOCamlVersion = "4.02";
106
107 src = fetchurl {
108 url = "https://github.com/flowtype/ocaml-${pname}/releases/download/v${version}/${pname}-v${version}.tbz";
109 sha256 = "09ygcxxd5warkdzz17rgpidrd0pg14cy2svvnvy1hna080lzg7vp";
110 };
111
112 meta = with lib; {
113 homepage = "https://github.com/flowtype/ocaml-wtf8";
114 description = "WTF-8 is a superset of UTF-8 that allows unpaired surrogates.";
115 license = licenses.mit;
116 maintainers = [ maintainers.eqyiel ];
117 };
118}
119```