Parts environment variables

When building a part to construct a snap, Snapcraft creates the following sets of environment variables that can optionally be used by a part’s build mechanism:

Environment variables can be accessed via the override- keywords with shell commands and Scriptlets, or more generally within your project’s build infrastructure.

See Adding parts for a general overview of what parts are and how to use them, and for more details on how parts are built within the snapcraft environment, including build stages and the directories they use, see Parts lifecycle.

For the various environment variables available to running snap applications, see Environment variables.

Locating directories

See Parts lifecycle and Parts directories for details on which directories are created and used when building a part.

Snapcraft exposes the following directory related environment variables. These can help when moving or locating files:

SNAPCRAFT_PART_SRC absolute path to where a part’s sources are pulled. It’s also the part’s working directory for the pull step
SNAPCRAFT_PART_BUILD absolute path to the sources used for the part’s build step. It is also the working directory of the build step.
SNAPCRAFT_PART_INSTALL absolute path to the results of the part’s build step. It also contains the staged packages of that part.
SNAPCRAFT_PRIME absolute path to where files are primed
SNAPCRAFT_PROJECT_DIR absolute path to the root of the snapcraft project
SNAPCRAFT_STAGE absolute path to where files are staged

Snapcraft configuration

When building a part, following snapcraft environment variables are set:

SNAPCRAFT_ARCH_TRIPLET the architecture triplet used for the selected base
SNAPCRAFT_PARALLEL_BUILD_COUNT the preferred number of jobs the project is to be built with
SNAPCRAFT_PROJECT_NAME the snapcraft project name as set by name in snapcraft.yaml
SNAPCRAFT_PROJECT_VERSION the snapcraft project version as set by snapcraft.yaml
SNAPCRAFT_PROJECT_GRADE the snapcraft project grade as set in snapcraft.yaml
SNAPCRAFT_TARGET_ARCH deb-style architecture that snap is being built for, e.g. “amd64”, “armhf”, “arm64”, etc.

Build flags

The following specific build flags are also set:

CFLAGS empty unless after is used in the part and headers are staged in the common include paths for which they will be included (i.e.; paths added with -I)
CPPFLAGS same behavior as CFLAGS
CXXFLAGS same behavior as CFLAGS
LDFLAGS empty unless after is used in the part and headers are staged in the common library paths (i.e.; paths added with -L)
PKG_CONFIG_PATH empty unless after is used in the part and .pc files are staged in the common pkgconfig paths

Plugin variables

A part’s plugin can add its own set of environment variables, or expand on the above build related flags.

The build-environment keyword can be used to override or expand on the default environment variables. Here is a basic example:

    source: gnu-hello.tar.gz
    plugin: autotools
    - CFLAGS: "$CFLAGS -O3"  # add -O3 to the existing flags
    - LDFLAGS: "-L$SNAPCRAFT_STAGE/non-standard/lib"

The above example will override default flags and search for libraries in a non-standard path.

For a complete list of environment variables, see Environment variables exposed by Snapcraft.

Last updated 2 months ago.