Files
lth-coder-template/main.tf
T

265 lines
7.8 KiB
Terraform

terraform {
required_providers {
coder = {
source = "coder/coder"
}
docker = {
source = "kreuzwerker/docker"
}
}
}
provider "docker" {}
data "coder_provisioner" "me" {}
data "coder_workspace" "me" {}
data "coder_workspace_owner" "me" {}
locals {
username = data.coder_workspace_owner.me.name
docker_name = lower("coder-${data.coder_workspace.me.template_name}-${local.username}-${data.coder_workspace.me.id}")
}
resource "coder_agent" "main" {
arch = data.coder_provisioner.me.arch
os = "linux"
display_apps {
vscode = false
}
startup_script = <<-EOT
set -e
# --- Logging Helper ---
log() {
echo -e "\e[1;34m[INIT]\e[0m $(date +'%H:%M:%S') - $1"
}
# 1. Prepare user home with default files
if [ ! -f ~/.init_done ]; then
log "Fresh workspace detected. Starting first-time setup..."
log "Copying system skeleton files to home..."
cp -rT /etc/skel ~
# 2. Install GHCup & Haskell
log "Installing GHCup, GHC, Cabal, and HLS (this will take a few minutes)..."
curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | \
BOOTSTRAP_HASKELL_NONINTERACTIVE=1 \
BOOTSTRAP_HASKELL_INSTALL_GHCUP=1 \
BOOTSTRAP_HASKELL_GHC_VERSION=latest \
BOOTSTRAP_HASKELL_CABAL_VERSION=latest \
BOOTSTRAP_HASKELL_HLS_VERSION=latest \
sh
log "Haskell toolchain installed successfully."
# 3. Install Scala (Coursier)
log "Downloading Coursier for Scala management..."
curl -fL https://github.com/coursier/launchers/raw/master/cs-x86_64-pc-linux.gz | gzip -d > cs
chmod +x cs
log "Running Scala setup (Installing scala, scalac, sbt, etc.)..."
./cs setup --yes
rm cs
log "Scala environment ready."
# 4. Python Venv
log "Creating Python virtual environment in ~/.venv..."
python3 -m venv ~/.venv
log "Python venv created. Tip: Use 'source ~/.venv/bin/activate' to use it."
# 5. Finalize
touch ~/.init_done
log "Setup complete! All tools are installed in your persistent home volume."
else
log "Persistent volume detected. Tools are already installed in /home/${local.username}."
fi
# This runs every time, even if setup is already done
log "Ensuring PATH is updated for current session..."
# Add GHCup and local bins to path if not already there
[ -f "$HOME/.ghcup/env" ] && source "$HOME/.ghcup/env"
log "Workspace is ready for coding."
EOT
# These environment variables allow you to make Git commits right away after creating a
# workspace. Note that they take precedence over configuration defined in ~/.gitconfig!
# You can remove this block if you'd prefer to configure Git manually or using
# dotfiles. (see docs/dotfiles.md)
# env = {
# GIT_AUTHOR_NAME = coalesce(data.coder_workspace_owner.me.full_name, data.coder_workspace_owner.me.name)
# GIT_AUTHOR_EMAIL = "${data.coder_workspace_owner.me.email}"
# GIT_COMMITTER_NAME = coalesce(data.coder_workspace_owner.me.full_name, data.coder_workspace_owner.me.name)
# GIT_COMMITTER_EMAIL = "${data.coder_workspace_owner.me.email}"
# }
# The following metadata blocks are optional. They are used to display
# information about your workspace in the dashboard. You can remove them
# if you don't want to display any information.
# For basic resources, you can use the `coder stat` command.
# If you need more control, you can write your own script.
metadata {
display_name = "CPU Usage"
key = "0_cpu_usage"
script = "coder stat cpu"
interval = 10
timeout = 1
}
metadata {
display_name = "RAM Usage"
key = "1_ram_usage"
script = "coder stat mem"
interval = 10
timeout = 1
}
metadata {
display_name = "Home Disk"
key = "3_home_disk"
script = "coder stat disk --path $${HOME}"
interval = 60
timeout = 1
}
}
module "vscode" {
count = data.coder_workspace.me.start_count
source = "registry.coder.com/coder/vscode-desktop/coder"
version = "1.1.1"
agent_id = coder_agent.main.id
folder = "/home/${local.username}"
open_recent = true
}
module "vscode-web" {
count = data.coder_workspace.me.start_count
source = "registry.coder.com/coder/vscode-web/coder"
version = "1.4.1"
agent_id = coder_agent.main.id
folder = "/home/${local.username}"
accept_license = true
extensions = [
"scalameta.metals",
"James-Yu.latex-workshop",
"scala-lang.scala",
"ms-python.python",
"formulahendry.code-runner",
"ritwickdey.LiveServer",
"ms-python.isort",
"njpwerner.autodocstring",
"ms-vscode.hexeditor",
"vscjava.vscode-java-pack"
]
settings = {
"workbench.startupEditor": "none",
"workbench.colorTheme": "Default Dark Modern",
"window.menuBarVisibility": "classic",
"files.exclude": {
".*/": true
}
}
}
module "git-config" {
count = data.coder_workspace.me.start_count
source = "registry.coder.com/coder/git-config/coder"
version = "1.0.32"
agent_id = coder_agent.main.id
allow_email_change = true
}
module "git-commit-signing" {
count = data.coder_workspace.me.start_count
source = "registry.coder.com/coder/git-commit-signing/coder"
version = "1.0.32"
agent_id = coder_agent.main.id
}
resource "docker_volume" "home_volume" {
name = local.docker_name
# Protect the volume from being deleted due to changes in attributes.
lifecycle {
ignore_changes = all
}
# Add labels in Docker to keep track of orphan resources.
labels {
label = "coder.owner"
value = data.coder_workspace_owner.me.name
}
labels {
label = "coder.owner_id"
value = data.coder_workspace_owner.me.id
}
labels {
label = "coder.workspace_id"
value = data.coder_workspace.me.id
}
# This field becomes outdated if the workspace is renamed but can
# be useful for debugging or cleaning out dangling volumes.
labels {
label = "coder.workspace_name_at_creation"
value = data.coder_workspace.me.name
}
}
resource "docker_image" "main" {
name = local.docker_name
build {
context = "./build"
build_args = {
USER = local.username
}
}
triggers = {
dir_sha1 = sha1(join("", [for f in fileset(path.module, "build/*") : filesha1(f)]))
}
}
resource "docker_container" "workspace" {
count = data.coder_workspace.me.start_count
image = docker_image.main.name
cpu_set = "0-11"
memory = 8192
# Uses lower() to avoid Docker restriction on container names.
name = "coder-${data.coder_workspace_owner.me.name}-${lower(data.coder_workspace.me.name)}"
# Hostname makes the shell more user friendly: coder@my-workspace:~$
hostname = data.coder_workspace.me.name
# Use the docker gateway if the access URL is 127.0.0.1
entrypoint = ["sh", "-c", replace(coder_agent.main.init_script, "/localhost|127\\.0\\.0\\.1/", "host.docker.internal")]
env = ["CODER_AGENT_TOKEN=${coder_agent.main.token}"]
host {
host = "host.docker.internal"
ip = "host-gateway"
}
volumes {
container_path = "/home/${local.username}"
volume_name = docker_volume.home_volume.name
read_only = false
}
labels {
label = "net.unraid.docker.icon"
value = "https://u.stws.cc/ubuntu-logo.png"
}
# Add labels in Docker to keep track of orphan resources.
labels {
label = "coder.owner"
value = data.coder_workspace_owner.me.name
}
labels {
label = "coder.owner_id"
value = data.coder_workspace_owner.me.id
}
labels {
label = "coder.workspace_id"
value = data.coder_workspace.me.id
}
labels {
label = "coder.workspace_name"
value = data.coder_workspace.me.name
}
}