2021-12-30 06:15:27 +01:00
|
|
|
{ depot, pkgs, lib, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
inherit (builtins) concatLists concatStringsSep toJSON unsafeDiscardStringContext;
|
|
|
|
inherit (depot.users) wpcarro;
|
|
|
|
inherit (pkgs) writeText;
|
|
|
|
|
|
|
|
images = import "${pkgs.path}/nixos/modules/virtualisation/gce-images.nix";
|
|
|
|
nixosImage = images."20.09";
|
|
|
|
in
|
|
|
|
{
|
|
|
|
googleCloudVM =
|
|
|
|
{ project
|
|
|
|
, name
|
|
|
|
, region
|
|
|
|
, zone
|
|
|
|
, configuration
|
|
|
|
, extraConfig ? { }
|
2022-01-30 17:06:58 +01:00
|
|
|
,
|
2021-12-30 06:15:27 +01:00
|
|
|
}:
|
|
|
|
let
|
|
|
|
inherit (configuration.users.users) root;
|
|
|
|
inherit (configuration.networking) firewall;
|
2022-01-30 17:06:58 +01:00
|
|
|
|
2021-12-30 06:15:27 +01:00
|
|
|
# Convert NixOS-style port numbers to Terraform-style.
|
|
|
|
asStrings = xs: map toString xs;
|
|
|
|
asRanges = xs: map (x: "${toString x.from}-${toString x.to}") xs;
|
2022-01-30 17:06:58 +01:00
|
|
|
|
2021-12-30 06:15:27 +01:00
|
|
|
sshKeys = concatStringsSep "\n"
|
|
|
|
(map (key: "root:${key}") root.openssh.authorizedKeys.keys);
|
2022-01-30 17:06:58 +01:00
|
|
|
|
2021-12-30 06:15:27 +01:00
|
|
|
os = depot.ops.nixos.nixosFor (_: {
|
|
|
|
imports = [
|
|
|
|
"${pkgs.path}/nixos/modules/virtualisation/google-compute-image.nix"
|
|
|
|
configuration
|
|
|
|
];
|
2022-01-30 17:06:58 +01:00
|
|
|
|
2021-12-30 06:15:27 +01:00
|
|
|
networking.hostName = name;
|
2022-01-30 17:06:58 +01:00
|
|
|
|
2021-12-30 06:15:27 +01:00
|
|
|
fileSystems."/nix" = {
|
|
|
|
device = "/dev/disk/by-label/google-${name}-disk";
|
|
|
|
fsType = "ext4";
|
2022-01-30 17:06:58 +01:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2021-12-30 06:15:27 +01:00
|
|
|
osRoot = os.config.system.build.toplevel;
|
|
|
|
osPath = unsafeDiscardStringContext (toString osRoot.outPath);
|
|
|
|
drvPath = unsafeDiscardStringContext (toString osRoot.drvPath);
|
2022-01-30 17:06:58 +01:00
|
|
|
in
|
2021-12-30 06:15:27 +01:00
|
|
|
writeText "terraform.tf.json" (toJSON (lib.recursiveUpdate extraConfig {
|
|
|
|
provider.google = {
|
|
|
|
inherit project region zone;
|
|
|
|
};
|
2022-01-30 17:06:58 +01:00
|
|
|
|
2021-12-30 06:15:27 +01:00
|
|
|
resource.google_compute_instance."${name}" = {
|
|
|
|
inherit name zone;
|
|
|
|
machine_type = "e2-standard-2";
|
2022-01-30 17:06:58 +01:00
|
|
|
|
2021-12-30 06:15:27 +01:00
|
|
|
tags = [
|
|
|
|
"http-server"
|
|
|
|
"https-server"
|
|
|
|
"${name}-firewall"
|
2022-01-30 17:06:58 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
boot_disk = {
|
2021-12-30 06:15:27 +01:00
|
|
|
device_name = "boot";
|
|
|
|
initialize_params = {
|
|
|
|
size = 10;
|
|
|
|
image = "projects/nixos-cloud/global/images/${nixosImage.name}";
|
|
|
|
};
|
2022-01-30 17:06:58 +01:00
|
|
|
};
|
|
|
|
|
2021-12-30 06:15:27 +01:00
|
|
|
attached_disk = {
|
|
|
|
source = "\${google_compute_disk.${name}.id}";
|
|
|
|
device_name = "${name}-disk";
|
2022-01-30 17:06:58 +01:00
|
|
|
};
|
|
|
|
|
2021-12-30 06:15:27 +01:00
|
|
|
network_interface = {
|
|
|
|
network = "default";
|
|
|
|
subnetwork = "default";
|
|
|
|
access_config = { };
|
2022-01-30 17:06:58 +01:00
|
|
|
};
|
|
|
|
|
2021-12-30 06:15:27 +01:00
|
|
|
# Copy root's SSH keys from the NixOS configuration and expose them to the
|
|
|
|
# metadata server.
|
|
|
|
metadata = {
|
|
|
|
inherit sshKeys;
|
|
|
|
ssh-keys = sshKeys;
|
2022-01-30 17:06:58 +01:00
|
|
|
|
2021-12-30 06:15:27 +01:00
|
|
|
# NixOS's fetch-instance-ssh-keys.bash relies on these fields being
|
|
|
|
# available on the metadata server.
|
|
|
|
ssh_host_ed25519_key = "\${tls_private_key.${name}.private_key_pem}";
|
|
|
|
ssh_host_ed25519_key_pub = "\${tls_private_key.${name}.public_key_pem}";
|
2022-01-30 17:06:58 +01:00
|
|
|
|
2021-12-30 06:15:27 +01:00
|
|
|
# Even though we have SSH access, having oslogin can still be useful for
|
|
|
|
# troubleshooting in the browser if for some reason SSH isn't working as
|
|
|
|
# expected.
|
|
|
|
enable-oslogin = "TRUE";
|
|
|
|
};
|
2022-01-30 17:06:58 +01:00
|
|
|
|
2021-12-30 06:15:27 +01:00
|
|
|
service_account.scopes = [ "cloud-platform" ];
|
|
|
|
};
|
2022-01-30 17:06:58 +01:00
|
|
|
|
2021-12-30 06:15:27 +01:00
|
|
|
resource.tls_private_key."${name}" = {
|
|
|
|
algorithm = "ECDSA";
|
|
|
|
ecdsa_curve = "P384";
|
|
|
|
};
|
2022-01-30 17:06:58 +01:00
|
|
|
|
2021-12-30 06:15:27 +01:00
|
|
|
resource.google_compute_firewall."${name}" = {
|
|
|
|
name = "${name}-firewall";
|
|
|
|
network = "default";
|
2022-01-30 17:06:58 +01:00
|
|
|
|
2021-12-30 06:15:27 +01:00
|
|
|
# Read the firewall configuration from the NixOS configuration.
|
|
|
|
allow = [
|
|
|
|
{
|
|
|
|
protocol = "tcp";
|
|
|
|
ports = concatLists [
|
|
|
|
(asStrings (firewall.allowedTCPPorts or [ ]))
|
|
|
|
(asRanges (firewall.allowedTCPPortRanges or [ ]))
|
|
|
|
];
|
|
|
|
}
|
|
|
|
{
|
|
|
|
protocol = "udp";
|
|
|
|
ports = concatLists [
|
|
|
|
(asStrings (firewall.allowedUDPPorts or [ ]))
|
|
|
|
(asRanges (firewall.allowedUDPPortRanges or [ ]))
|
|
|
|
];
|
|
|
|
}
|
|
|
|
];
|
|
|
|
source_ranges = [ "0.0.0.0/0" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
resource.google_compute_disk."${name}" = {
|
|
|
|
inherit zone;
|
|
|
|
name = "${name}-disk";
|
|
|
|
size = 100;
|
|
|
|
};
|
|
|
|
|
|
|
|
resource.null_resource.deploy_nixos = {
|
|
|
|
triggers = {
|
|
|
|
# Redeploy when the NixOS configuration changes.
|
|
|
|
os = "${osPath}";
|
|
|
|
# Redeploy when a new machine is provisioned.
|
|
|
|
machine_id = "\${google_compute_instance.${name}.id}";
|
2022-01-30 17:06:58 +01:00
|
|
|
};
|
|
|
|
|
2021-12-30 06:15:27 +01:00
|
|
|
connection = {
|
|
|
|
host = "\${google_compute_instance.${name}.network_interface[0].access_config[0].nat_ip}";
|
2022-01-30 17:06:58 +01:00
|
|
|
};
|
|
|
|
|
2021-12-30 06:15:27 +01:00
|
|
|
provisioner = [
|
|
|
|
{ remote-exec.inline = [ "true" ]; }
|
|
|
|
{
|
|
|
|
local-exec.command = ''
|
|
|
|
export PATH="${pkgs.openssh}/bin:$PATH"
|
2022-01-30 17:06:58 +01:00
|
|
|
|
2021-12-30 06:15:27 +01:00
|
|
|
scratch="$(mktemp -d)"
|
|
|
|
function cleanup() {
|
|
|
|
rm -rf $scratch
|
|
|
|
}
|
|
|
|
trap cleanup EXIT
|
2022-01-30 17:06:58 +01:00
|
|
|
|
2021-12-30 06:15:27 +01:00
|
|
|
# write out ssh key
|
|
|
|
echo -n "''${tls_private_key.${name}.private_key_pem}" > $scratch/id_rsa.pem
|
|
|
|
chmod 0600 $scratch/id_rsa.pem
|
2022-01-30 17:06:58 +01:00
|
|
|
|
2021-12-30 06:15:27 +01:00
|
|
|
export NIX_SSHOPTS="\
|
|
|
|
-o StrictHostKeyChecking=no\
|
|
|
|
-o UserKnownHostsFile=/dev/null\
|
|
|
|
-o GlobalKnownHostsFile=/dev/null\
|
|
|
|
-o IdentityFile=$scratch/id_rsa.pem
|
|
|
|
"
|
2022-01-30 17:06:58 +01:00
|
|
|
|
2021-12-30 06:15:27 +01:00
|
|
|
nix-build ${drvPath}
|
|
|
|
nix-copy-closure --to \
|
|
|
|
root@''${google_compute_instance.${name}.network_interface[0].access_config[0].nat_ip} \
|
|
|
|
${osPath} --gzip --use-substitutes
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
{
|
|
|
|
remote-exec.inline = [
|
|
|
|
"nix-env --profile /nix/var/nix/profiles/system --set ${osPath}"
|
|
|
|
"${osPath}/bin/switch-to-configuration switch"
|
|
|
|
];
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
}));
|
|
|
|
}
|