···
1
+
# xdge - XDG Base Directory Specification for Eio
3
+
This library implements the [XDG Base Directory
4
+
Specification](https://specifications.freedesktop.org/basedir-spec/latest/) for
5
+
OCaml applications using the [Eio](https://github.com/ocaml-multicore/eio)
6
+
effects-based I/O library.
10
+
The XDG Base Directory Specification defines standard locations for user-specific files on Unix-like systems, keeping home directories clean and organized:
12
+
- Config (`~/.config/app`): User preferences and settings
13
+
- Data (`~/.local/share/app`): Persistent application data
14
+
- Cache (`~/.cache/app`): Non-essential cached data (safe to delete)
15
+
- State (`~/.local/state/app`): Logs, history, and runtime state
16
+
- Runtime (`$XDG_RUNTIME_DIR/app`): Sockets, pipes, and session-bound files
18
+
The specification also defines system-wide search paths (`/etc/xdg`,
19
+
`/usr/share`) and a precedence system using environment variables
20
+
(`XDG_CONFIG_HOME`, `XDG_DATA_HOME`, and so on).
24
+
Eio uses a **capability-based** approach to I/O where filesystem access must be
25
+
explicitly passed to functions. This design aligns naturally with XDG directory
26
+
management. For example:
29
+
(* Filesystem access is an explicit capability *)
30
+
let xdg = Xdge.create env#fs "myapp"
33
+
The capability model provides the benefit that code that needs filesystem
34
+
access must receive the `fs` capability, with no hidden global state or ambient
35
+
authority. The `Eio.Path.t` type returned by xdge encapsulates both the
36
+
filesystem capability and the path, preventing path traversal outside the
37
+
granted capability. Applications can restrict filesystem access by passing a
38
+
sandboxed `fs` capability, and xdge respects those boundaries.
43
+
Eio_main.run @@ fun env ->
44
+
let xdg = Xdge.create env#fs "myapp" in
46
+
(* Access XDG directories as Eio paths *)
47
+
let config = Xdge.config_dir xdg in
48
+
let data = Xdge.data_dir xdg in
50
+
(* Search for files following XDG precedence *)
51
+
match Xdge.find_config_file xdg "settings.json" with
52
+
| Some path -> (* use path *)
53
+
| None -> (* use defaults *)
56
+
For CLI applications, xdge provides Cmdliner terms that handle environment
57
+
variable precedence and command-line overrides:
61
+
Eio_main.run @@ fun env ->
62
+
let term = Xdge.Cmd.term "myapp" env#fs () in
63
+
(* Generates --config-dir, --data-dir, etc. flags *)
64
+
(* Respects MYAPP_CONFIG_DIR > XDG_CONFIG_HOME > default *)