💻

Macの環境をdotfilesでセットアップしてみた改

2023/05/27に公開

以前以下のブログで公開したMac用のdotfilesを色々改良してみたので、改良版として本記事で公開する。

https://dev.classmethod.jp/articles/joined-mac-dotfiles-customize/

なおdotfilesとは、ホームディレクトリに存在するドット(.)から始まる設定ファイル群の事を指す。

dotfiles の中身

$ tree

.
├── .bin
│   ├── .Brewfile
│   ├── .bash_profile
│   ├── .bashrc
│   ├── .gitconfig
│   ├── .gitignore_global
│   ├── brew.sh
│   ├── defaults.sh
│   ├── init.sh
│   └── link.sh
├── .github
│   └── workflows
│       └── main.yml
├── Makefile
├── README.md
├── raycast
│   └── Raycast.rayconfig
└── vscode
    ├── extensions
    ├── settings.json
    └── sync.sh

dotfilesの詳細は、以下のGitHubのURLに上げている。

https://github.com/tsukuboshi/dotfiles

以下ではセットアップ用スクリプト、及びソフトウェア用設定ファイルに分けて解説する。

セットアップ用スクリプト

本項目では、Macにのdotfilesに格納されているファイルを用いてセットアップする際に使用するスクリプトについて説明する。

セットアップ自動化スクリプト(Makefile)

本dotfilesのルートディレクトリでmakeコマンドを叩く事で、Makefileに記載のあるスクリプトが全て実行され、セットアップを自動で実施する。

なおmake initmake linkmake defaultsmake brewコマンドにより、各々のスクリプト(init.sh, link.sh, brew.sh, defaults.sh)を個別で実行できる。

https://github.com/tsukuboshi/dotfiles/blob/main/Makefile

初期設定スクリプト(.bin/init.sh)

init.shは、Macで一番最初に実行するべき初期設定を実施する。

本dotfilesでは、zshからbashへの切り替え、及びXcodeとbrewのインストールを実行する。

https://github.com/tsukuboshi/dotfiles/blob/main/.bin/init.sh

シンボリックリンク作成スクリプト(.bin/link.sh)

link.shは、ホームディレクトリに対してドットから始める設定ファイルのシンボリックリンクを作成する。

なお.git.github.DS_storeといったシンボリックリンクの作成の必要がないファイルについては、あらかじめ除外する。

https://github.com/tsukuboshi/dotfiles/blob/main/.bin/link.sh

追加コマンド/アプリ導入スクリプト(.bin/brew.sh)

brew.shは、Macに追加で必要なコマンドツールやデスクトップアプリをインストールする。

追加で必要なコマンド及びアプリを.Brewfileに予め記載した上で、このスクリプトを実行すると一括インストールが可能。

https://github.com/tsukuboshi/dotfiles/blob/main/.bin/brew.sh

.Brewfileには、追加したいコマンド及びアプリを一覧で記載する。

https://github.com/tsukuboshi/dotfiles/blob/main/.bin/.Brewfile

Mac 環境設定スクリプト(.bin/defaults.sh)

defaults.shは、Mac自体の環境設定を操作する。

https://github.com/tsukuboshi/dotfiles/blob/main/.bin/defaults.sh

なお一部のdefaultsコマンドは、実行後にPC再起動が必要となるため注意。

本ファイルで記載しているdefaultsコマンドについては、主に以下の記事を参考にしている。

https://qiita.com/djmonta/items/17531dde1e82d9786816

https://qiita.com/keitean/items/bf82da152fd587fa29ef

https://qiita.com/dodonki1223/items/2bb296111e561c93035e

テスト自動化スクリプト(.github/workflows/main.yml)

main.ymlには、GitHub Actionsを用いて、Githubリポジトリにpushした際、自動で処理されるテストの内容を記載する。

本dotfilesではMacOSにおけるmakeコマンドのテストのみ実施しているが、他のコマンドを追記してテストする事も可能。

https://github.com/tsukuboshi/dotfiles/blob/main/.github/workflows/main.yml

ソフトウェア設定ファイル

以下では、ソフトウェアの設定が記載されているファイルについて説明する。

Bash 設定ファイル(.bin/.bash_profile & .bin/.bashrc)

.bash_profile及び.bashrcには、ターミナルで使用するBashの設定が記載されている。

.bash_profileには、主に環境変数を設定するexportや、補完定義を設定するcomplete/souceを記載している。

https://github.com/tsukuboshi/dotfiles/blob/main/.bin/.bash_profile

対して、.bashrcには主にターミナルで用いるコマンド群を、引数を持たないaliasと、引数を持つfunctionに分けて記載している。

https://qiita.com/belion_freee/items/528ff57b920d559abf3d

.bash_profile.bashrcの使い分け方は好みの問題なので、どちらに記載しても構わない。

https://github.com/tsukuboshi/dotfiles/blob/main/.bin/.bashrc

Git 設定ファイル(.bin/.gitconfig & .bin/.gitignore_global)

.gitconfigには、Gitの設定が記載されている。

もしEメールがそのまま書かれている場合は、以下を参考にコミットメールアドレスを別で設定すると良い。

https://docs.github.com/ja/github-ae@latest/account-and-profile/setting-up-and-managing-your-personal-account-on-github/managing-email-preferences/setting-your-commit-email-address

https://github.com/tsukuboshi/dotfiles/blob/main/.bin/.gitconfig

.gitignore_globalには、Gitによる追跡から除外するファイルが記載されている。

GitHub公式から、.gitignoreのテンプレートがサービス毎に提供されているので、参考にしながら自分用にカスタマイズすると良さげ。

https://github.com/github/gitignore

https://github.com/tsukuboshi/dotfiles/blob/main/.bin/.gitignore_global

VSCode 設定ファイル(vscode/配下)

settings.jsonにはVSCode自体の設定が記載されている。

https://github.com/tsukuboshi/dotfiles/blob/main/vscode/settings.json

対してextensionには、VSCode拡張機能の名称が記載されており、どのvscode拡張機能が導入されているかを一目で確認できる。

https://github.com/tsukuboshi/dotfiles/blob/main/vscode/extensions

そしてsync.shを実行する事で、settings.jsonのシンボリックリンクの作成、及びextensionsに記載されている拡張機能のインストールを合わせて実施できる。

https://github.com/tsukuboshi/dotfiles/blob/main/vscode/sync.sh

ちなみに.bashrcに記載されている以下のコマンドで、現在VSCodeに導入されている拡張機能を上記のextensionsのように一覧で出力できる。

.bashrc
alias cle='code --list-extensions'

Raycast 設定ファイル(raycast/配下)

Raycast.rayconfigにはRaycastの設定が記載されており、RaycastのImport機能で取り込み可能。

(バイナリファイルのためファイル内容は記載しない)

Importのやり方は以下を参照。

https://www.raycast.com/changelog/1-22-0

dotfilesを始めよう

万が一自身のPCに何かが起きた際の設定のバックアップになるので、ぜひ簡単なdotfilesを作ってみてほしい。

またスクリプトを作成する事でBashを学べたり、GitHubリポジトリにバックアップとしてアップロードする事でGitの勉強にもなるので、エンジニア初心者がこの辺りの技術を学ぶにはちょうど良い教材な気もする。

(2024/3/28追記)
スクリプトの内容が見やすいよう、可能な限りファイルをGitHubの埋め込みに変更。

Discussion