aboutsummaryrefslogtreecommitdiff
path: root/nixpkgs/nixos/lib/build-vms.nix
diff options
context:
space:
mode:
authorKatharina Fey <kookie@spacekookie.de>2019-10-05 12:43:18 +0000
committerKatharina Fey <kookie@spacekookie.de>2019-10-05 12:44:52 +0000
commitcf85056ba64caf3267d43255ef4a1243e9c8ee3b (patch)
tree3051519e9c8275b870aac43f80af875715c9d124 /nixpkgs/nixos/lib/build-vms.nix
parent1148b1d122bc03e9a3665856c9b7bb96bd4e3994 (diff)
parent2436c27541b2f52deea3a4c1691216a02152e729 (diff)
Add 'nixpkgs/' from commit '2436c27541b2f52deea3a4c1691216a02152e729'
git-subtree-dir: nixpkgs git-subtree-mainline: 1148b1d122bc03e9a3665856c9b7bb96bd4e3994 git-subtree-split: 2436c27541b2f52deea3a4c1691216a02152e729
Diffstat (limited to 'nixpkgs/nixos/lib/build-vms.nix')
-rw-r--r--nixpkgs/nixos/lib/build-vms.nix101
1 files changed, 101 insertions, 0 deletions
diff --git a/nixpkgs/nixos/lib/build-vms.nix b/nixpkgs/nixos/lib/build-vms.nix
new file mode 100644
index 00000000000..1bad63b9194
--- /dev/null
+++ b/nixpkgs/nixos/lib/build-vms.nix
@@ -0,0 +1,101 @@
+{ system
+, # Use a minimal kernel?
+ minimal ? false
+, # Ignored
+ config ? null
+ # Nixpkgs, for qemu, lib and more
+, pkgs
+, # NixOS configuration to add to the VMs
+ extraConfigurations ? []
+}:
+
+with pkgs.lib;
+with import ../lib/qemu-flags.nix { inherit pkgs; };
+
+rec {
+
+ inherit pkgs;
+
+ qemu = pkgs.qemu_test;
+
+
+ # Build a virtual network from an attribute set `{ machine1 =
+ # config1; ... machineN = configN; }', where `machineX' is the
+ # hostname and `configX' is a NixOS system configuration. Each
+ # machine is given an arbitrary IP address in the virtual network.
+ buildVirtualNetwork =
+ nodes: let nodesOut = mapAttrs (n: buildVM nodesOut) (assignIPAddresses nodes); in nodesOut;
+
+
+ buildVM =
+ nodes: configurations:
+
+ import ./eval-config.nix {
+ inherit system;
+ modules = configurations ++ extraConfigurations;
+ baseModules = (import ../modules/module-list.nix) ++
+ [ ../modules/virtualisation/qemu-vm.nix
+ ../modules/testing/test-instrumentation.nix # !!! should only get added for automated test runs
+ { key = "no-manual"; documentation.nixos.enable = false; }
+ { key = "qemu"; system.build.qemu = qemu; }
+ { key = "nodes"; _module.args.nodes = nodes; }
+ ] ++ optional minimal ../modules/testing/minimal-kernel.nix;
+ };
+
+
+ # Given an attribute set { machine1 = config1; ... machineN =
+ # configN; }, sequentially assign IP addresses in the 192.168.1.0/24
+ # range to each machine, and set the hostname to the attribute name.
+ assignIPAddresses = nodes:
+
+ let
+
+ machines = attrNames nodes;
+
+ machinesNumbered = zipLists machines (range 1 254);
+
+ nodes_ = forEach machinesNumbered (m: nameValuePair m.fst
+ [ ( { config, nodes, ... }:
+ let
+ interfacesNumbered = zipLists config.virtualisation.vlans (range 1 255);
+ interfaces = forEach interfacesNumbered ({ fst, snd }:
+ nameValuePair "eth${toString snd}" { ipv4.addresses =
+ [ { address = "192.168.${toString fst}.${toString m.snd}";
+ prefixLength = 24;
+ } ];
+ });
+ in
+ { key = "ip-address";
+ config =
+ { networking.hostName = mkDefault m.fst;
+
+ networking.interfaces = listToAttrs interfaces;
+
+ networking.primaryIPAddress =
+ optionalString (interfaces != []) (head (head interfaces).value.ipv4.addresses).address;
+
+ # Put the IP addresses of all VMs in this machine's
+ # /etc/hosts file. If a machine has multiple
+ # interfaces, use the IP address corresponding to
+ # the first interface (i.e. the first network in its
+ # virtualisation.vlans option).
+ networking.extraHosts = flip concatMapStrings machines
+ (m': let config = (getAttr m' nodes).config; in
+ optionalString (config.networking.primaryIPAddress != "")
+ ("${config.networking.primaryIPAddress} " +
+ optionalString (config.networking.domain != null)
+ "${config.networking.hostName}.${config.networking.domain} " +
+ "${config.networking.hostName}\n"));
+
+ virtualisation.qemu.options =
+ forEach interfacesNumbered
+ ({ fst, snd }: qemuNICFlags snd fst m.snd);
+ };
+ }
+ )
+ (getAttr m.fst nodes)
+ ] );
+
+ in listToAttrs nodes_;
+
+}