Try   HackMD

建立 .dotfiles 以便在任何 Macbook 上都可以擁有相同的開發環境

tags: 開發環境

.dotfiles 是什麼?

一般來說軟體的設定檔案都會在 Home directory(家目錄)建立隱藏 directory(檔名開頭為 . 開頭),這些隱藏檔案裡面就會有軟體的設定檔案,比如說目前開發使用的 Neovim,就會在 Home directory 的 .config 建立 nvim,在 nvim 裡面就是 Neovim 設定檔案存放的地方。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

. 開頭為隱藏檔案)

.dotfiles 其實就是一個 directory,達配指令就可以把設定檔案移到 .dotfile 內,並且同時「連結」到 Home directory,我們就可以把 .dotfile 內的設定檔案推上雲端帶著走,確保在每個裝置上都可以保持相同的 workflow。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

(家目錄)

取得使用 Homebrew 下載的軟體清單

Homebrew 是 macOS 的套件管理工具,使用 macOS 的電腦推薦下載 Homebrew,並且「儘量」透過 Homebrew 下載軟體。

A key practice is to install EVERYTHING possible using brew, brew cask, or mas. Even things like fonts!

但不要使用 Homebrew 下載 nvm(管理 Node 檔案的工具),因為官方有說明使用 Homebrew 下載 nvm 會出現一些問題,因此 nvm 還是上網找怎麼不使用 Homebrew 下載

可以查看 Homebrew 筆記 內有基本 brew 指令

建立 Brewfile

brew bundle dump --describe

可以透過指令建立 Brewfile(裡面是所有透過 Homebrew 下載的檔案清單)

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

更新 Brewfile

記得先移動到 .dotfiles 再建立 Brewfile

Brewfile 已經存在但想要更新的話就要下另一組指令

brew bundle dump --force or brew bundle dump --describe --force

其餘的 Brewfile 的說明可以看這篇 Brew Bundle Brewfile Tips

把在 Home directory 的設定檔移到 .dotfiles

mv sourceFile targetDirectory

可以把檔案移到想要的地方,比如說 mv ~/.zshrc ~/.dotfiles/zsh/ 那麼原本在 Home directory 的 .zshrc 就會移到 ~/.dotfile/zsh/ 的 directory 之內了。

快速地將 .dotfiles 的設定檔連結到 Home directory 之內

因為軟體還是會從 Home directory 找設定檔案,當我們把設定檔案移到 .dotfiles 的話就會造成軟體吃不到設定,所以需要使用另一個指令連結 .dotfile 內的設定檔案

ln -s sourceFile targetDirectory

ln (stands for link)
-s (stands for symbolic)

比如說使用 ln -s ~/dotfiles/.zshrc ~/.zshrc 那麼就會把 .dotfile 內的 .zshrc 連結到 Home director。所以在 Home directory 就會出現被連結的 .zshrc

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

(被連結的 .zshrc 在 Finder 中會出現小小的箭頭標示)

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

(用終端機看也可以發現 .zshrc 有顯示連結資訊)

用 GNU Stow 可以更快速地連結檔案

使用 stow 可以更快速地連結檔案,但是要注意檔案結構要與 Home directory 相同

一樣透過 Homebrew 下載

brew install stow

.dotfiles 是一個外層檔案夾,每個直接放在 Home directory 的設定檔多用一層包起來就可以

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

  • .dotfiles 是一個裝檔案的容器
  • 為每個設定檔建立 directory
  • 如果該設定檔原本在 Home directory 就直接裸露(沒有任何 directory 包覆)那麼就建立一層 directory 即可
/Users/liuweilun ├── .dotfiles │ ├── zsh │ │ └── .zshrc ├── .zshrc
  • 如果該設定檔本來在 Home directory 是巢狀的結構,那麼在 .dotfiles 內建立第一層 directory 後就要模仿 Home directory 的結構
/Users/liuweilun ├── .dotfiles │ ├── nvim │ └── .config │ └── nvim | ├── init.lua | ├── lua | └── spell ├── .config | └── nvim | ├── init.lua | ├── lua | └── spell

可以看到 .dotfiles/nvim/ 的結構跟原先 Home directory 相同

使用指令 stow 快速建立連結

完成設定檔移到 .dotfiles 內的工作後,就可以用 GNU Stow 提供的指令快速在 Home directory 建立連結

  1. stow directoryName 依照 directoryName 在 Home directory 建立連結

stow zsh 那麼在 Home directory 就會建立其連結

  1. stow */ # Everything (the '/' ignores the README)

但要記得在新的電腦事先安裝 stow

教學 Youtube

  1. Manage your dotfiles across multiple machines with GNU Stow and Git
  2. Manage Your dotfiles Like A Superhero

拿到新電腦後該如何快速建立舊有的開發環境

Step 1: 下載 Homebrew

Step 2: 下載 nvm

可以參考 [NodeJS] 透過 NVM 安裝與使用 Node.js

Step 3: clone Github 上建立 dotfiles repo 至本機端

git clone Repo ~/.dotfiles
clone repo 至 ~/.dotfiles

Step 4: 下載 stow

brew install stow

Step 5: 下載 Brewfile 中所有的 packages

brew bundle --file ~/myFolder/Brewfile

ref. Manage all your installed software at one place with Homebrew Bundle
(根據這個人分享,這樣子 Homebrew 就會自動下載 Brewfile 中的所有 packages 了)

或者 brew bundle install
(但這個指令會需要 Brewfile 在以下 path: ~/Brewfile

別擔心,並不會重複下載已經有的 packages

It will automatically skip software that’s already installed.
ref. Easy macOS Loads By Way of Homebrew Bundle - Casey Liss

另外,如果卡住要等一段時間的話也是很正常的,就慢慢等吧。

iTerm 配置

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Gitlab 建立 SSH key

讓 sourcetree 可以直接 clone gitlab 的專案

可以參考這邊的教學

  1. clone 的時候選擇 ssh

  2. 把複製的 ssh 貼上 source url,sourcetree 就會自動填入資訊

如果 tmux 無法刪除文字可以試試這個方法

Backspace not working in tmux command prompt

tmux 記得要手動下指令下載套件

如果發現 brew bundle install 多一層,就必須把裡面的東西拿出來

.dotfile/ -> 設定檔案(v)
.dotfile/ -> dotfile/ -> 設定檔案(x)

Mos 設定

截圖 2024-03-11 20.35.17
截圖 2024-03-11 20.35.23

參考資訊

  1. Setting up new M1 Max MacBook Pro - Apps that I use for my app dev -> 內有基本安裝 Homebrew 及 iterm2 後抓 dotfiles 的流程
  2. Manage Your dotfiles Like a Superhero
  3. Using GNU Stow to Manage Symbolic Links for Your Dotfiles
  4. brew-bundle-brewfile-tips.md => brewfile 的 tips