Configuration
Config separates what a program needs from where settings live. A Config.Config<T> value is an immutable descriptor—building one reads nothing. Running try descriptor decodes a typed value from the active ConfigProvider, failing with ConfigError when a key is missing or invalid.
Declare typed configuration
const Host = Config.NonEmptyString("HOST")
const Port = Config.Port("PORT")
effect function serverSettings() {
const host = try Host
const port = try Port
return { host, port }
}
// { host: string; port: number }!ConfigError
// Run in an effect function: provide ConfigProvider.layer(ConfigProvider.fromUnknown({ HOST: "localhost", PORT: 9091 })) { { const jsonValue = try serverSettings(); try Console.log(try Effect.try(() => JSON.stringify(jsonValue))) } }
// Output: {"host":"localhost","port":9091}Constructing Host and Port performs no I/O. Inside serverSettings, try Host and try Port read from the active provider—by default the process environment—so .vibe code never touches process.env directly.
| Constructor | Decoded value |
|---|---|
Config.String, Config.NonEmptyString | String, optionally non-empty |
Config.Finite, Config.Int, Config.Port | Finite number, integer, or TCP port (1–65535) |
Config.Boolean | Boolean (case-sensitive: true/false, yes/no, on/off, 1/0, y/n) |
Config.Literal, Config.Literals | One literal value, or membership in a set |
Config.Duration, Config.Date, Config.URL | Parsed duration, date, or URL |
Config.LogLevel | Effect log severity |
Config.Redacted | Secret string masked in logs and formatting |
Prefer Config.Finite over Config.Number for numeric settings; Number also accepts NaN and Infinity. Compose descriptors as data with Config.all, Config.nested, and Config.withDefault, then execute the composed descriptor with try.
Parse with schemas
effect function teamName() {
return try Config.schema(
Schema.String.check(Schema.isMinLength(3)),
"TEAM_NAME"
)
}
// string!ConfigError
// Run in an effect function:
// provide ConfigProvider.layer(ConfigProvider.fromUnknown({ TEAM_NAME: "search" })) { try Console.log(try teamName()) }
// provide ConfigProvider.layer(ConfigProvider.fromUnknown({ TEAM_NAME: "ab" })) { teamName() catch effect (error) => { try Console.log(error._tag) } }
// Output: search
// Output: ConfigErrorConfig.schema turns any Schema into a Config descriptor. Constraints live in the schema itself, so malformed values fail with ConfigError at load time rather than deeper in application logic. Structured schemas map field paths directly to nested object properties or environment keys.
// descriptors.vibe
export const server = Config.schema(Schema.Struct({
host: Schema.String,
port: Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 65535 }))
}), "server")
// Run in an effect function:
// {
// const jsonValue = try server.parse(ConfigProvider.fromUnknown({ server: { host: "localhost", port: 9091 } }))
// try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
// }
// Output: {"host":"localhost","port":9091}import { server } from "./descriptors.vibe"
effect function localServer() {
return try server.parse(ConfigProvider.fromUnknown({
server: { host: "127.0.0.1", port: 9091 }
}))
}
// Run in an effect function:
// {
// const jsonValue = try localServer()
// try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
// }
// Output: {"host":"127.0.0.1","port":9091}Call try descriptor.parse(provider) to decode against an explicit provider—ideal in tests—instead of reading the ambient provider via try descriptor.
Read arrays and records
effect function routingSettings() {
const regions = try Config.Array(Schema.String, "REGIONS")
const labels = try Config.Record(Schema.String, Schema.String, "LABELS")
return { regions, labels }
}
// Run in an effect function: provide ConfigProvider.layer(ConfigProvider.fromUnknown({ REGIONS: "north,south", LABELS: "team=search,tier=internal" })) { { const jsonValue = try routingSettings(); try Console.log(try Effect.try(() => JSON.stringify(jsonValue))) } }
// Output: {"regions":["north","south"],"labels":{"team":"search","tier":"internal"}}Config.Array accepts either a native array or a delimiter-separated string (default ,); Config.Record accepts either an object or key=value pairs. Both accept custom separator options. To reject string encodings and require structural input only, pass a plain array or record schema to Config.schema.
Nest and combine configuration
// database-config.vibe
export const database = Config.nested(Config.all({
host: Config.NonEmptyString("HOST"),
port: Config.Port("PORT")
}), "DATABASE")
// Run in an effect function:
// {
// const jsonValue = try database.parse(ConfigProvider.fromEnv({ env: { DATABASE_HOST: "db.internal", DATABASE_PORT: "5433" } }))
// try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
// }
// Output: {"host":"db.internal","port":5433}import { database } from "./database-config.vibe"
effect function testDatabaseSettings() {
return try database.parse(ConfigProvider.fromEnv({
env: { DATABASE_HOST: "db.internal", DATABASE_PORT: "5433" }
}))
}
// Run in an effect function:
// {
// const jsonValue = try testDatabaseSettings()
// try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
// }
// Output: {"host":"db.internal","port":5433}Config.all combines descriptors while preserving their tuple or object shape. Config.nested prepends a path segment to every child lookup; fromEnv joins path segments with _. Libraries accepting arbitrary nested records of descriptors can type the parameter as Config.Wrap<T> and collapse it with Config.unwrap.
Defaults and fallbacks
// optional-config.vibe
export const port = Config.withDefault(Config.Port("PORT"), 9091)
export const apiKey = Config.option(Config.String("API_KEY"))
export const host = Config.orElse(
Config.NonEmptyString("HOST"),
() => Config.NonEmptyString("BACKUP_HOST")
)
// Run in an effect function:
// const provider = ConfigProvider.fromUnknown({ BACKUP_HOST: "standby.internal" })
// try Console.log(try port.parse(provider), Option.isNone(try apiKey.parse(provider)), try host.parse(provider))
// port.parse(ConfigProvider.fromUnknown({ PORT: "invalid" })) catch effect (error) => { try Console.log(error._tag) }
// Output: 9091 true standby.internal
// Output: ConfigErrorimport { port, apiKey, host } from "./optional-config.vibe"
effect function defaultsExample() {
const provider = ConfigProvider.fromUnknown({ BACKUP_HOST: "standby.internal" })
return {
port: try port.parse(provider),
apiKey: try apiKey.parse(provider),
host: try host.parse(provider)
}
}
// Run in an effect function:
// const result = try defaultsExample()
// try Console.log(result.port, Option.isNone(result.apiKey), result.host)
// Output: 9091 true standby.internal| Combinator | On missing key | On invalid value |
|---|---|---|
Config.withDefault | Returns fallback value | Fails with ConfigError |
Config.option | Returns Option.none() | Fails with ConfigError |
Config.orElse | Tries secondary descriptor | Tries secondary descriptor |
Because orElse recovers from any ConfigError, including invalid data, use it deliberately. Placing withDefault on a Config.all group replaces the entire group if any child is absent (provided no other child failed validation or I/O); attach defaults to individual fields to preserve supplied siblings.
Transform parsed values
// origin-config.vibe
export const origin = Config.map(Config.all({
host: Config.NonEmptyString("HOST"),
port: Config.Port("PORT")
}), ({ host, port }) => `https://${host}:${port}`)
// Run in an effect function: try Console.log(try origin.parse(ConfigProvider.fromUnknown({ HOST: "example.test", PORT: 9091 })))
// Output: https://example.test:9091import { origin } from "./origin-config.vibe"
effect function reportOrigin() {
const value = try origin
try Console.log(value)
return value
}
// Run in an effect function: provide ConfigProvider.layer(ConfigProvider.fromUnknown({ HOST: "example.test", PORT: 9091 })) { try reportOrigin() }
// Output: https://example.test:9091Use Config.map for infallible transformations, a Schema for validation, and Config.mapEffect when transformation requires an Effect failing with ConfigError. If a derived value does not need to be shared as a descriptor, load the raw settings first and compute it with ordinary statements.
Protect secrets
effect function secretSetting() {
const token = try Config.Redacted("ACCESS_TOKEN")
try Console.log(String(token))
return token
}
// Run in an effect function: provide ConfigProvider.layer(ConfigProvider.fromUnknown({ ACCESS_TOKEN: "documentation-placeholder" })) { try Console.log(String(try secretSetting())) }
// Output: <redacted>
// Output: <redacted>Config.Redacted wraps a string so String(...), JSON serialization, and loggers print <redacted>. Unwrap with Redacted.value(token) only at the exact call site that sends the credential. For non-string secrets, combine Config.schema with Schema.RedactedFromValue (for example, wrapping Schema.FiniteFromString).
Load providers
const TestProvider = ConfigProvider.fromUnknown({ PORT: 9091 })
const EnvProvider = ConfigProvider.fromEnv({ env: { PORT: "9092" } })
const DotEnvProvider = ConfigProvider.fromDotEnvContents("PORT=9093")
effect function testPort() {
provide ConfigProvider.layer(TestProvider) { return try Config.Port("PORT") }
}
effect function fileProvider() {
return try ConfigProvider.fromDotEnv({ path: ".env.local" })
}
// Run in an effect function:
// try Console.log(try testPort(), try Config.Port("PORT").parse(EnvProvider), try Config.Port("PORT").parse(DotEnvProvider))
// provide FileSystem.layerNoop({ readFileString: effect (path) => { try Console.log(path); return "PORT=9095" } }) { try Console.log(try Config.Port("PORT").parse(try fileProvider())) }
// Output: 9091 9092 9093
// Output: .env.local
// Output: 9095| Provider | Source | Platform layers required |
|---|---|---|
ConfigProvider.fromUnknown | Plain JS objects and arrays | None |
ConfigProvider.fromEnv | Process environment, or { env } override | None |
ConfigProvider.fromDotEnvContents | .env string already in memory | None |
ConfigProvider.fromDotEnv | .env file on disk | FileSystem |
ConfigProvider.fromDir | Directory tree of secret/config files | FileSystem, Path |
ConfigProvider.make | Custom backing store | Whatever your store requires |
fromUnknown and fromEnv treat empty strings as missing keys; pass preserveEmptyStrings: true when "" is a valid value.
Remap provider paths
const DeploymentProvider = ConfigProvider.constantCase(
ConfigProvider.fromEnv({ env: { SERVICE_PORT: "9094" } })
)
effect function deploymentPort() {
provide ConfigProvider.layer(DeploymentProvider) { return try Config.Port("servicePort") }
}
// Run in an effect function: try Console.log(try deploymentPort())
// Output: 9094Path adapters decouple descriptor names from backend conventions: constantCase maps servicePort to SERVICE_PORT, ConfigProvider.nested prepends a fixed prefix, and ConfigProvider.mapInput rewrites arbitrary path segments. Transform the provider when an entire source shares a convention; use Config.nested when one group of descriptors has a prefix.
Compose and install providers
const Combined = ConfigProvider.orElse(
ConfigProvider.fromEnv({ env: { HOST: "edge.internal" } }),
ConfigProvider.fromUnknown({ HOST: "localhost", PORT: 9091 })
)
effect function configuredServer() {
provide ConfigProvider.layer(Combined) {
return { host: try Config.String("HOST"), port: try Config.Port("PORT") }
}
}
// Run in an effect function:
// {
// const jsonValue = try configuredServer()
// try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
// }
// Output: {"host":"edge.internal","port":9091}ConfigProvider.orElse consults the secondary provider only when a path is absent—it never hides source I/O failures or validation errors. ConfigProvider.layer replaces the active provider for its block; ConfigProvider.layerAdd appends a fallback, or prepends a higher-precedence override when given { asPrimary: true }.
Coming from Effect
Compose Config descriptors directly as values in .vibe, then read them with try descriptor or try descriptor.parse(provider). Returning a descriptor does not execute it—just as returning an Exit, a service key, an error instance, or new ... never runs an Effect.
The matching Effect page: effect.website/docs/v4/configuration