78 Commits

Author SHA1 Message Date
Philip Taron e03d0a783d various: reduce // merges, optimize ++ chains across hot paths (#506793) 2026-05-27 22:06:37 +00:00
Sigmanificient 0bbc8dffae treewide: set meta.license on hooks 2026-05-25 13:48:20 +02:00
Aliaksandr fbfe0b610e rust/hooks: replace // optionalAttrs with nullable attr names
Convert 5 identical optionalAttrs (stdenv.isLinux) for testCross
in cargo{Build,Check,Install,Nextest,Setup}Hook passthru.tests.
2026-05-18 03:54:27 +03:00
Ben Siraphob 869d641281 treewide: replace stdenv.is* with stdenv.hostPlatform.is* 2026-05-10 10:06:35 -07:00
Ben Siraphob 5fadc89f02 treewide: remove empty buildInputs assignments 2026-04-20 10:34:26 -07:00
Bryce Berger 92a51cfbfe rust: hooks: avoid fancy progress reporting by nextest
In [0.9.109], nextest added a `--show-progress=running` mode that
shows an interactive view of currently running tests. This is nice in
interactive use. However, it's very annoying when viewing logs after the
fact, such as through `nix log ...`.

`--show-progress=none` reverts to the simple interface, reporting a
single line for each test run with no control codes.

[0.9.109]: https://github.com/nextest-rs/nextest/releases/tag/cargo-nextest-0.9.109
2026-02-24 07:13:35 -05:00
Martin Weinelt 3095fa5abd maturin: 1.9.6 -> 1.10.0
https://github.com/PyO3/maturin/blob/v1.10.0/Changelog.md
2025-11-25 12:39:07 -08:00
Yueh-Shun Li cdb722fa01 rustPlatform: run cargoDepsHook inside cargoSetupPostUnpackHook 2025-11-04 05:25:30 +08:00
dramforever d387841843 rust/hooks: Use rustcTargetSpec for --target
The --target option expects the target spec JSON, not the short target
name, so use rustcTargetSpec.
2025-08-13 16:13:10 +02:00
Wolfgang Walther 5a0711127c treewide: run nixfmt 1.0.0 2025-07-24 13:55:40 +02:00
dramforever e05a1a15ac cargoSetupHook: Force frame pointers for !isx86_32
We did this for C in #399014 but not Rust. Add
-Cforce-frame-pointers=yes to rustflags in cargoSetupHook
2025-07-02 21:10:48 +02:00
Wolfgang Walther 40c35a8a7a rustPlatform: remove useless callPackage from hooks
Also removes a few unused arguments.
2025-06-18 15:57:28 +02:00
Alyssa Ross 112d13c5c7 cargoSetupHook: fix setting crt-static
As is retrospectively obvious from the fact that it was looking at
pkgsTargetTarget, this should have been in the section for the target,
not the host.  This fixes e.g. pkgsStatic.xq for 64-bit RISC-V.

(For platforms other than 64-bit RISC-V this being wrong was masked by
the musl Rust targets being static by default.)
2025-05-13 19:00:42 +02:00
Daniel Barter 009de0667f rust: re add setEnv to cargo build hooks 2025-03-11 12:17:51 +01:00
Peder Bergebakken Sundt 5aba99242e treewide: fix typos in comments
Made with

```shell
git restore .
fd '\.nix$' pkgs/ --type f -j1 -x bash -xc "$(cat <<"EOF"
    typos --no-check-filenames --write-changes "$1"
    git diff --exit-code "$1" && exit
    #( git diff "$1" | grep -qE "^\+ +[^# ]") && git restore "$1"
    count1="$( bat --language nix --diff --style changes "$1" --theme "Monokai Extended" --color always | aha --no-header | grep -E '^<span style="color:olive;">~</span> ' | wc -l )"
    count2="$( bat --language nix --diff --style changes "$1" --theme "Monokai Extended" --color always | aha --no-header | grep -E '^<span style="color:olive;">~</span> (<span style="color:#f8f8f2;"> *</span>)?<span style="color:#75715e;">.*</span>$' | wc -l )"
    [[ $count1 -ne $count2 ]] && git restore "$1"
EOF
)" -- {}
```

and filtered with `GIT_DIFF_OPTS='--unified=15' git -c interactive.singleKey=true add --patch`

I initially tried using the tree-sitter cli, python bindings and even ast-grep through various means, but this is what I ended up with.
2025-02-24 10:44:41 +01:00
K900 1446a9364a cargoCheckHook: pass test-threads using an environment variable (#368699) 2025-02-09 09:28:17 +03:00
DavHau 154f977ac6 rust/hooks: move tests to hooks and add to passthru 2025-02-04 12:02:15 +07:00
DavHau db4cf6cb1d rust: fix splicing for rust hooks
This fixes a long standing issue where rust hooks behave differently when used inside buildRustPackage vs inside mkDerivation, which lead to surprising behavior, like for example the package being built for the wrong paltform or the linker not being found especially in cross compilation scenarios.

The reason for this inconsitency was, that buildRustPackage consumed the hooks in a non-spliced form, via [this inherit statement](https://github.com/NixOS/nixpkgs/blob/4506ece030a0c82d078edd0360ea8af6b4d94035/pkgs/development/compilers/rust/make-rust-platform.nix#L60), and therefore the usual platform shift on the hooks introduced by putting them in `nativeBuildInputs` was not applied here.

Thoug whenever the hook was used inside other builders like `mkDerivation` the platform shift did apply correctly as the hook was consumed via the spliced package set, introducing the inconsitecy.

Because of the wrong (non-spliced) use in buildRustPackage, most rust hooks have been designed with the wrong build/host/target shift in mind which is fixed by this change.

Due to the inconsitent behavior between different builders, workarounds like `rust.envVars`, which were previously introduced, likely become obsolete by this change.

This likely fixes a bunch of cross compilation issues for rust packages that are not based on `buildRustPackage` but instead consume the hooks directly.

Done:
- ensure that `buildRustPackage` consumes spliced hooks by using makeScopeWithSplicing' in make-rust-platform.nix.
- refactor hooks to make them refer to correct build/host/target packages.
- remove `rust.envVars` workaround from all rust hooks
- implement tests for most rust hooks in /pkgs/test/rut-hooks

The newly added tests can be executed for native as well as cross compilation via:
```
nix-build -A tests.rust-hooks -A pkgsCross.riscv64.tests.rust-hooks
```
2025-01-31 18:18:46 +07:00
DavHau 4e54dd0c51 format pkgs/build-support/rust/hooks/default.nix 2025-01-31 18:18:46 +07:00
misuzu 5e06bab187 rustPlatform.maturinBuildHook: specify the interpreter name
Required for cross-compilation.
2024-12-29 18:29:18 +02:00
lucasew f9a85f2330 cargoCheckHook: pass test-threads using an environment variable
Signed-off-by: lucasew <lucas59356@gmail.com>
2024-12-27 22:37:02 -03:00
Reno Dakota 52bf1163fa treewide: use getLib when accessing clang / libclang / stdenv.cc.cc
In preparation to eliminate the lib output for the unwrapped clang, use
`lib.getLib` to access the `lib` output.
2024-11-07 10:27:41 +00:00
github-actions[bot] 275a4ece4b Merge master into staging-next 2024-10-18 00:14:07 +00:00
Julius Michaelis 88c41d8a91 Revert "rust: allow linker to be different from compiler"
This reverts commit b8076b893e.
2024-10-17 22:17:39 +02:00
binarycat 683f97e378 rustPlatform: cargo test is now called with the same environment variables as cargo build
this means that cargo dependancies will no longer be built twice.
2024-10-09 09:42:38 +02:00
Jörg Thalheim a50dabe867 rustPlatform.buildRustPackage: provide debug symbols on darwin (#327136) 2024-09-21 18:30:46 +02:00
Manuel Mendez 17b3df2861 rust: Write to .cargo/config.toml instead of .cargo/config
Seeing the following new warnings pop up on stderr when cargo was bumped
to 1.78:

```
warning: `/build/.cargo/config` is deprecated in favor of `config.toml`
note: if you need to support cargo 1.38 or earlier, you can symlink `config` to `config.toml`
```

which happens to break commitmsgfmt builds in nix (#320294).

closes #320294
2024-09-11 21:03:32 -04:00
Wolfgang Walther d5013e942e rust: shellcheck setup hooks 2024-09-10 08:32:33 +02:00
Wolfgang Walther 9220a19a4d rust: support structuredAttrs in setup hooks
Tested the following packages with and without structuredAttrs:
- rust-analyzer: cargo-build-hook, cargo-check-hook
- jujutsu: cargo-nextest-hook
- kornia-rs: maturin-build-hook
2024-09-10 08:32:33 +02:00
Nathan Henrie 16f3c7ebcb rustPlatform.buildRustPackage: provide debug symbols on darwin
Currently we cannot debug rust binaries on darwin (via lldb).

The debug symbols seem to be provided by default in a number of files
in `target/debug/deps/*.rcgu.o`. As far as I can tell these have
hardcoded paths referring to the ephemeral build directory. However,
`split-debuginfo=packed` conveniently produces a `.dSYM` file that can
be copied to `$out/bin/` and immediately provide debugging information.

Fixes https://github.com/NixOS/nixpkgs/issues/262131
2024-08-23 13:07:16 -06:00
Winter 2de1fd60fc Revert "rust: Write config.toml not config" 2024-06-23 12:32:29 -04:00
Manuel Mendez 3f7663c1d7 rust: Write to .cargo/config.toml instead of .cargo/config
Seeing the following new warnings pop up on stderr when cargo was bumped
to 1.78:

```
warning: `/build/.cargo/config` is deprecated in favor of `config.toml`
note: if you need to support cargo 1.38 or earlier, you can symlink `config` to `config.toml`
```

which happens to break commitmsgfmt builds in nix (#320294).

closes #320294
2024-06-19 14:56:51 -04:00
Dmitry Kalinkin fbd21c5067 rustPlatform.maturinBuildHook: specify the output directory (#291025) 2024-06-13 19:21:52 -04:00
Alyssa Ross 4816a73bb5 rustPlatform: --frozen -> --offline
--frozen is stricter than we need in Nixpkgs.  If a Cargo.lock is
slightly wrong, or (in my use case) if building a subproject that is
not a member of the top-level workspace, but the correct Cargo.lock
can be entirely resolved from the existing top-level Cargo.lock, it
should be deterministic, and shouldn't cause any problems, to let
cargo generate the new Cargo.lock.  This should result in less need to
bother upstreams about fixing their Cargo.lock files in cases where
they could have been automatically fixed.
2024-05-18 11:18:59 +02:00
Janne Heß 6486868c28 cargoBuildHook: Fix features with __structuredAttrs 2024-04-10 09:24:17 +02:00
Yureka b8076b893e rust: allow linker to be different from compiler 2024-01-15 22:34:02 +01:00
Martin Weinelt 8742c0cd2e maturinBuildHook: use dist dir relative to cargoRoot
With `cargoRoot` set to a subdirectory of the source, where the
Cargo.{lock,toml} are found, the final mv would previously fail, since
the build results appear relative to cargoRoot, not to the original
build directory.
2023-12-20 20:46:08 +01:00
Alyssa Ross e3e57b8f18 lib.systems: elaborate Rust metadata
We need this stuff to be available in lib so make-derivation.nix can
access it to construct the Meson cross file.

This has a couple of other advantages:

 - It makes Rust less special.  Now figuring out what Rust calls a
   platform is the same as figuring out what Linux or QEMU call it.

 - We can unify the schema used to define Rust targets, and the schema
   used to access those values later.  Just like you can set "config"
   or "system" in a platform definition, and then access those same
   keys on the elaborated platform, you can now set "rustcTarget" in
   your crossSystem, and then access "stdenv.hostPlatform.rustcTarget"
   in your code.

"rustcTarget", "rustcTargetSpec", "cargoShortTarget", and
"cargoEnvVarTarget" have the "rustc" and "cargo" prefixes because
these are not exposed to code by the compiler, and are not
standardized.  The arch/os/etc. variables are all named to match the
forms in the Rust target spec JSON.

The new rust.target-family only takes a list, since we don't need to
worry about backwards compatibility when that name is used.

The old APIs are all still functional with no warning for now, so that
it's possible for external code to use a single API on both 23.05 and
23.11.  We can introduce the warnings once 23.05 is EOL, and make them
hard errors when 23.11 is EOL.
2023-11-09 10:02:24 +01:00
Alyssa Ross 7262026f0f rustPlatform.cargoSetupHook: fix platform check (#260068)
Cargo will never need to link for the target platform — that'd be for
the package being built to do at runtime.  Cargo should know about the
build and host linkers.

This fixes e.g. pkgsCross.musl64.fd from x86_64-linux.

Fixes: 67a4f828b4 ("rust: hooks: fix cross compilation")
2023-10-11 23:36:56 +02:00
Alyssa Ross 1cbe5c3e8b rust.toRustTargetForUseInEnvVars: support custom targets
> If using a target spec JSON file, the <triple> value is the filename
> stem. For example --target foo/bar.json would match [target.bar].

- https://doc.rust-lang.org/cargo/reference/config.html#target

I've also exposed toRustTargetSpecShort as a public function, because
it's useful to be able to know what the target subdirectory will be.
2023-10-03 12:30:04 +00:00
figsoda 14d44173e4 Merge pull request #256949 from andresilva/build-rust-package-profiles
buildRustPackage: support custom cargo profiles
2023-09-29 15:54:49 -04:00
Adam Joseph 67a4f828b4 rust: hooks: fix cross compilation
Currently there is a state of severe confusion in
pkgs/build-support/rust/hooks/ regarding host vs target; right now
there is only "host" defined, but whether it means "host" or
"target" seems to fluctuate.

This commit corrects that, ensuring that all variables come in all
three flavors (build, host, target) and are used consistently with
the nixpkgs convention.

This also fixes the cross-compilation of packages which use
`maturinBuildHook` -- hooks go in `nativeBuildInputs` and are
phase-shifted backwards by one platform, so they need to be careful
about distinguishing between build and host.

Closes #247441
2023-09-26 06:30:44 +00:00
André Silva 8674922276 buildRustPackage: support custom cargo profiles 2023-09-24 18:40:24 +01:00
David Arnold b3d2765271 rustPlatform.maturinBuildHook: fix postBuild hook to use ./dist contract 2023-07-16 16:09:37 -05:00
Alyssa Ross f5d8384094 rustPlatform.cargoBuildHook: don't let cargo strip
This fixes debug info of cloud-hypervisor, which recently added
strip = true to its release profile in Cargo.toml.
2023-06-20 09:51:42 +00:00
Alyssa Ross 470e6130b3 rust: fix overriding rust flags on musl
If RUSTFLAGS is set in the environment, Cargo will ignore rustflags
settings in its TOML configuration.  So setting RUSTFLAGS=-g (like
separateDebugInfo does) to generate debug info breaks
dynamically-linked Rust packages on musl.  This breakage is visible
for any packages that call into C dynamic libraries.  If the binary is
linked directly to a C dynamic library, it will fail to build, and if
it depends on a Rust library which links a C dynamic library, it will
segfault at runtime when it tries to call a function from the C
library.  I noticed this because pkgsMusl.crosvm is broken for this
reason, since it sets separateDebugInfo = true.

It shouldn't be possible to end up with broken binaries just by using
RUSTFLAGS to do something innocuous like enable debug info, so I think
that, even though we liked the approach of modiyfing .cargo/config
better at the time, it's become clear that it's too brittle, and we
should bite the bullet and patch the compiler instead when targetting
musl.  It does not appear to be necessary to modify the compiler at
all when cross-compiling /from/ dynamically-linked Musl to another
target, so I'm only checking whether the target system is
dynamically-linked Musl when deciding whether to make the modification
to the compiler.

This reverts commit c2eaaae50d
("cargoSetupHook: pass host config flags"), and implements the
compiler patching approach instead.
2023-03-16 02:29:46 +00:00
figsoda eedbf71d0d Merge pull request #218472 from figsoda/cargo-setup 2023-03-03 19:40:55 -05:00
Weijia Wang 39a2b0b3bf rust: remove aarch64-linux workaround
This commit reverts #209113, since aarch64-linux now uses GCC 12 by default.
2023-03-01 18:42:07 +02:00
figsoda 3e18607be3 rustPlatform.cargoSetupHook: dereference symlinks in cargoDeps
unpackFile doesn't dereference symlinks if cargoDeps is a directory, and
some cargo builds run into permission issues because the files the
symlinks point to are not writable.
2023-02-26 11:55:50 -05:00
Bob van der Linden e3a10a12c7 rustPlatform.cargoSetupHook: improve cargoHash instructions
Currently cargo-setup-hook instructs the builder upon cargoSha256 or
cargoHash being out-of-date compared to the Cargo.lock file.

The instructions can be simplified a bit, because nowadays it is fine to
keep a hash empty, instead of filling it with
`0000000000000000000000000000000000000000000000000000`.

Nix nowadays outputs SRI hashes, which should usually be placed in
`cargoHash` instead of `cargoSha256`, but the instructions are still
only referring to `cargoSha256`.

Lastly, the output of Nix doesn't include `got: sha256: ` anymore, as it
now outputs `got: sha256-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX=`.
It would be nice to make it clear that the trailing `=` is important as
well, so the full example SRI hash is mentioned.
2023-02-14 16:14:08 -05:00