From 7494e88f866d89390daee3f39cc6666ce542c98f Mon Sep 17 00:00:00 2001 From: Philip Taron Date: Tue, 24 Mar 2026 15:10:21 -0700 Subject: [PATCH] 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. --- nixos/tests/common/ec2.nix | 10 +++++++++- nixos/tests/common/imds-server.nix | 4 ++++ nixos/tests/ec2-image.nix | 5 +---- 3 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 nixos/tests/common/imds-server.nix diff --git a/nixos/tests/common/ec2.nix b/nixos/tests/common/ec2.nix index cb66009b5a83..df1a85638fde 100644 --- a/nixos/tests/common/ec2.nix +++ b/nixos/tests/common/ec2.nix @@ -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" ) diff --git a/nixos/tests/common/imds-server.nix b/nixos/tests/common/imds-server.nix new file mode 100644 index 000000000000..4b867316d988 --- /dev/null +++ b/nixos/tests/common/imds-server.nix @@ -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) diff --git a/nixos/tests/ec2-image.nix b/nixos/tests/ec2-image.nix index 9d8090a9cb52..0e0d5fde4a17 100644 --- a/nixos/tests/ec2-image.nix +++ b/nixos/tests/ec2-image.nix @@ -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 =