nixos/amazon-init: add user-data shell script support

Changed files
+51
nixos
modules
virtualisation
tests
+10
nixos/modules/virtualisation/amazon-init.nix
···
userData=/etc/ec2-metadata/user-data
if [ -s "$userData" ]; then
# If the user-data looks like it could be a nix expression,
# copy it over. Also, look for a magic three-hash comment and set
···
userData=/etc/ec2-metadata/user-data
+
# Check if user-data looks like a shell script and execute it with the
+
# runtime shell if it does. Otherwise treat it as a nixos configuration
+
# expression
+
if IFS= LC_ALL=C read -rN2 shebang < $userData && [ "$shebang" = '#!' ]; then
+
# NB: we cannot chmod the $userData file, this is why we execute it via
+
# `pkgs.runtimeShell`. This means we have only limited support for shell
+
# scripts compatible with the `pkgs.runtimeShell`.
+
exec ${pkgs.runtimeShell} $userData
+
fi
+
if [ -s "$userData" ]; then
# If the user-data looks like it could be a nix expression,
# copy it over. Also, look for a magic three-hash comment and set
+1
nixos/tests/all-tests.nix
···
_3proxy = handleTest ./3proxy.nix {};
acme = handleTest ./acme.nix {};
agda = handleTest ./agda.nix {};
ammonite = handleTest ./ammonite.nix {};
atd = handleTest ./atd.nix {};
avahi = handleTest ./avahi.nix {};
···
_3proxy = handleTest ./3proxy.nix {};
acme = handleTest ./acme.nix {};
agda = handleTest ./agda.nix {};
+
amazon-init-shell = handleTest ./amazon-init-shell.nix {};
ammonite = handleTest ./ammonite.nix {};
atd = handleTest ./atd.nix {};
avahi = handleTest ./avahi.nix {};
+40
nixos/tests/amazon-init-shell.nix
···
···
+
# This test verifies that the amazon-init service can treat the `user-data` ec2
+
# metadata file as a shell script. If amazon-init detects that `user-data` is a
+
# script (based on the presence of the shebang #! line) it executes it and
+
# exits.
+
# Note that other tests verify that amazon-init can treat user-data as a nixos
+
# configuration expression.
+
+
{ system ? builtins.currentSystem,
+
config ? {},
+
pkgs ? import ../.. { inherit system config; }
+
}:
+
+
with import ../lib/testing-python.nix { inherit system pkgs; };
+
with pkgs.lib;
+
+
makeTest {
+
name = "amazon-init";
+
meta = with maintainers; {
+
maintainers = [ urbas ];
+
};
+
machine = { ... }:
+
{
+
imports = [ ../modules/profiles/headless.nix ../modules/virtualisation/amazon-init.nix ];
+
services.openssh.enable = true;
+
networking.hostName = "";
+
environment.etc."ec2-metadata/user-data" = {
+
text = ''
+
#!/usr/bin/bash
+
+
echo successful > /tmp/evidence
+
'';
+
};
+
};
+
testScript = ''
+
# To wait until amazon-init terminates its run
+
unnamed.wait_for_unit("amazon-init.service")
+
+
unnamed.succeed("grep -q successful /tmp/evidence")
+
'';
+
}