Detect operating system, distro and version information
README.md

osrelease #

Detect the operating system, distribution, version and CPU architecture of the machine OCaml code runs on.

Warning. This is a work-in-progress experimental fork of osrelease. Do not use it in production; use osrelease instead.

A tool that installs system packages or picks a prebuilt binary needs to know whether it runs on Ubuntu or Alpine, macOS with Homebrew or MacPorts, and on x86-64 or arm64. OCaml's Sys.os_type only says Unix, Win32 or Cygwin. This library answers the finer question by reading environment variables and /etc/os-release and by running commands such as uname, and returns each answer as a polymorphic variant with an `Unknown or `Other case for what it does not recognise.

Installation #

$ opam install osrelease

If opam cannot find the package, it has not reached the public opam-repository yet. Add the overlay repository first:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install osrelease

Usage #

let detect () =
  let arch = Osrelease_unix.Arch.v () in
  let os = Osrelease_unix.OS.v () in
  Fmt.pr "Running on %a / %a@." Osrelease.OS.pp os Osrelease.Arch.pp arch;
  match Osrelease_unix.Distro.v () with
  | Ok distro -> Fmt.pr "Distro: %a@." Osrelease.Distro.pp distro
  | Error (`Msg msg) -> Fmt.epr "Detection failed: %s@." msg

API #

The package has two libraries. osrelease is the vocabulary: the Arch, OS and Distro variants with to_string, of_string and pp, and the os-release file parser Osrelease.Distro.os_release_fields_of_string. It performs no I/O and links no unix, so a pure codec can depend on it. osrelease.unix (module Osrelease_unix) detects the running system; it reads files and runs commands through bos, and so links unix.

  • Osrelease_unix.Arch.v () is the CPU architecture (`X86_64, `Aarch64, `Arm32 _, `Ppc64 _ and others).
  • Osrelease_unix.OS.v () is the operating system (`Linux, `MacOS, `FreeBSD, `Win32 and others).
  • Osrelease_unix.Distro.v () is the distribution (`Linux `Ubuntu, `MacOS `Homebrew and others), or an error when detection fails.
  • Osrelease_unix.Distro.os_release_field key reads one field of /etc/os-release.
  • Osrelease_unix.Version.v () is the version string of the operating system, if it has one.

Supported platforms #

Linux (Alpine, Arch, CentOS, Debian, Fedora, Gentoo, NixOS, OpenSUSE, Oracle Linux, RHEL, Ubuntu and Android), macOS (Homebrew and MacPorts), FreeBSD, OpenBSD, DragonFly, Cygwin and Win32.

License #

ISC. See LICENSE.md.