Nix でソフトウェアのビルドを行おうとする際、最初から完璧な Nix スクリプトが書けるわけではないので、少し Nix を書いて動かしてはビルドがエラーで失敗し、それを修正してまたビルドすると失敗し……というイテレーションを回すことになります。ビルドが一瞬で終わるようなソフトウェアであればまだ良いのですが、昨日の記事で書いた Karukan のような複雑なソフトウェアだとビルドに時間がかかるのでかなり面倒です。特に installPhase で失敗する場合、とても時間がかかる buildPhase が動いた後に失敗することになるのでストレスフルです。
こんな時は breakpointHook が便利だということを、昨日の Karukan のビルドスクリプトを書いているときに Mastodon で natsukium さんに教えていただきました。以下のように nativeBuildInputs に追加しビルドします。
{
breakpointHook,
# ...
}:
stdenv.mkDerivation {
# ...
nativeBuildInputs = [
breakpointHook
];
# ...
}
ビルド中にエラーが発生すると以下のような表示が出て、このコマンドを実行するとビルド環境に入ることができます。
build for karukan-0.1.0 failed in installPhase with exit code 1
To attach, run the following command:
sudo /nix/store/qfw97gn2g82dnxsb64d12dv4kf1sgjc0-attach/bin/attach 5181207
❯ sudo /nix/store/qfw97gn2g82dnxsb64d12dv4kf1sgjc0-attach/bin/attach 5181207
[sudo: authenticate] Password:
bash-5.3$ ls
CLAUDE.md Cargo.toml LICENSE-MIT THIRD_PARTY_LICENSES icon.png karukan-cli karukan-im target
Cargo.lock LICENSE-APACHE README.md docs images karukan-engine scripts tests
bash-5.3$ pwd
/build/source
bash-5.3$ ls $out
ls: cannot access '/nix/store/0k9kdp6f3pa5i8avca24cbdvfxbzxi82-karukan-0.1.0': No such file or directory
bash-5.3$ ls $src
CLAUDE.md Cargo.toml LICENSE-MIT THIRD_PARTY_LICENSES icon.png karukan-cli karukan-im tests
Cargo.lock LICENSE-APACHE README.md docs images karukan-engine scripts
bash-5.3$