Best practice for cross-friendly bin functionality `passthru.tests` test case?

<pkg>.passthru.tests is the place for test cases for a package in the form of a derivation set, and some of them test the binary functionality of the package. In terms of dependency platforms, they are the reverse-nativeBuildInputs of this package.

For example, we may add a test case for pkgs.hello that tests the execution of

set -eu -o pipefail
HELLO_OUTPUT="$(hello)"
[[ "$HELLO_OUTPUT" == "Hello,world!" ]]

When buildPlatform == hostPlatform, it could be

{ lib
, stdenv
, runCommandLocal
, ...
}:
stdenv.mkDerivation (finalAttrs: {
  # ...
  passthru.tests.run = runCommandLocal "test-hello-run" {
    nativeBuildInputs = [ finalAttrs.finalPackage ];
  } ''
    set -eu -o pipefail
    HELLO_OUTPUT="$(hello)"
    [[ "$HELLO_OUTPUT" == "Hello,world!" ]]
  '';
})

How should it be when we consider the situation of cross compilation?

Don’t consider cross here. When buildPlatform != hostPlatform, regular build-time tests are also disabled.

Edit: More precisely, they’re disabled if the buildPlatform cannot execute the hostPlatform.

1 Like

When enabling binfmt is it running the tests?

Not by default. Whether one platform can execute another are inherit properties of the platform itself.

binfmt should in theory allow running any binary but it’s a giant hack. There are some extremely light usages of it in Nixpkgs for i.e. installing shell completions using the built binary but I have my reservations against even that.

1 Like