nixos/nspawn-container: hash name, remove assertion

the code comment is copied from the commit that added the assertion

https://github.com/NixOS/nixpkgs/commit/af0238c70efc80925c31d293da3bf8affca6437f
This commit is contained in:
zowoq
2026-07-06 17:13:54 +10:00
parent 36eac5db86
commit 814dbea27f
2 changed files with 8 additions and 28 deletions
@@ -81,34 +81,6 @@ in
which is prohibited within the Nix build sandbox where the test is run.
'';
}
{
# Check every interface defined in allInterfaces.
# Containers try to create a bridge "${config.system.name}-${interfaceName}"
assertion = lib.all (
iface:
let
hostName = "${config.system.name}-${iface.name}";
in
lib.stringLength hostName <= 15
) (lib.attrValues cfg.allInterfaces);
message =
let
offendingInterfaces = lib.filter (
iface: lib.stringLength "${config.system.name}-${iface.name}" > 15
) (lib.attrValues cfg.allInterfaces);
offenderList = map (
i:
"${config.system.name}-${i.name} (${toString (lib.stringLength "${config.system.name}-${i.name}")} chars)"
) offendingInterfaces;
in
''
The following generated host interface names exceed the Linux 15-character limit:
${lib.concatStringsSep "\n " offenderList}
Please shorten 'config.system.name' or the interface names in 'virtualisation.interfaces'.
'';
}
];
virtualisation.systemd-nspawn.options = [
@@ -1,6 +1,7 @@
import contextlib
import dataclasses
import fcntl
import hashlib
import logging
import os
import signal
@@ -124,6 +125,13 @@ def mk_veth(
vlan: int,
) -> typing.Generator[None, None, None]:
host_intf_name = f"{container_name}-{container_intf_name}"
# If the names for systemd-nspawn containers are too long,
# the generated bridge interface names will surpass the
# kernel limit IFNAMSIZ (15 characters + '\0').
if len(host_intf_name) > 15:
hashed = hashlib.sha256(host_intf_name.encode()).hexdigest()[:6]
host_intf_name = f"{host_intf_name[:8]}-{hashed}"
with ensure_vlan_bridge(vlan) as bridge_name:
logger.info("creating interface %s", host_intf_name)
run_ip(