os_info
A Rust crate that detects the current operating system's type, version, edition, bitness, and architecture with a single function call.
Repository Health
Technical Analysis
os_info is a Rust library for detecting the host operating system at runtime. A single os_info::get() call returns a structured Info value describing the OS type, version, optional edition and codename, bitness (32/64-bit), and processor architecture, with a Display implementation for human-readable output.
The crate targets an unusually wide platform surface: Windows, macOS, iOS, the BSD family (FreeBSD, NetBSD, OpenBSD, DragonFly), AIX, illumos, Android, Emscripten, Redox, Cygwin, Hurd, and Linux, where it distinguishes more than 60 individual distributions (Ubuntu, Debian, Fedora, Arch, NixOS, and many others) by parsing /etc/os-release and distro-specific release files. Optional serde and schemars features let the Info type be serialized or exposed as a JSON schema, and a companion os_info_cli binary wraps the library for command-line use.
What You Get
- A single
os_info::get()function returning anInfostruct withos_type(),version(),edition(),codename(),bitness(), andarchitecture()accessors - Detection for 60+ distinct Linux distributions via
/etc/os-releaseand legacy release-file parsing, in addition to Windows, macOS, iOS, BSDs, AIX, illumos, Android, Redox, Cygwin, Hurd, and Emscripten - Optional
serdederive support (enabled by default) for serializingInfoto JSON or other formats, plus an optionalschemarsfeature for JSON Schema generation - A companion
os_info_clibinary (installable viacargo install os_info_cli) that prints OS information from the command line - Platform-specific bitness and architecture detection using native APIs (Win32
IsWow64Process,uname -m, and equivalents) rather than compile-time target assumptions
Common Use Cases
- Tailoring installer or setup logic to the exact Linux distribution or Windows edition a program is running on
- Including OS type/version/architecture in diagnostic reports, crash logs, or telemetry payloads
- Gating platform-specific code paths or feature availability at runtime instead of relying solely on
cfg(target_os) - Printing environment details for CLI tools’
--version/--infooutput to help with support and bug triage - Selecting the correct package format or binary variant to download/install based on detected distro and bitness
Under The Hood
Architecture
os_info uses a compile-time strategy pattern: src/lib.rs selects a platform module (mod imp) via #[cfg(target_os = "...")] gates, with one implementation per target — linux/mod.rs, windows/mod.rs, macos/mod.rs, android/mod.rs, ios/mod.rs, plus BSD, AIX, illumos, Cygwin, Redox, Hurd, and a fallback unknown/mod.rs — each exposing a current_platform() function that returns the shared Info struct (src/info.rs). Cross-cutting concerns live in dedicated modules: os_type.rs defines the Type enum shared across all platforms, version.rs and bitness.rs model version/bitness values, matcher.rs provides regex-based text matching used by Linux distro detection, and uname.rs wraps the POSIX uname call for Unix-like targets. This keeps platform-specific logic fully isolated while sharing one public API surface, so adding a new OS means adding a new imp module rather than touching shared code.
Tech Stack
Built in Rust (edition 2018, minimum supported Rust 1.60) as a Cargo workspace with two members: the os_info library and an os_info_cli binary crate. Core dependencies are minimal — log for tracing and optional serde/schemars (enabled by default for serde) for serialization and JSON Schema support. Platform-gated dependencies bring in exactly what each target needs: windows-sys (Win32 Registry, SystemInformation, and process APIs) on Windows, objc2/objc2-foundation/objc2-ui-kit for Apple Foundation bindings on macOS/iOS, android_system_properties on Android, and nix for Unix syscalls elsewhere. CI (GitHub Actions) runs rustfmt checks, tests, code coverage via Codecov, a cargo-bloat size-tracking workflow, and deny.toml-driven dependency/license auditing via cargo-deny.
Code Quality
Tests are colocated with implementation code, including a dedicated linux/tests directory and #[cfg(test)] modules across roughly two dozen source files that assert correct OS-type resolution per platform; public API methods also carry executable doctest examples in their rustdoc comments. Error handling favors Option/fallback chaining (e.g. Linux detection tries lsb_release output, then falls back to release-file parsing, then to a generic Type::Linux) rather than panics. #![deny(missing_docs, unsafe_code, missing_debug_implementations)] at the crate root enforces documentation coverage and forbids unsafe code in the shared layer, and a CONTRIBUTING.md codifies the PR/rebase workflow for contributors.
API Design
The public surface is deliberately small: one os_info::get() call returns an Info value with os_type(), version(), edition(), codename(), bitness(), and architecture() accessors, plus a Display implementation for one-line human-readable output — no configuration or setup is required to get a result. Optional features (serde, schemars) are additive and off-by-default for anything beyond the default serde support, keeping compile times low for consumers who only need the core detection. Documentation leans heavily on runnable rustdoc examples embedded directly in doc comments for nearly every public method, which doubles as both API reference and test coverage.