ZIO Core Providers
ZIO itself ships the ConfigProvider type and several providers: environment variables, system properties, command-line arguments and maps. It also has a built-in way to read configuration anywhere in a program, ZIO.config. ZIO Config builds on both: every provider from the ZIO Config modules is an ordinary ConfigProvider, so everything on this page works with HOCON, YAML, TOML, XML and AWS sources too.
Reading Config With ZIO.configโ
ZIO.config(description) returns a ZIO[Any, Config.Error, A] that loads the description from the provider the ZIO runtime holds. When the description is available as an implicit Config[A], the short form ZIO.config[A] finds it. Two functions change which provider that is:
| Function | Effect |
|---|---|
Runtime.setConfigProvider(provider) | A ZLayer that replaces the runtime's provider; put it in ZIOAppDefault#bootstrap |
ZIO.withConfigProvider(provider)(effect) | Runs one effect with another provider |
Without either, the provider is ConfigProvider.defaultProvider, which reads environment variables first and system properties second. To run one effect with a map as its source:
import zio._
val port: IO[Config.Error, Int] =
ZIO.withConfigProvider(ConfigProvider.fromMap(Map("port" -> "9999")))(ZIO.config(Config.int("port")))
ConfigProvider#load and ZIO.config read a description the same way. Use ConfigProvider#load when you hold the provider yourself, and ZIO.config when the provider is set for the whole program. How to set the config provider for a whole application shows the full setup, with bootstrap, layers and tests.
Built-in Providersโ
The examples below share these definitions and a helper, show, which loads a description from a provider:
import zio._
import zio.config._
final case class Database(url: String, poolSize: Int)
val database: Config[Database] =
(Config.string("url") zip Config.int("poolSize")).to[Database].nested("db")
def show[A](provider: ConfigProvider, config: Config[A]): Either[Config.Error, A] =
Unsafe.unsafe { implicit unsafe =>
Runtime.default.unsafe.run(provider.load(config).either).getOrThrow()
}
ZIO core has these providers. The path delimiter joins the segments of a nested key, and the sequence delimiter splits one value into a list:
| Provider | Reads | Path delimiter | Sequence delimiter |
|---|---|---|---|
ConfigProvider.fromMap(map, pathDelim, seqDelim) | A Map[String, String] | . | , |
ConfigProvider.fromEnv(pathDelim, seqDelim) | Environment variables | _ | , |
ConfigProvider.envProvider | Same as fromEnv() | _ | , |
ConfigProvider.fromProps(pathDelim, seqDelim) | JVM system properties | . | , |
ConfigProvider.propsProvider | Same as fromProps() | . | , |
ConfigProvider.fromAppArgs(args, pathDelim, seqDelim) | Command-line arguments | . | none |
ConfigProvider.defaultProvider | envProvider, then propsProvider | โ | โ |
All of them except ConfigProvider.fromAppArgs split values on commas by default, when a key is read as a list. The providers from the ZIO Config modules don't; see Config providers.
Mapsโ
ConfigProvider.fromMap reads a Map[String, String]. It is the simplest provider and the usual choice in tests:
show(ConfigProvider.fromMap(Map("db.url" -> "postgres://localhost/app", "db.poolSize" -> "10")), database)
// res1: Either[Config.Error, Database] = Right(
// value = Database(url = "postgres://localhost/app", poolSize = 10)
// )
A value that holds commas is split when it is read as a list:
show(ConfigProvider.fromMap(Map("ports" -> "8080,8443")), Config.listOf("ports", Config.int))
// res2: Either[Config.Error, List[Int]] = Right(value = List(8080, 8443))
Environment Variablesโ
ConfigProvider.envProvider joins the segments of a path with _, turns - into _, and upper-cases the result before it looks it up, so the description above reads DB_URL and DB_POOLSIZE. The field name poolSize has no word boundary in that name; to read DB_POOL_SIZE, add ConfigProvider#snakeCase. The environment can't be changed inside these docs, so the next example uses ConfigProvider.fromMap with the same delimiter, plus ConfigProvider#upperCase, because a map provider doesn't upper-case keys:
val envLike: ConfigProvider =
ConfigProvider.fromMap(Map("DB_URL" -> "postgres://db/app", "DB_POOL_SIZE" -> "20"), pathDelim = "_")
With ConfigProvider#snakeCase and ConfigProvider#upperCase, the key db.poolSize is looked up as DB_POOL_SIZE:
show(envLike.snakeCase.upperCase, database)
// res3: Either[Config.Error, Database] = Right(
// value = Database(url = "postgres://db/app", poolSize = 20)
// )
With the real environment, ConfigProvider.envProvider.snakeCase is enough, because the environment provider already upper-cases every key. The names it lists, for example the keys of a Config.table, come back upper-cased too.
Command-Line Argumentsโ
ConfigProvider.fromAppArgs reads arguments of the form --key=value or --key value. In a ZIOAppDefault, get the arguments with ZIOAppDefault#getArgs:
show(ConfigProvider.fromAppArgs(ZIOAppArgs(Chunk("--db.url=postgres://cli/app", "--db.poolSize", "5"))), database)
// res4: Either[Config.Error, Database] = Right(
// value = Database(url = "postgres://cli/app", poolSize = 5)
// )
Combining and Adapting Providersโ
Providers compose. These functions on ConfigProvider return a new provider:
| Function | Effect |
|---|---|
ConfigProvider#orElse(that) | Use that for every key this provider can't supply |
ConfigProvider#nested(name) | Read everything from under the prefix name of this source |
ConfigProvider#unnested(name) | Strip the prefix name from the keys the description asks for |
ConfigProvider#upperCase | Upper-case the keys before looking them up |
ConfigProvider#lowerCase | Lower-case the keys before looking them up |
ConfigProvider#snakeCase | Convert camelCase keys to snake_case before looking them up |
ConfigProvider#kebabCase | Convert camelCase keys to kebab-case before looking them up |
ConfigProvider#contramapPath(f) | Apply any String => String to each key segment before looking it up |
ConfigProvider#within(path)(f) | Apply the transformation f only to the part of the source under path |
ConfigProvider#orElse is how you layer sources. The first provider wins for every key it has:
val overrides = ConfigProvider.fromMap(Map("db.poolSize" -> "50"))
val defaults = ConfigProvider.fromMap(Map("db.url" -> "postgres://localhost/app", "db.poolSize" -> "10"))
poolSize comes from overrides and url from defaults:
show(overrides.orElse(defaults), database)
// res5: Either[Config.Error, Database] = Right(
// value = Database(url = "postgres://localhost/app", poolSize = 50)
// )
ConfigProvider#nested reads a whole description from one part of a source, for example the staging part of a shared map:
val shared = ConfigProvider.fromMap(Map("staging.db.url" -> "postgres://staging/app", "staging.db.poolSize" -> "3"))
Nested under staging, the provider answers the description's db.url with staging.db.url:
show(shared.nested("staging"), database)
// res6: Either[Config.Error, Database] = Right(
// value = Database(url = "postgres://staging/app", poolSize = 3)
// )
The case conversions on a provider come from ZIO, and since ZIO 2.1.20 they split digits as their own word. ZIO Config's Config#toKebabCase and the @kebabCase annotation don't, so the two can disagree for names with digits:
val digits = Config.string("myValue123")
The provider looks the key up as my-value-123, while the description's own conversion asks for my-value123:
show(ConfigProvider.fromMap(Map("my-value-123" -> "from provider")).kebabCase, digits)
// res7: Either[Config.Error, String] = Right(value = "from provider")
show(ConfigProvider.fromMap(Map("my-value123" -> "from description")), digits.toKebabCase)
// res8: Either[Config.Error, String] = Right(value = "from description")
Pick one place to convert key names, either the description or the provider, and use it throughout.
Next Stepsโ
- Config providers compares every source, including the ZIO Config modules.
- How to override settings per environment layers providers for each deployment.
- How to test config uses
ConfigProvider.fromMapandZIO.withConfigProviderin tests.