Skip to main content
Version: 2.x

DeriveConfig

DeriveConfig[A] is the type class behind derivation. It holds a Config[A], and deriveConfig[A] returns that description. The zio-config-magnolia module provides instances for common types and derives instances for case classes and sealed traits at compile time.

You need DeriveConfig directly in two cases: to give derivation an instance for a type it can't derive, and to look up which types work out of the box.

Definitionโ€‹

DeriveConfig is a case class that wraps a description. On Scala 2 it also records whether the type is a case object:

final case class DeriveConfig[T](desc: Config[T], isObject: Boolean = false, constValue: Option[T] = None)

On Scala 3 it carries metadata about names instead:

final case class DeriveConfig[A](desc: Config[A], metadata: Option[DeriveConfig.Metadata] = None)

The extra fields are used by derivation itself. Code that builds an instance uses DeriveConfig.apply, the transformation methods below, or (on Scala 3) DeriveConfig.from.

Getting a Descriptionโ€‹

The examples on this page use these imports:

import zio._
import zio.config._
import zio.config.magnolia._

final case class Endpoint(host: String, port: Int)

deriveConfig[A] summons the instance and returns its description. DeriveConfig[A] returns the instance itself, and DeriveConfig#desc is its description:

val viaFunction: Config[Endpoint] = deriveConfig[Endpoint]
val instance: DeriveConfig[Endpoint] = DeriveConfig[Endpoint]
val viaInstance: Config[Endpoint] = instance.desc

Built-in Instancesโ€‹

DeriveConfig has instances for these types. Each one uses the ZIO Config description in the second column:

TypeDescription used
StringConfig.string
BooleanConfig.boolean
Byte, Short, Int, LongConfig.byte, Config.short, Config.int, Config.long
BigIntConfig.bigInt
Float, Double, BigDecimalConfig.float, Config.double, Config.bigDecimal
java.util.UUIDConfig.uuid
java.net.URIConfig.uri
zio.DurationConfig.duration
java.time.LocalDate, LocalTime, LocalDateTime, OffsetDateTimeConfig.localDate, Config.localTime, Config.localDateTime, Config.offsetDateTime
zio.LogLevelConfig.logLevel
zio.Config.SecretConfig.secret
Option[A]Config#optional
Either[A, B]Config#orElseEither: tries A, then B
List[A], Seq[A]Config.listOf
Vector[A], Set[A]Config.vectorOf, Config.setOf
Chunk[A], NonEmptyChunk[A]Config.chunkOf, Config.nonEmptyChunkOf
Map[String, A]Config.table

The collection and Option instances need an instance for the element type. With zio-config-refined, Refined[A, P] also has an instance when A has one; see Validation.

How Types Become Descriptionsโ€‹

Derivation follows fixed rules for case classes and sealed traits:

Scala shapeDescription produced
Case class with fieldsEvery field read with Config#nested(fieldName) and combined; a field is required unless its type is Option or it has a default value
Field with a default valueThat field's description with Config#withDefault(default)
Case object, or case class without fieldsConfig.constant(name): the value at the current path must equal the name
Sealed traitOne alternative per subtype, joined with Config#orElse; subtypes with fields sit under Config#nested(subtypeName)
Sealed trait with @discriminator(key)Config.string(key) followed by Config#switch over the subtype names
Value class (Scala 2 only)The description of its single field, without an extra key

The annotations that change the names are described on the Derivation page.

Writing an Instanceโ€‹

To support a type that has no instance, transform an existing instance. The methods mirror the ones on Config:

MethodUse it when
DeriveConfig#mapThe conversion always succeeds
DeriveConfig#mapAttemptThe conversion can throw; the exception becomes an InvalidData error
DeriveConfig#mapOrFailThe conversion returns Either[Config.Error, B]
DeriveConfig#describe, DeriveConfig#??You add a description for documentation generation

To read a java.nio.file.Path from a string, with a description:

import java.nio.file.{Path, Paths}

implicit val derivePath: DeriveConfig[Path] =
DeriveConfig[String].mapAttempt(Paths.get(_)) ?? "a file system path"

final case class Storage(dataDir: Path, maxFiles: Int)

With that instance in scope, Storage derives as usual:

Unsafe.unsafe { implicit unsafe =>
Runtime.default.unsafe.run(
ConfigProvider.fromMap(Map("dataDir" -> "/var/lib/app", "maxFiles" -> "100")).load(deriveConfig[Storage])
).getOrThrow()
}
// res1: Storage = Storage(dataDir = /var/lib/app, maxFiles = 100)

To build an instance from a hand-written description, wrap it with DeriveConfig.apply on Scala 2, or DeriveConfig.from on Scala 3:

final case class HostPort(host: String, port: Int)

implicit val deriveHostPort: DeriveConfig[HostPort] =
DeriveConfig(Config.string.mapOrFail { text =>
text.split(":") match {
case Array(host, port) if port.forall(_.isDigit) => Right(HostPort(host, port.toInt))
case _ => Left(Config.Error.InvalidData(message = s"expected host:port, got $text"))
}
})

A field of type HostPort now reads one value such as db.internal:5432. Leave the key out of the hand-written description (Config.string, not Config.string("name")): derivation adds the field name.

A case class with a HostPort field reads it from a single key:

final case class Replica(primary: HostPort)

Unsafe.unsafe { implicit unsafe =>
Runtime.default.unsafe.run(
ConfigProvider.fromMap(Map("primary" -> "db.internal:5432")).load(deriveConfig[Replica])
).getOrThrow()
}
// res2: Replica = Replica(
// primary = HostPort(host = "db.internal", port = 5432)
// )

On Scala 3, any given Config[A] in scope is also used as a DeriveConfig[A], so given Config[HostPort] = ... works as well.

Scala 2 and Scala 3 Differencesโ€‹

Derivation uses Magnolia on Scala 2 and Scala 3 mirrors on Scala 3. The results are the same except for these points:

BehaviourScala 2Scala 3
Value class (extends AnyVal)Derived and unwrappedNeeds an instance
@describe on a case classAdded to each field's descriptionIgnored; @describe on a field works
Annotations re-exported from zio.config.magnoliaAlldescribe, name, discriminator, kebabCaseLegacy only; import the others from zio.config.derivation
derives Config on a case classNot availableAvailable
ConfigProvider#autoLoad[A]Not availableLoads A with its derived description
A given Config[A] used as DeriveConfig[A]NoYes
Building an instance from a Config[A]DeriveConfig(config)DeriveConfig.from(config) or DeriveConfig(config)

@describe on a sealed trait is ignored on both versions.

Limitsโ€‹

  • Recursive types are not supported.
  • Scala 3 enum types are not covered by tests; use a sealed trait.
  • On Scala 2, two subtypes with the same short name in one sealed trait are read as Name_0 and Name_1; Scala 3 does not rename them. Rename one with @name.

Next Stepsโ€‹

Derivation shows derivation step by step, with the annotations. The functions every derived description supports are listed in Config descriptors.

ZIO Chat

Hi there! ๐Ÿ‘‹ I'm ZIO Chat, your friendly neighborhood ZIO expert. I'm here to help you navigate the world of ZIO. Whether you're a seasoned pro or just starting out, fire away with your ZIO questions. I will hit you back with the best insights I've got. Let's dive into the world of ZIO together!