diff options
author | Dan Peebles <pumpkingod@gmail.com> | 2015-04-10 06:06:52 +0200 |
---|---|---|
committer | Dan Peebles <pumpkingod@gmail.com> | 2015-06-11 23:16:35 -0400 |
commit | 4b758e374e19774509a5c93726d79dde4744a8bc (patch) | |
tree | 2c527e13b109ae8fee67547a604e0f6f86fb42e3 /nixos/modules/virtualisation | |
parent | f0753327f00940c9274320f41b76766bbf2b21a3 (diff) |
Initial attempt at configuring from EC2 userdata (with input from cstrahan). Now with VM tests!
Diffstat (limited to 'nixos/modules/virtualisation')
-rw-r--r-- | nixos/modules/virtualisation/amazon-init.nix | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/nixos/modules/virtualisation/amazon-init.nix b/nixos/modules/virtualisation/amazon-init.nix new file mode 100644 index 0000000000000..c5dfb3643e130 --- /dev/null +++ b/nixos/modules/virtualisation/amazon-init.nix @@ -0,0 +1,52 @@ +{ config, pkgs, modulesPath, ... }: + +# This attempts to pull a nix expression from this EC2 instance's user-data. + +let + bootScript = pkgs.writeScript "bootscript.sh" '' + #!${pkgs.stdenv.shell} -eux + + echo "attempting to fetch configuration from user-data..." + + export PATH=${pkgs.nix}/bin:${pkgs.wget}/bin:${pkgs.systemd}/bin:${pkgs.gnugrep}/bin:${pkgs.gnused}/bin:${config.system.build.nixos-rebuild}/bin:$PATH + export NIX_PATH=/nix/var/nix/profiles/per-user/root/channels/nixos:nixos-config=/etc/nixos/configuration.nix:/nix/var/nix/profiles/per-user/root/channels + + userData="$(mktemp)" + wget -q --wait=1 --tries=0 --retry-connrefused -O - http://169.254.169.254/2011-01-01/user-data > "$userData" + + if [[ $? -eq 0 ]]; then + echo "user-data fetched" + # 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 + # that as the channel. + if sed '/^\(#\|SSH_HOST_.*\)/d' < "$userData" | grep -q '\S'; then + channels="$(grep '^###' "$userData" | sed 's|###\s*||')" + printf "%s" "$channels" | while read channel; do + echo "writing channel: $channel" + done + + if [[ -n "$channels" ]]; then + printf "%s" "$channels" > /root/.nix-channels + nix-channel --update + fi + + echo "setting configuration" + cp "$userData" /etc/nixos/configuration.nix + else + echo "user-data does not appear to be a nix expression; ignoring" + fi + else + echo "failed to fetch user-data" + fi + + type -f nixos-rebuild + + nixos-rebuild switch + ''; +in { + imports = [ "${modulesPath}/virtualisation/amazon-image.nix" ]; + ec2.metadata = true; + boot.postBootCommands = '' + ${bootScript} & + ''; +} |