Use the env-injector extension¶
The env-injector extension lets you expose environment variables within the snap to the user. These variables are accessible by the snap’s apps and can modify their behavior as they would in a bare host environment.
The user has multiple available methods for setting these environment variables. Your snap’s apps and design should be oriented toward the most optimal method, and its documentation should cover which variables are available and the correct method of assigning them.
Set up the env-injector extension¶
To add the env-injector extension to an app in your snap:
Since env-injector is an experimental extension, it’s blocked by default. To enable experimental extensions during build, your host must set the following environment variable when packing with Snapcraft:
SNAPCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS=1
Your snap must enable configurations with a configure hook. If your snap doesn’t use this hook yet, it needs at minimum an executable file named
configure
, without an extension, insnap/hooks/
. It should contain at minimum:#!/bin/sh # Optional validation logic
In your snap’s project file, the target app’s
extensions
key must listenv-injector
. For example, if your snap had an app namedserver
, the key would declare:snapcraft.yaml¶apps: server: command: run.sh daemon: simple extensions: [ env-injector ]
Once set up, the user can set any available environment variables for the snap’s apps.
Set an environment variable¶
When an app in a snap has behavior bound to an environment variable, the user can set it
either through the snap’s configuration or by reading an environment
(.env
) file.
Environment variables are applied to apps in one of two ways:
Globally, where the environment variable is passed to all apps that use env-injector.
Locally, where the environment variable is passed to a specific app that uses env-injector. The app’s name is taken from its definition in the snap project file. The name according to the extension can be overridden with an alias to avoid naming conflicts.
As a snap configuration option¶
The user can set environment variables one at a time as snap configuration options
with the snap set
command.
To set an environment variable for all apps in a snap, the user can call snap set
and target the snap and its app. The passed environment variable name must be lowercase.
For example, to set HTTP_PORT=8080
for all apps in a snap that use the env-injector,
the user would run:
sudo snap set <snap-name> env.http-port=8080
To set a local environment variable and target a specific app, they can call snap
set
and prefix the option name with apps.<app-name>
. To target only the server app
in the previous example, the user would run:
sudo snap set <snap-name> apps.server.env.http-port=8080
The app’s name is taken from the snap’s project file.
When running snap set
, the user must adjust the environment variable name. For the
complete details on how snap options interpret environment variables, see
Snap option naming and rules.
With an environment file¶
The user can pass environment variables in .env
files to the snap with the snap
set
command.
If a snap is confined, its file system needs access to the file, either by storing the file in its writable area or through a file interface.
For a simple example, to globally export the contents of an environment file stored in the local host, the user would run:
sudo snap set <my-snap> envfile=/var/snap/my-snap/common/config.env
The environment variables inside config.env
are then exported to all apps that use
the extension.
To export the contents of the same file as local environment variables of the server app, the user would run:
bash sudo snap set <my-snap> apps.server.envfile=/var/snap/my-snap/common/server.env
Give an app an alias for the environment¶
The app’s name is taken from its definition in the snap’s project file. You can override
how the app is referred to in the environment by setting its env_alias
key.
For example, to override an app named server
with web-server
, the project file
would declare:
apps:
server:
command: run.sh
daemon: simple
extensions: [ env-injector ]
environments:
env_alias: web-server
Then, the user could set a local environment variable on the app with:
sudo snap set <my-name> apps.web-server.env.http-port=8080
Similarly, the user could override the app’s local .env
file with:
sudo snap set <my-name> apps.web-server.envfile=/var/snap/my-snap/common/server.env