Search code examples
homebrewzshapple-m1

How to check CPU type to source from Homebrew?


I use both M1 and Intel MBP. They do share the same .zshrc from my dotfiles. I want to source different files depending on CPU type. For example:

On M1, only these files should be sourced:

source /opt/homebrew/share/zsh-autosuggestions/zsh-autosuggestions.zsh
source /opt/homebrew/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

On Intel , only these files should be sourced:

source /usr/local/share/zsh-autosuggestions/zsh-autosuggestions.zsh
source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

How can I achieve this?


Solution

  • zsh automatically sets CPUTYPE on startup, so you can use that instead of running sysctl.

    declare -A foo
    foo[x86_64]=/usr/local/
    foo[arm64]=/opt/homebrew
    
    source $foo[$CPUTYPE]/share/zsh-autosuggestions/zsh-autosuggetions.zsh
    source $foo[$CPUTYPE]/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
    

    Note that in some sense, you are just using the CPU type as a proxy for a specific machine. (You could just as easily have two different Intel machines with different locations for the ZSH configuration files.) A better solution might be to use $HOST instead of $CPUTYPE to identify machine-specific options.