From a2625656503c7b23c9f94f5e6d55ea4269c440cf Mon Sep 17 00:00:00 2001 From: Ben Potter Date: Fri, 24 Apr 2026 13:50:34 +0000 Subject: [PATCH] refactor(incus-vm): split NixOS provisioning into nixos.tf Move local.is_nixos and null_resource.provision_nixos out of main.tf into a dedicated nixos.tf to keep main.tf focused on core infrastructure. --- registry/bpmct/templates/incus-vm/main.tf | 113 +------------------- registry/bpmct/templates/incus-vm/nixos.tf | 117 +++++++++++++++++++++ 2 files changed, 118 insertions(+), 112 deletions(-) create mode 100644 registry/bpmct/templates/incus-vm/nixos.tf diff --git a/registry/bpmct/templates/incus-vm/main.tf b/registry/bpmct/templates/incus-vm/main.tf index 19982ade..881d4a20 100644 --- a/registry/bpmct/templates/incus-vm/main.tf +++ b/registry/bpmct/templates/incus-vm/main.tf @@ -208,9 +208,7 @@ resource "incus_image" "image" { remote = local.incus_remote source_image = { remote = "images" - # NixOS images on images.linuxcontainers.org use just "nixos/25.11" (no arch suffix in alias). - # Other distros like ubuntu append the arch: "ubuntu/jammy/cloud/amd64". - name = local.is_nixos ? data.coder_parameter.image.value : "${data.coder_parameter.image.value}/${data.coder_parameter.host.value == "ThinkStation" ? "amd64" : "arm64"}" + name = local.is_nixos ? data.coder_parameter.image.value : "${data.coder_parameter.image.value}/${data.coder_parameter.host.value == "ThinkStation" ? "amd64" : "arm64"}" type = "virtual-machine" architecture = data.coder_parameter.host.value == "ThinkStation" ? "x86_64" : "aarch64" } @@ -357,112 +355,6 @@ resource "null_resource" "token_refresh" { } } -# Provisioner for NixOS VMs. -# NixOS does not support cloud-init in the traditional sense. -# We use incus file push + nixos-rebuild to declare the user and coder-agent service. -resource "null_resource" "provision_nixos" { - count = data.coder_workspace.me.start_count == 1 && local.is_nixos ? 1 : 0 - - triggers = { - agent_token = local.agent_token - instance = incus_instance.dev.name - } - - depends_on = [incus_instance.dev] - - provisioner "local-exec" { - # Write the nix module and coder agent files into the VM, then run nixos-rebuild. - # We use incus file push for files containing sensitive values or complex content, - # and incus exec for commands. This avoids shell quoting issues with heredocs. - command = <<-EOT - set -e - REMOTE="${local.incus_remote}" - INSTANCE="${incus_instance.dev.name}" - WUSER="${local.workspace_user}" - ARCH="${data.coder_parameter.host.value == "ThinkStation" ? "amd64" : "arm64"}" - - echo "Waiting for NixOS VM incus-agent to be ready..." - for i in $(seq 1 60); do - if incus exec "$REMOTE:$INSTANCE" -- true 2>/dev/null; then - echo "incus-agent ready after $i attempts" - break - fi - echo "Attempt $i: incus-agent not ready yet, waiting..." - sleep 5 - done - - # Write init script into the VM - incus exec "$REMOTE:$INSTANCE" -- mkdir -p /opt/coder - echo "${base64encode(local.agent_init_script)}" | base64 -d | incus file push - "$REMOTE:$INSTANCE/opt/coder/init" - incus exec "$REMOTE:$INSTANCE" -- chmod 755 /opt/coder/init - - # Write env file into the VM - printf 'CODER_AGENT_TOKEN=${local.agent_token}\nCODER_AGENT_URL=${data.coder_workspace.me.access_url}\n' \ - | incus file push - "$REMOTE:$INSTANCE/opt/coder/init.env" --mode 0600 - - # Write the NixOS coder module, substituting the username - NIXMOD=$(cat </dev/null; then + echo "incus-agent ready after $i attempts" + break + fi + echo "Attempt $i: incus-agent not ready yet, waiting..." + sleep 5 + done + + # Write init script into the VM + incus exec "$REMOTE:$INSTANCE" -- mkdir -p /opt/coder + echo "${base64encode(local.agent_init_script)}" | base64 -d | incus file push - "$REMOTE:$INSTANCE/opt/coder/init" + incus exec "$REMOTE:$INSTANCE" -- chmod 755 /opt/coder/init + + # Write env file into the VM + printf 'CODER_AGENT_TOKEN=${local.agent_token}\nCODER_AGENT_URL=${data.coder_workspace.me.access_url}\n' \ + | incus file push - "$REMOTE:$INSTANCE/opt/coder/init.env" --mode 0600 + + # Write the NixOS coder module, substituting the username + NIXMOD=$(cat <