Skip to main content

Module env

Module env 

Source
Expand description

Environment variables, and .env files, as HOCON config.

This is the bulk-mount case: a whole prefixed namespace becomes a config subtree. Reading one variable needs nothing from here — HOCON’s own ${?VAR} already does that.

§How names become paths

__ is the only thing that creates hierarchy. A single _ stays part of the segment, and a literal . is key text, not a separator (spec F1.2):

APP_DB__MAX_CONN=10   ->  db.max_conn    (nested)
APP_FOO.BAR=flat      ->  "foo.bar"      (one key that contains a dot)

The two spellings are distinct paths, so they coexist: the first is read as cfg.get_string("foo.bar"), the second needs the quoted path cfg.get_string("\"foo.bar\""). Segments are lowercased after mapping (F1.3).

Two names that really do map to one path (APP_A__B and APP_a__b) are an error, not a last-wins, because environment iteration order is not deterministic (F1.6). A .env file has a definite line order, so there the later line wins (F0.7).

§Non-UTF-8 entries

load errors when an entry matching the mount prefix has a name or value that is not valid UTF-8 (spec F1.9b). A bulk mount is a request for a whole namespace, so omitting one key would produce a subtree that looks complete while the operator’s setting is missing, and a stale config default would then win invisibly. It would also defeat F1.6: if the undecodable entry were dropped, a colliding name would silently win, making the surviving value depend on an encoding property of the other value — precisely the nondeterminism F1.6 exists to forbid.

Entries that do not match the prefix are ignored regardless, so an undecodable variable elsewhere in the environment never fails a mount. That is the opposite of the ${VAR} path, where an undecodable entry is simply absent (F1.9a) because ${?VAR} explicitly means “optional”.

Structs§

Options
How variable names become config paths.

Functions§

load
Mount a prefixed slice of the process environment.
load_from
Mount a prefixed slice of the supplied variables. Used by load, and by tests that must not touch the real environment.
parse_dotenv
Read .env file content.