All projects

brew-rs

A native macOS package manager, written in Rust, that installs Homebrew’s packages without running Homebrew.

What it is

  • A command-line package manager for macOS, brewrs, that installs Homebrew’s prebuilt packages (bottles) and apps (casks) into its own separate prefix.
  • It never calls brew. Every download is checked against its SHA-256 hash, and binaries are rewritten and re-signed so they run from their new location.
  • Open source, with a prebuilt v0.1.0 release for Apple Silicon Macs.

The problem

A Homebrew bottle is built for Homebrew’s own layout. Its binaries and libraries carry the paths of the libraries they load inside their Mach-O headers, so copying them somewhere else breaks them, and a binary that has been edited has to be signed again.

Installing those packages without brew means doing natively what Homebrew does for you: ordering versions by Homebrew’s rules, unpacking archives and disk images, fixing up binaries and upgrading without leaving a half-replaced package behind. A package manager writes to your system on your behalf, so each of those steps has to fail safely.

How I built it

  • One Rust 2024 crate, split into layers: the CLI, commands, a domain layer for planning, version rules and tap manifests, and an infrastructure layer for downloads, the store, relocation and cask extraction.
  • The only programs it runs are Apple’s own tools at absolute paths, install_name_tool, codesign, ditto, hdiutil, tar and sw_vers, plus your editor for config edit. Never brew.
  • Downloads stream on Tokio and reqwest into a staging file that is hashed as it arrives, and only a matching SHA-256 is moved into the cache. Bottle URLs must be GitHub Container Registry blobs whose digest matches the one the formula declares.
  • It rewrites Mach-O load commands so libraries resolve inside the new prefix, then ad-hoc re-signs each binary and verifies the signature.
  • Every write to a prefix holds a lock file that records its owner, and the lock is only removed by the process that created it.
  • Upgrades stage and verify the replacement before touching what is installed, and roll back to the previous install if anything fails.
  • Third-party taps are declarative manifests, and reserved names such as brew cannot be claimed.

The interesting parts

Relocating Mach-O binaries

A macOS binary records its own install name in LC_ID_DYLIB, the libraries it loads in LC_LOAD_*DYLIB commands, and its search paths in LC_RPATH, often through @rpath and @loader_path. brewrs reads those 64-bit load commands natively, works out the new targets for the isolated prefix and applies them with install_name_tool. Every rewritten path must land in a version-pinned keg or the prefix’s own lib folder, never an opt alias, so a package cannot be pointed at another version or outside the prefix. Editing a binary invalidates its signature, so each one is ad-hoc re-signed with codesign, keeping any entitlements it already had, and checked with codesign --verify --strict. After every tool step the binary is parsed again and compared with the plan: same header, same load commands in the same order, every path exactly as planned, and no Homebrew placeholder left anywhere in the file.

Extracting archives and apps safely

Bottles are tarballs, and a hostile or broken one can try to write outside its folder. Extraction accepts only regular files, folders and symlinks, rejects symlinks that escape the destination and symlink cycles, and caps archive size, expanded size, entry count and path depth. Casks ship as ZIPs and disk images: ZIP central directories are checked up front and then unpacked with ditto so macOS code signatures survive, and .dmg files are mounted with hdiutil behind an RAII guard that always detaches the image.

Upgrades that follow Homebrew’s rules

Versions are ordered the way Homebrew orders them: version_scheme first, then dotted numeric versions, then the _revision suffix. Numbers are compared as digit strings, so no version can overflow, and anything outside that shape, such as a prerelease tag, is refused rather than guessed at. That lets brewrs reject a downgrade, and reject a bottle whose checksum changed while its version stayed the same. If another installed package is pinned to the old version and checksum, the old keg is kept so it does not break. A real upgrade is staged and verified in private before anything installed moves. The old files are then set aside, and if any step fails they are put back.

How it runs

  • CI runs on macos-15 through scripts/verify.sh: git diff --check, cargo fmt, clippy on all targets with warnings as errors, the test suite with locked, offline dependencies, and a release build with warnings denied.
  • Releases are cut from v* tags. The workflow checks it is on an arm64 runner, runs the same verification, checks the binary with codesign --verify, and publishes a tarball with its .sha256.
  • Integration tests run the real binary with a fake brew first on the PATH that logs every call and fails, so any command that reaches for Homebrew fails the suite.
  • Verified on an Apple Silicon Mac running macOS 15 by installing ripgrep 16.0.0.