nixosTests: share IMDSv2 server across EC2 test infrastructure

- Extract imds-server derivation into common/imds-server.nix so both
  ec2-image.nix and common/ec2.nix share the same definition.

- Update common/ec2.nix (makeEc2Test) to use the IMDSv2 server
  instead of micro_httpd, and add token/instance-id to the metadata
  directory so the full IMDSv2 flow works.

- Simplify ec2-image.nix to import from the shared definition.
This commit is contained in:
Philip Taron
2026-03-24 15:38:21 -07:00
parent b1444cab3c
commit 7494e88f86
3 changed files with 14 additions and 5 deletions
+9 -1
View File
@@ -2,7 +2,12 @@
with pkgs.lib;
let
imdsServer = import ./imds-server.nix { inherit pkgs; };
in
{
inherit imdsServer;
makeEc2Test =
{
name,
@@ -18,9 +23,12 @@ with pkgs.lib;
name = "metadata";
buildCommand = ''
mkdir -p $out/1.0/meta-data
mkdir -p $out/latest/api
ln -s ${pkgs.writeText "userData" userData} $out/1.0/user-data
echo "${hostname}" > $out/1.0/meta-data/hostname
echo "(unknown)" > $out/1.0/meta-data/ami-manifest-path
echo "i-1234567890abcdef0" > $out/1.0/meta-data/instance-id
echo "test-imdsv2-token" > $out/latest/api/token
''
+ optionalString (sshPublicKey != null) ''
mkdir -p $out/1.0/meta-data/public-keys/0
@@ -67,7 +75,7 @@ with pkgs.lib;
start_command = (
"qemu-kvm -m 1024"
+ " -device virtio-net-pci,netdev=vlan0"
+ " -netdev 'user,id=vlan0,net=169.0.0.0/8,guestfwd=tcp:169.254.169.254:80-cmd:${pkgs.micro-httpd}/bin/micro_httpd ${metaData}'"
+ " -netdev 'user,id=vlan0,net=169.0.0.0/8,guestfwd=tcp:169.254.169.254:80-cmd:${getExe imdsServer} ${metaData}'"
+ f" -drive file={disk_image},if=virtio,werror=report"
+ " $QEMU_OPTS"
)
+4
View File
@@ -0,0 +1,4 @@
# Minimal IMDSv2-compatible metadata server for NixOS EC2 tests.
# Runs in inetd mode (stdin/stdout), drop-in for micro_httpd in
# QEMU guestfwd and socat contexts.
{ pkgs }: pkgs.writers.writePython3Bin "imds-server" { } (builtins.readFile ./imds-server.py)
+1 -4
View File
@@ -13,10 +13,7 @@ let
inherit (lib) mkAfter mkForce;
pkgs = config.node.pkgs;
# Minimal IMDSv2-compatible metadata server (inetd-mode, drop-in for micro_httpd)
imdsServer = pkgs.writers.writePython3Bin "imds-server" { } (
builtins.readFile ./common/imds-server.py
);
imdsServer = import ./common/imds-server.nix { inherit pkgs; };
# Build an EC2 image configuration
imageCfg =