-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathmain.tf
94 lines (75 loc) · 2.38 KB
/
main.tf
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
variable "bucket_name" {
description = "Bucket where to store the image"
}
variable "nixos_config" {
description = "Path to a nixos configuration.nix file"
}
variable "NIX_PATH" {
type = string
description = "Allow to pass custom NIX_PATH. Ignored if `-` or empty."
default = "-"
}
variable "gcp_project_id" {
type = string
default = ""
description = "The ID of the project in which the resource belongs. If it is not provided, the provider project is used."
}
variable "licenses" {
type = list(string)
default = [
"https://www.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx",
]
description = "A list of license URIs to apply to this image. Changing this forces a new resource to be created."
}
# ----------------------------------------------------
data "external" "nix_build" {
program = ["${path.module}/nixos-build.sh", var.NIX_PATH, var.nixos_config]
}
locals {
out_path = data.external.nix_build.result.out_path
image_path = data.external.nix_build.result.image_path
# 3x2d4rdm9kjzk9d9sz87rmhzvcphs23v
out_hash = element(split("-", basename(local.out_path)), 0)
# Example: 3x2d4rdm9kjzk9d9sz87rmhzvcphs23v-19-03pre-git-x86-64-linux
#
# Remove a few things so that it matches the required regexp for image names
# (?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)
image_name = "x${substr(local.out_hash, 0, 12)}-${replace(
replace(
basename(local.image_path),
"/\\.raw\\.tar\\.gz|nixos-image-/",
"",
),
"/[._]+/",
"-",
)}"
# 3x2d4rdm9kjzk9d9sz87rmhzvcphs23v-nixos-image-19.03pre-git-x86_64-linux.raw.tar.gz
image_filename = "${local.out_hash}-${basename(local.image_path)}"
}
resource "google_storage_bucket_object" "nixos" {
name = "images/${local.image_filename}"
source = local.image_path
bucket = var.bucket_name
content_type = "application/tar+gzip"
lifecycle {
create_before_destroy = true
}
}
resource "google_compute_image" "nixos" {
name = local.image_name
family = "nixos"
project = var.gcp_project_id
licenses = var.licenses
raw_disk {
source = "https://${var.bucket_name}.storage.googleapis.com/${google_storage_bucket_object.nixos.name}"
}
lifecycle {
create_before_destroy = true
}
}
output "self_link" {
value = google_compute_image.nixos.self_link
}
output "NIX_PATH" {
value = var.NIX_PATH
}