Loading...
Loading...
Ossido lets you declare a single, typed source of truth for your app's
environment. You define an Environment struct in Rust; its fields are parsed
from the OS environment at startup, and the ones you mark #[public] are exposed
to the frontend with generated TypeScript types.
The whole feature is optional - if you don't define an Environment struct,
nothing is generated and no env global is injected.
Create the struct anywhere under src/ (convention: src/env.rs). Bring the
attribute into scope with use ossido::Environment;, mark the struct with
#[Environment], and mark the fields you want to expose to the browser with
#[public].
SCREAMING_SNAKE to match the OS convention. The name is upper-cased for the
OS lookup, so a snake field would still work and map to its upper-cased var.String and any FromStr scalar (u16, bool, ...).Option field is required: a missing or
unparseable value panics at startup with the offending field/var name. An
Option<T> field is optional (absent becomes None).#[public] - exposes the field to the frontend. Everything else stays
server-only.get_env!Read any field (public or private) with the get_env! macro. It returns a typed
copy of the value.
Pass a second argument to collapse an Option<T> field to a concrete T,
using the fallback when the variable is unset:
The fallback form only applies to Option<T> fields - a required field is always
present, so passing one is a type error.
If no Environment struct exists, get_env! does not compile at the call site -
the Rust equivalent of the frontend getEnv throwing at runtime.
getEnvOnly #[public] fields are available. Types are generated into .ossido/types.ts,
so keys and value types are checked.
Like get_env!, a second argument collapses an optional value to a concrete
T - the fallback is returned whenever the value is absent (an optional variable
unset, or no public environment available), so this form never throws:
getEnv works during SSR and on the client. It throws if no public
environment is available (no Environment struct) or if the key is not a public
variable. Private fields are not accessible here - read those in Rust.
.env files.env loading is automatic when an Environment struct exists. By default the
usual cascade is loaded, in order (later overrides earlier), skipping any key
already set in the real OS environment:
Set env in ossido.config.ts to a path or array of paths. This replaces the
default cascade (the listed files are loaded in order):
#[Environment] macro adds serde derives, a from_env() parser, and a
public-only JSON serializer.@ossido-labs/ossido/env type augmentation in .ossido/types.ts (public
fields only), and.ossido/main.rs that parses the singleton at startup
(fail-fast) and registers the public JSON.window.__OSSIDO_PUBLIC_ENV__ global (and mirrored onto globalThis for server
rendering), which getEnv reads. Private fields never leave the server.Only #[public] fields are serialized into the SSR payload and the browser
global. Non-public fields (secrets, connection strings, API keys) stay in Rust and
are never sent to the client.
Next: Page & Layout Handlers · Back to Ossido Application