macospathhomebrew

Homebrew PATH warning on multi-user Mac setting


I use a Mac with ARM, and I since I wanted to use Homebrew with multiple users, I created a separate account called Homebrew dedicated to managing it. I followed the instructions from here - https://kuvalkin.com/blog/homebrew-installation-on-multi-user-system/

And on my other accounts I use brew with these lines added to my .zshrc

# Set PATH, MANPATH, etc., for Homebrew.
eval "$(/opt/homebrew/bin/brew shellenv)"
alias brew="sudo -H -i -u Homebrew brew"

So far everything works well, apart from an annoying warning by brew whenever I call doctor:

Warning: No Cask quarantine support available: unknown reason.

Warning: /usr/bin occurs before /opt/homebrew/bin in your PATH.
This means that system-provided programs will be used instead of those
provided by Homebrew. Consider setting your PATH so that
/opt/homebrew/bin occurs before /usr/bin. Here is a one-liner:
  echo 'export PATH="/opt/homebrew/bin:$PATH"' >> ~/.zshrc

The following tools exist at both paths:
...

However, when checking both my PATH and the one of the Homebrew user, I obtain a well behaved PATH, contrary to the warning.

/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/TeX/texbin:/Applications/iTerm.app/Contents/Resources/utilities:/Users/username1/.scripts

Calling sudo -H -i -u Homebrew echo $PATH returns the same PATH. Calling the programs works as intended - homebrew version has precedence, contrary to what the warning says.

Where does this error come from? Can the warning be silenced?


Solution

  • sudo -H -i -u Homebrew echo $PATH prints your current PATH (the shell expands it before running sudo). You want something like

    sudo -H -i -u Homebrew sh -c 'echo "$PATH"'
    

    to print the Homebrew user's PATH.

    Trivially sudo -E preserves your current environment, but that's perhaps too fast and loose. Maybe instead

    alias brew='sudo -H -i -u Homebrew env PATH="$PATH" brew'
    

    (I guess you might want to specify the full path /opt/homebrew/bin/brew?)

    This still passes on the current user's PATH. It would be better to fetch the Homebrew user's PATH and prepend /opt/homebrew/bin, but it gets pretty hairy.

    _hbpath=$(sudo -H -i -u Homebrew sh -c 'echo "$PATH"')
    alias brew="sudo -H -i -u Homebrew \
        env PATH=\"/opt/homebrew/bin:$_hbpath\"" \
        brew'
    unset _hbpath