env-injector extension¶
The env-injector extension, referred to internally as env-injector
, provides an
interface for altering snap behavior with user-defined environment variables.
When you add the extension to individual apps inside a snap, you open the way for the user to configure the snap or affect its behaviour with environment variables.
How it works¶
This extension splits environment variables into two types:
Global environment variables are visible to all apps in the snap that use env-injector.
Local environment variables are visible only to apps with env-injector that the user specifically targets.
At a high level, the extension has the following processes and rules during build and runtime.
Environment variable order of precedence¶
Environment variables are processed in the following order:
Variables extracted from the global
.env
file.Variables extracted from the local
.env
file.Variables from global snap config.
Variables from local snap config.
App build process¶
For each app that uses the extension:
The app responsible for processing environment variables (the exporter program) is added to its command chain.
The app’s name is taken either from its internal name or an environment alias.
Runtime process¶
When the snap runs, for each app that uses the extension, the exporter program:
Runs before the app is executed.
Makes a request to the snapd API through the snapd Unix socket.
Reads the available environment variables or paths to the
.env
files.Translates the snap options into environment variables.
If available, loads the
.env
files and sets the listed variables.Sets all converted environment variables.
Executes the app’s command.
Snap option naming and rules¶
Because there’s a chance of conflict and collision between the names of snap options and environment variables, this extension is governed by naming rules.
Snap options map to environment variables:
Type |
Snap configuration option |
Environment variable |
---|---|---|
Global |
|
|
Local |
|
|
When mapped, key names are sanitized before being converted into environment variable names:
Character in snap option |
Result in environment variable |
---|---|
Lowercase letters |
Uppercase letters |
Number not at key start |
Number |
Hyphen not at key start or end |
Underscore |
Tip
The first column is the rule set for snap option names. Any character not detailed in it is invalid in a snap option name.
User-selected environment files¶
With the special envfile
snap options, the user can also pass whole environment
(.env
) files to the snap, provided the file is already packed inside the snap.
Type |
Snap option |
---|---|
Global |
|
Local |
|