nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 lib,
3 stdenv,
4 cargo,
5 makeWrapper,
6 rustPlatform,
7 rustc,
8 asNightly ? false,
9}:
10
11rustPlatform.buildRustPackage {
12 pname = "rustfmt" + lib.optionalString asNightly "-nightly";
13 inherit (rustc) version src;
14
15 # the rust source tarball already has all the dependencies vendored, no need to fetch them again
16 cargoVendorDir = "vendor";
17 buildAndTestSubdir = "src/tools/rustfmt";
18
19 # changes hash of vendor directory otherwise
20 dontUpdateAutotoolsGnuConfigScripts = true;
21
22 nativeBuildInputs = [
23 makeWrapper
24 ];
25
26 buildInputs = [
27 rustc.llvm
28 ];
29
30 # rustfmt uses the rustc_driver and std private libraries, and Rust's build process forces them to have
31 # an install name of `@rpath/...` [0] [1] instead of the standard on macOS, which is an absolute path
32 # to itself.
33 #
34 # [0]: https://github.com/rust-lang/rust/blob/f77f4d55bdf9d8955d3292f709bd9830c2fdeca5/src/bootstrap/builder.rs#L1543
35 # [1]: https://github.com/rust-lang/rust/blob/f77f4d55bdf9d8955d3292f709bd9830c2fdeca5/compiler/rustc_codegen_ssa/src/back/linker.rs#L323-L331
36 preFixup = lib.optionalString stdenv.hostPlatform.isDarwin ''
37 install_name_tool -add_rpath "${rustc.unwrapped}/lib" "$out/bin/rustfmt"
38 install_name_tool -add_rpath "${rustc.unwrapped}/lib" "$out/bin/git-rustfmt"
39 '';
40
41 env = {
42 # As of 1.0.0 and rustc 1.30 rustfmt requires a nightly compiler
43 RUSTC_BOOTSTRAP = 1;
44
45 # As of rustc 1.45.0, these env vars are required to build rustfmt (due to
46 # https://github.com/rust-lang/rust/pull/72001)
47 CFG_RELEASE = rustc.version;
48 CFG_RELEASE_CHANNEL = if asNightly then "nightly" else "stable";
49 };
50
51 postInstall = ''
52 wrapProgram $out/bin/cargo-fmt \
53 --suffix PATH : ${lib.makeBinPath [ cargo ]}
54 '';
55
56 meta = {
57 description = "Tool for formatting Rust code according to style guidelines";
58 homepage = "https://github.com/rust-lang-nursery/rustfmt";
59 license = with lib.licenses; [
60 mit
61 asl20
62 ];
63 mainProgram = "rustfmt";
64 maintainers = with lib.maintainers; [
65 globin
66 basvandijk
67 ];
68 };
69}