[env]
[env]required = ["DATABASE_URL", "API_KEY"]| Key | Type | Default | Description |
|---|---|---|---|
required | list of strings | [] | Environment variables that must resolve at build time. Any comptime env() lookup on a missing required variable fails the build. |
How it works
comptime let bindings call env(name, default?) inside the sandboxed interpreter at build time. If a name appears in [env] required but is not set in the build environment, the lookup fails the build with tyc::comptime:
comptime let DB_URL: str = env("DATABASE_URL")[env]required = ["DATABASE_URL"]Build without DATABASE_URL set:
error[tyc::comptime]: required environment variable `DATABASE_URL` is not set ┌─ src/main.ty:1:33 │1 │ comptime let DB_URL: str = env("DATABASE_URL") │ ^^^^^^^^^^^^^^^^^^^^Build with it set:
DATABASE_URL=postgres://localhost/myapp tyc build# success; DB_URL inlined as the literal "postgres://localhost/myapp"Why declare requirements?
You could rely on env("X") failing the build implicitly — a missing key raises a sandbox error. Listing them in [env] required does two extra things:
- Documents the contract. Anyone reading the manifest sees what the build needs.
- Fail-fast at the start of the build. The compiler can check the environment before doing any work — missing required vars surface immediately, not after partial compilation.
Optional env vars
Use env("NAME", "default") for optional values. No need to list in [env] required — the default kicks in if unset:
comptime let PORT: int = int(env("PORT", "8080"))What env vars are not
- Not Python’s
os.environ. That’s a runtime concern.comptime env()reads the build-time environment only. - Not secrets-management. Treat values inlined into the build as visible in the emitted Python. For secret-rotation, read at runtime instead.
CI integration
- name: Build env: DATABASE_URL: ${{ secrets.DATABASE_URL }} API_KEY: ${{ secrets.API_KEY }} run: tyc buildMake sure CI has the required vars before running tyc build, the same way you’d ensure they’re set in production.
Where next
comptime— the sandboxed evaluator.- Build-Time Env Validation (recipe) — worked patterns.