Skip to content

Unwanted boilerplate: finalAttrs.finalPackage.doCheck #272978

@pbsds

Description

@pbsds

Currently much cross logic is not included in the finalAttrs fixed point, meaning that if you use finalAttrs.doCheck internally in your derivation then this does not automatically account for whether stdenv.buildPlatform.canExecute stdenv.hostPlatform.

The current most elegant fix is to inject finalPackage into the expression, but this is not documented.

Example

For example, consider:

cmakeFlags = [
"-DSIDX_BUILD_TESTS=${if finalAttrs.doCheck then "ON" else "OFF"}"
];

nix-repl> libspatialindex.cmakeFlags
[ "-DSIDX_BUILD_TESTS=ON" ]

nix-repl> pkgsCross.aarch64-multiplatform.libspatialindex.cmakeFlags
[ "-DSIDX_BUILD_TESTS=ON" "-DCMAKE_SYSTEM_NAME=Linux" "-DCMAKE_SYSTEM_PROCESSOR=aarch64" "-DCMAKE_HOST_SYSTEM_NAME=Linux" "-DCMAKE_HOST_SYSTEM_PROCESSOR=x86_64" ]

The fix is to inject finalPackage:

cmakeFlags = [
"-DSIDX_BUILD_TESTS=${if finalAttrs.finalPackage.doCheck then "ON" else "OFF"}"
];

which will get us

nix-repl> libspatialindex.cmakeFlags
[ "-DSIDX_BUILD_TESTS=ON" ]

nix-repl> pkgsCross.aarch64-multiplatform.libspatialindex.cmakeFlags
[ "-DSIDX_BUILD_TESTS=OFF" "-DCMAKE_SYSTEM_NAME=Linux" "-DCMAKE_SYSTEM_PROCESSOR=aarch64" "-DCMAKE_HOST_SYSTEM_NAME=Linux" "-DCMAKE_HOST_SYSTEM_PROCESSOR=x86_64" ]

The current recommendation in the docs (added in 2017) is to hoist the cross logic up into the derivation:

#### My package’s testsuite needs to run host platform code. {#cross-testsuite-runs-host-code}
Add the following to your `mkDerivation` invocation.
```nix
doCheck = stdenv.buildPlatform.canExecute stdenv.hostPlatform;
```

Tree-wide fixes are simple to apply, but are ultimately a bodge. Hence this issue

Related: #266485


Add a 👍 reaction to issues you find important.

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.status: stalehttps://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md6.topic: cross-compilationBuilding packages on a different platform than they will be used on6.topic: stdenvStandard environment9.needs: documentationThis needs to be documented well.
    No fields configured for issues without a type.

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions