Skip to content

Environment variables

Almost all of Stalwart’s configuration lives in its data store, but a few settings are needed before the data store can be read, or describe the environment the process runs in. Stalwart reads these from environment variables when it starts.

A normal installation needs none of them. They exist for specific situations: selecting the configuration file, switching to recovery mode, setting the public URL behind a proxy, identifying a cluster node and tuning the process. All of them are read only at startup, so a change takes effect after a restart.

Where the variables are set depends on how the server was installed. Installations made with the installation script read them from the environment file /etc/stalwart/stalwart.env, or /usr/local/etc/stalwart/stalwart.env on FreeBSD. That file lists the most common variables, commented out, as described in Linux and macOS. Containers receive the variables through the container environment, and Windows services through the service wrapper.

The server reads the following variables:

VariableDefaultEffect
CONFIG_PATHnonePath of the configuration file. When set, it is used instead of the command line, which is then not read at all.
STALWART_RECOVERY_MODEoff1 or true starts the server in recovery mode. Any other value is ignored.
STALWART_RECOVERY_MODE_PORT8080Port of the plain HTTP listener used in recovery and bootstrap mode. The listener always binds all interfaces, as explained in Protect the setup listener.
STALWART_RECOVERY_MODE_LOG_LEVELinfoLevel of the console log in recovery and bootstrap mode.
STALWART_RECOVERY_ADMINnoneThe recovery administrator, as username:password. Accepted in every mode while set.
STALWART_PUBLIC_URLbuilt from the hostnameBase URL published in discovery documents; see Public URLs.
STALWART_HTTPS_PORTnonePort for the public URL when STALWART_PUBLIC_URL is not set; see Public URLs.
STALWART_HOSTNAMEthe machine’s host nameName of the node, used for its cluster node id and proposed as server hostname by the setup wizard.
STALWART_ROLEnoneThe cluster role of the node, which selects the tasks and listeners it runs. Must name a role defined in the configuration.
STALWART_PUSH_SHARD1The push notification shard the node serves in a cluster, counted from 1.
RUN_AS_USER, RUN_AS_GROUPnoneUser and group to switch to after the listeners are bound, for a server started as root; see Running as an unprivileged user. Not available on Windows.
TOKIO_WORKER_THREADSone per CPUNumber of worker threads that run the server’s network and protocol work.
EXPORT_TYPESallWith --export, the families of data to export, as a comma-separated list; see Backup and restore.
STALWART_HEALTHCHECK_URLhttps://127.0.0.1:443/healthz/liveContainer image only: the URL probed by the image’s health check; see Docker.

Four variables control recovery mode and bootstrap mode: STALWART_RECOVERY_MODE, STALWART_RECOVERY_MODE_PORT, STALWART_RECOVERY_MODE_LOG_LEVEL and STALWART_RECOVERY_ADMIN.

One of them needs particular care. STALWART_RECOVERY_ADMIN is also accepted in normal operation, where it acts as a full-access credential. It must therefore be removed from the environment once it is no longer needed, as described in Retire the recovery administrator.

In a cluster, every node reads the same configuration from the shared data store. What tells the nodes apart is set in the environment instead, through three variables.

STALWART_HOSTNAME gives a node a distinct identity where machine host names are not unique, as in containers. It does not change the published URLs. STALWART_ROLE selects the node’s role. STALWART_PUSH_SHARD selects the push notification shard the node serves, counted from 1.

Clients find the server’s HTTP endpoints through the OAuth, OpenID Connect and JMAP discovery documents. These documents publish absolute URLs, all built from one base URL. The same URL also limits OAuth sign-in, which the server accepts only for its host and for the configured service host names.

The server takes the base URL from the first of these sources that is available:

  1. STALWART_PUBLIC_URL, with a trailing slash removed, for example https://mail.example.com or https://mail.example.com:8443.
  2. When STALWART_HTTPS_PORT is set, https://<host>:<port>, where the host is STALWART_HOSTNAME or the machine’s host name.
  3. https:// followed by the host name of the JMAP entry in SystemSettings.services when it has one, or by SystemSettings.defaultHostname.

Set STALWART_PUBLIC_URL when clients reach the server at a URL other than https://<hostname>, for example through a reverse proxy or on another port. Reverse proxy explains the consequences.

The variable changes only the published URLs. The ports the server listens on are set by its listeners, and the server hostname keeps its other uses, such as the SMTP greeting and certificate requests.

TOKIO_WORKER_THREADS sets the size of the thread pool that runs the server’s network connections and protocol processing. When the variable is unset, the pool has one thread per CPU. Disk operations are not part of this pool: the RocksDB and SQLite data stores run them on a separate pool, sized by their poolWorkers setting. Worker threads describes when changing the number of threads helps.