package xdge

  1. Overview
  2. Docs
XDG Base Directory Specification support for Eio

Install

dune-project
 Dependency

Authors

Maintainers

Sources

xdge-1.0.0.tbz
md5=0053c1eb54e5cf17a9822c283b3d04a5
sha512=02fe624cf299f764b2e34c61b532e3f228d43d8e34c05e8f85dc45a863d00f7868a56e167c2abde53cb705d32eaf9b8705d462308e3cd106827c1f30eec6366f

Description

This library implements the XDG Base Directory Specification with Eio capabilities to provide safe access to configuration, data, cache, state, and runtime directories. The library exposes Cmdliner terms that allow for proper environment variable overrides and command-line flags.

Published: 01 Dec 2025

README

xdge - XDG Base Directory Specification for Eio

This library implements the XDG Base Directory Specification for OCaml applications using the Eio effects-based I/O library.

What is XDG?

The XDG Base Directory Specification defines standard locations for user-specific files on Unix-like systems, keeping home directories clean and organized:

  • Config (~/.config/app): User preferences and settings
  • Data (~/.local/share/app): Persistent application data
  • Cache (~/.cache/app): Non-essential cached data (safe to delete)
  • State (~/.local/state/app): Logs, history, and runtime state
  • Runtime ($XDG_RUNTIME_DIR/app): Sockets, pipes, and session-bound files

The specification also defines system-wide search paths (/etc/xdg, /usr/share) and a precedence system using environment variables (XDG_CONFIG_HOME, XDG_DATA_HOME, and so on).

Why Eio?

Eio uses a capability-based approach to I/O where filesystem access must be explicitly passed to functions. This design aligns naturally with XDG directory management. For example:

(* Filesystem access is an explicit capability *)
let xdg = Xdge.create env#fs "myapp"

The capability model provides the benefit that code that needs filesystem access must receive the fs capability, with no hidden global state or ambient authority. The Eio.Path.t type returned by xdge encapsulates both the filesystem capability and the path, preventing path traversal outside the granted capability. Applications can restrict filesystem access by passing a sandboxed fs capability, and xdge respects those boundaries.

Usage

Eio_main.run @@ fun env ->
  let xdg = Xdge.create env#fs "myapp" in

  (* Access XDG directories as Eio paths *)
  let config = Xdge.config_dir xdg in
  let data = Xdge.data_dir xdg in

  (* Search for files following XDG precedence *)
  match Xdge.find_config_file xdg "settings.json" with
  | Some path -> (* use path *)
  | None -> (* use defaults *)

For CLI applications, xdge provides Cmdliner terms that handle environment variable precedence and command-line overrides:

let () =
  Eio_main.run @@ fun env ->
  let term = Xdge.Cmd.term "myapp" env#fs () in
  (* Generates --config-dir, --data-dir, etc. flags *)
  (* Respects MYAPP_CONFIG_DIR > XDG_CONFIG_HOME > default *)

Installation

opam install xdge

License

ISC

Dependencies (7)

  1. eio_main
  2. xdg
  3. fmt >= "0.11.0"
  4. cmdliner >= "1.2.0"
  5. eio >= "1.1"
  6. ocaml >= "5.1.0"
  7. dune >= "3.20"

Dev Dependencies (2)

  1. alcotest with-test & >= "1.7.0"
  2. odoc with-doc

Used by

None

Conflicts

None