OCaml By Examples

publishing

my_lib.ml

...
let add_one i = i + 1

my_lib.opam

A library (or set of dune libraries) can be published to an opam repository. For this, an opam package file needs to be created with these fields.
opam-version: "2.0"
name: "my_lib"
version: "0.2.0"
synopsis: "A library to do things"
maintainer: "your_email@gmail.com"
authors: "your_email@gmail.com"
license: "Apache-2.0"
homepage: "https://github.com/mimoo/my_lib"
bug-reports: "your_email@gmail.com"
depends: [
  "ocaml" {>= "4.12.0" & < "4.13"}
  "core" {>= "v0.14.0" & < "v.0.15"}
]
build: ["dune" "build" "-p" name "-j" jobs]
dev-repo: "git+https://github.com/mimoo/my_lib.git"

dune

The corresponding dune file must have a `public_name` field sharing the same name. This `public_name` field will be used by other dune files to list their dependency on your library, while the `name` will be used by OCaml code that wants to interact with the module. (In theory the `name` field can be anything but you should avoid that.)
(library
 (name my_lib)
 (public_name my_lib)
 (libraries core))

dune2

An opam package can be a set of dune libraries. This sub-library will have to be imported as `my_lib.thing` by other dune files, but will be used as `my_lib_thing` by OCaml code.
(library
 (name my_lib_thing)
 (public_name my_lib.thing)
 (libraries core))

LICENSE

Before publishing an opam package you need a license in your repository.
whatever license you prefer

README.md

And a README, we're not fools.
# My Lib

That's my cool lib!

CHANGES.md

You will also need to keep track of a changelog for each new version of your library.
## 0.2.0 (2020-05-18)

New minor version... Nothing should break.

## 0.1.0 (2020-05-14)

Wow, first version!

console

To publish a package you can install `dune-release`.
$ opam install dune-release
Make sure your package is correctly set up
$ dune-release lint
This will automatically detect the new version (in `CHANGES.md` and your opam package file `my_lib.opam`) and tag the repository accordingly.
$ dune-release tag
Finally, you can use this command to push the tag to your remote repository and create a pull request to the opam repository.
$ dune-release
next: pins