lol
1{ fetchurl, lib, stdenv, zstd
2, testers, buck2 # for passthru.tests
3}:
4
5# NOTE (aseipp): buck2 uses a precompiled binary build for good reason — the
6# upstream codebase extensively uses unstable `rustc` nightly features, and as a
7# result can't be built upstream in any sane manner. it is only ever tested and
8# integrated against a single version of the compiler, which produces all usable
9# binaries. you shouldn't try to workaround this or get clever and think you can
10# patch it to work; just accept it for now. it is extremely unlikely buck2 will
11# build with a stable compiler anytime soon; see related upstream issues:
12#
13# - NixOS/nixpkgs#226677
14# - NixOS/nixpkgs#232471
15# - facebook/buck2#265
16# - facebook/buck2#322
17#
18# worth noting: it *is* possible to build buck2 from source using
19# buildRustPackage, and it works fine, but only if you are using flakes and can
20# import `rust-overlay` from somewhere else to vendor your compiler. See
21# nixos/nixpkgs#226677 for more information about that.
22
23# NOTE (aseipp): this expression is mostly automated, and you are STRONGLY
24# RECOMMENDED to use to nix-update for updating this expression when new
25# releases come out, which runs the sibling `update.sh` script.
26#
27# from the root of the nixpkgs git repository, run:
28#
29# nix-shell maintainers/scripts/update.nix \
30# --argstr commit true \
31# --argstr package buck2
32
33let
34
35 # build hashes, which correspond to the hashes of the precompiled binaries
36 # procued by GitHub Actions. this also includes the hash for a download of a
37 # compatible buck2-prelude
38 buildHashes = builtins.fromJSON (builtins.readFile ./hashes.json);
39
40 # our version of buck2; this should be a git tag
41 version = "2023-10-15";
42
43 # the platform-specific, statically linked binary — which is also
44 # zstd-compressed
45 src =
46 let
47 suffix = {
48 # map our platform name to the rust toolchain suffix
49 # NOTE (aseipp): must be synchronized with update.sh!
50 x86_64-darwin = "x86_64-apple-darwin";
51 aarch64-darwin = "aarch64-apple-darwin";
52 x86_64-linux = "x86_64-unknown-linux-musl";
53 aarch64-linux = "aarch64-unknown-linux-musl";
54 }."${stdenv.hostPlatform.system}" or (throw "Unsupported system: ${stdenv.hostPlatform.system}");
55
56 name = "buck2-${version}-${suffix}.zst";
57 hash = buildHashes."${stdenv.hostPlatform.system}";
58 url = "https://github.com/facebook/buck2/releases/download/${version}/buck2-${suffix}.zst";
59 in fetchurl { inherit name url hash; };
60
61 # compatible version of buck2 prelude; this is exported via passthru.prelude
62 # for downstream consumers to use when they need to automate any kind of
63 # tooling
64 prelude-src =
65 let
66 prelude-hash = "880be565178cf1e08ce9badef52b215f91e48479";
67 name = "buck2-prelude-${version}.tar.gz";
68 hash = buildHashes."_prelude";
69 url = "https://github.com/facebook/buck2-prelude/archive/${prelude-hash}.tar.gz";
70 in fetchurl { inherit name url hash; };
71
72in stdenv.mkDerivation {
73 pname = "buck2";
74 version = "unstable-${version}"; # TODO (aseipp): kill 'unstable' once a non-prerelease is made
75 inherit src;
76
77 nativeBuildInputs = [ zstd ];
78
79 doCheck = true;
80 dontConfigure = true;
81 dontStrip = true;
82
83 unpackPhase = "unzstd ${src} -o ./buck2";
84 buildPhase = "chmod +x ./buck2";
85 checkPhase = "./buck2 --version";
86 installPhase = ''
87 mkdir -p $out/bin
88 install -D buck2 $out/bin/buck2
89 '';
90
91 passthru = {
92 prelude = prelude-src;
93
94 updateScript = ./update.sh;
95 tests = testers.testVersion {
96 package = buck2;
97
98 # NOTE (aseipp): the buck2 --version command doesn't actually print out
99 # the given version tagged in the release, but a hash, but not the git
100 # hash; the entire version logic is bizarrely specific to buck2, and needs
101 # to be reworked the open source build to behave like expected. in the
102 # mean time, it *does* always print out 'buck2 <hash>...' so we can just
103 # match on "buck2"
104 version = "buck2";
105 };
106 };
107
108 meta = with lib; {
109 description = "Fast, hermetic, multi-language build system";
110 homepage = "https://buck2.build";
111 changelog = "https://github.com/facebook/buck2/releases/tag/${version}";
112 license = with licenses; [ asl20 /* or */ mit ];
113 mainProgram = "buck2";
114 maintainers = with maintainers; [ thoughtpolice ];
115 platforms = [
116 "x86_64-linux" "aarch64-linux"
117 "x86_64-darwin" "aarch64-darwin"
118 ];
119 };
120}