🙆‍♀️

CraneでBuild Profileを変更する

に公開

結論

CARGO_PROFILEを設定することでbuild profileの変更が可能

flake.nix
craneLib.buildPackage {
  CARGO_PROFILE = "<custom-profile>";
}

もし対象のProfileにdebug symbolを含めたい場合、nix側でもstripを止める必要がある。

flake.nix
craneLib.buildPackage {
  dontStrip = true
}

解説

異なるProfileを使用する

対応方法の候補として以下を考えた。

  1. cargoExtraArgs
  2. CARGO_PROFILE

1. cargoExtraArgs

cargoに追加の引数を渡せるようになる。

cargoExtraArgs: additional flags to be passed in the cargo invocation (e.g. enabling specific features)

  • Default value: "--locked"

最初はCargoExtraArgsにprofileの情報を渡すことで、build profileを変更できると考えていた。
しかし、内部的にはcargoBuildCommandに追加されるだけなので、期待した挙動にはならない。

flake.nix
craneLib.buildPackage {
  cargoExtraArgs = "--profile <custom-project>";
}
$ nix build .#custom-profile
       > error: conflicting usage of --profile=custom and --release
       > The `--release` flag is the same as `--profile=release`.
       > Remove one flag or the other to continue.
  1. CARGO_PROFILE

この環境変数を変更することでbuild profileの変更が可能。
derivationとしても渡すことができる。

CARGO_PROFILE can be set on the derivation to alter which cargo profile is selected; setting it to "" will omit specifying a profile altogether.

つまり、以下のように書けばbuild profileの変更が可能になる。

flake.nix
craneLib.buildPackage {
  CARGO_PROFILE = "<custom-profile>";
}

debug symbolを含める

Cargo.tomlのprofileでdebug=trueを設定したとしても、nix側でstripが行われてしまう。
stripを行わないためには、dontStrip=trueの設定が必要。

flake.nix
craneLib.buildPackage {
  dontStrip = true
}

これにより、実行可能バイナリにデバックシンボルが含まれていることが確認できた。

備考

craneにおいてbuild profileを変更する方法を調べたが、結局使用しなかった。

元々の目的はrelease buildでBACKTRACEを確認できるようにすることだった。

これを実現するために、debug symbolをバイナリに含めたいと考えた。
しかし、tracing crateを使用してSPANTRACEを表示できるようにした方が適していた。

調べたものが無駄になるのはもったいないので、備忘録として残す。

参考

https://crane.dev/API.html

Discussion