at 15.09-beta 1.8 kB view raw
1{ config, pkgs, modulesPath, ... }: 2 3# This attempts to pull a nix expression from this EC2 instance's user-data. 4 5let 6 bootScript = pkgs.writeScript "bootscript.sh" '' 7 #!${pkgs.stdenv.shell} -eux 8 9 echo "attempting to fetch configuration from user-data..." 10 11 export PATH=${config.nix.package}/bin:${pkgs.wget}/bin:${pkgs.systemd}/bin:${pkgs.gnugrep}/bin:${pkgs.gnused}/bin:${config.system.build.nixos-rebuild}/bin:$PATH 12 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 13 14 userData="$(mktemp)" 15 wget -q --wait=1 --tries=0 --retry-connrefused -O - http://169.254.169.254/2011-01-01/user-data > "$userData" 16 17 if [[ $? -eq 0 ]]; then 18 echo "user-data fetched" 19 # If the user-data looks like it could be a nix expression, 20 # copy it over. Also, look for a magic three-hash comment and set 21 # that as the channel. 22 if sed '/^\(#\|SSH_HOST_.*\)/d' < "$userData" | grep -q '\S'; then 23 channels="$(grep '^###' "$userData" | sed 's|###\s*||')" 24 printf "%s" "$channels" | while read channel; do 25 echo "writing channel: $channel" 26 done 27 28 if [[ -n "$channels" ]]; then 29 printf "%s" "$channels" > /root/.nix-channels 30 nix-channel --update 31 fi 32 33 echo "setting configuration" 34 cp "$userData" /etc/nixos/configuration.nix 35 else 36 echo "user-data does not appear to be a nix expression; ignoring" 37 fi 38 else 39 echo "failed to fetch user-data" 40 fi 41 42 type -f nixos-rebuild 43 44 nixos-rebuild switch 45 ''; 46in { 47 imports = [ "${modulesPath}/virtualisation/amazon-image.nix" ]; 48 boot.postBootCommands = '' 49 ${bootScript} & 50 ''; 51}