Miao's blog

macOS 新安装记录

用了一年的 macOS 不知为何时不时就弹出来风火轮,很是恼火。虽然是 M1 Pro 老机器了,但性能应该还过得去。索性抹掉系统重新安装,记录一下最常用的一些命令行工具和 homebrewconda 配置,以后要是再重装或者换新设备可能还用得到。

1. 安装 homebrew

homebrew 当然是最重要的,不然 macOS 就没法玩了。

# 参考 https://mirrors.tuna.tsinghua.edu.cn/help/homebrew/

xcode-select --install

# 环境变量临时设置清华源
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
export HOMEBREW_INSTALL_FROM_API=1
export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"
export HOMEBREW_PIP_INDEX_URL="https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple"

# 从清华源安装 homebrew
git clone --depth=1 https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/install.git brew-install
/bin/bash brew-install/install.sh
rm -rf brew-install

# 也可从 GitHub 获取官方安装脚本安装 homebrew
# /bin/bash -c "$(curl -fsSL <https://github.com/Homebrew/install/raw/master/install.sh>)"

# 根据安装脚本后续提示执行,这些环境会被执行并写在 .zprofile
echo >> /Users/miao/.zprofile
echo '# Homebrew PATH 设置' >> /Users/miao/.zprofile
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/miao/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
echo '# Homebrew 镜像源环境变量' >> /Users/miao/.zprofile
echo '# Set non-default Git remotes for Homebrew/brew and Homebrew/homebrew-core.' >> /Users/miao/.zprofile
echo 'export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"' >> /Users/miao/.zprofile
echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"' >> /Users/miao/.zprofile
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"

# 继续写入部分环境
echo 'export HOMEBREW_INSTALL_FROM_API=1' >> ~/.zprofile
echo 'export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"' >> ~/.zprofile
echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.zprofile
export HOMEBREW_INSTALL_FROM_API=1
export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"

# 健康检查
brew doctor

2. 通过 homebrew 安装必备软件

通过 homebrew 可以方便安装一些必备的图形界面软件和终端工具:

# 安装图形界面应用程序,通常会被安装到 /Applications
brew install --cask 1password visual-studio-code keka orbstack zotero vivaldi iterm2 tabby pearcleaner bartender raycast macupdater find-any-file pictogram shottr google-chrome notion iina telegram input-source-pro

# 安装必备终端工具,通常会被安装到 /opt/homebrew
brew install git wget curl htop bat jq fastfetch

# 安装终端字体 maple-font
brew install --cask font-maple-mono-nf-cn

# 定期维护
brew update && brew upgrade

3. 使用 Ghostty 作为终端模拟器

虽然 iTerm2 很好了,但是有新的性能更好的 Ghostty 也可以试试看。

brew install --cask ghostty

在用的 Ghostty 配置如下:

# Config generated by Ghostty Config

scrollback-limit = 100000
clipboard-read = allow
clipboard-write = allow
window-save-state = "always"
window-theme = dark
window-padding-x = 10
window-padding-y = 2,10
window-padding-balance = true
background-opacity = 0.85
background-blur = 30
theme = GruvboxDarkHard
bold-is-bright = true
background = #1d2021
foreground = #ebdbb2
selection-background = #665c54
selection-foreground = #ebdbb2
selection-invert-fg-bg = true
cursor-color = #ebdbb2
cursor-text = #1d2021
cursor-style-blink = true
palette = 0=#1d2021
palette = 1=#cc241d
palette = 2=#98971a
palette = 3=#d79921
palette = 4=#458588
palette = 5=#b16286
palette = 6=#689d6a
palette = 7=#a89984
palette = 8=#928374
palette = 9=#fb4934
palette = 10=#b8bb26
palette = 11=#fabd2f
palette = 12=#83a598
palette = 13=#d3869b
palette = 14=#8ec07c
palette = 15=#ebdbb2
font-thicken = true
font-family = "Maple Mono NF CN"
adjust-cell-width = -5%
grapheme-width-method = "unicode"

4. 通过 homebrew 安装 zsh

从 macOS Catalina 版本开始默认预装了 zsh(Z shell),并且将其作为默认的 shell,取代了之前的 bash。但是预装的 zsh 位于系统保护目录下 /bin/zsh ,版本可能较为陈旧且无法修改。通过 homebrew 安装的 zsh 位于 /opt/homebrew/bin/zsh,可获得最新版本、更灵活的自定义选项、更新支持、以及便捷的管理方式。

brew install zsh

zsh 拥有多个配置文件,其中两种常用的是 .zprofile(登录 shell)和 .zshrc(交互式 shell)。 .zprofile 只在登录时执行一次,如系统启动后首次打开终端。.zshrc 则每次新开终端窗口/标签页都会执行。

先前的 homebrew 相关环境配置应该存放在 .zprofile 中,避免每次打开终端都要重复加载。当前 .zprofile 内容应该如下:

# Homebrew PATH 设置
eval "$(/opt/homebrew/bin/brew shellenv)"
# Homebrew 镜像源环境变量
# Set non-default Git remotes for Homebrew/brew and Homebrew/homebrew-core.
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
export HOMEBREW_INSTALL_FROM_API=1
export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"

# Added by OrbStack: command-line tools and integration
# This won't be added again if you remove it.
source ~/.orbstack/shell/init.zsh 2>/dev/null || :

安装 zsh 的 3 个插件

brew install zsh-autosuggestions zsh-completions zsh-syntax-highlighting

根据输出,创建 .zshrc 并在 .zshrc 末尾完善配置内容

# zsh-autosuggestions
source /opt/homebrew/share/zsh-autosuggestions/zsh-autosuggestions.zsh

# zsh-completions
if type brew &>/dev/null; then
  FPATH="$(brew --prefix)/share/zsh-completions:$FPATH"
  autoload -Uz compinit
  compinit
fi

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

并在终端执行完善安装

rm -f ~/.zcompdump; compinit

chmod go-w '/opt/homebrew/share'
chmod -R go-w '/opt/homebrew/share/zsh'

同样,上面安装的 curl 也需要完善配置

echo >> /Users/miao/.zprofile
echo '# 使用 Homebrew 的 curl 替代系统 curl' >> ~/.zprofile
echo 'export PATH="/opt/homebrew/opt/curl/bin:$PATH"' >> ~/.zprofile

# 刷新 zsh 配置
source ~/.zprofile
source ~/.zshrc

如果经常编译软件,也可以在 .zprofile 加上这些

export LDFLAGS="-L/opt/homebrew/opt/curl/lib $LDFLAGS"
export CPPFLAGS="-I/opt/homebrew/opt/curl/include $CPPFLAGS"

5. 安装 powerlevel10k 主题

# 拉取项目并 zsh 配置
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ~/powerlevel10k
echo -e '\nsource ~/powerlevel10k/powerlevel10k.zsh-theme' >> ~/.zshrc

# 刷新 zsh 配置
source ~/.zshrc

6. 安装 atuin 管理命令历史

# 通过官方脚本安装
curl --proto '=https' --tlsv1.2 -LsSf https://setup.atuin.sh | sh

# 导入现有的 zsh 命令历史
atuin import zsh

7. 安装 miniconda

没有 conda 还怎么玩 Python 呢?环境太复杂。

# 安装 miniconda
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh
bash ~/Miniconda3-latest-MacOSX-arm64.sh
conda init zsh

# 清空所有 Conda 频道
conda config --remove-key channels
# 防止 Conda 自动添加 defaults
conda config --remove-key default_channels

# 添加清华源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
conda config --set show_channel_urls true

# 始终优先使用 Conda-Forge
conda config --set channel_priority strict

# 检查当前 conda 环境配置文件
# 如果输出多个配置文件,只保留 /Users/<your_username>/.condarc 的全局配置
conda config --show-sources

必须注意源的顺序,conda-forge 必须在最前面,否则会出现包依赖版本冲突!最终 /Users/<your_username>/.condarc 内容是

channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
channel_priority: strict
show_channel_urls: true

更新软件包

# 清理缓存
conda clean --all

# 更新
conda update --all
conda update python

创建新的环境,免得 base 环境搞乱:

conda create -n learn python=3
conda activate learn

安装必备的 R 环境,装做一个爱学习的人

conda install r-base r-essentials r-irkernel jupyterlab
R -e 'IRkernel::installspec(user = TRUE)'

.zshrc 中定义默认启动 learn 环境:

echo >> /Users/<your_username>/.zshrc
echo '# 激活 learn 独立 conda 环境' >> /Users/miao/.zshrc
echo 'conda activate learn' >> /Users/miao/.zshrc

最终总的 .zshrc

# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi

# 加载 powerlevel10k 主题
source ~/powerlevel10k/powerlevel10k.zsh-theme

# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

# atuin
. "$HOME/.atuin/bin/env"
eval "$(atuin init zsh)"

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/Users/miao/miniconda3/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/Users/miao/miniconda3/etc/profile.d/conda.sh" ]; then
        . "/Users/miao/miniconda3/etc/profile.d/conda.sh"
    else
        export PATH="/Users/miao/miniconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

# 激活 learn 独立 conda 环境
conda activate learn

# zsh-autosuggestions
source /opt/homebrew/share/zsh-autosuggestions/zsh-autosuggestions.zsh

# zsh-completions
if type brew &>/dev/null; then
  FPATH="$(brew --prefix)/share/zsh-completions:$FPATH"
  autoload -Uz compinit
  compinit
fi

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

8. 安装 helix

Helix -- A Kakoune / Neovim inspired editor, written in Rust.

开箱即用、vim-like 的免配置免折腾的现代终端代码编辑器,比 nanovim 之类的好用多了。在 Linux 上也应该用这个。

brew install helix

9. 配置 Tabby 的 1Password SSH 认证

Tabby 暂未适配 1Password 的 IdentityAgent,只能曲线救国了:

# 注意:需要在 1Password 设置中启用 SSH Agent 功能
# 1Password -> Settings -> Developer -> Use the SSH agent
# 参考 https://github.com/Eugeny/tabby/discussions/5939

cat << EOF > ~/Library/LaunchAgents/com.1password.SSH_AUTH_SOCK.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>com.1password.SSH_AUTH_SOCK</string>
  <key>ProgramArguments</key>
  <array>
<string>/bin/sh</string>
<string>-c</string>
<string>/bin/ln -sf $HOME/Library/Group\ Containers/2BUA8C4S2C.com.1password/t/agent.sock \$SSH_AUTH_SOCK</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
</dict>
</plist>
EOF
launchctl load -w ~/Library/LaunchAgents/com.1password.SSH_AUTH_SOCK.plist

10. 防止 CursorUIViewService 进程卡死

咖喱味的 macOS 不知道从哪一个版本开始就出现辣鸡 CursorUIViewService 进程时不时卡死。这个进程是指示大小写切换。曲线救国,在进程卡死的时候强制 kill 并重启。

从源码编译:

# 参考 https://github.com/ktiays/fcuvs

brew install cmake

git clone https://github.com/ktiays/fcuvs.git && cd fcuvs

env CC=/usr/bin/clang CXX=/usr/bin/clang++ cmake -B build/release -DCMAKE_BUILD_TYPE=Release
cmake --build build/release

sudo cp build/release/fcuvs /usr/local/bin/
cat << EOF > ~/Library/LaunchAgents/me.ktiays.fcuvs.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>me.ktiays.fcuvs</string>
<key>ProgramArguments</key>
	<array>
		<string>/usr/local/bin/fcuvs</string>
	</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>WorkingDirectory</key>
	<string>/var/tmp</string>
</dict>
</plist>
EOF

launchctl load -w ~/Library/LaunchAgents/me.ktiays.fcuvs.plist

11. 避免移动和网络设备上出现 .DS_Store

.DS_Store 是苹果用来存储文件夹的自定义显示属性的隐藏文件,但是也会在移动硬盘之类的移动设备中也出现,再使用 Windows 等其他操作系统打开时会很恶心,主要是这玩意儿在移动设备上真没必要。

defaults write com.apple.desktopservices DSDontWriteUSBStores -bool true
defaults write com.apple.desktopservices DSDontWriteNetworkStores -bool true

如果已经出现,可以用命令清除当前路径及子目录下的所有 .DS_Store

find . -name '.DS_Store' -type f -delete

12. 为 VSCode 配置好看的 Monokai Pro 主题

Monokai Pro 是一款超美的 VSCode 和 Sublime Text 主题。通过算号器获得的激活码:

# 算号器 https://maximilionus.github.io/monokai_pro_keygen/

email: this@monokai.pro
vscode key: 7d679-6e993-57473-70ebe-63e17
sublime text key: fbb0c-1710a-eb873-f778e-2b0c5

推荐使用正版 Monokai Pro 主题,或者先试用免费版本。