1# Chapter 26: toolchains, the compilers buck uses, all from nix
2
3buck2 does not bring its own compilers. It asks a *toolchain* target "what
4is your C compiler, your Rust compiler, your Python?" and runs whatever it is
5told. This folder answers, and every answer is a path into the nix store.
6
7They are the buck prelude's own `system_demo_toolchains`, copied and
8configured as that macro's documentation asks, with one change in where the
9paths come from: not from `PATH`, which buck's C shim never searches (it
10execs by absolute path), but from `.buckconfig.local`, which the devshell
11writes on every entry (chapter 16). So a build sees exactly the tools the
12flake pins, and nothing else on the machine.
13
14```mermaid
15flowchart LR
16  F["flake.nix devshell"] -->|"shellHook writes"| L[".buckconfig.local: [nix] cc, cxx, ar, python, [rust] rustc_identity"]
17  L -->|"read_root_config"| T["toolchains/BUCK: cxx, python, rust, genrule, test, zip_file"]
18  T --> B["every buck action"]
19```
20
21The Rust toolchain carries three settings worth knowing about. Optimisation
22is explicit (`-Copt-level=3`), because build scripts read their `OPT_LEVEL`
23from it. Debug assertions stay on for development and tests (the executor's
24thread checks are `debug_assert`s), and the nix package turns them off for
25release. And every compile carries an inert `--cfg rustc_identity="…"` with
26the compiler's store path.
27
28> **Aside: a flag that does nothing, on purpose.** No code reads
29> `rustc_identity`. It is there because the prelude's toolchain invokes the
30> bare name `rustc`, so buck's cache key never recorded *which* rustc
31> compiled a crate. With a build cache shared across repositories, a library
32> compiled by another repository's older rustc was served back and failed
33> with E0514, "compiled by an incompatible version of rustc". Putting the
34> compiler's path in a flag puts it in every key, and the collision is gone.
35
36> **Try it.** `cat .buckconfig.local` inside `nix develop`: every path in it
37> is the one this folder hands to buck.
38
39## For the people who maintain it
40
41| File | What |
42| --- | --- |
43| [BUCK](BUCK) | The toolchains: `cxx` (clang from nix), `genrule`, `python` and `python_bootstrap` (nix's python), `rust` (edition from `build/defs.bzl`, the flags above), `remote_test_execution`, `test`, `zip_file`. |
44
45← Previous: [Chapter 25, record-gates/src/](../tools/record-gates/src/) · Up: [postjevsql](../) · Next: [Chapter 27, platforms/](../platforms/) →