In this tutorial, you will learn how to initialize an OCaml project that can be developed using dune
.
Initialize a new project
Make
dune
executable available:Scaffold a new project using
dune
:Enter the project directory:
Add flake.nix to the project
Initialize the template from the root directory of the project:
Open
flake.nix
and set thepname
andversion
of the package to be built, e.g.:The template includes a custom
.envrc
for OCaml. Allow direnv:
Developing
Once the Nix shell is enabled, you can use dune
inside the project directory, as you would do in a normal OCaml project.
Adding OCaml dependencies
To add library dependencies to your OCaml project, follow this instruction. In this example, we will be adding cmdliner
as a new dependency.
Add the libraries to
propagatedBuildInputs
(orcheckInputs
if it's a test dependency) attribute to your package:Add them to
depends
field of yourpackage
in thedune-project
file:Also add them to
libraries
field of a suitable stanza in the relevantdune
file.
Generating documentation for the dependencies
To generate documentation of the dependencies, use odig
.
Run the following command to generate a documentation:
Or use a
justfile
recipe bundled in the template:To search through the documentation, you can run a local instance of sherlodoc or open a generated HTML file directly.
Running sherlodoc
To run sherlodoc, follow this instruction.
Enable
sherlodoc
in theflake.nix
:Run
sherlodoc index
on*.odocl
files. The template contains ajustfile
recipe, so you can just run:To browse the documentation, run:
or
For a detailed description, consult the documentation of sherlodoc.