1#!/usr/bin/env nix-shell
2#! nix-shell -i bash -p google-cloud-sdk
3
4set -euo pipefail
5
6BUCKET_NAME="${BUCKET_NAME:-nixos-images}"
7TIMESTAMP="$(date +%Y%m%d%H%M)"
8export TIMESTAMP
9
10nix-build '<nixpkgs/nixos>' \
11 -A config.system.build.googleComputeImage \
12 --arg configuration "{ imports = [ <nixpkgs/nixos/modules/virtualisation/google-compute-image.nix> ]; }" \
13 --argstr system x86_64-linux \
14 -o gce \
15 -j 10
16
17img_path=$(echo gce/*.tar.gz)
18img_name=$(basename "$img_path")
19img_id=$(echo "$img_name" | sed 's|.raw.tar.gz$||;s|\.|-|g;s|_|-|g')
20if ! gsutil ls "gs://${BUCKET_NAME}/$img_name"; then
21 gsutil cp "$img_path" "gs://${BUCKET_NAME}/$img_name"
22fi
23gcloud compute images create "$img_id" --source-uri "gs://${BUCKET_NAME}/$img_name"