Skip to content

Conversation

@Sudha247
Copy link
Collaborator

@Sudha247 Sudha247 commented Nov 24, 2025

Closes #11652

Adds a tutorial for setting up an OxCaml project with Dune package management.

I contemplated where this should go, and thought this might be a good place, after all. It shows (1) how to setup an OxCaml project, (2) servers as a live example of the previous two tutorials.

#12111 is a more comprehensive tutorial about parametric libraries and OxCaml, but it doesn't specifically cover the topic of Dune package management with OxCaml. I recently ported an OxCaml project to build with Dune package management and the configuration was not as straightforward as I expected, and thought a tutorial on this topic might be useful.

This takes some material from https://github.com/gridbugs/hello-oxcaml (thanks @gridbugs!).

@Alizter
Copy link
Collaborator

Alizter commented Nov 24, 2025

Here are some screenshots:

image

I think having these drop-down code excerpts is kind of pointless. Perhaps it would be better just to have the code inline like we do elsewhere in the docs.

@Alizter
Copy link
Collaborator

Alizter commented Nov 24, 2025

The main title doesn't need to say Example: probably.

Copy link
Member

@shonfeder shonfeder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glad to have this included!

@Sudha247 Sudha247 force-pushed the hello-oxcaml branch 2 times, most recently from 65b4bc6 to 64d39ec Compare November 25, 2025 14:48
@Alizter
Copy link
Collaborator

Alizter commented Nov 26, 2025

At some point these how-to's turned into markdown rather than being rst documents which is slightly confusing. In rst we have syntax highlighting for dune files etc. Does anybody remember why markdown was chosen for these?

I would suggest writing this tutorial in rst, not only for consistency, but because it will play better with cross-linking that we have elsewhere in the docs.

Signed-off-by: Sudha Parimala <[email protected]>
@Sudha247
Copy link
Collaborator Author

I would suggest writing this tutorial in rst, not only for consistency, but because it will play better with cross-linking that we have elsewhere in the docs.

I'm not opposed to doing this, but wouldn't be weird if only one of the dune-package-management entries is in rst, while others are in markdown?

@Alizter
Copy link
Collaborator

Alizter commented Nov 26, 2025

Ideally they should be all in rst, but would blow this PR out of scope. If you think it would be too much work to do in this PR, let's instead create an issue so that we remember to do it eventually. I won't block on this point.

@Leonidas-from-XIV
Copy link
Collaborator

Ah, I dug a bit around and it is because of this PR by @emillon where he notes:

  • it maps fully to reST
  • it's more contributor friendly
  • editor tooling is better
  • it alleviates some vendor lock-in (since we're likely to migrate to ocaml.org at some point, and it's more likely to use a variant of md than sphinx support odoc syntax)

Which all sound like pretty decent points.

@Sudha247 Sudha247 requested a review from Alizter November 27, 2025 09:25

As an example of how to add custom repositories and pin dependencies in projects
using Dune Package Management, let's look at setting up a basic OxCaml project.
[OxCaml](https://oxcaml.org/) is a compiler branch with fast moving set of
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put the link on the mention of OxCaml just before.

## Setting up the `dune-workspace`

OxCaml has a custom `opam-repository` that provides compatible dependencies.
This is specified in the `dune-workspace`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds like the opam repository is configured in dune-workspace. I would probably say something like: Dune can use packages from this repositories by configuring it in the dune-workspace file using the repository (link) and lock_dir (link) stanzas.

This is specified in the `dune-workspace`.

```{code-block} dune
(lang dune 3.20)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this 3.21

Comment on lines +27 to +29
In the `dune-project`, we also need to add a constraint to depend on the OxCaml
compiler. Otherwise, the solver may pick up the latest upstream compiler, which
does not support the OxCaml extensions.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"We would like dune to pick the oxcaml compiler rather than the latest upstream one. This can be achieved by manually adding a constraint on the compiler that is picked in the package stanza (link) in the dune-project.


## Building the project

As usual, let's build the project with:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not assume users are used to the usual way of doing things here. Let's instead say, we can now build and execute our binary compiled with OxCaml. We use the dune exec command which will first build all the packages we need, including the OxCaml compiler and then builds our binary and runs it.

You should probably mention the passage of time here, since this step takes a while and dune atm doesn't give too much feedback.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Unfortunately at the moment the compilation of OxCaml is glacial, partly because of limited concurrency but possibly also due to flambda being enabled. The former we can fix with #12737, the latter is outside of our control.


```{code-block} dune

(lang dune 3.20)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lang 3.21

tools in the `dune-workspace` if you intend to use them for your development
workflows.

For `ocamlformat` it would look something like this:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should add a warning that this configuration is likely to change in the future as our method for achieving this settles. Especially once the patches themselves are upstreamed. I think some prose explaining and linking the stanzas would also be a good idea.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nice to document configuring dev-tools in general as part of #12756, then the resulting document could be linked here.

As an example to adding custom repositories and pinning projects, let's look at
setting up a basic OxCaml project. [OxCaml](https://oxcaml.org/) is a compiler
branch with fast moving set of extenstions to OCaml. Some tweaks are required to
set up an OxCaml project with dune package management.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
set up an OxCaml project with dune package management.
set up an OxCaml project with Dune package management.


As an example to adding custom repositories and pinning projects, let's look at
setting up a basic OxCaml project. [OxCaml](https://oxcaml.org/) is a compiler
branch with fast moving set of extenstions to OCaml. Some tweaks are required to
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
branch with fast moving set of extenstions to OCaml. Some tweaks are required to
branch with fast moving set of extensions to OCaml. Some tweaks are required to

tools in the `dune-workspace` if you intend to use them for your development
workflows.

For `ocamlformat` it would look something like this:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nice to document configuring dev-tools in general as part of #12756, then the resulting document could be linked here.

using Dune Package Management, let's look at setting up a basic OxCaml project.
[OxCaml](https://oxcaml.org/) is a compiler branch with fast moving set of
extensions to OCaml. Some tweaks are required to set up a project that compiles
with OxCaml when using dune package management.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick but we should be consistent about how we capitalize Dune (and Dune package management).


## Building the project

As usual, let's build the project with:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Unfortunately at the moment the compilation of OxCaml is glacial, partly because of limited concurrency but possibly also due to flambda being enabled. The former we can fix with #12737, the latter is outside of our control.

(version 3.1.0+ox)))

(lock_dir
(path "dune.lock")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This path is probably optional.

(repositories :standard oxcaml))

(lock_dir
(path "dev-tools.locks/ocamlformat")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This path needs to be adjusted now that #12671 is merged, it should be "_build/.dev-tools.locks/ocamlformat"

let local_ i = 42 in
let j = i + 1 in
print_endline (Printf.sprintf "%d" j)

No newline at end of file
Copy link
Collaborator

@Leonidas-from-XIV Leonidas-from-XIV Nov 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some newline issue here.

(* The `local_` keyword requires OxCaml *)
let local_ i = 42 in
let j = i + 1 in
print_endline (Printf.sprintf "%d" j)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
print_endline (Printf.sprintf "%d" j)
Printf.printf "%d\n" j

@Alizter
Copy link
Collaborator

Alizter commented Nov 27, 2025

We also have a macros for the latest dune lang version which might be sensible to use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[pkg] Build OxCaml with Dune package management

5 participants