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; [ 22 ocaml 23 findlib 24 dune_2 25 ocaml-lsp 26 ]; 27 # dependencies 28 buildInputs = with ocamlPackages; [ ocamlgraph ]; 29} 30``` 31 32## Packaging guide {#sec-language-ocaml-packaging} 33 34OCaml 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`. 35 36Given 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`. 37 38Here is a simple package example. 39 40- It defines an (optional) attribute `minimalOCamlVersion` (see note below) 41 that will be used to throw a descriptive evaluation error if building with 42 an older OCaml is attempted. 43 44- It uses the `fetchFromGitHub` fetcher to get its source. 45 46- It also accepts a `duneVersion` parameter (valid values are `"1"`, `"2"`, and 47 `"3"`). The recommended practice is to set it only if you don't want the default 48 value and/or it depends on something else like package version. You might see 49 a not-supported argument `useDune2`. The behavior was `useDune2 = true;` => 50 `duneVersion = "2";` and `useDune2 = false;` => `duneVersion = "1";`. It was 51 used at the time when dune3 didn't exist. 52 53- It sets the optional `doCheck` attribute such that tests will be run with 54 `dune runtest -p angstrom` after the build (`dune build -p angstrom`) is 55 complete, but only if the OCaml version is at at least `"4.05"`. 56 57- It uses the package `ocaml-syntax-shims` as a build input, `alcotest` and 58 `ppx_let` as check inputs (because they are needed to run the tests), and 59 `bigstringaf` and `result` as propagated build inputs (thus they will also be 60 available to libraries depending on this library). 61 62- The library will be installed using the `angstrom.install` file that dune 63 generates. 64 65```nix 66{ 67 lib, 68 fetchFromGitHub, 69 buildDunePackage, 70 ocaml, 71 ocaml-syntax-shims, 72 alcotest, 73 result, 74 bigstringaf, 75 ppx_let, 76}: 77 78buildDunePackage (finalAttrs: { 79 pname = "angstrom"; 80 version = "0.15.0"; 81 82 minimalOCamlVersion = "4.04"; 83 84 src = fetchFromGitHub { 85 owner = "inhabitedtype"; 86 repo = "angstrom"; 87 tag = finalAttrs.version; 88 hash = "sha256-MK8o+iPGANEhrrTc1Kz9LBilx2bDPQt7Pp5P2libucI="; 89 }; 90 91 buildInputs = [ ocaml-syntax-shims ]; 92 93 propagatedBuildInputs = [ 94 bigstringaf 95 result 96 ]; 97 98 doCheck = lib.versionAtLeast ocaml.version "4.05"; 99 checkInputs = [ 100 alcotest 101 ppx_let 102 ]; 103 104 meta = { 105 homepage = "https://github.com/inhabitedtype/angstrom"; 106 description = "OCaml parser combinators built for speed and memory efficiency"; 107 license = lib.licenses.bsd3; 108 maintainers = with lib.maintainers; [ sternenseemann ]; 109 }; 110}) 111``` 112 113Here 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. 114 115```nix 116{ 117 lib, 118 fetchurl, 119 buildDunePackage, 120}: 121 122buildDunePackage (finalAtts: { 123 pname = "wtf8"; 124 version = "1.0.2"; 125 126 minimalOCamlVersion = "4.02"; 127 128 src = fetchurl { 129 url = "https://github.com/flowtype/ocaml-wtf8/releases/download/v${finalAtts.version}/wtf8-v${finalAtts.version}.tbz"; 130 hash = "sha256-d5/3KUBAWRj8tntr4RkJ74KWW7wvn/B/m1nx0npnzyc="; 131 }; 132 133 meta = { 134 homepage = "https://github.com/flowtype/ocaml-wtf8"; 135 description = "WTF-8 is a superset of UTF-8 that allows unpaired surrogates"; 136 license = lib.licenses.mit; 137 maintainers = [ lib.maintainers.eqyiel ]; 138 }; 139}) 140``` 141 142The build will automatically fail if two distinct versions of the same library 143are added to `buildInputs` (which usually happens transitively because of 144`propagatedBuildInputs`). Set `dontDetectOcamlConflicts` to true to disable this 145behavior.