Skip to content

Commit 21a2b91

Browse files
committed
updates
Signed-off-by: Enes Öztürk <enes.ozturk@unikie.com>
1 parent 63ac0ee commit 21a2b91

File tree

5 files changed

+144
-28
lines changed

5 files changed

+144
-28
lines changed

modules/common/common.nix

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,34 @@ let
1515
optionalAttrs
1616
hasAttrByPath
1717
;
18+
# Internal network host entry
19+
hostEntrySubmodule = types.submodule {
20+
options = {
21+
name = mkOption {
22+
type = types.nullOr types.str;
23+
description = "Host name as string.";
24+
default = null;
25+
};
26+
mac = mkOption {
27+
type = types.nullOr types.str;
28+
description = "MAC address as string.";
29+
default = null;
30+
31+
};
32+
ipv4 = mkOption {
33+
type = types.nullOr types.str;
34+
description = "IPv4 address as string.";
35+
default = null;
36+
37+
};
38+
ipv6 = mkOption {
39+
type = types.nullOr types.str;
40+
description = "IPv6 address as string.";
41+
default = null;
42+
43+
};
44+
};
45+
};
1846
in
1947
{
2048
imports = [
@@ -69,8 +97,14 @@ in
6997
"app-vm"
7098
];
7199
};
100+
extraNetworking = {
101+
hosts = mkOption {
102+
type = types.attrsOf hostEntrySubmodule;
103+
description = "Extra host entries that override or extend the generated ones.";
104+
default = { };
105+
};
106+
};
72107
};
73-
74108
config = {
75109

76110
# Populate the shared namespace

modules/common/networking/hosts.nix

Lines changed: 83 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,41 @@ let
1616
;
1717

1818
# Internal network host entry
19+
# TODO Add sockets
1920
hostEntrySubmodule = types.submodule {
2021
options = {
2122
name = mkOption {
2223
type = types.str;
23-
description = "Host name as string.";
24+
description = ''
25+
Host name as string.
26+
'';
2427
};
2528
mac = mkOption {
2629
type = types.str;
27-
description = "MAC address as string.";
30+
description = ''
31+
MAC address as string.
32+
'';
2833
};
2934
ipv4 = mkOption {
3035
type = types.str;
31-
description = "IPv4 address as string.";
36+
description = ''
37+
IPv4 address as string.
38+
'';
3239
};
3340
ipv6 = mkOption {
3441
type = types.str;
35-
description = "IPv6 address as string.";
42+
description = ''
43+
IPv6 address as string.
44+
'';
3645
};
3746
cid = mkOption {
3847
type = types.int;
39-
description = "Vsock CID (Context IDentifier) as integer.";
48+
description = ''
49+
Vsock CID (Context IDentifier) as integer:
50+
- VMADDR_CID_HYPERVISOR (0) is reserved for services built into the hypervisor
51+
- VMADDR_CID_LOCAL (1) is the well-known address for local communication (loopback)
52+
- VMADDR_CID_HOST (2) is the well-known address of the host
53+
'';
4054
};
4155
};
4256
};
@@ -77,9 +91,29 @@ let
7791

7892
# Evaluate generated hosts as attrset
7993
generatedHostAttrs = listToAttrs (map (host: nameValuePair host.name host) generatedHosts);
80-
combinedHosts =
81-
generatedHostAttrs
82-
// builtins.trace "extraHosts: ${builtins.toJSON config.ghaf.networking.extraHosts}" config.ghaf.networking.extraHosts;
94+
# Extract names of all extra hosts
95+
extraHostNames = lib.attrNames config.ghaf.common.extraNetworking.hosts;
96+
97+
# Merge logic per host
98+
mergedExtraHosts = listToAttrs (
99+
map (
100+
name:
101+
let
102+
gen = generatedHostAttrs.${name};
103+
extra = config.ghaf.common.extraNetworking.hosts.${name};
104+
in
105+
nameValuePair name {
106+
inherit name;
107+
mac = if extra ? mac && extra.mac != null then extra.mac else gen.mac;
108+
ipv4 = if extra ? ipv4 && extra.ipv4 != null then extra.ipv4 else gen.ipv4;
109+
ipv6 = if extra ? ipv6 && extra.ipv6 != null then extra.ipv6 else gen.ipv6;
110+
inherit (gen) cid;
111+
}
112+
) extraHostNames
113+
);
114+
115+
# Combine generated and extra hosts (extra overrides or extends)
116+
combinedHosts = generatedHostAttrs // mergedExtraHosts;
83117

84118
# Trace
85119
tracedCombinedHosts = builtins.trace "ghaf.networking.hosts (merged): ${builtins.toJSON combinedHosts}" combinedHosts;
@@ -91,7 +125,47 @@ let
91125
}) (lib.attrValues combinedHosts)
92126
);
93127
tracedNetworkingHosts = builtins.trace "networking.hosts (JSON): ${builtins.toJSON networkingHosts}" networkingHosts;
128+
# Extract values to check for uniqueness
129+
allHosts = lib.attrValues combinedHosts;
130+
getField = field: map (h: h.${field}) allHosts;
131+
132+
checkUnique =
133+
field:
134+
let
135+
values = builtins.trace "Values: ${builtins.toJSON (getField field)}" (getField field);
136+
unique = lib.lists.unique values;
137+
138+
# Find duplicates by filtering values that occur more than once
139+
duplicates = lib.lists.filter (
140+
value: lib.lists.length (lib.lists.filter (x: x == value) values) > 1
141+
) unique;
94142

143+
# Create a list of duplicates with the corresponding host names
144+
duplicateNames = lib.lists.filter (
145+
host: lib.lists.length (lib.lists.filter (x: x == host.${field}) values) > 1
146+
) allHosts;
147+
148+
in
149+
{
150+
inherit field;
151+
ok = values == unique;
152+
inherit duplicates;
153+
duplicateNames = map (host: host.name) duplicateNames; # Extract host names for duplicates
154+
};
155+
156+
uniquenessChecks = map checkUnique [
157+
"mac"
158+
"ipv4"
159+
"ipv6"
160+
"cid"
161+
"name"
162+
];
163+
164+
uniquenessAssertions = map (check: {
165+
assertion = check.ok;
166+
message = "Duplicate ${check.field} values detected: ${lib.concatStringsSep ", " check.duplicates}, conflict between:${lib.concatStringsSep ", " check.duplicateNames}";
167+
168+
}) uniquenessChecks;
95169
in
96170
{
97171
options.ghaf.networking = {
@@ -101,11 +175,6 @@ in
101175
default = { };
102176
};
103177

104-
extraHosts = mkOption {
105-
type = types.attrsOf hostEntrySubmodule;
106-
description = "Extra host entries that override or extend the generated ones.";
107-
default = { };
108-
};
109178
};
110179

111180
config = {
@@ -114,7 +183,7 @@ in
114183
assertion = lib.length config.ghaf.common.vms < 255;
115184
message = "Too many VMs defined - maximum is 254";
116185
}
117-
];
186+
] ++ uniquenessAssertions;
118187

119188
ghaf.networking.hosts = tracedCombinedHosts;
120189

modules/microvm/sysvms/adminvm.nix

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,18 @@ in
136136
imports = adminvmBaseConfiguration.imports ++ cfg.extraModules;
137137
};
138138
};
139+
140+
ghaf.common.extraNetworking.hosts = {
141+
142+
admin-vm = {
143+
# name = "chrome-vm";
144+
ipv4 = builtins.trace "admin-vm ip change:" "192.168.100.120";
145+
# mac = "02:00:00:00:00:01";
146+
# ipv6 = "2001:db8::1";
147+
# cid = 8;
148+
};
149+
150+
};
151+
139152
};
140153
}

modules/microvm/sysvms/netvm.nix

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ in
154154
};
155155

156156
config = lib.mkIf cfg.enable {
157-
microvm.vms = {
157+
microvm = {
158158

159-
"${vmName}" = {
159+
vms."${vmName}" = {
160160
autostart = true;
161161
restartIfChanged = false;
162162
inherit (inputs) nixpkgs;
@@ -172,5 +172,16 @@ in
172172

173173
};
174174
};
175+
ghaf.common.extraNetworking.hosts = {
176+
177+
net-vm = {
178+
# name = "chrome-vm";
179+
ipv4 = builtins.trace "net-vm ip change:" "192.168.100.115";
180+
# mac = "02:00:00:00:00:01";
181+
# ipv6 = "2001:db8::1";
182+
# cid = 8;
183+
};
184+
185+
};
175186
};
176187
}

modules/reference/profiles/mvp-user-trial.nix

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,6 @@ in
2727
"chrome-vm"
2828
];
2929

30-
# networking.extraHosts = lib.mkForce {
31-
32-
# chrome-vm = {
33-
# name = "chrome-vm";
34-
# ipv4 = builtins.trace "host---" lib.mkForce "192.168.100.1";
35-
# mac = "02:00:00:00:00:01";
36-
# ipv6 = "2001:db8::1";
37-
# cid = 8;
38-
# };
39-
40-
# };
4130
virtualization.microvm.appvm = {
4231
enable = true;
4332
vms = {

0 commit comments

Comments
 (0)