Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Darwin: The Open Source Core

Darwin is the open-source operating system that forms the foundation of macOS, iOS, iPadOS, watchOS, tvOS, and visionOS. While Apple’s consumer products include proprietary components layered on top, Darwin itself has been available under various open-source licenses since its initial release in 2000.

What Is Darwin?

Darwin is a complete Unix operating system, comprising:

  • XNU: The hybrid kernel combining Mach and BSD
  • BSD subsystem: Process management, networking, filesystem interfaces
  • IOKit: The driver framework for hardware interaction
  • Userland tools: Command-line utilities derived from FreeBSD
  • Core libraries: libSystem and related foundational libraries

When you type commands in Terminal.app, you’re interacting directly with Darwin. The graphical macOS experience—Aqua, Finder, the Dock—sits above Darwin but depends entirely on it.

Darwin’s Open Source Status

Darwin’s licensing is complex. Different components use different licenses:

ComponentLicense
XNU kernelApple Public Source License (APSL) 2.0
BSD userlandVarious BSD licenses
IOKitAPSL 2.0
Mach componentsVarious (CMU, Apple)

The Apple Public Source License is OSI-approved but not GPL-compatible. This means Darwin code can’t be directly combined with GPL-licensed projects like Linux.

Accessing Darwin Source Code

Apple publishes Darwin source code at opensource.apple.com. You can browse and download:

# View available Darwin components
$ curl -s https://opensource.apple.com/tarballs/ | grep -o 'href="[^"]*"' | head -20

Key repositories include:

  • xnu: The kernel
  • Libc: The C standard library
  • shell_cmds: Shell built-in commands
  • file_cmds: File manipulation commands (ls, cp, mv, etc.)
  • network_cmds: Networking utilities

Building Darwin

While Apple publishes source code, building a complete Darwin system is challenging. The community-driven PureDarwin project maintains bootable Darwin distributions, though these lack the polish and hardware support of macOS.

Darwin vs macOS

The relationship between Darwin and macOS is analogous to the relationship between the Linux kernel and a Linux distribution like Ubuntu:

DarwinmacOS
Open-source Unix foundationComplete commercial product
Kernel + userland toolsDarwin + Aqua GUI + Apple frameworks + apps
No graphical interfaceFull desktop experience
Freely redistributableLicensed per device

However, there’s a crucial difference: Darwin without macOS is rarely used, while Linux without any particular distribution doesn’t exist as a consumer product.

Examining Your Darwin Installation

You can inspect Darwin’s presence on your system:

# Display Darwin version
$ uname -a
Darwin MacBook-Pro.local 23.4.0 Darwin Kernel Version 23.4.0: Fri Mar 15 00:12:49 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6020 arm64

# View Darwin kernel version details
$ sysctl kern.ostype kern.osrelease kern.version
kern.ostype: Darwin
kern.osrelease: 23.4.0
kern.version: Darwin Kernel Version 23.4.0: Fri Mar 15 00:12:49 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6020

The uname output reveals key information:

  • Darwin: The OS type
  • 23.4.0: The Darwin kernel version (not the macOS version)
  • xnu-10063.101.17~1: The specific XNU build
  • RELEASE_ARM64_T6020: Release build for ARM64, specifically the M2 Pro chip

Darwin Version vs macOS Version

Darwin versions don’t match macOS marketing versions. Here’s the mapping for recent releases:

macOS VersionMarketing NameDarwin Version
macOS 14.xSonoma23.x
macOS 13.xVentura22.x
macOS 12.xMonterey21.x
macOS 11.xBig Sur20.x
macOS 10.15Catalina19.x
macOS 10.14Mojave18.x

The pattern: Darwin version = macOS major version + 9 (approximately, with some variation).

Darwin Components in Detail

The C Library (libSystem)

Unlike Linux (which uses glibc or musl) or FreeBSD (which has its own libc), macOS uses libSystem, an umbrella library that combines:

  • Libc: Standard C library functions
  • libinfo: Directory services
  • libkvm: Kernel memory interface
  • libm: Math library
  • libpthread: POSIX threads
# Examine libSystem
$ otool -L /bin/ls | grep libSystem
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1336.61.1)

BSD Commands

Most command-line tools come from FreeBSD, with Apple modifications. You can verify this:

# Check version strings in common commands
$ what /bin/ls
/bin/ls:
    PROGRAM:ls  PROJECT:file_cmds-430.100.5

$ what /usr/bin/grep
/usr/bin/grep:
    PROGRAM:grep  PROJECT:text_cmds-154

The what command extracts version information embedded in binaries—a BSD convention.

Darwin’s Unique Position

Darwin occupies an interesting position in the Unix ecosystem:

  1. Certified Unix: Darwin-based macOS is UNIX 03 certified
  2. BSD heritage: Derived primarily from FreeBSD
  3. Unique kernel: XNU is unlike both monolithic Linux and pure Mach microkernels
  4. Apple stewardship: Developed primarily by Apple, with limited community input
  5. Commercial backing: Unusual for an open-source Unix

This combination means Darwin benefits from significant engineering investment while maintaining Unix compatibility. However, Apple’s control means Darwin’s direction serves Apple’s product needs, not the broader open-source community.

Working with Darwin

For practical purposes, working with Darwin means working with macOS’s command line. Throughout this book, we’ll explore Darwin’s capabilities:

  • Using BSD commands (and understanding how they differ from GNU)
  • Managing processes with Darwin’s unique tools
  • Understanding the kernel’s behavior and configuration
  • Leveraging Darwin’s networking stack
  • Working with the filesystem abstractions Darwin provides

Darwin is your gateway to macOS’s Unix power. Understanding it transforms macOS from a consumer operating system into a professional Unix workstation.