19. January 2019

Homebrew: Patching an existing package

As Homebrew can build packages compared to using a pre-built bottle, it’s easy to apply source level patches to packages. For example, I was struggling with a bug in the ledger accounting software. A fix was already in HEAD but not released yet. If the patch can be applied to the latest stable release as it is often the case with bug fixes, it’s simple to build a package with homebrew with the patch applied:

  1. Find a patch/commit diff for the bugfix and compute the SHA-256 checksom for it:

    $ curl https://github.com/ledger/ledger/commit/850a2cec79.diff?full_index=1 | shasum -a 256
    6affeedda826497caa9b4a9d7667c675d677b591d4deebbe66c69ba86f8bd0fa
  2. Edit the Homebrew formula:

    brew edit ledger

    Add a patch here:

    // ...
    depends_on "mpfr"
    depends_on "python@2"
    
    # Apply fix for https://github.com/ledger/ledger/issues/546: --date-format prints gibberish dates
    patch do
      url "https://github.com/ledger/ledger/commit/850a2cec79.diff?full_index=1"
      sha256 "6affeedda826497caa9b4a9d7667c675d677b591d4deebbe66c69ba86f8bd0fa"
    end
    
    // ...
    ```
  3. Reinstall the package from source:

    brew reinstall ledger -s