Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ let
importJSON importTOML warn warnIf warnIfNot throwIf throwIfNot checkListOfEnum
info showWarnings nixpkgsVersion version isInOldestRelease
mod compare splitByAndCompare
functionArgs setFunctionArgs isFunction toFunction
functionArgs setFunctionArgs isFunction toFunction lazyFunction
toHexString toBaseDigits inPureEvalMode;
inherit (self.fixedPoints) fix fix' converge extends composeExtensions
composeManyExtensions makeExtensible makeExtensibleWithCustomName;
Expand Down
2 changes: 1 addition & 1 deletion lib/systems/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ rec {
emulator = pkgs:
if (final.emulatorAvailable pkgs)
then selectEmulator pkgs
else throw "Don't know how to run ${final.config} executables.";
else throw "Don't know how to run ${final.config} executables on ${pkgs.stdenv.hostPlatform.system}.";

}) // mapAttrs (n: v: v final.parsed) inspect.predicates
// mapAttrs (n: v: v final.gcc.arch or "default") architectures.predicates
Expand Down
27 changes: 27 additions & 0 deletions lib/trivial.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{ lib }:

let
inherit (lib) mapAttrs functionArgs setFunctionArgs;
in

rec {

## Simple (higher order) functions
Expand Down Expand Up @@ -467,6 +471,29 @@ rec {
then v
else k: v;

/*
Make an attribute-set matching function such as `{ a, b }: b` delay
its evaluation of the attribute set in the argument.

This is not compatible with ellipsis patterns, `{ ... }:`. It removes the
ability to do `args@{ a, ... }: args.b`, although this limitation should
not be relied on.

Example:

lib.fix (lib.lazyFunction ({ a, b }: { a = b; b = 1; }))
*/
lazyFunction = f:
setFunctionArgs
(args:
f
(mapAttrs
(k: _: args.${k})
(functionArgs f)
)
)
(functionArgs f) ;

/* Convert the given positive integer to a string of its hexadecimal
representation. For example:

Expand Down
4 changes: 3 additions & 1 deletion nixos/lib/testing-python.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{ system
, pkgs ? import ../.. { inherit system config; }
, hostPkgs ? pkgs
# Use a minimal kernel?
, minimal ? false
# Ignored
Expand All @@ -25,7 +26,8 @@ rec {

extraTestModule = {
config = {
hostPkgs = pkgs;
_module.args.pkgs = pkgs;
inherit hostPkgs;
};
};

Expand Down
9 changes: 5 additions & 4 deletions nixos/lib/testing/nodes.nix
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
testModuleArgs@{ config, lib, hostPkgs, nodes, ... }:
testModuleArgs@{ config, lib, hostPkgs, pkgs, nodes, ... }:

let
inherit (lib) mkOption mkForce optional types mapAttrs mkDefault mdDoc;

system = hostPkgs.stdenv.hostPlatform.system;

baseOS =
import ../eval-config.nix {
inherit system;
inherit (config.node) specialArgs;
modules = [ config.defaults ];
baseModules = (import ../../modules/module-list.nix) ++
Expand All @@ -17,10 +14,14 @@ let
({ config, ... }:
{
virtualisation.qemu.package = testModuleArgs.config.qemu.package;
virtualisation.host.pkgs = hostPkgs;

# Ensure we do not use aliases. Ideally this is only set
# when the test framework is used by Nixpkgs NixOS tests.
nixpkgs.config.allowAliases = false;
nixpkgs = {
inherit (pkgs.stdenv) hostPlatform buildPlatform;
};
})
testModuleArgs.config.extraBaseModules
] ++ optional config.minimal ../../modules/testing/minimal-kernel.nix;
Expand Down
2 changes: 1 addition & 1 deletion nixos/lib/testing/pkgs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
config = {
# default pkgs for use in VMs
_module.args.pkgs = hostPkgs;
_module.args.pkgs = lib.mkDefault hostPkgs;

defaults = {
# TODO: a module to set a shared pkgs, if options.nixpkgs.* is untouched by user (highestPrio) */
Expand Down
9 changes: 8 additions & 1 deletion nixos/lib/testing/run.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
{ config, hostPkgs, lib, ... }:
let
inherit (lib) types mkOption mdDoc;

vmHostPlatform = hostPkgs.stdenv.buildPlatform;

virtualisationFeature =
if vmHostPlatform.isLinux then [ "kvm" ]
else if vmHostPlatform.isDarwin then [ "hvf" ]
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need additional flag for this. hvf is always available on Darwin, I don't think there's any hardware platform left that doesn't support it.
On the other hand, it would be good to allow to run tests even in plain emulation. It will be slow, but will still allow to test locally.

Copy link
Member Author

Choose a reason for hiding this comment

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

I know some people virtualize macOS on apple hardware in order to make their build farm setup reproducible.
It seems unlikely that nested virtualization would be available on such VMs.

else throw "Don't know how to require virtualisation on platform ${vmHostPlatform.system}";
in
{
options = {
Expand Down Expand Up @@ -33,7 +40,7 @@ in
derivation = hostPkgs.stdenv.mkDerivation {
name = "vm-test-run-${config.name}";

requiredSystemFeatures = [ "kvm" "nixos-test" ];
requiredSystemFeatures = virtualisationFeature ++ [ "nixos-test" ];

buildCommand = ''
mkdir -p $out
Expand Down
7 changes: 4 additions & 3 deletions nixos/tests/all-tests.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{ system,
pkgs,
hostPkgs ? pkgs,

# Projects the test configuration into a the desired value; usually
# the test runner: `config: config.test`.
Expand Down Expand Up @@ -28,10 +29,10 @@ let
# in that file, which are then forwarded to the test definition
# following the `import make-test-python.nix` expression
# (if it is a function).
discoverTests (val { inherit system pkgs; })
discoverTests (val { inherit system pkgs hostPkgs; })
else val;
handleTest = path: args:
discoverTests (import path ({ inherit system pkgs; } // args));
discoverTests (lazyFunction (import path) ({ inherit system pkgs hostPkgs; } // args));
Copy link
Member Author

Choose a reason for hiding this comment

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

This silently ignores make-test-python.nix-based runners that don't pass hostPkgs through, which has the effect of running the test on a linux remote builder.

handleTestOn = systems: path: args:
if elem system systems then handleTest path args
else {};
Expand All @@ -45,7 +46,7 @@ let

inherit
(rec {
doRunTest = arg: ((import ../lib/testing-python.nix { inherit system pkgs; }).evalTest {
doRunTest = arg: ((import ../lib/testing-python.nix { inherit system pkgs hostPkgs; }).evalTest {
imports = [ arg ];
}).config.result;
findTests = tree:
Expand Down
3 changes: 2 additions & 1 deletion nixos/tests/make-test-python.nix
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
f: {
system ? builtins.currentSystem,
pkgs ? import ../.. { inherit system; },
hostPkgs ? pkgs,
...
} @ args:

with import ../lib/testing-python.nix { inherit system pkgs; };
with import ../lib/testing-python.nix { inherit system pkgs hostPkgs; };

let testConfig = makeTest (if pkgs.lib.isFunction f then f (args // { inherit pkgs; inherit (pkgs) lib; }) else f);
in testConfig.test # For nix-build
Expand Down
29 changes: 24 additions & 5 deletions pkgs/top-level/all-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,35 @@ with pkgs;
### Push NixOS tests inside the fixed point

# See also allTestsForSystem in nixos/release.nix
nixosTests = import ../../nixos/tests/all-tests.nix {
inherit pkgs;
system = stdenv.hostPlatform.system;
nixosTests = let
systemArgs =
if stdenv.hostPlatform.isLinux && (stdenv.buildPlatform.isLinux || stdenv.buildPlatform.isDarwin)
then {
hostPkgs = pkgs.buildPackages;
pkgs = pkgs;
system = stdenv.hostPlatform.system;
}
else if stdenv.hostPlatform.isDarwin && stdenv.buildPlatform.isDarwin
then
# In the context of a normal darwin compile, repurpose nixosTests as a
# non-cross build. This requires a "remote" builder such as a local
# Linux VM, but all VM hosting happens on darwin.
let linuxSystem = "${stdenv.buildPlatform.parsed.cpu.name}-linux";
in {
hostPkgs = pkgs;
pkgs = import ../.. { inherit config overlays; system = linuxSystem; };
system = linuxSystem;
}
else throw "Don't know how to configure NixOS for VM host ${stdenv.buildPlatform.system} and/or VM guest ${stdenv.hostPlatform.system} built on ${stdenv.buildPlatform.system}";
Comment on lines +143 to +161
Copy link
Member Author

Choose a reason for hiding this comment

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

This logic is useful in other places, so it should be factored or moved to a common location, such as the nixos/lib/testing/pkgs.nix module.

Copy link
Member Author

Choose a reason for hiding this comment

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

How about pkgsLinux (in pkgs)

in
import ../../nixos/tests/all-tests.nix {
inherit (systemArgs) hostPkgs pkgs system;
callTest = config: config.test;
} // {
# for typechecking of the scripts and evaluation of
# the nodes, without running VMs.
allDrivers = import ../../nixos/tests/all-tests.nix {
inherit pkgs;
system = stdenv.hostPlatform.system;
inherit (systemArgs) hostPkgs pkgs system;
callTest = config: config.test.driver;
};
};
Expand Down