1#! /bin/sh -e
2
3export NIXOS_CONFIG=$(dirname $(readlink -f $0))/amazon-base-config.nix
4
5version=$(nix-instantiate --eval-only '<nixpkgs/nixos>' -A config.system.nixosVersion | sed s/'"'//g)
6echo "NixOS version is $version"
7
8buildAndUploadFor() {
9 system="$1"
10 arch="$2"
11
12 echo "building $system image..."
13 nix-build '<nixpkgs/nixos>' \
14 -A config.system.build.amazonImage --argstr system "$system" -o ec2-ami
15
16 ec2-bundle-image -i ./ec2-ami/nixos.img --user "$AWS_ACCOUNT" --arch "$arch" \
17 -c "$EC2_CERT" -k "$EC2_PRIVATE_KEY"
18
19 for region in eu-west-1; do
20 echo "uploading $system image for $region..."
21
22 name=nixos-$version-$arch-s3
23 bucket="$(echo $name-$region | tr '[A-Z]_' '[a-z]-')"
24
25 if [ "$region" = eu-west-1 ]; then s3location=EU;
26 elif [ "$region" = us-east-1 ]; then s3location=US;
27 else s3location="$region"
28 fi
29
30 ec2-upload-bundle -b "$bucket" -m /tmp/nixos.img.manifest.xml \
31 -a "$EC2_ACCESS_KEY" -s "$EC2_SECRET_KEY" --location "$s3location" \
32 --url http://s3.amazonaws.com
33
34 kernel=$(ec2-describe-images -o amazon --filter "manifest-location=*pv-grub-hd0_1.04-$arch*" --region "$region" | cut -f 2)
35 echo "using PV-GRUB kernel $kernel"
36
37 ami=$(ec2-register "$bucket/nixos.img.manifest.xml" -n "$name" -d "NixOS $system r$revision" -O "$EC2_ACCESS_KEY" -W "$EC2_SECRET_KEY" \
38 --region "$region" --kernel "$kernel" | cut -f 2)
39
40 echo "AMI ID is $ami"
41
42 echo " \"14.12\".\"$region\".s3 = \"$ami\";" >> ec2-amis.nix
43
44 ec2-modify-image-attribute --region "$region" "$ami" -l -a all -O "$EC2_ACCESS_KEY" -W "$EC2_SECRET_KEY"
45
46 for cp_region in us-east-1 us-west-1 us-west-2 eu-central-1 ap-southeast-1 ap-southeast-2 ap-northeast-1 sa-east-1; do
47 new_ami=$(aws ec2 copy-image --source-image-id $ami --source-region $region --region $cp_region --name "$name" | json ImageId)
48 echo " \"14.12\".\"$cp_region\".s3 = \"$new_ami\";" >> ec2-amis.nix
49 done
50 done
51}
52
53buildAndUploadFor x86_64-linux x86_64