module use "std/env.nib"
Env
Configuration by name: Env.Vars reads the process environment, Env.Cfg reads values an operator supplied on purpose. Both answer by get, and Env.Process answers both.
Env is how a program asks for a setting it does not hard-code: a port, a data directory, an API base URL. Both interfaces have one operation, get(name), which answers the value as a String and raises Err.Missing when there is none. "Not set" and "set to the empty string" are different answers: an empty variable reads as "".
let dir = try @Env.Vars.get("NIB_DOCS_EXAMPLE_UNSET") rescue Err.Missing(_) => "./data" end;
"serving files from {dir}"
serving files from ./data
There are two interfaces, with the same operation, because they make different promises about where a value came from. Env.Vars is whatever the process happened to be started with; Env.Cfg is configuration the deployment sets deliberately. A function's effect row says which one it depends on, and a wire can supply one without the other. Env.Process answers both from the process environment; a deployment that keeps its configuration elsewhere binds Env.Cfg to an instance of its own.
Interfaces
interface Vars where
get(name: String) -> String raises Missing
The process environment. get(name) answers the variable's value, or raises Err.Missing when it is not set. A function that performs it is saying it may behave differently on another machine.
Bind it to Env.Process.
let token = try @Env.Vars.get("NIB_DOCS_EXAMPLE_UNSET") rescue Err.Missing(msg) => "(unset)" end;
"token: {token}"
token: (unset)
interface Cfg where
get(name: String) -> String raises Missing
The deployment's configuration: values an operator supplies on purpose. get(name) answers the value, or raises Err.Missing when there is none.
The operation is the same as Env.Vars; the difference is the promise. A test binds a fixed table of its own:
use "std/env.nib"
effector Fixed is Env.Cfg where
get(name) =
if name == "port" then "8080"
else if name == "host" then "127.0.0.1"
else raise Err.Missing(name)
module App where
func address :: -> String ! Env.Cfg raises Err.Missing =
"{@Env.Cfg.get("host")}:{@Env.Cfg.get("port")}"
func main :: -> String ! Env.Cfg raises Err.Missing =
let mode = try @Env.Cfg.get("mode") rescue Err.Missing(_) => "production" end;
"{App.address()} in {mode} mode"
wire Test runs App.main where Env.Cfg = Fixed
127.0.0.1:8080 in production mode
interface Secrets where
secret(name: String) -> Secret raises Missing
Credentials. secret(name) answers the value as a Secret labelled with name, so the program can use it without ever holding a printable String, or raises Err.Missing when it is not set. A journal records the label and never the value.
Bind it to Env.Process, which reads the process environment:
let key = try Secret.label(@Env.Secrets.secret("NIB_DOCS_EXAMPLE_UNSET")) rescue Err.Missing(_) => "(no key)" end;
"credential: {key}"
credential: (no key)
Effectors
effector Env.Process is Env.Vars, Env.Cfg, Env.Secrets -- installed by nibc
get :: String -> String raises Err.Missing
secret :: String -> Secret raises Err.Missing
The environment nibc was started with, installed by the tool. It answers all three interfaces from the same variables: Env.Vars and Env.Cfg as Strings, Env.Secrets as Secrets labelled with the variable's name. Which of them a program may ask is up to the wire, which binds each separately:
use "std/env.nib"
module App where
func home :: -> String ! Env.Vars raises Err.Missing =
@Env.Vars.get("HOME")
wire Prod runs App.home where Env.Vars = Env.Process