Open30

Dotfilesを構築する

ピン留めされたアイテム
Kazuk1 ShimamotoKazuk1 Shimamoto

今後やりたいこと

  • .zshrcを分割する
    • sourceコマンドを使ってできそう
  • Makefileでインストールする
  • dotfiles のインストールはワンコマンドでする
  • インストールスクリプトのテスト
    • 現状brew doctorでwarningが出たらそこでストップしてしまう
    • github actionsで試す?
Kazuk1 ShimamotoKazuk1 Shimamoto

dotfilesの構成

  • README.md
  • zsh
    • .zshrc
    • .zlogin
    • .zlogout
    • .zshenv
    • .zprofile
  • git
    • .gitconfig
    • .gitconfig_global
  • vscode
    • keybinding.json
    • settings.json
  • tmux
    • .tmux.conf
  • Brewfile
  • .config
    • starship.toml
Kazuk1 ShimamotoKazuk1 Shimamoto

最終的な構成

❯ tree
.
├── Brewfile
├── Brewfile.lock.json
├── README.md
├── extensions
├── install
└── packages
    ├── git
    │   ├── .gitconfig
    │   └── .gitignore_global
    ├── starship
    │   └── .config
    │       └── starship.toml
    ├── tmux
    │   └── .tmux.conf
    ├── vscode
    │   ├── keybindings.json
    │   └── settings.json
    └── zsh
        └── .zshrc
Kazuk1 ShimamotoKazuk1 Shimamoto

インストール方法に必要なこと

  • preztoのインストール
  • brewfileの管理
  • 設定ファイルのシンボリックリンク作成
Kazuk1 ShimamotoKazuk1 Shimamoto

preztoのインストール

以下の3手順を実行する(詳しい情報は、preztoのREADMEに乗っている)

git clone --recursive https://github.com/sorin-ionescu/prezto.git "${ZDOTDIR:-$HOME}/.zprezto"`

setopt EXTENDED_GLOB

for rcfile in "${ZDOTDIR:-$HOME}"/.zprezto/runcoms/^README.md(.N); do
  ln -s "$rcfile" "${ZDOTDIR:-$HOME}/.${rcfile:t}"
done

これでpreztoのファイル設定を読み込んでいるが、これからは自分の.zshrcなどを更新して管理するので、
↑のスクリプトを使わなくて済むはず。

preztoのアンインストール

rm -rf ~/.zprezto ~/.zlogin ~/.zlogout ~/.zpreztorc ~/.zprofile ~/.zshenv ~/.zshrc
Kazuk1 ShimamotoKazuk1 Shimamoto

Brewfile bundle

まずはbrewが入っているかどうかを調べて、brewのインストールをする
次に、すでにbrewで入っているCLIのリストをBrewfileに書き出す

brew bundle dump

書き出したBrewfileから、brew bundleで書いてあるコマンドのインストールをする

brew bundle -v --file=~/dotfiles/Brewfile
Kazuk1 ShimamotoKazuk1 Shimamoto

nerd-fontを入れる

brew tap homebrew/cask-fonts
brew install --cask font-hack-nerd-font

Fontsのインストール先は/Users/kazuki.shimamoto/Library/Fontsだと思われる

Kazuk1 ShimamotoKazuk1 Shimamoto

シンボリックリンクを貼る

シンボリックリンクの作成にはstowを使った

stowとは

packageディレクトリ配下のファイルからシンボリックリンクを作成するCLIだと思います。
参考にしたqiita

stow -v -d ~/dotfiles/packages -t ~ zsh
Kazuk1 ShimamotoKazuk1 Shimamoto

インストールスクリプト

githubから直接インストールスクリプトを取得して起動する

curl -o - https://raw.githubusercontent.com/kazukishimamoto/dotfiles/main/install | zsh
Kazuk1 ShimamotoKazuk1 Shimamoto

設定をリセットしてから再度インストールスクリプトを実行してみる

アンインストール

# シンボリックリンクの削除
VSCODE_SETTING_DIR=~/Library/Application\ Support/Code/User
stow -D -v -d ~/dotfiles/packages -t "${VSCODE_SETTING_DIR}" vscode
stow -D -v -d ~/dotfiles/packages -t ~ zsh starship tmux git

# preztoの削除
rm -rf ~/.zprezto ~/.zlogin ~/.zlogout ~/.zpreztorc ~/.zprofile ~/.zshenv ~/.zshrc ~/.tmux.conf ~/.gitconfig ~/.gitignore_global
rm -rf ~/dotfiles

リインストール

curl -o - https://raw.githubusercontent.com/kazukishimamoto/dotfiles/main/install | zsh
Kazuk1 ShimamotoKazuk1 Shimamoto

set -eをしていると、brew doctorの終了ステータスが1になってインストールスクリプトが止まってしまう

解決するために

  1. set -eを消す
  2. brew doctorのwarningを全て解決する

2で対応した

Kazuk1 ShimamotoKazuk1 Shimamoto

set -e を消したら、最後までインストールスクリプトが実行されることをまず確認してみる

Kazuk1 ShimamotoKazuk1 Shimamoto

シンボリックリンクを作成するファイルは事前に削除する必要がある

  • .zshrc
  • .gitconfig
  • vscode
    • settings.json
    • keybindings.json

stow コマンドで上書き作成する方法がわからない

Kazuk1 ShimamotoKazuk1 Shimamoto

なぜかHomebrewで入れたコマンドがbrew 〇〇 reinstallしなければ全て使えなくなった

Homebrew自体をアンインストールする

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall.sh)"

パッケージだけをワンライナーで削除する場合

brew uninstall --force $(brew list)

https://qiita.com/anon5r/items/d262c80b718739caf076

Kazuk1 ShimamotoKazuk1 Shimamoto

シンボリックリンクを作成しようとしているファイルが存在しているとstowが実行できない

シンボリックリンクを作成するファイルは事前に削除する必要がある
stowでいい感じに上書きできないか調べたけど、見た感じ無理そう

## remove vscode settings
for file in settings.json keybindings.json; do
  rm -rf "${VSCODE_SETTING_DIR}/${file}"
done

## remove dotfiles
for file in ~/.zshrc ~/.gitconfig; do
  rm -rf "${file}"
done

こういう書き方もある

dotfiles=(.zshrc .tmux.conf)

# .zshrc と .tmux.conf という設定ファイルのシンボリックリンクを
# ホームディレクトリ直下に作成する
for file in "${dotfiles[@]}"; do
        ln -svf $file ~/
done
Kazuk1 ShimamotoKazuk1 Shimamoto

再インストール時のエラーメモ

By default, binaries installed by gem will be placed into:
  /usr/local/lib/ruby/gems/3.0.0/bin

You may want to add this to your PATH.

ruby is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have ruby first in your PATH, run:
  echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.zshrc

For compilers to find ruby you may need to set:
  export LDFLAGS="-L/usr/local/opt/ruby/lib"
  export CPPFLAGS="-I/usr/local/opt/ruby/include"

For pkg-config to find ruby you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/ruby/lib/pkgconfig"

==> Summary
🍺  /usr/local/Cellar/ruby/3.0.2: 16,390 files, 38.5MB
==> Caveats
==> ruby
By default, binaries installed by gem will be placed into:
  /usr/local/lib/ruby/gems/3.0.0/bin

You may want to add this to your PATH.

ruby is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have ruby first in your PATH, run:
  echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.zshrc

For compilers to find ruby you may need to set:
  export LDFLAGS="-L/usr/local/opt/ruby/lib"
  export CPPFLAGS="-I/usr/local/opt/ruby/include"

For pkg-config to find ruby you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/ruby/lib/pkgconfig"

Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink lib/dtrace/node.d
Target /usr/local/lib/dtrace/node.d
already exists. You may want to remove it:
  rm '/usr/local/lib/dtrace/node.d'

To force the link and overwrite all conflicting files:
  brew link --overwrite node

To list all files that would be deleted:
  brew link --overwrite --dry-run node

Possible conflicting files are:
/usr/local/lib/dtrace/node.d
==> Summary
🍺  /usr/local/Cellar/node/16.8.0: 2,409 files, 48.3MB
==> Installing yarn
==> Pouring yarn--1.22.11.all.bottle.tar.gz
🍺  /usr/local/Cellar/yarn/1.22.11: 15 files, 5MB
Installing yarn has failed!

Installing font-hack-nerd-font
Installing font-hack-nerd-font cask. It is not currently installed.
==> Downloading https://github.com/ryanoasis/nerd-fonts/releases/download/v2.1.0/Hack.zip
==> Downloading from https://github-releases.githubusercontent.com/27574418/0a32bc80-4532-11ea-9bd4-55afb776ee6f?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20210902%2Fus-east-1%2
######################################################################## 100.0%
==> Installing Cask font-hack-nerd-font
==> Purging files for version 2.1.0 of Cask font-hack-nerd-font
Error: It seems there is already a Font at '/Users/kazuki.shimamoto/Library/Fonts/Hack Italic Nerd Font Complete.ttf'.
Installing font-hack-nerd-font has failed!
Kazuk1 ShimamotoKazuk1 Shimamoto

node

brew link --overwrite node

nerdfont

/Users/kazuki.shimamoto/Library/Fonts/配下の既存のフォントを削除

Kazuk1 ShimamotoKazuk1 Shimamoto

ruby の設定がいりそう?

ruby 普通に使えそうだったので無視する

~ 
❯ ruby -v
ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin20]

~ 
❯ irb
irb(main):001:0> 

~ 
❯ echo "puts 'hello'" > hello.rb

~ 
❯ ruby hello.rb
hello
Kazuk1 ShimamotoKazuk1 Shimamoto

これ、どれやればいいかわかってない…

✗ brew link node                                                                                                                                                                                         ✘ 127 
Linking /usr/local/Cellar/node/16.8.0... 
Error: Could not symlink lib/dtrace/node.d
Target /usr/local/lib/dtrace/node.d
already exists. You may want to remove it:
  rm '/usr/local/lib/dtrace/node.d'

To force the link and overwrite all conflicting files:
  brew link --overwrite node

To list all files that would be deleted:
  brew link --overwrite --dry-run node
Kazuk1 ShimamotoKazuk1 Shimamoto

ターゲット/usr/local/lib/dtrace/node.d
もう存在している。あなたはそれを削除したいかもしれません:
rm '/ usr / local / lib / dtrace / node.d'

リンクを強制し、競合するすべてのファイルを上書きするには:
brew link --overwrite node

削除されるすべてのファイルを一覧表示するには:
brew link --overwrite --dry-run node

Kazuk1 ShimamotoKazuk1 Shimamoto

github actionsでinstallのテストをする

  • zshではなく、shで実行できるようにする
  • もしくは、zshのインストールや設定などもやってしまう

メモ

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2

      # Runs a single command using the runners shell
      - name: Run a install script
        run: ./install

      # Runs a set of commands using the runners shell
      - name: Run a multi-line script
        run: |
          echo Add other actions to build,
          echo test, and deploy your project.
Kazuk1 ShimamotoKazuk1 Shimamoto

ファイル修飾子について

やりたいことはREADMEを覗いたファイルを全て並べること
grepでファイル一覧からREADMEを除外するので良さそう
^README.md(.N)の意味は
ファイル名修飾子とは、ファイル名指定の後に付けるファイルの種類で絞り込みを行うための記号のこと

記号 意味
Text Text
Text Text
. 通常ファイル
/ ディレクトリ
@ シンボリックリンク
* 実行ファイル
D ドットで始まるファイルも含める
^ 後ろの修飾子の意味を反転させる
fspec アクセス権で絞り込む
m[Mwhms]-n 修正時刻(modify time)で絞り込む
a[Mwhms]-n アクセス時刻(access time)で絞り込む
c[Mwhms]-n ステータス変更時刻(change time)で絞り込む
N マッチしない(ディレクトリが存在しない)時にエラーの代わりに黙って空文字列に展開されるようになる

^README.md(.N)という書き方はzshでしか使えないので別の書き方にする

  for rcfile in $(ls -1 "${ZDOTDIR:-$HOME}"/.zprezto/runcoms/ | grep -v README); do
    ln -s "$rcfile" "${ZDOTDIR:-$HOME}/.${rcfile:t}"
  done
Kazuk1 ShimamotoKazuk1 Shimamoto

preztoとか、.zshrcとかzsh前提のdotfilesになっているので、installスクリプトからzshの設定までやってしまう