2022-06-06 13:29:04 +02:00
|
|
|
{ lib, nodes, ... }:
|
|
|
|
|
2022-06-06 23:46:39 +02:00
|
|
|
let
|
|
|
|
inherit (lib)
|
|
|
|
attrNames concatMap concatMapStrings flip forEach head
|
|
|
|
listToAttrs mkDefault mkOption nameValuePair optionalString
|
2023-05-22 05:38:52 +02:00
|
|
|
range toLower types zipListsWith zipLists
|
2022-06-27 20:06:30 +02:00
|
|
|
mdDoc
|
2022-06-06 23:46:39 +02:00
|
|
|
;
|
2022-06-06 13:29:04 +02:00
|
|
|
|
2022-06-06 23:46:39 +02:00
|
|
|
nodeNumbers =
|
|
|
|
listToAttrs
|
|
|
|
(zipListsWith
|
|
|
|
nameValuePair
|
|
|
|
(attrNames nodes)
|
|
|
|
(range 1 254)
|
|
|
|
);
|
2022-06-06 13:29:04 +02:00
|
|
|
|
2022-06-06 23:46:39 +02:00
|
|
|
networkModule = { config, nodes, pkgs, ... }:
|
|
|
|
let
|
2023-05-22 05:38:52 +02:00
|
|
|
qemu-common = import ../qemu-common.nix { inherit lib pkgs; };
|
|
|
|
|
|
|
|
# Convert legacy VLANs to named interfaces and merge with explicit interfaces.
|
|
|
|
vlansNumbered = forEach (zipLists config.virtualisation.vlans (range 1 255)) (v: {
|
|
|
|
name = "eth${toString v.snd}";
|
|
|
|
vlan = v.fst;
|
|
|
|
assignIP = true;
|
|
|
|
});
|
|
|
|
explicitInterfaces = lib.mapAttrsToList (n: v: v // { name = n; }) config.virtualisation.interfaces;
|
|
|
|
interfaces = vlansNumbered ++ explicitInterfaces;
|
|
|
|
interfacesNumbered = zipLists interfaces (range 1 255);
|
|
|
|
|
|
|
|
# Automatically assign IP addresses to requested interfaces.
|
|
|
|
assignIPs = lib.filter (i: i.assignIP) interfaces;
|
|
|
|
ipInterfaces = forEach assignIPs (i:
|
|
|
|
nameValuePair i.name { ipv4.addresses =
|
|
|
|
[ { address = "192.168.${toString i.vlan}.${toString config.virtualisation.test.nodeNumber}";
|
2022-06-06 23:46:39 +02:00
|
|
|
prefixLength = 24;
|
|
|
|
}];
|
|
|
|
});
|
2022-06-06 13:29:04 +02:00
|
|
|
|
2023-05-22 05:38:52 +02:00
|
|
|
qemuOptions = lib.flatten (forEach interfacesNumbered ({ fst, snd }:
|
|
|
|
qemu-common.qemuNICFlags snd fst.vlan config.virtualisation.test.nodeNumber));
|
|
|
|
udevRules = forEach interfacesNumbered ({ fst, snd }:
|
|
|
|
# MAC Addresses for QEMU network devices are lowercase, and udev string comparison is case-sensitive.
|
2023-05-23 00:38:42 +02:00
|
|
|
''SUBSYSTEM=="net",ACTION=="add",ATTR{address}=="${toLower(qemu-common.qemuNicMac fst.vlan config.virtualisation.test.nodeNumber)}",NAME="${fst.name}"'');
|
2023-05-22 05:38:52 +02:00
|
|
|
|
2022-06-06 23:46:39 +02:00
|
|
|
networkConfig =
|
|
|
|
{
|
|
|
|
networking.hostName = mkDefault config.virtualisation.test.nodeName;
|
2022-06-06 13:29:04 +02:00
|
|
|
|
2023-05-22 05:38:52 +02:00
|
|
|
networking.interfaces = listToAttrs ipInterfaces;
|
2022-06-06 13:29:04 +02:00
|
|
|
|
2022-06-06 23:46:39 +02:00
|
|
|
networking.primaryIPAddress =
|
2023-05-22 05:38:52 +02:00
|
|
|
optionalString (ipInterfaces != [ ]) (head (head ipInterfaces).value.ipv4.addresses).address;
|
2022-06-06 13:29:04 +02:00
|
|
|
|
2022-06-06 23:46:39 +02:00
|
|
|
# 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 (attrNames nodes)
|
|
|
|
(m':
|
|
|
|
let config = nodes.${m'}; in
|
|
|
|
optionalString (config.networking.primaryIPAddress != "")
|
|
|
|
("${config.networking.primaryIPAddress} " +
|
|
|
|
optionalString (config.networking.domain != null)
|
|
|
|
"${config.networking.hostName}.${config.networking.domain} " +
|
|
|
|
"${config.networking.hostName}\n"));
|
2022-06-06 13:29:04 +02:00
|
|
|
|
2023-05-22 05:38:52 +02:00
|
|
|
virtualisation.qemu.options = qemuOptions;
|
|
|
|
boot.initrd.services.udev.rules = concatMapStrings (x: x + "\n") udevRules;
|
2022-06-06 23:46:39 +02:00
|
|
|
};
|
2022-06-06 13:29:04 +02:00
|
|
|
|
2022-06-06 23:46:39 +02:00
|
|
|
in
|
|
|
|
{
|
2023-05-22 05:38:52 +02:00
|
|
|
key = "network-interfaces";
|
2022-06-06 23:46:39 +02:00
|
|
|
config = networkConfig // {
|
|
|
|
# Expose the networkConfig items for tests like nixops
|
|
|
|
# that need to recreate the network config.
|
|
|
|
system.build.networkConfig = networkConfig;
|
|
|
|
};
|
|
|
|
};
|
2022-06-06 13:29:04 +02:00
|
|
|
|
2022-06-06 23:46:39 +02:00
|
|
|
nodeNumberModule = (regular@{ config, name, ... }: {
|
|
|
|
options = {
|
|
|
|
virtualisation.test.nodeName = mkOption {
|
|
|
|
internal = true;
|
|
|
|
default = name;
|
|
|
|
# We need to force this in specilisations, otherwise it'd be
|
|
|
|
# readOnly = true;
|
2022-06-27 20:06:30 +02:00
|
|
|
description = mdDoc ''
|
2022-06-06 23:46:39 +02:00
|
|
|
The `name` in `nodes.<name>`; stable across `specialisations`.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
virtualisation.test.nodeNumber = mkOption {
|
|
|
|
internal = true;
|
|
|
|
type = types.int;
|
|
|
|
readOnly = true;
|
|
|
|
default = nodeNumbers.${config.virtualisation.test.nodeName};
|
2022-06-27 20:06:30 +02:00
|
|
|
description = mdDoc ''
|
2022-06-06 23:46:39 +02:00
|
|
|
A unique number assigned for each node in `nodes`.
|
|
|
|
'';
|
|
|
|
};
|
2022-06-06 13:29:04 +02:00
|
|
|
|
2022-06-06 23:46:39 +02:00
|
|
|
# specialisations override the `name` module argument,
|
|
|
|
# so we push the real `virtualisation.test.nodeName`.
|
|
|
|
specialisation = mkOption {
|
|
|
|
type = types.attrsOf (types.submodule {
|
|
|
|
options.configuration = mkOption {
|
2022-06-25 23:29:49 +02:00
|
|
|
type = types.submoduleWith {
|
|
|
|
modules = [
|
|
|
|
{
|
|
|
|
config.virtualisation.test.nodeName =
|
|
|
|
# assert regular.config.virtualisation.test.nodeName != "configuration";
|
|
|
|
regular.config.virtualisation.test.nodeName;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
2022-06-06 13:29:04 +02:00
|
|
|
};
|
2022-06-06 23:46:39 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
2022-06-06 13:29:04 +02:00
|
|
|
|
|
|
|
in
|
|
|
|
{
|
|
|
|
config = {
|
2022-09-17 15:34:14 +02:00
|
|
|
extraBaseModules = { imports = [ networkModule nodeNumberModule ]; };
|
2022-06-06 13:29:04 +02:00
|
|
|
};
|
|
|
|
}
|