Module Irmin

Irmin public API.

Irmin is a library to design and use persistent stores with built-in snapshot, branching and reverting mechanisms. Irmin uses concepts similar to Git but it exposes them as a high level library instead of a complex command-line frontend. It features a bidirectional Git backend, where an application can read and persist its state using the Git format, fully-compatible with the usual Git tools and workflows.

Irmin is designed to use a large variety of backends. It is written in pure OCaml and does not depend on external C stubs; it is thus very portable and aims to run everywhere, from Linux to browser and MirageOS unikernels.

Consult the basics and Examples of use for a quick start. See also the documentation for the unix backends.

Release 2.6.1 - %%HOMEPAGE%%

val version : string

The version of the library.


module Type = Repr

Dynamic types for Irmin values.

module Info : sig ... end

Commit info are used to keep track of the origin of write operations in the stores. Info models the metadata associated with commit objects in Git.

module Merge : sig ... end

Merge provides functions to build custom 3-way merge operators for various user-defined contents.

module Diff : sig ... end

Differences between values.

type 'a diff = 'a Diff.t

The type for representing differences betwen values.

module Perms : sig ... end

Types representing permissions 'perms for performing operations on a certain type 'perms t.

Low-level Stores

An Irmin store is automatically built from a number of lower-level stores, each implementing fewer operations, such as content-addressable and atomic-write stores. These low-level stores are provided by various backends.

module type CONTENT_ADDRESSABLE_STORE = sig ... end

Content-addressable backend store.

module type APPEND_ONLY_STORE = sig ... end

Append-only backend store.

module type ATOMIC_WRITE_STORE = sig ... end

Atomic-write stores.

User-Defined Contents

module Path : sig ... end

Store paths.

module Hash : sig ... end

Hashing functions.

module Metadata : sig ... end

Metadata defines metadata that is attached to contents but stored in nodes. The Git backend uses this to indicate the type of file (normal, executable or symlink).

module Contents : sig ... end

Contents specifies how user-defined contents need to be serializable and mergeable.

module Branch : sig ... end

User-defined branches.

type remote = ..

The type for remote stores.

type config

The type for backend-specific configuration values.

Every backend has different configuration options, which are kept abstract to the user.

module Private : sig ... end

Private defines functions only useful for creating new backends. If you are just using the library (and not developing a new backend), you should not use this module.

High-level Stores

An Irmin store is a branch-consistent store where keys are lists of steps.

An example is a Git repository where keys are filenames, i.e. lists of '/'-separated strings. More complex examples are structured values, where steps might contain first-class field accessors and array offsets.

Irmin provides the following features:

exception Closed

The exception raised when any operation is attempted on a closed store, except for S.close, which is idempotent.

module type S = sig ... end

Irmin stores.

module Json_tree : sig ... end
module type S_MAKER = functor (M : Metadata.S) -> functor (C : Contents.S) -> functor (P : Path.S) -> functor (B : Branch.S) -> functor (H : Hash.S) -> S with type key = P.t and type step = P.step and type metadata = M.t and type contents = C.t and type branch = B.t and type hash = H.t and type Private.Sync.endpoint = unit

S_MAKER is the signature exposed by any backend providing S implementations. M is the implementation of user-defined metadata, C is the one for user-defined contents, B is the implementation for branches and H is the implementation for object (blobs, trees, commits) hashes. It does not use any native synchronization primitives.

module type KV = S with type key = string list and type step = string and type branch = string

KV is similar to S but chooses sensible implementations for path and branch.

module type KV_MAKER = functor (C : Contents.S) -> KV with type contents = C.t

KV_MAKER is like S_MAKER but where everything except the contents is replaced by sensible default implementations.


val remote_store : (module S with type t = 'a) -> 'a -> remote

remote_store t is the remote corresponding to the local store t. Synchronization is done by importing and exporting store slices, so this is usually much slower than native synchronization using Store.remote but it works for all backends.

module type SYNC = sig ... end

SYNC provides functions to synchronize an Irmin store with local and remote Irmin stores.

module Sync (S : S) : SYNC with type db = S.t and type commit = S.commit

The default Sync implementation.


These examples are in the examples directory of the distribution.

Syncing with a remote

A simple synchronization example, using the Git backend and the Sync helpers. The code clones a fresh repository if the repository does not exist locally, otherwise it performs a fetch: in this case, only the missing contents are downloaded.

open Lwt.Infix
module S = Irmin_unix.Git.FS.KV (Irmin.Contents.String)
module Sync = Irmin.Sync (S)

let config = Irmin_git.config "/tmp/test"

let upstream =
  if Array.length Sys.argv = 2 then
    Uri.of_string (Store.remote Sys.argv.(1))
  else (
    Printf.eprintf "Usage: sync [uri]\n%!";
    exit 1)

let test () =
  S.Repo.v config >>= S.master >>= fun t ->
  Sync.pull_exn t upstream `Set >>= fun () ->
  S.get t [ "" ] >|= fun r -> Printf.printf "%s\n%!" r

let () = (test ())

Mergeable logs

The complete code for the following can be found in examples/

We will demonstrate the use of custom merge operators by defining mergeable debug log files. We first define a log entry as a pair of a timestamp and a message, using the combinator exposed by Irmin.Type:

open Lwt.Infix
open Astring

let time = ref 0L
let failure fmt = Fmt.kstrf failwith fmt

(* A log entry *)
module Entry : sig
  include Irmin.Type.S

  val v : string -> t
  val timestamp : t -> int64
end = struct
  type t = { timestamp : int64; message : string } [@@deriving irmin]

  let compare x y = x.timestamp y.timestamp

  let v message =
    time := Int64.add 1L !time;
    { timestamp = !time; message }

  let timestamp t = t.timestamp

  let pp ppf { timestamp; message } = ppf "%04Ld: %s" timestamp message

  let of_string str =
    match String.cut ~sep:": " str with
    | None -> Error (`Msg ("invalid entry: " ^ str))
    | Some (x, message) -> (
        try Ok { timestamp = Int64.of_string x; message }
        with Failure e -> Error (`Msg e))

  let t = ~pp ~of_string ~compare t

A log file is a list of entries (one per line), ordered by decreasing order of timestamps. The 3-way merge operator for log files concatenates and sorts the new entries and prepend them to the common ancestor's ones.

(* A log file *)
module Log : sig
  include Irmin.Contents.S

  val add : t -> Entry.t -> t
  val empty : t
end = struct
  type t = Entry.t list [@@deriving irmin]

  let empty = []
  let pp_entry = Irmin.Type.pp Entry.t
  let lines ppf l = List.iter ( ppf "%a\n" pp_entry) (List.rev l)

  let of_string str =
    let lines = String.cuts ~empty:false ~sep:"\n" str in
        (fun acc l ->
          match Irmin.Type.of_string Entry.t l with
          | Ok x -> x :: acc
          | Error (`Msg e) -> failwith e)
        [] lines
      |> fun l -> Ok l
    with Failure e -> Error (`Msg e)

  let t = ~pp:lines ~of_string t
  let timestamp = function [] -> 0L | e :: _ -> Entry.timestamp e

  let newer_than timestamp file =
    let rec aux acc = function
      | [] -> List.rev acc
      | h :: _ when Entry.timestamp h <= timestamp -> List.rev acc
      | h :: t -> aux (h :: acc) t
    aux [] file

  let merge ~old t1 t2 =
    let open Irmin.Merge.Infix in
    old () >>=* fun old ->
    let old = match old with None -> [] | Some o -> o in
    let ts = timestamp old in
    let t1 = newer_than ts t1 in
    let t2 = newer_than ts t2 in
    let t3 =
      List.sort ( Entry.t) (List.rev_append t1 t2)
    Irmin.Merge.ok (List.rev_append t3 old)

  let merge = Irmin.Merge.(option (v t merge))
  let add t e = e :: t

Note: The serialisation primitives used in that example are not very efficient in this case as they parse the file every time. For real usage, you would write buffered versions of Log.pp and Log.of_string.

To persist the log file on disk, we need to choose a backend. We show here how to use the on-disk Git backend on Unix.

(* Build an Irmin store containing log files. *)
module Store = Irmin_unix.Git.FS.KV (Log)

(* Set-up the local configuration of the Git repository. *)
let config = Irmin_git.config ~bare:true Config.root

(* Convenient alias for the info function for commit messages *)
let info =

We can now define a toy example to use our mergeable log files.

let log_file = [ "local"; "debug" ]

let all_logs t =
  Store.find t log_file >|= function None -> Log.empty | Some l -> l

(** Persist a new entry in the log. Pretty inefficient as it reads/writes
    the whole file every time. *)
let log t fmt =
    (fun message ->
      all_logs t >>= fun logs ->
      let logs = Log.add logs (Entry.v message) in
      Store.set_exn t ~info:(info "Adding a new entry") log_file logs)

let print_logs name t =
  all_logs t >|= fun logs -> "-----------\n%s:\n-----------\n%a%!" name (Irmin.Type.pp Log.t)

let main () =
  Config.init ();
  Store.Repo.v config >>= fun repo ->
  Store.master repo >>= fun t ->
  (* populate the log with some random messages *)
    (fun msg -> log t "This is my %s " msg)
    [ "first"; "second"; "third" ]
  >>= fun () ->
  Printf.printf "%s\n\n" what;
  print_logs "lca" t >>= fun () ->
  Store.clone ~src:t ~dst:"test" >>= fun x ->
  log x "Adding new stuff to x" >>= fun () ->
  log x "Adding more stuff to x" >>= fun () ->
  log x "More. Stuff. To x." >>= fun () ->
  print_logs "branch 1" x >>= fun () ->
  log t "I can add stuff on t also" >>= fun () ->
  log t "Yes. On t!" >>= fun () ->
  print_logs "branch 2" t >>= fun () ->
  Store.merge_into ~info:(info "Merging x into t") x ~into:t >>= function
  | Ok () -> print_logs "merge" t
  | Error _ -> failwith "conflict!"

let () = (main ())


module Dot (S : S) : Dot.S with type db = S.t

Dot provides functions to export a store to the Graphviz `dot` format.


API to create new Irmin backends. A backend is an implementation exposing either a concrete implementation of S or a functor providing S once applied.

There are two ways to create a concrete Irmin.S implementation:

module type APPEND_ONLY_STORE_MAKER = functor (K : Type.S) -> functor (V : Type.S) -> sig ... end

APPEND_ONLY_STORE_MAKER is the signature exposed by append-only store backends. K is the implementation of keys and V is the implementation of values.

module type CONTENT_ADDRESSABLE_STORE_MAKER = functor (K : Hash.S) -> functor (V : Type.S) -> sig ... end

CONTENT_ADDRESSABLE_STOREMAKER is the signature exposed by content-addressable store backends. K is the implementation of keys and V is the implementation of values.

module Content_addressable (S : APPEND_ONLY_STORE_MAKER) (K : Hash.S) (V : Type.S) : sig ... end
module type ATOMIC_WRITE_STORE_MAKER = functor (K : Type.S) -> functor (V : Type.S) -> sig ... end

ATOMIC_WRITE_STORE_MAKER is the signature exposed by atomic-write store backends. K is the implementation of keys and V is the implementation of values.

Simple store creator. Use the same type of all of the internal keys and store all the values in the same store.

module Of_private (P : Private.S) : S with type key = P.Node.Path.t and type contents = P.Contents.value and type branch = P.Branch.key and type hash = P.Hash.t and type step = P.Node.Path.step and type metadata = P.Node.Metadata.t and type Key.step = P.Node.Path.step and type repo = P.Repo.t and type slice = P.Slice.t and module Private = P

Advanced store creator.

module Export_for_backends : sig ... end

Helper module containing useful top-level types for defining Irmin backends. This module is relatively unstable.