Skip to content

modules.nixosTest.setPackage: init#299542

Draft
roberth wants to merge 5 commits intoNixOS:masterfrom
hercules-ci:nixos-test-overriding
Draft

modules.nixosTest.setPackage: init#299542
roberth wants to merge 5 commits intoNixOS:masterfrom
hercules-ci:nixos-test-overriding

Conversation

@roberth
Copy link
Member

@roberth roberth commented Mar 27, 2024

Description of changes

Add overriding methods to the NixOS test package attrset.

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 24.05 Release Notes (or backporting 23.05 and 23.11 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@github-actions github-actions bot added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: documentation This PR adds or changes documentation 6.topic: testing Tooling for automated testing of packages and modules labels Mar 27, 2024
@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 8.has: package (new) This PR adds a new package labels Mar 27, 2024
@wegank wegank added the 2.status: merge conflict This PR has merge conflicts with the target branch label Apr 5, 2024
@roberth roberth force-pushed the nixos-test-overriding branch from 2d0af0b to c11f55b Compare June 20, 2024 13:40
@roberth roberth requested a review from RaitoBezarius June 20, 2024 13:49
@RaitoBezarius
Copy link
Member

Thanks for the ping, I will review this when I have some time.

@ofborg ofborg bot added 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. and removed 2.status: merge conflict This PR has merge conflicts with the target branch 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. labels Jun 20, 2024
@roberth roberth force-pushed the nixos-test-overriding branch from c11f55b to 9cc5158 Compare September 18, 2024 11:51
@roberth roberth force-pushed the nixos-test-overriding branch from cfd5632 to a60ee70 Compare September 18, 2024 15:52
@wegank wegank added the 2.status: merge conflict This PR has merge conflicts with the target branch label Nov 1, 2024
@roberth roberth force-pushed the nixos-test-overriding branch from a60ee70 to b183dc0 Compare May 28, 2025 11:30
@ofborg ofborg bot removed the 2.status: merge conflict This PR has merge conflicts with the target branch label May 28, 2025
@roberth roberth mentioned this pull request May 28, 2025
13 tasks
@github-actions github-actions bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. and removed 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. labels May 28, 2025
@github-actions github-actions bot added the 6.topic: flakes The experimental Nix feature label Jun 1, 2025
@roberth
Copy link
Member Author

roberth commented Jun 1, 2025

I've applied this to two tests and packages I maintain, which led me to develop the set-package.nix module for the test framework.
The new setPackage isn't as general as extendNixOS, but it is more guided and it takes care of the unsupported intersection of passthru tests, overriding and a non-Linux virtualization host, and does so in a mostly transparent way.

This has increased the diff size a fair bit, but most of the changes to the two packages are trivial.

The commit history also shows the non-set-package.nix solution which wouldn't support e.g. ghostunnel.tests.nixos on Darwin. That's not essential for review.


Tested happy flow with:

(set -x; for package in cassandra_4 ghostunnel; do for system in x86_64-linux aarch64-darwin; do nix-build -A $package.tests.nixos --argstr system $system; nix-build -A nixosTests.$package --argstr system $system; done; done)

Tested expected error with:

nix-repl> with import ./. { system = "aarch64-darwin"; }; (ghostunnel.overrideAttrs { name = "yolotunnel"; }).tests.nixos

<snip>

       … while evaluating the option `nodes.service.services.ghostunnel.package':

       … while evaluating definitions from `/home/user/h/nixpkgs/nixos/tests/ghostunnel.nix, via option setPackage.<function body>':

       error: It seems that you are trying to run a NixOS test which is part of a package that has been overridden.
       This can be done - just not through the package `tests` attribute, because this package only knows about its own overrides, not the ones that should be applied to the Linux variation of it.
       You can construct the correct test, e.g.:

           let
             # Let's assume you have package sets like this:
             pkgsForLinux = nixpkgs { system = ...; }; # e.g. aarch64-linux
             nativePkgs = nixpkgs { system = ...; }; # e.g. aarch64-darwin

             myOverrides = ...;

             # You just invoked something like myFoo.tests where
             #   myFoo = nativePkgs.foo.overrideAttrs myOverrides;
             # or it might have been:
             #   nativePkgs.nixosTests.foo.setPackage myFoo

             # Instead, create
             myFooForLinux = pkgsLinux.foo.overrideAttrs myOverrides;
           in
             # So that you can construct the correct test like this:
             nativePkgs.nixosTests.foo.setPackage myFooForLinux

       Or if you're defining the test file outside of Nixpkgs, it might look like this:

           (nativePkgs.testers.runNixOSTest ./foo-test.nix).setPackage myFooForLinux

@roberth roberth requested a review from a team June 22, 2025 20:01
Comment on lines 10 to 15
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could work well for the callPackageWith case, too.

Comment on lines 11 to 28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the name.. but I needed "override existing option" already as well, when prototyping a module-based system argument for #402298 (comment). Good thing.

Comment on lines 295 to 318
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this will probably also help with nixosTests.postgresql, which currently is a matrix of tests for each major version.

Maybe we can then just have nixosTests.postgresql be the tests for v17 (latest) and the other versions are tested via passthru only.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is 🚀

Comment on lines 38 to 43
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some back & forth between commits with formatting of ghostunnel here. Looking at the commit messages, I assume you don't want to squash this, so this should be cleaned up.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setPackage is odd to me. I would expect it to be called overridePackage, that's what it does, right? Aka, without setPackage I still get a test with the default package?

Although the whole thing... looks a lot like there is a test function which needs a package argument, which has a default value. On the nixpkgs-side, that's just .override { package = finalAttrs.finalPackage; }. I see similarity to what .callPackage does, here. But callPackage does it for many packages, more generalized.

Can this be generalized, too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests aren't callPackage-based anymore, to simplify two "dependency injectors" into just one, which is the module system with its module arguments.

setPackage "takes" that general functionality and narrows it down to a single use case in a way that supports running the tests on macOS VM hosts. And it tries to do so in a developer friendly way, with a workaround for a common error scenario and a descriptive error when the combination of features (overriding and cross-platform passthru tests) is not supported.

So the easy answer is it doesn't need to be generalized. Contributors can use module arguments or options directly.
OTOH yes, it could be generalized to support multiple packages at once.

@nixpkgs-ci nixpkgs-ci bot added 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md and removed 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md labels Jun 25, 2025
@roberth
Copy link
Member Author

roberth commented Jul 1, 2025

I'm not so jsure that the way we run NixOS tests on VM hosts like darwin is the right way.
The unpleasant workaround to partially recover the interaction with overriding is proof of that.

Perhaps the nixosTests should not be available on non-NixOS platforms, and instead, macOS users should call a (TBD) attribute like (import ./. { system = "aarch64-linux"; }).nixosTests.acme.onVMHost.aarch64-darwin.
In other words, the test framework would be responsible for onVMHost.
By "composing" the various choices this way, we don't need to magically translate Darwin packages to Linux packages. All the overriding happens in the Linux world, and the alternate virtualizer is added last, as nature intended.

Should I remove the setPackage stuff for now? extendNixOS or a simpler setPackage would get the job done within the outlined alternate framework, at the cost of NixOS development on Darwin having a more different developer experience.
I do lean that way, because we can only stack so much complexity on top of unnecessary complexity.

@wolfgangwalther
Copy link
Contributor

I don't claim to understand the details and implications of this, but I think one important property for nixpkgs-review is to be able to build the same nixosTests.<...> attributes on all 4 systems the same way. Otherwise, that part would become annoying, quickly. Also <pkg>.passthru.tests should work the same for all 4.

@roberth
Copy link
Member Author

roberth commented Jul 1, 2025

Right. At least setPackage tries its best for all that, but regardless of this PR, we'll have to accept that either

  • pkg.tests.nixos is a bit of a lie and doesn't support (pkg.override* foo).tests.nixos at all on non-Linux platforms,
  • or all packages need to only add NixOS tests in an // optionalAttrs hostPlatform.isLinux

The latter is more correct, manages expectations better, doesn't lead to subtle impossibilities, and keeps our "infra code" simpler, but it does burden the package authors and the nixosTests.<...> and pkg.tests users a bit.

@roberth roberth changed the title nixos.runTest: Add extend, overrideTestDerivation nixos.runTest: Add extend, overrideTestDerivation (and more) Jul 1, 2025
@roberth
Copy link
Member Author

roberth commented Jul 1, 2025

I've split out the first part into

@wolfgangwalther
Copy link
Contributor

Right. At least setPackage tries its best for all that, but regardless of this PR, we'll have to accept that either

  • pkg.tests.nixos is a bit of a lie and doesn't support (pkg.override* foo).tests.nixos at all on non-Linux platforms,

  • or all packages need to only add NixOS tests in an // optionalAttrs hostPlatform.isLinux

The latter is more correct, manages expectations better, doesn't lead to subtle impossibilities, and keeps our "infra code" simpler, but it does burden the package authors and the nixosTests.<...> and pkg.tests users a bit.

OK, I'm starting to understand the problem space a bit more...

pkg.tests.nixos on darwin is not testing the darwin package. Thus, it shouldn't be there. Otherwise nixpkgs-review output is confusing. Not only is the (pkg.override* foot).tests.nixos a lie, but also the regular pkg.tests.nixos - it suggests that the darwin-build had been tested, but that's not true.

The whole point of enabling nixosTests on darwin is to allow nixpkgs developers on darwin to actually develop for NixOS - and run the tests for that. Correct?

Thus, nixosTests should work on darwin, but pkg.tests.nixos should not. That would be the best UX/DX.

all packages need to only add NixOS tests in an // optionalAttrs hostPlatform.isLinux

Can we maybe make this part of what's currently called setPackage?

Aka, turns this function into a "do everything that needs to be done to run this test via pkg.passthru.tests sensibly". Set the right package, return an empty attrset for darwin, .. maybe something else (in the future).

@roberth
Copy link
Member Author

roberth commented Jul 4, 2025

all packages need to only add NixOS tests in an // optionalAttrs hostPlatform.isLinux

Can we maybe make this part of what's currently called setPackage?

Unfortunately this optionalAttrs needs to go in passthru.tests. It's the part that would implement the idea "pkg.tests.nixos on darwin is not testing the darwin package. Thus, it shouldn't be there."

@wolfgangwalther
Copy link
Contributor

Unfortunately this optionalAttrs needs to go in passthru.tests. It's the part that would implement the idea "pkg.tests.nixos on darwin is not testing the darwin package. Thus, it shouldn't be there."

I don't understand why it needs to be in passthru.tests. passthru.tests needs to call .setPackage, so the conditional could sure also be in there? If .setPackage just returns an empty attrset, there will be an "empty passthru test", which is exactly what we want on darwin, right? What am I missing?

@nixpkgs-ci nixpkgs-ci bot added the 2.status: merge conflict This PR has merge conflicts with the target branch label Jul 5, 2025
roberth added 5 commits July 6, 2025 13:50
Tested with

    (ghostunnel.overrideAttrs { name = "yolotunnel"; }).tests.nixos

and

     nix-store -q --tree

Also it builds just fine, fwiw.
This file simplifies configuring the test with a specific package,
for those tests where overriding involves a single package.

This improves the cassandra test by recovering the passthru test
on Darwin.
@roberth roberth changed the title nixos.runTest: Add extend, overrideTestDerivation (and more) modules.nixosTest.setPackage: init Jul 6, 2025
@roberth roberth marked this pull request as draft July 6, 2025 11:55
@roberth roberth force-pushed the nixos-test-overriding branch from ed53fbb to ef1589d Compare July 6, 2025 11:55
@roberth
Copy link
Member Author

roberth commented Jul 6, 2025

Rebased onto merged #421480 and cassandra changes, by dropping the cassandra changes.
I'm not convinced that setPackage is the right solution at this point. See earlier comment.

@ofborg ofborg bot removed the 2.status: merge conflict This PR has merge conflicts with the target branch label Jul 6, 2025
@roberth
Copy link
Member Author

roberth commented Jul 7, 2025

Alternative direction:

(also independently useful, I'd say)

@wolfgangwalther
Copy link
Contributor

I'd say it's not really an alternative, both are orthogonal. I think we'd want both:

@nixpkgs-ci nixpkgs-ci bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jan 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md 6.topic: flakes The experimental Nix feature 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 6.topic: testing Tooling for automated testing of packages and modules 8.has: documentation This PR adds or changes documentation 8.has: package (new) This PR adds a new package 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants