To get started, create a module to manage your application’s environment variables (e.g. environment.rkt).
To specify which environment variables your application depends on, specify each variable’s name on a separate line:
Each line of your environment manifest will produce a variable with the given name bound to the value of the equivalent environment variable. The name of the environment variable will be generated from the name of the Racket variable by converting the identifier to ALL_CAPS, converting dashes to underscores, and stripping question marks. In the above example, Envy would fetch the values for SOME_ENVIRONMENT_VARIABLE and ANOTHER_ENVIRONMENT_VARIABLE.
When the module runs, the values for the specified variables will be loaded, but it’s possible that the variables don’t actually exist in the environment. In this case, an error will be thrown.
envy: The required environment variable
"SOME_ENVIRONMENT_VARIABLE" is not defined.
If the environment variable does exist, its value will be stored in the binding.
- : String
To use the values of these environment variables in another module, just require the module, optionally with a prefix.
All environment variables are natively strings, but it is extremely common to store other kinds of configuration data, such as booleans and numbers. Envy permits specifying a type with each variable, and it will automatically parse the value to match the specified type.
For example, given the following environment:
...one could use the following environment definition:
- : String
- : Boolean
- : Integer [more precisely: Positive-Integer]
Note that the values are defined with the specified types, useful for Typed Racket users. Also, since String is the default type, including it is not strictly necessary.
Sometimes, configuration variables may be optional, in which case it is useful to provide a default value instead of raising an error upon an undefined variable. This can be done with the #:default option.
optional-value #:default #f
- : (U False String)
Note that the type is still properly preserved for Typed Racket users, so if the default value’s type is different from the type of the environment variable, the resulting type will be a union.
Sometimes it is desired to define a variable with a different name from the name used in the environment. This can be done with the #:name option.
custom-name #:name "ENVIRONMENT_NAME"