1<section xmlns="http://docbook.org/ns/docbook"
2 xmlns:xlink="http://www.w3.org/1999/xlink"
3 xml:id="sec-language-ocaml">
4 <title>OCaml</title>
5
6 <para>
7 OCaml libraries should be installed in
8 <literal>$(out)/lib/ocaml/${ocaml.version}/site-lib/</literal>. Such
9 directories are automatically added to the <literal>$OCAMLPATH</literal>
10 environment variable when building another package that depends on them
11 or when opening a <literal>nix-shell</literal>.
12 </para>
13
14 <para>
15 Given that most of the OCaml ecosystem is now built with dune,
16 nixpkgs includes a convenience build support function called
17 <literal>buildDunePackage</literal> that will build an OCaml package
18 using dune, OCaml and findlib and any additional dependencies provided
19 as <literal>buildInputs</literal> or <literal>propagatedBuildInputs</literal>.
20 </para>
21
22 <para>
23 Here is a simple package example. It defines an (optional) attribute
24 <literal>minimumOCamlVersion</literal> that will be used to throw a
25 descriptive evaluation error if building with an older OCaml is attempted.
26 It uses the <literal>fetchFromGitHub</literal> fetcher to get its source.
27 It sets the <literal>doCheck</literal> (optional) attribute to
28 <literal>true</literal> which means that tests will be run with
29 <literal>dune runtest -p angstrom</literal> after the build
30 (<literal>dune build -p angstrom</literal>) is complete.
31 It uses <literal>alcotest</literal> as a build input (because it is needed
32 to run the tests) and <literal>bigstringaf</literal> and
33 <literal>result</literal> as propagated build inputs (thus they will also
34 be available to libraries depending on this library).
35 The library will be installed using the <literal>angstrom.install</literal>
36 file that dune generates.
37 </para>
38
39 <programlisting>
40{ stdenv, fetchFromGitHub, buildDunePackage, alcotest, result, bigstringaf }:
41
42buildDunePackage rec {
43 pname = "angstrom";
44 version = "0.10.0";
45
46 minimumOCamlVersion = "4.03";
47
48 src = fetchFromGitHub {
49 owner = "inhabitedtype";
50 repo = pname;
51 rev = version;
52 sha256 = "0lh6024yf9ds0nh9i93r9m6p5psi8nvrqxl5x7jwl13zb0r9xfpw";
53 };
54
55 buildInputs = [ alcotest ];
56 propagatedBuildInputs = [ bigstringaf result ];
57 doCheck = true;
58
59 meta = {
60 homepage = https://github.com/inhabitedtype/angstrom;
61 description = "OCaml parser combinators built for speed and memory efficiency";
62 license = stdenv.lib.licenses.bsd3;
63 maintainers = with stdenv.lib.maintainers; [ sternenseemann ];
64 };
65}
66 </programlisting>
67
68 <para>
69 Here is a second example, this time using a source archive generated with
70 <literal>dune-release</literal>. It is a good idea to use this archive when
71 it is available as it will usually contain substituted variables such as a
72 <literal>%%VERSION%%</literal> field. This library does not depend
73 on any other OCaml library and no tests are run after building it.
74 </para>
75
76 <programlisting>
77{ stdenv, fetchurl, buildDunePackage }:
78
79buildDunePackage rec {
80 pname = "wtf8";
81 version = "1.0.1";
82
83 minimumOCamlVersion = "4.01";
84
85 src = fetchurl {
86 url = "https://github.com/flowtype/ocaml-${pname}/releases/download/v${version}/${pname}-${version}.tbz";
87 sha256 = "1msg3vycd3k8qqj61sc23qks541cxpb97vrnrvrhjnqxsqnh6ygq";
88 };
89
90 meta = with stdenv.lib; {
91 homepage = https://github.com/flowtype/ocaml-wtf8;
92 description = "WTF-8 is a superset of UTF-8 that allows unpaired surrogates.";
93 license = licenses.mit;
94 maintainers = [ maintainers.eqyiel ];
95 };
96}
97 </programlisting>
98
99</section>