From 0d1a3536000b845bf407910083286720fb310fec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kier=C3=A1n=20Meinhardt?= Date: Wed, 28 Jan 2026 10:44:09 +0100 Subject: [PATCH] nixos/test-driver: provide all machines from read-only allMachines option --- nixos/lib/testing/driver.nix | 10 +--------- nixos/lib/testing/network.nix | 19 +++++-------------- nixos/lib/testing/nodes.nix | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/nixos/lib/testing/driver.nix b/nixos/lib/testing/driver.nix index 8a3f40e7c44a..864fc9058a95 100644 --- a/nixos/lib/testing/driver.nix +++ b/nixos/lib/testing/driver.nix @@ -38,19 +38,11 @@ let vmMachineNames = map (c: c.system.name) (lib.attrValues config.nodes); containerMachineNames = map (c: c.system.name) (lib.attrValues config.containers); - allMachineNames = - let - overlappingNames = lib.intersectLists vmMachineNames containerMachineNames; - in - assert ( - lib.asserts.assertMsg (overlappingNames == [ ]) - "Names of QEMU VM nodes and systemd-nspawn containers must not overlap. Overlapping names: ${toString overlappingNames}" - ); - vmMachineNames ++ containerMachineNames; theOnlyMachine = let exactlyOneMachine = lib.length (lib.attrValues config.nodes) == 1; + allMachineNames = map (c: c.system.name) (lib.attrValues config.allMachines); in lib.optional (exactlyOneMachine && !lib.elem "machine" allMachineNames) "machine"; diff --git a/nixos/lib/testing/network.nix b/nixos/lib/testing/network.nix index 99edf16676ae..ff3c57412546 100644 --- a/nixos/lib/testing/network.nix +++ b/nixos/lib/testing/network.nix @@ -1,6 +1,4 @@ -{ - containers, - nodes, +testModuleArgs@{ lib, ... }: @@ -24,16 +22,9 @@ let zipLists ; - nodesAndContainers = - let - nodeNames = lib.attrNames nodes; - containerNames = lib.attrNames containers; - conflictingNames = lib.intersectLists nodeNames containerNames; - message = "`nodes` and `containers` must have unique names. Conflicting names: ${lib.concatStringsSep " " conflictingNames}"; - in - lib.throwIfNot (builtins.length conflictingNames == 0) message (nodes // containers); - - nodeNumbers = listToAttrs (zipListsWith nameValuePair (attrNames nodesAndContainers) (range 1 254)); + nodeNumbers = listToAttrs ( + zipListsWith nameValuePair (attrNames testModuleArgs.config.allMachines) (range 1 254) + ); networkModule = { config, ... }: @@ -93,7 +84,7 @@ let + optionalString ( config.networking.primaryIPv6Address != "" ) "${config.networking.primaryIPv6Address} ${hostnames}" - ) nodesAndContainers; + ) testModuleArgs.config.allMachines; }; in diff --git a/nixos/lib/testing/nodes.nix b/nixos/lib/testing/nodes.nix index b2ee6420d186..ceec0b8fab7f 100644 --- a/nixos/lib/testing/nodes.nix +++ b/nixos/lib/testing/nodes.nix @@ -176,6 +176,25 @@ in ''; }; + allMachines = mkOption { + readOnly = true; + internal = true; + description = '' + Basically a merge of [{option}`nodes`](#test-opt-nodes) and [{option}`containers`](#test-opt-containers). + + This ensures that there are no name collisions between nodes and containers. + ''; + default = + let + overlappingNames = lib.intersectLists (lib.attrNames config.nodes) ( + lib.attrNames config.containers + ); + in + lib.throwIfNot (overlappingNames == [ ]) + "The following names are used in both `nodes` and `containers`: ${lib.concatStringsSep ", " overlappingNames}" + (config.nodes // config.containers); + }; + defaults = mkOption { description = '' NixOS configuration that is applied to all [{option}`nodes`](#test-opt-nodes) and [{option}`containers`](#test-opt-containers).