summary refs log tree commit diff
path: root/nixos/modules/virtualisation/amazon-init.nix
blob: 96cd57e6db5d89b3133ea5faf7c7d730d2a1abaf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
{ 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=${config.nix.package}/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 {
  boot.postBootCommands = ''
    ${bootScript} &
  '';
}