<a id="configuration"></a>

# Configuration

The configuration consists of files in YAML format on disk.
The package comes with multiple example configurations
in  */opt/halon/share/examples*.

The YAML configuration files can be
edited directly, and validated against the [JSON schemas](https://docs.halon.io/json-schemas/6.12-stable/) using the `halonconfig` script.
The schemas can be browsed using a tool like Bovet’s [docson](http://lbovet.github.io/docson/index.html#https://docs.halon.io/json-schemas/6.12-stable/smtpd-app.schema.json).
They are also included in our [Visual Studio Code integration](vscode.html.md), which
provides validation, autocomplete and hover support when editing them.

The YAML configuration allows for byte format and time format syntax in a lot of places.
For example time such as [`servers[].timeout.idle`](config_smtpd.html.md#confval-servers-.timeout.idle) may be specified in the format of `?d?h?m?s`.
Size fields such as [`servers[].phases.data.maxsize`](config_smtpd.html.md#confval-servers-.phases.data.maxsize) may be specified in the format of `?(TiB|GiB|MiB|KiB|B)`.

The YAML configurations support `${ENVIRONMENT:DEFAULT}` replacements.
In order to interpet a `${STRING}` as a replacement use the custom `!Env` tag in the YAML file as well as start the
`smtpd` process with the `--config-env-substitution` flag.
This can be used to template in strings and numbers (such a default values and API keys etc).

```yaml
version: !Env "${VERSION:6.12}"
...
monitor:
  apikeys:
    - !Env "${MYAPIKEY}"
```

If you are using `halonconfig` and want to cast a environment variable to a `int` or a `float` so it passes the linting you can use the following syntax:

```yaml
version: !Env "${VERSION:6.12}"
...
monitor:
  listeners:
    port: !Env "tag:yaml.org,2002:int ${MONITORPORT}"
```

> **Warning**
> 
> Note that the `tag:yaml.org,2002:*` syntax is not supported by the `smtpd` process itself since it will cast the values regardless, this is why `halonconfig` will strip it out after validating the configuration.

> **Tip**
> 
> For services started by `systemd`, command-line flags such as `--config-env-substitution`, and environment variables are set by editing the service configuration.

* Edit the service configuration with `sudo systemctl edit halon`
* Make your edits above the  *### Edits below this comment will be discarded* line
* The empty `ExecStart=` line is important. This tells systemd to “forget” the command from the original service file and use your new one instead.

```ini
[Service]
Environment="HALON_TYPE=staging"
ExecStart=
ExecStart=/opt/halon/sbin/smtpd -c /etc/halon/smtpd.yaml --config-env-substitution
```

* Restart the service. You can check which flags are set using `ps aux | grep halon`
* You can check the environment variables of the service using `sudo tr '\0' '\n' < /proc/$(pgrep smtpd)/environ`

**Startup and running configuration**

The [running configuration](config_smtpd.html.md) contains the bulk of the configuration data, including all the
Halon [script](https://docs.halon.io/hsl/archive/6.12-stable/). It can be softly reloaded and deployed in a
blue-green fashion using per-connection conditions, using the [control sockets](api_control_sockets.html.md),
[API](https://docs.halon.io/api/), [command line interface](cli.html.md) or web administration.
The default
[startup configuration](config_startup.html.md)
loads the running configuration from  */etc/halon/smtpd-app.yaml*
as per the [`environment.appconf`](config_startup.html.md#confval-environment.appconf) directive.

* [Startup configuration](config_startup.html.md)
  * [Server directives](config_startup.html.md#server-directives)
  * [Monitor directives](config_startup.html.md#monitor-directives)
  * [Queue directives](config_startup.html.md#queue-directives)
  * [Threads directives](config_startup.html.md#threads-directives)
    * [Scripts](config_startup.html.md#scripts)
    * [Events](config_startup.html.md#events)
  * [Other directives](config_startup.html.md#other-directives)
  * [Clustering directives](config_startup.html.md#clustering-directives)
  * [Environment directives](config_startup.html.md#environment-directives)
    * [Performance and log](config_startup.html.md#performance-and-log)
    * [Configuration paths](config_startup.html.md#configuration-paths)
    * [Other environment](config_startup.html.md#other-environment)
* [Running configuration](config_smtpd.html.md)
  * [Script directives](config_smtpd.html.md#script-directives)
  * [Server directives](config_smtpd.html.md#server-directives)
  * [Transport directives](config_smtpd.html.md#transport-directives)
  * [Monitor directives](config_smtpd.html.md#monitor-directives)
  * [Queue directives](config_smtpd.html.md#queue-directives)
  * [General directives](config_smtpd.html.md#general-directives)
  * [List directives](config_smtpd.html.md#list-directives)
  * [Resolver directives](config_smtpd.html.md#resolver-directives)
  * [Other directives](config_smtpd.html.md#other-directives)
  * [Example](config_smtpd.html.md#example)
* [Suspend configuration](config_suspend.html.md)
  * [Suspend directives](config_suspend.html.md#suspend-directives)
* [Policy configuration](config_policy.html.md)
  * [Policy directives](config_policy.html.md#policy-directives)
  * [Rate directives](config_policy.html.md#rate-directives)
* [Delivery configuration](config_delivery.html.md)
  * [Delivery directives](config_delivery.html.md#delivery-directives)
* [Performance tuning](config_tuning.html.md)
  * [Thread tuning](config_tuning.html.md#thread-tuning)
    * [servers[].threads.event](config_tuning.html.md#servers-threads-event)
    * [threads.scripts[].count](config_tuning.html.md#threads-scripts-count)
    * [threads.scripts[].concurrent](config_tuning.html.md#threads-scripts-concurrent)
    * [queues.threads.pickup](config_tuning.html.md#queues-threads-pickup)
    * [resolver.threads.event](config_tuning.html.md#resolver-threads-event)
    * [resolver.threads.ready](config_tuning.html.md#resolver-threads-ready)
  * [Startup configuration](config_tuning.html.md#startup-configuration)
    * [environment.syslog.mask](config_tuning.html.md#environment-syslog-mask)
    * [spool.fsync](config_tuning.html.md#spool-fsync)
    * [spool.loader.threads.wait](config_tuning.html.md#spool-loader-threads-wait)
    * [environment.uuid.version](config_tuning.html.md#environment-uuid-version)
  * [Running configuration](config_tuning.html.md#running-configuration)
    * [servers[].logging](config_tuning.html.md#servers-logging)
    * [servers[].phases.connect.remoteptr](config_tuning.html.md#servers-phases-connect-remoteptr)
    * [servers[].phases.data.multipart](config_tuning.html.md#servers-phases-data-multipart)
    * [servers[].phases.data.fixheaders](config_tuning.html.md#servers-phases-data-fixheaders)
  * [Resolver subsystem](config_tuning.html.md#resolver-subsystem)
    * [resolver.concurrency](config_tuning.html.md#resolver-concurrency)
    * [resolver.ednsbuffersize](config_tuning.html.md#resolver-ednsbuffersize)
    * [resolv.conf](config_tuning.html.md#resolv-conf)
      * [systemd](config_tuning.html.md#systemd)
      * [K8s](config_tuning.html.md#k8s)
  * [Queueing subsystem](config_tuning.html.md#queueing-subsystem)
    * [queues.pickup.prereaddata](config_tuning.html.md#queues-pickup-prereaddata)
    * [queues.pickup.backpressure](config_tuning.html.md#queues-pickup-backpressure)
    * [pooling](config_tuning.html.md#pooling)
  * [Memory tuning](config_tuning.html.md#memory-tuning)
  * [Disk tuning](config_tuning.html.md#disk-tuning)
