How Fastfetch Supports Different Operating Systems: Linux, macOS, and Windows Detection
Fastfetch uses a unified abstraction layer where platform-specific detection logic in separate source files populates a common FFOSResult structure, enabling seamless cross-platform OS detection without conditional compilation clutter in the core renderer.
Fastfetch is a neofetch-like system information tool written in C that runs natively on Linux, macOS, and Windows. According to the fastfetch-cli/fastfetch source code, the project achieves cross-platform OS detection through a modular architecture that isolates platform-specific APIs behind a consistent interface. This design allows the tool to parse OS metadata on each platform while presenting a uniform output format to users.
OS Detection Architecture
Unified Data Structure and API
At the heart of fastfetch's OS detection lies the FFOSResult structure defined in src/detection/os/os.h. This struct standardizes fields such as name, prettyName, id, version, and codename across all platforms. The public API exposes a single entry point: ffDetectOS(), which returns a pointer to a populated FFOSResult instance cached in instance.state.platform.os.
Compile-Time Platform Selection
Rather than using runtime conditionals, fastfetch selects the appropriate implementation at compile time. The build system compiles only the relevant platform-specific file based on preprocessor directives (#if __linux__, #elif __APPLE__, #elif _WIN32). Each file implements the internal ffDetectOSImpl() function, which the core ffDetectOS() wrapper calls to populate the result structure. This approach eliminates unnecessary dependencies and keeps binary sizes optimized for each target.
Platform-Specific Implementation Details
Linux and GNU Detection
On Linux systems, src/detection/os/os_linux.c parses /etc/os-release or falls back to /etc/lsb-release and /usr/lib/os-release. The implementation applies distribution-specific tweaks to handle Ubuntu flavours, Debian-derived distros, and Fedora variants. It extracts the PRETTY_NAME, ID, and VERSION_ID fields, normalizing them into the common FFOSResult format using standard C and POSIX filesystem calls.
macOS Detection
For macOS, fastfetch utilizes Objective-C to interact with Apple's Foundation framework. The src/detection/os/os_apple.m file reads /System/Library/CoreServices/SystemVersion.plist to extract ProductName, ProductUserVisibleVersion, and ProductBuildVersion. It then maps numeric version identifiers to marketing codenames like "Ventura" or "Sonoma" before populating the result structure.
Windows Detection
The Windows implementation in src/detection/os/os_windows.c calls the undocumented BrandingFormatString(L"%WINDOWS_LONG%") function to obtain the full OS name. It strips the "Microsoft" prefix and reads the registry key HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion to retrieve the build version and optional fields like DisplayVersion, CSDVersion, and ReleaseId.
Practical Usage and Code Examples
Fastfetch exposes this detection logic through both its CLI and internal APIs.
Display OS information in the default output:
fastfetch
Show only the OS module for scripting:
fastfetch --disable-all --module os
Reuse fastfetch's detection in a custom C program:
#include "src/detection/os/os.h"
int main(void)
{
const FFOSResult* os = ffDetectOS();
printf("Detected OS: %s %s (%s)\n",
os->name.chars,
os->version.chars,
os->codename.chars);
return 0;
}
Verify Windows detection via PowerShell:
fastfetch --module os
Summary
- Fastfetch supports different operating systems through a compile-time abstraction layer that isolates platform-specific code into separate source files.
- The
FFOSResultstructure insrc/detection/os/os.hstandardizes OS metadata across Linux, macOS, and Windows. - Linux detection parses
/etc/os-releaseinos_linux.c, macOS uses Foundation framework APIs inos_apple.m, and Windows queries registry values and branding strings inos_windows.c. - The
ffDetectOS()function provides a unified entry point that caches results ininstance.state.platform.osfor the renderer to consume.
Frequently Asked Questions
How does fastfetch detect the OS version on Linux without lsb_release?
Fastfetch reads /etc/os-release as the primary source, falling back to /usr/lib/os-release if necessary. The implementation in src/detection/os/os_linux.c handles distro-specific variations directly without requiring the lsb_release binary, parsing standard fields like PRETTY_NAME and VERSION_ID.
Why does the macOS detection use an Objective-C file instead of C?
The macOS implementation requires access to Apple's Foundation framework to properly read SystemVersion.plist and map version numbers to codenames. The .m extension allows src/detection/os/os_apple.m to compile as Objective-C while still exposing a C-compatible interface to the rest of the codebase.
Does fastfetch use standard Windows APIs for OS detection?
While fastfetch uses standard registry access via HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion, it also calls the undocumented BrandingFormatString function in src/detection/os/os_windows.c to retrieve the full Windows display name, providing more accurate branding than standard version APIs alone.
Can I use fastfetch's OS detection logic in my own C project?
Yes. By including src/detection/os/os.h and linking against the relevant platform-specific implementation file (os_linux.c, os_apple.m, or os_windows.c), you can call ffDetectOS() to obtain a populated FFOSResult structure containing standardized OS metadata.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →