Repository navigation
Description
Issue description
While convenient, pkgs.nixosTests has surprising behavior. For example, this should fail, but it doesn't:
(import ./. { overlays = [ (self: super: { nginxStable = null; }) ]; }).nixosTests.nginx
So what is wrong here, is that the NixOS tests do not use the pkgs that they are passed. The NixOS tests assume that they can call Nixpkgs however they want. Although not a requirement for all tests, this means that in general the tests must reinvoke Nixpkgs, thus ignoring the Nixpkgs where .nixosTest was defined.
The documentation doesn't explain this ("Push NixOS tests inside the fixed point", which is misleading at best) and the implementation of nixosTests looks just fine, passing pkgs along as one would expect when reusing Nixpkgs for some tests. However, that is not what happens. pkgs will only be used to get qemu, lib, probably perl and perhaps some other things.
Why this is a problem: someone will use it to test whether NixOS, as built with their company overlay, still works. They will get false positives.
Steps to reproduce
nix-build --expr '(import ./. { overlays = [ (self: super: { nginxStable = null; }) ]; }).nixosTests.nginx'
Watch it succeed. It shouldn't.
Solutions
Ideally, we would pass pkgs to the NixOS machines. This can be done for some tests, but not for all the NixOS tests, because many depend on config.nixpkgs.config, which is unsupported by config.nixpkgs.pkgs (which is how you inject an existing Nixpkgs invocation).
So I don't see a way to make this work as one might predict. I suggest deleting pkgs.nixosTests.
@Ekleog do we really need this? It seems like just a convenience attribute.
Activity
Ekleog commentedon Nov 13, 2018
@roberth This, theoretically, should have been solved in the refactoring at #50233. It looks like it didn't actually.
The issue is that it is kind of required for usage in
meta.tests(see this comment).I must say I am not sure what
config.nixpkgs.configexactly does, but there is at least a place where thenginxmodule still has an import tonixpkgsthat I missed in the above refactoring. I'll try to have a look at how and why it's there!roberth commentedon Nov 13, 2018
This is where NixOS gets its Nixpkgs, unless
config.nixpkgs.pkgsis set. That's the code path for how everyone has been building NixOS, although that can change (but probably hasn't) since about a month ago. So that doesn't seem to be something we can change.I've had another look at the actual tests that would break if we set
config.nixpkgs.pkgs. We seem to have the following situations:config.nixpkgs.config.packageOverrides. These can be replaced byoverlayssince NixOS: use overlays when nixpkgs.pkgs is set #49256.config.nixpkgs.localSystem. This is needs to be taken frompkgsanyway. See nixpkgs.{system,localSystem,crossSystem} *values* are useless #49765.config.nixpkgs.allowUnfreeandconfig.nixpkgs.allowUnfreePredicate. I'm not sure whether doing this onhydra.nixos.orgis even ok with policy. This only applies toquake3.nixandvirtualbox.nix.(1) and (2) can be fixed easily. (3) is a bit trickier. What I'd like to do is make sure that
config.nixpkgs.configis not set whenconfig.nixpkgs.pkgsis set, by means ofassertions. This will break the (3) tests, but only when invoked via Nixpkgs instead of via NixOS, so that seems ok to me. I don't suppose hydra will buildsomepkgs.teststhrough Nixpkgs, but only builds the tests when building NixOS. Is that correct? If not, we can make an exception for quake3 and virtualbox.Regarding the implementation, we can:
a. Make
meta.testsreturn only paths to tests. This puts the burden on the users, which solves the 'leaky abstraction' by removing the leakiness. Doesn't seem necessary anymore, but is an option.b. Make
meta.testsusepkgs.nixosTest. This should reduce some duplication. Ifmeta.testsrequirements turn out to differ from user-sidenixosTestrequirements, we should duplicate the function such thatmeta.testsuses the new variation.c. Make
meta.testssetconfig.nixpkgs.pkgsby itself, right away, not usingpkgs.nixosTest.I'm in favor of (b).
Appendix:
grep 'nixpkgs\.'innixos/testsEkleog commentedon Nov 13, 2018
Thank you for your investigation!
allowUnfreein testsFor (3), I'd think neither hydra nor ofborg should build them. Currently we're kind of OK given none of them defaults to building them, but it does mean that it's currently possible to trigger some ofborg builds for unfree stuff by relaying through NixOS tests (cc @LnL7 in the absence of Graham, even though I don't really think it's that urgent an issue as ofborg never redistributes build products anyway).
That said, this question raises the proper question of what license these tests should be considered as having. Maybe it'd make sense to have them set
meta.licensetounfreein this case? This would both allow them to stay inmeta.testsfor easy human test, and avoid that either ofborg or hydra build them.Now, if we want to do that, it will mean having a way to override
nixpkgswith a customallowUnfreePredicatebefore passing it to the test. I would hope it wouldn't be that hard to do, but… nixpkgs always finds a way to surprise me in this domain.Fixing tests
I must say I'm not sure exactly what your point (b) entails. If I understand correctly, it is basically a replacement for
make-test.nix, right? As such, it would require fixing all tests. This sounds like the best path forward to me too.Ideally it would even handle the
allowUnfreecase as proposed above by propagating a non-falseallowUnfreeto settingmeta.licensetounfreein the “test” derivation.Now, do I read correctly that this proposal is mostly changes to the tests themselves, and that it would fix
all-tests.nixby making it actually respect its expected interface, ie. use itspkgsargument as the package set to use instead of relying on re-importing it?nixosTestsIf my analysis above is correct, I think it's compatible with
nixosTests, which is basically just importing saidall-tests.nixwith the outerpkgsso that packages could easily depend on it for setting theirmeta.tests. As you didn't mentionnixosTestsin your latest message I'm not sure what your ideas for it currently are, sorry if that's already what you meant :)roberth commentedon Nov 13, 2018
So we can disable the 'unfree' tests, with the method of disabling depending on the outcome of your question what license these tests should be considered as having.
To run those tests manually, the user can simply pass
allowUnfreeinto the Nixpkgs invocation that produces thepkgsinpkgs.nixosTests.So (3) seems can be solved.
The solutions for fixing the actual machines in the tests are provided by (1), (2) and the paragraph above.
The test definitions themselves (such as
tests/nginx.nix) need to be changed slightly to allow injecting the test runner instead of always callingmake-test.nix.make-test.nixwill be the default, so you can justnix-buildthese tests.I don't expect any problems replacing
make-test.nixbypkgs.nixosTestafter fixing the machines, but I haven't tried it yet.Your analysis seems correct and what you suggest is roughly what needs to be done in
all-tests.nixand related files.Plans
I'm a little busy, but I'll try to make a PR for this soon.
If you feel like improving the "top-level" Nixpkgs and NixOS interfaces, have a look at #49765 in the meantime ;)
Future use in NixOS jobs?
(Skip this for now)
In theory, NixOS could reuse
pkgsinpkgs.nixosTestto achieve an evaluation speedup, but I'd rather try not to change the way NixOS callsall-tests.nixfor now. I also think we should have some tests that exercise the 'normal'modules/misc/nixpkgs.nixfunctionality, so perhapsall-tests.nixshould then have access to two runners: an efficient one and a 'standalone' one.LnL7 commentedon Nov 13, 2018
I didn't even know this was a thing, and I think
nix-build nixos/tests/virtualbox.nix --arg enableUnfree truemakes much more sense than passing an already imported package set. This way each test can define what parameters are available and what actually makes sense.As for ofborg, we could provide builds and tests for unfree software since no caches are redistributed. However there are some ideas on doing that for debugging purposes so for now it's best not to diverge too much from hydra in terms of what we build and how we evaluate IMHO.
Ekleog commentedon Nov 14, 2018
@roberth Sounds great, thank you!
About the way NixOS calls
all-tests.nix, as I splitall-tests.nixout ofrelease.nixonly a few days ago I think there's no better time than now to change it if it needs to :)But I also think that if
pkgs.nixosTestmeans that the tests will actually use theirpkgsargument without re-importingnixpkgs, then the evaluation-time gains should come in immediately: the refactor ofrelease.nixtoall-tests.nixalready included passing the same importednixpkgsas thepkgsargument (see commit message 83b27f6, though I was wrong in that it actually only went from includingnixpkgstwice per test to including it once per test plus twice globally). So the gain should come for free! Leading to actually includingnixpkgstwice globally, and not once more per test.@LnL7
I think we totally agree that
nix-build nixos/tests/virtualbox.nix --arg enableUnfree truemakes the most sense for manually running the tests. The question is more for ofborg (which currently runs approx.nix-build nixos/release.nix -A tests.virtualbox), hydra (same thing but viarelease-{small,combined}.nix) andmeta.tests, all of which “need to” take defaults and can't pass additional arguments.For all these, re-using the already-existing
nixpkgswould be a gain both in purity (nixpkgsimporting does quite some impure stuff, and even thoughrestrict-evalprevents most or all of it most users won't run local builds asrestrict-eval, so it impactsmeta.tests) and evaluation time (importingnixpkgswas ~3s on my computer last I checked, multiplied by the number of tests that need to be run).But this doesn't prevent keeping the dual-interface
all-tests.nix/ directnix-build, with tests taking in either apkgsargument or fetching it themselves with the arguments they get provided :)LnL7 commentedon Nov 14, 2018
I'm not familiar with how it works now. But not having the module system import nixpkgs means what you're testing is not what actually happens, so what are you testing at that point? That said I do agree evaluation time is important, given how expensive this has become over time.
Ekleog commentedon Nov 15, 2018
So the idea is: you need to test what actually happens, but with which
nixpkgs?The objective is:
nix-build nixos/tests/foo.nix, then it'll default to importing../...nix-build nixos/release.nix -A tests.foo, then it'll default to, innixos/release.nix, importing./..and using it intests.foo(and using the same import for all tests if you-A tests, even though that's likely to fail because lots of tests are broken).nix-build -E '(import <nixpkgs> { overlays = [ ./overlay.nix ]; }).foo.tests, it'll defer tonixosTests.foo(from the samenixpkgs), which will call the test with the same overlay'dnixpkgs.Before 83b27f6 there were two imports of
nixpkgsper test, independently from how they were invoked: one for the tools used bymake-test.nixand one for the test itself. After that commit, there's only thenixpkgsimport used by the test itself remaining. Once this issue will be fixed, there should remain nonixpkgsimport, except if nopkgsargument is passed to the test in which case it'll default to importing../...Does that sound good to you? :)
matthewbauer commentedon Feb 14, 2019
Why do we even have nixosTests to begin with? It seems like it violates the separation between nixos and nixpkgs. If you want to have a reference from nixpkgs to the nixos test, why not just use a string? So you could do:
The current architecture is misguided IMO. Nixpkgs shouldn't import NixOS at all.
Ekleog commentedon Feb 14, 2019
@matthewbauer I totally agree with you that Nixpkgs shouldn't need to import NixOS at all. But it's all we have until something like #36842 lands, to be able to have at least a way to link from packages to some tests that show they're non-broken.
The end-goal of
passthru.testsis to have ofborg automatically run them, and using strings for doing that would be a complete non-starter:passthru.testsis defined as a set of derivations that have as semantics that the test passes iff the derivation builds. These semantics are unrelated tonixosTestsand I'm pretty sure we definitely do not want these to be replaced by a list of strings that need to be imported instead, among other things because we may want to say “to testgcc, I want to buildcoreutils,firefoxandemacs” (to check a wide range of hacks that may not be supported by more recent gcc). We wouldn't want to give string paths to these derivations, would we? Some derivations don't even have files for themselves but a file that only defines a function that's later called inall-packages.nix.(Note: you can see an example of
passthru.testsbeing used withoutnixosTestsin pkgs/development/tools/build-managers/bazel/default.nix)And then
nixosTestscomes into play, because we just don't have any better way of specifying integration tests for packages for the time being: we need a way to propagate the few tests we actually have to nixpkgs so that they're automatically run by thepassthru.testsinfrastructure.Also, BTW, your example doesn't tie Nixpkgs less to NixOS: nix is a lazy language, and a string that refers to a path is not particularly less bad that an import of that path, so long as the import is not used… and if it's used, then the string would have been imported anyway, so it's just as much of an import.
TL;DR: I totally agree we shouldn't need to import nixos into nixpkgs, but it's the least bad option we've got to be able to automatically run tests for the time being.
roberth commentedon Feb 15, 2019
Closing in favor of #55798
Ericson2314 commentedon Apr 25, 2021
I forgot about
nixosTestsuntil just now. It's strikes me as layering violation, and I really don't like those. Can we map nixos tests to pkgs outsidepkgs/instead?roberth commentedon Apr 26, 2021
@Ericson2314 The relation "has integration test" is indeed the opposite of "tests" and "depends", regardless of which software we're discussing. As such, it seems fair to say that this complexity (lack of an architectural rule) is part of the domain ie intrinsic complexity.
So I don't think we have to give up on
passthru.tests, which is a nice interface that's probably easier to use than its decoupled alternative. Specifically, if you override a package, how would you be able to test it? I can't imagine an easier way than getting itstestsattribute.Fixing this problem is just a matter of having nixpkgs inject itself into the nixos tests and the packages injecting themselves into the test config. The latter can be done after #119942. It seems that I've assumed wrongly before about
config.nixpkgsusage in the tests.This is clearly not the case (anymore?). This can be done.