野良 homebrew-cask の作成 - M1 MacBook Air インストール覚書(8)

はじめに

会計から取引先に確認してもらいましたが、未だに納期未定とのことです。 発表時に発注したのにどれだけバックオーダー抱えてるんだろうか。 後から発注した人で Apple に直発注された人がいたら、そっちの方が早く届きそうです。 待ちぼうけしてても仕方ないので、環境構築を進めます。

M1 に対応したアプリがベータとしてパラパラと出ていますが、当然そういうものは本家には入りません。 インストーラをダウンロードしてからインストーラを起動するのは面倒なので、野良のリポジトリを作ってみようと思います。

リポジトリの作成

とりあえず、野良 cask のリポジトリを先に作ります。 GitHub に「homebrew-」を接頭子にしたリポジトリを作ると、brew tap で取り入れられるようです。 そこで、Cask 用に homebrew-m1-beta-cask リポジトリを作成しました(当初、homebrew-m1_beta_cask のように「_」を使っていましたが、指摘されたので「-」に変更しました)。

せっかくなので、Big Sur にした MacBook で野良 cask の作成テストをやってみます。 このリポジトリをタップしてみると、Taps の下に野良リポジトリが clone されてきました。

hkob@thin ~> brew tap hkob/m1-beta-cask
==> Tapping hkob/m1-beta-cask
Cloning into '/usr/local/Homebrew/Library/Taps/hkob/homebrew-m1-beta-cask'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.
Tapped (29 files, 25.6KB).

cask 作成

Cask の作り方は、本家に書かれていました。これを参考に作ってみようと思います。 Adding a Cask

Universal なもので、まだ本家が対応していないものとして、macFUSE でテストしてみようと思います。 sshfs を使いたいのですが、これが FUSE に依存しているためです。 Home - macFUSE を確認すると、4.0.0 から Apple Silicon に対応したようです。名前も osxfuse から macfuse に変わっていました(確かに OS X ではなくなりましたからね)。 Cask の本家は現在、3.11.2 のままのようです。 homebrew-cask/osxfuse.rb at master · Homebrew/homebrew-cask · GitHub

まず、brew create で cask を作成してみます。本家のやり方通りに brew cask create とやったら、deprecated だから、brew create --cask --set-name <name> <url> で実施するように指示されました。 せっかくなので、新しいコマンドの方でやり直してみました。URL の部分に dmg などを記載すると、自動的に shasum を自動で計算してくれるようです。オリジナルと被りたくないので、名前は「m1-beta-macfuse」としてみました。そのまま vi の編集画面に行きますが、即終了します。

hkob@thin ~ > brew create --cask --set-name m1-beta-macfuse https://github.com/osxfuse/osxfuse/releases/download/macfuse-4.0.4/macfuse-4.0.4.dmg
==> Downloading https://github.com/osxfuse/osxfuse/releases/download/macfuse-4.0.4/macfuse-4.0.4.dmg
==> Downloading from https://github-production-release-asset-2e65be.s3.amazonaws.com/1867347/05927e00-3293-11eb-8a00-c0a3a3ec657f?X-Amz-Algor
######################################################################## 100.0%
Please run `brew audit --cask --new m1-beta-macfuse` before submitting, thanks.
Editing /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask/Casks/m1-beta-macfuse.rb
Warning: Using vim because no editor was set in the environment.
This may change in the future, so we recommend setting EDITOR,
or HOMEBREW_EDITOR to your preferred text editor.

当然のことながら、homebrew-cask/Casks の下に作られます。野良 cask におきたいので、移動しておきます。fish なので、若干コマンドが違います(zsh だと $() になるはず)。brew のリポジトリは M1 native と場所が違うので、brew に教えてもらうのがいいです。今は Intel の MacBook なので、上に書かれているように /usr/local/Homebrew になります。

# リポジトリに Casks フォルダを作成
mkdir -p (brew --repository)/Library/Taps/hkob/homebrew-m1-beta-cask/Casks
# homebrew-cask/Casks に作られたものを自分のリポジトリに移動
mv (brew --repository)/Library/Taps/{homebrew/homebrew-cask/Casks/m1-beta-macfuse.rb, hkob/homebrew-m1-beta-cask/Casks}

先ほど編集をやめていたので、vi で編集してみます。

vi (brew --repository)/Library/Taps/hkob/homebrew-m1-beta-cask/Casks/m1-beta-macfuse.rb

こんな感じである程度の雛形が作成されています。

cask "m1_beta_macfuse" do
  version "4.0.4"
  sha256 "3e1b52a78ba6530cba2fb34a64c1da2ef969e03aeb2a58ae622cb0f09772d450"

  url "https://github.com/osxfuse/osxfuse/releases/download/macfuse-#{version}/macfuse-#{version}.dmg"
  name ""
  desc ""
  homepage ""

  app ""
end

現在の osxfure の caskと比較してみます。

cask "osxfuse" do
  version "3.11.2"
  sha256 "0f9fd021810063ded2f9a40347e11961369238af27615842063831568a0860ce"

  # github.com/osxfuse/ was verified as official when first introduced to the cask
  url "https://github.com/osxfuse/osxfuse/releases/download/osxfuse-#{version}/osxfuse-#{version}.dmg"
  appcast "https://github.com/osxfuse/osxfuse/releases.atom"
  name "OSXFUSE"
  desc "File system integration"
  homepage "https://osxfuse.github.io/"

  pkg "Extras/FUSE for macOS #{version}.pkg"

  postflight do
    set_ownership ["/usr/local/include", "/usr/local/lib"]
  end

  uninstall pkgutil: [
    "com.github.osxfuse.pkg.Core",
    "com.github.osxfuse.pkg.MacFUSE",
    "com.github.osxfuse.pkg.PrefPane",
  ],
            kext:    "com.github.osxfuse.filesystems.osxfuse"

  zap trash: "~/Library/Caches/com.github.osxfuse.OSXFUSEPrefPane"

  caveats do
    reboot
  end
end

これに合わせてダウンロードされた dmg を探検してみます。Cache に入っている先ほどダウンロードした dmg を開いてみます。

open Library/Caches/Homebrew/Cask/m1_beta_osxfuse--4.0.4.dmg

Install macFUSE というエイリアスが見えます。ls で確認してみます。

hkob@thin ~> ls -l /Volumes/macFUSE/
total 56
drwxr-xr-x  7 hkob  staff    238 11 28 04:55 Extras/
lrwxr-xr-x@ 1 hkob  staff     24 11 28 04:55 Install macFUSE.pkg@ -> Extras/macFUSE 4.0.4.pkg
-rw-r--r--@ 1 hkob  staff  23494 11 20 18:29 License.rtf

パッケージ名が Extras/macFUSE 4.0.4.pkg になっていますね。pkg の名前を直しましょう。 さらに、pkgutil コマンドで pkg の中身を確認してみます。

hkob@thin ~> pkgutil --payload-files /Volumes/macFUSE/Extras/macFUSE\ 4.0.4.pkg
(中略 )
./usr/local/include/fuse
(中略 )
./usr/local/lib
(中略 )
./Library/Filesystems/macfuse.fs/Contents/Resources/uninstall_macfuse.app
(中略 )
./Library/Frameworks/macFUSE.framework
(中略 )
./Library/PreferencePanes/macFUSE.prefPane
(中略 )

やはり中身は osxfuse ではなく、macfuse に書き換わっているようです。uninstall ですが、中に uninstall_macfuse.app が存在しています。最終的にはこれを呼び出す形にしたいのですが、uninstall は macfuse に書き換えるだけにしておき、実際にインストールしてみてから、このスクリプトが正しく動くかを確かめてみます。 また、url と homepage のサイトが異なるときに、以前はコメントを書いてあっただけでしたが、verified タグが必要になったようです。

ここで修正した段階で、brew audit でテストしてみます。一応、パスしたみたいです。実は、一回目に「-」を「_」にしていたため怒られました。このため最初からやり直しています。

hkob@thin ~ > brew audit --cask --new m1-beta-macfuse
==> Downloading https://github.com/osxfuse/osxfuse/releases/download/macfuse-4.0.4/macfuse-4.0.4.dmg
Already downloaded: /Users/hkob/Library/Caches/Homebrew/downloads/d2036e64701da6bf92cd798f827962f4d852daf1c57226258b9d5d5d616aa280--macfuse-4.0.4.dmg
audit for m1-beta-macfuse: passed

インストールテスト

すでに OSXFUSE が入ってしまっているので、これらを削除します。インストールしているはずの sshfs はインストールされていませんでした。また、以前はbrew cask uninstall だったのですが、こちらも brew uninstall --cask に変わっていました。基本、cask はオプション扱いになったということですね。

hkob@thin ~ > brew uninstall --cask osxfuse
==> Uninstalling Cask osxfuse
==> Unloading kernel extension com.github.osxfuse.filesystems.osxfuse
Password:
Executing: /usr/bin/kmutil showloaded --list-only --bundle-identifier com.github.osxfuse.filesystems.osxfuse
No variant specified, falling back to release
==> Uninstalling packages:
com.github.osxfuse.pkg.Core
com.github.osxfuse.pkg.PrefPane
==> Purging files for version 3.11.2 of Cask osxfuse

早速、作成した cask でインストールしてみます。

hkob@thin ~> brew install --cask m1-beta-macfuse
==> Caveats
m1-beta-macfuse requires a kernel extension to work.
If the installation fails, retry after you enable it in:
  System Preferences → Security & Privacy → General

For more information, refer to vendor documentation or this Apple Technical Note:
  https://developer.apple.com/library/content/technotes/tn2459/_index.html

You must reboot for the installation of m1-beta-macfuse to take effect.

==> Downloading https://github.com/osxfuse/osxfuse/releases/download/macfuse-4.0.4/macfuse-4.0.4.dmg
Already downloaded: /Users/hkob/Library/Caches/Homebrew/downloads/d2036e64701da6bf92cd798f827962f4d852daf1c57226258b9d5d5d616aa280--macfuse-4.0.4.dmg
==> Installing Cask m1-beta-macfuse
==> Running installer for m1-beta-macfuse; your password may be necessary.
==> Package installers may write to any location; options such as --appdir are ignored.
installer: Package name is macFUSE
installer: Installing at base path /
installer: The install was successful.
==> Changing ownership of paths required by m1-beta-macfuse; your password may be necessary
🍺  m1-beta-macfuse was successfully installed!

ちゃんと reboot が必要だとか出てますね。とりあえずインストールは問題なさそうです。再起動して実際に動くかどうか確認してみます。

hkob@thin ~> brew install --cask m1-beta-macfuse
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and homebrew/cask).
==> Updated Formulae
Updated 3 formulae.
==> New Casks
gluemotion                                                             snapmotion
==> Updated Casks
boxcryptor       fractal-bot      gramps           macupdater       qownnotes        qtvlm            texmacs          textadept

==> Caveats
m1-beta-macfuse requires a kernel extension to work.
If the installation fails, retry after you enable it in:
  System Preferences → Security & Privacy → General

For more information, refer to vendor documentation or this Apple Technical Note:
  https://developer.apple.com/library/content/technotes/tn2459/_index.html

You must reboot for the installation of m1-beta-macfuse to take effect.

==> Downloading https://github.com/osxfuse/osxfuse/releases/download/macfuse-4.0.4/macfuse-4.0.4.dmg
Already downloaded: /Users/hkob/Library/Caches/Homebrew/downloads/d2036e64701da6bf92cd798f827962f4d852daf1c57226258b9d5d5d616aa280--macfuse-4.0.4.dmg
==> Installing Cask m1-beta-macfuse
==> Running installer for m1-beta-macfuse; your password may be necessary.
==> Package installers may write to any location; options such as --appdir are ignored.
installer: Package name is macFUSE
installer: Installing at base path /
installer: The install was successful.
==> Changing ownership of paths required by m1-beta-macfuse; your password may be necessary
🍺  m1-beta-macfuse was successfully installed!

再起動して、システム環境設定を開いてみました。ちゃんと macFUSE が入っていますね。

f:id:hkob:20201202120854p:plain
macFUSE in システム環境設定

macFUSE パネルを開くと、4.0.4 と表示されていました。

f:id:hkob:20201202121033p:plain
macFUSE パネル

ここにある Remove FUSE をクリックすると、uninstall_macfuse.sh が動作するようです。 Big Sur で機能拡張関係が修正されているので、アンインストールについてはこのスクリプトに任せた方がよいかと思いました。最終的にこんな感じになりました。

cask "m1-beta-macfuse" do
  version "4.0.4"
  sha256 "3e1b52a78ba6530cba2fb34a64c1da2ef969e03aeb2a58ae622cb0f09772d450"

  # github.com was verified as official when first introduced to the cask
  url "https://github.com/osxfuse/osxfuse/releases/download/macfuse-#{version}/macfuse-#{version}.dmg",
    verified: "https://github.com/osxfuse"
  appcast "https://github.com/osxfuse/osxfuse/releases.atom"
  name "MACFUSE"
  desc "File system integration"
  homepage "https://osxfuse.github.io/"

  pkg "Extras/macFUSE #{version}.pkg"

  postflight do
    set_ownership ["/usr/local/include", "/usr/local/lib"]
  end

  uninstall script:  {
    executable: "/Library/Filesystems/macfuse.fs/Contents/Resources/uninstall_macfuse.app/Contents/Resources/Scripts/uninstall_macfuse.sh",
    sudo:       true,
  }

  zap trash: "~/Library/Caches/com.github.osxfuse.OSXFUSEPrefPane"

  caveats do
    reboot
  end
end

実際にアンインストールしてみます。

brew uninstall --cask m1-beta-macfuse
==> Uninstalling Cask m1-beta-macfuse
==> Unloading kernel extension com.github.osxfuse.filesystems.macfuse
Password:
Executing: /usr/bin/kmutil showloaded --list-only --bundle-identifier com.github.osxfuse.filesystems.macfuse
No variant specified, falling back to release
==> Uninstalling packages:
io.macfuse.installer.components.core
io.macfuse.installer.components.preferencepane
==> Purging files for version 4.0.4 of Cask m1-beta-macfuse

削除できているような気がします。システム環境設定を開いてみると、パネルがなくなっていました。

macFUSE が動作するか sshfs でテストしてみようと思いますが、ここまでかなり長くなったので一旦切ります。 macFUSE 自体はベータでもなんでもないので、このままプルリクエスト送ってしまってもいいのではないかと思いました。M1 MacBook Air が届いて、そちらでもちゃんと動作するか確認できたら、プルリクエストを送ってみようかと思います(その前に誰かが出してくれそうな気がしますが)。

次の記事: 野良 homebrew Formula の作成 - M1 MacBook Air インストール覚書(9) - hkob’s blog 前の記事: Big Sur の環境設定(4段目) - M1 MacBook Air インストール覚書(7) - hkob’s blog

hkob.hatenablog.com