Ebuild environment

Ebuilds are sourced and executed by bash, in an environment that the package manager controls. This section describes the properties of that environment which ebuilds may rely on.

Bash version

Ebuilds and eclasses may use the features of the bash version that their EAPI guarantees:

EAPI Bash version
7 4.2
8 5.0
9 5.3

Global scope

The global scope of an ebuild (and of any eclasses it inherits) is executed every time the ebuild is sourced. This includes metadata generation, which may happen on a different machine and without any of the ebuild's dependencies installed. See The Portage cache for the resulting requirement of metadata invariance.

When being sourced, an ebuild must not do any of the following:

  • Call external programs, including via command substitution
  • Write anything to standard output or standard error
  • Modify the state of the system in any way

Bash builtins and the package manager functions that are documented as callable in global scope, like has or ver_cut, are fine:

# DO NOT DO THIS!
MY_PV=$(echo ${PV} | tr . _)

# Do this instead
MY_PV=$(ver_rs 1- _)

Declaring variables

Whether variables defined at the top level of an ebuild or eclass are evaluated in global scope or as if within a function body is unspecified, and ebuilds must not rely on either behaviour. Therefore, at the top level:

  • The local builtin must not be used
  • The declare builtin must only be used with the -g option when defining variables
# DO NOT DO THIS!
declare -A MY_MODULES=( [foo]=1.0 [bar]=2.1 )

# Do this instead
declare -gA MY_MODULES=( [foo]=1.0 [bar]=2.1 )

Name reference variables (declare -n, available since bash 4.3) must only be used in local scope, i.e. declared with local -n inside a function:

append_flag() {
	local -n arr=${1}
	arr+=( "${2}" )
}

Locale settings

For characters in the ASCII range, case modification and collation order (LC_CTYPE and LC_COLLATE) are guaranteed to behave as in the C locale. Ebuilds can therefore rely on bash constructs like ${var^^} or on the sorting order of glob expansion, without having to set these variables themselves.

Filtering the environment

The ENV_UNSET variable holds a whitespace delimited list of variables that the package manager removes from the environment before sourcing an ebuild. It is set in profiles and in make.conf, not in ebuilds.

Its purpose is to keep variables from the calling user's environment from leaking into builds and changing their outcome. The profiles use it for variables such as the XDG_* directory settings, which are meaningless for a build and regularly break one when they are inherited from a running desktop session.

Failed glob expansion

The failglob option of bash is set in the global scope of ebuilds. As a consequence, a pattern that matches no file results in an error when the ebuild is being sourced, instead of being silently left as a literal string.

This mostly affects arrays that are populated from ${FILESDIR} in global scope:

PATCHES=( "${FILESDIR}"/${P}-*.patch )

If no patch matches the pattern, sourcing the ebuild fails, which will break even simple operations like dependency resolution. Reference the files explicitly to avoid this:

PATCHES=(
	"${FILESDIR}"/${P}-fix-build.patch
	"${FILESDIR}"/${P}-musl.patch
)