3.3 Environment variables

Systemd service units has the Environment field in the Service section to manage environment variables of the executable. This field contains a space-separated list of elements in the form key=value (or "key=value" if value contain spaces) where key is the environment variable name and value is its content.


def-group GroupA {
    sd {
        (section) Service {
            (add) Environment "KEY_A=A" "KEY_B=B" "\"KEY_C=C SPACES\""
        }
    }
}

This way to manage environment variables has several drawbacks:

For those reasong, Tomloader also provides the node envs in order to manage environment variables like any other systemd field. Like section nodes, the envs node contains child nodes with type set, reset, add whose name is exactly the correspective environment variable.

Caution: the reset operation just removes that variable from the list, it does neither set the content to an empty string nor unset it. If you want to unset a specific environment variable then in addition you should add it to the UnsetEnvironment systemd field.


def-group GroupA {
    envs {
        (set) KEY_A A
        (set) KEY_B B
        (set) KEY_C C SPACES
    }
}

Just like other fields, child nodes in envs may accept several values that will be merged at the end as a whitespace-separated string. Just before the generation of the systemd unit, all envs blocks will be merged into a single (add) Environment with proper quotation in order to preserve correct values. Therefore, the last example will be equivalent to the first one (if no additional group is loaded).

You can still clear and/or fully rewrite the content of the Environment field in the unit configuration file (see Unit configuration).

Caution: Tomloader always reorder values provided to a field as separate strings, which is problematic for several environment variables like PATH. Therefore, we do not provide special handling of colon-separated environment variables like PATH and instead encourage you to provide the full value in a single set operation:


    (set) PATH "/home/tomloader/bin:/home/tomloader/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin"