EAPI usage and description
The Package Manager Specification is a standardization effort to ensure that the ebuild file format, the ebuild repository format (of which the Gentoo repository is Gentoo's main incarnation) as well as behaviour of the package managers interacting with these ebuilds is properly written down and agreed upon.
EAPI is a version defined in ebuilds and other package manager related files which informs the package manager about the file syntax and content. It is, in effect, the version of the Package Manager Specification that the file adheres to.
This section provides usage and descriptions of the different EAPIs.
Usage of EAPIs
app-doc/pms in the main tree.
You must set the EAPI variable by specifying it at the top of the ebuild:
# Copyright 1999-2021 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
EAPI=8
When writing new ebuilds developers can choose whatever EAPI they think is the best. Using the features of the latest EAPI is encouraged. EAPI 7 is deprecated and should no longer be used for new ebuilds; where its behaviour differs from later EAPIs, this is pointed out in the relevant sections of this guide.
Upgrade path
Gentoo policy is to support upgrades for installations at least a year old
with no/little intervention and up to two years old with minor intervention.
To achieve this, developers must avoid using the latest EAPI in ebuilds within
the @system set
(see Implicit system dependency)
or its dependencies.
The Base System project has rules governing their use of newer EAPIs, as does the Python project.
It is also convention that blockers within ebuilds are retained for at least
2 years after the last ebuild matching the block is removed from the tree to
avoid file collisions for users upgrading older systems. pkgcheck has
a warning for this called OutdatedBlocker (or even
NonexistentBlocker for when the match is from pre-git times if using
a non-grafted repository).
EAPIs 0 to 6
EAPIs 0 to 6 are obsolete and must no longer be used. Refer to the Package Manager Specification for details about them.
EAPI 8
EAPI 8 tree layout
- Less strict naming rules for updates directory
-
Up to EAPI 7, the files in the
profiles/updatesdirectory had to follow strict naming by quarters like2Q-2021, indicating the quarter and the year when they were added. Such a choice of name had the side effect that lexical sorting of filenames was unsuitable.In EAPI 8, the naming requirement is removed. Eventually, this will allow switching to a more convenient scheme sorted by year. Different lengths of time periods will also be possible.
Note that this change actually requires changing the repository EAPI (found in
profiles/eapi), so it will not affect Gentoo for at least the next two years.
EAPI 8 ebuild format
- Bash version is now 5.0
-
The Bash version used for ebuilds is changed from 4.2 to 5.0. This means not only that ebuilds are now permitted to use features provided by the new Bash version but also the
BASH_COMPATvalue used for the ebuild environment is updated, switching the shell behaviour.The only really relevant difference in behaviour is:
-
Quotes are now removed from the RHS argument of a
"${var/.../"..."}"substitution:var=foo echo "${var/foo/"bar"}"The above snippet yields
"bar"in Bash 4.2 but justbarin 4.3+.
Potentially interesting new features include:
-
Negative subscripts can now be used to set and unset array elements (Bash 4.3+):
$ foo=( 1 2 3 ) $ foo[-1]=4 $ unset 'foo[-2]' $ declare -p foo declare -a foo=([0]="1" [2]="4")
-
Nameref variables are introduced that work as references to other variables (4.3+):
$ foo=( 1 2 3 ) $ declare -n bar=foo $ echo "${bar[@]}" 1 2 3 $ bar[0]=4 $ echo "${foo[@]}" 4 2 3 $ declare -n baz=foo[1] $ echo "${baz}" 2 $ baz=100 $ echo "${bar[@]}" 4 100 3 -
The
[[ -v ... ]]test operator can be used with array indices to test for array elements being set (4.3+). The two following lines are now equivalent:[[ -n ${foo[3]+1} ]] [[ -v foo[3] ]] -
mapfile(AKAreadarray) now accepts a delimiter via-d, with a-toption to strip it from read data (Bash 4.4+). The two following solutions to grab output fromfind(1)are now equivalent:# old solution local x files=() while read -d '' -r x; do files+=( "${x}" ) done < <(find -print0) # new solution local files=() mapfile -d '' -t files < <(find -print0) -
A new set of transformations is available via
${foo@...}parameter expansion (4.4+), e.g. to print a value with necessary quoting:$ var="foo 'bar' baz" $ echo "${var@Q}" 'foo '\''bar'\'' baz'For more details, see:
info bashor the Bash reference manual. -
local -can be used to limit single-letter (mangled viaset) shell option changes to the scope of the function, and restore them upon returning from it (4.4+). The following two functions are now equivalent:# old solution func() { local prev_shopt=$(shopt -p -o noglob) set -o noglob ${prev_shopt} } # new solution func() { local - set -o noglob }The complete information on all changes and new features can be found in the Bash 5.0 (and earlier) release notes.
-
EAPI 8 variables
- Selective fetch/mirror restriction
-
Before EAPI 8, fetch and mirror restrictions applied globally. That is, if you needed to apply the respective restriction to at least one distfile, you had to apply it to them all. However, sometimes packages used a combination of proprietary and free distfiles, the latter including e.g. third party patches, artwork. Until now, they had to be mirror-restricted completely.
EAPI 8 allows undoing fetch and mirror restriction for individual files. To use this, set
RESTRICTas before, then use the specialfetch+prefix to specify URLs that can be fetched from, or themirror+prefix to reenable mirroring of individual files.Similarly to how the
fetchrestriction implies amirrorrestriction, themirroroverride implies afetchoverride.EAPI=8 SRC_URI=" ${P}.tgz fetch+https://example.com/${P}-patch-1.tgz mirror+https://example.com/${P}-fanstuff.tgz" RESTRICT="fetch"The following table summarises the new behaviour:
RESTRICTURI prefix Fetching Mirroring (none) (any) allowed allowed mirror (none) / fetch+ allowed prohibited mirror+ allowed allowed fetch (none) prohibited prohibited fetch+ allowed prohibited mirror+ allowed allowed - Install-time dependencies (
IDEPEND) -
The primary use for install-time dependencies is to specify dependencies that are needed during the
pkg_postinstphase and that can be unmerged afterwards. That's pretty much the same asRDEPEND, except for the unmerging part — and uninstalling a few tools did not seem a goal justifying another dependency type.With cross-compilation support in EAPI 7, a new dependency type focused on the build host (
CBUILD) tools was added —BDEPEND. Unfortunately, this had missed the important use case of running executables installed to the target system when cross-compiling.RDEPENDwas no longer a suitable method of pulling in tools forpkg_postinst; and sinceBDEPENDis not used when installing from a binary package, something new was needed.This is where
IDEPENDcomes in. It is roughly toRDEPENDwhatBDEPENDis toDEPEND. Similarly toBDEPEND, it specifies packages that must be built for theCBUILDtriplet and installed intoBROOT(and therefore queried usinghas_version -b). However, similarly toRDEPEND, it is used when the package is being merged rather than built from source. It is guaranteed to be satisfied throughoutpkg_preinstandpkg_postinst, and it can be uninstalled afterwards.EAPI=8 inherit xdg-utils IDEPEND="dev-util/desktop-file-utils" pkg_postinst() { xdg_desktop_database_update } pkg_postrm() { xdg_desktop_database_update }In the example provided above, the ebuild needs to update the desktop database upon being installed or uninstalled. By placing the respective tool in
IDEPEND, the ebuild requests it to be available at the time ofpkg_postinst. When cross-compiling, the tool will be built forCBUILDand therefore directly executable by the ebuild.The dependency types table for EAPI 8 is presented below.
Dependency type BDEPEND IDEPEND DEPEND RDEPEND PDEPEND Present at build install build install n/a Binary compatible with CBUILD CHOST Base unprefixed path /SYSROOT ROOT Relevant offset-prefix BROOT EPREFIX (unless SYSROOT != ROOT) EPREFIX Path combined with prefix BROOT ESYSROOT EROOT PM query command option -b-d-r -
PROPERTIESandRESTRICTare now accumulated across eclasses -
Up to EAPI 7,
PROPERTIESandRESTRICTwere treated like regular Bash variables when sourcing eclasses. This meant that if an eclass or an ebuild wanted to modify them, they had to explicitly append to them, e.g. via+=. This was inconsistent with how some other variables (but not all) were handled, and confusing to developers. For example, consider the following snippet:EAPI=7 inherit git-r3 PROPERTIES+=" live"Note how you needed to append to
PROPERTIESset by git-r3 eclass, otherwise the ebuild would have overwritten it. In EAPI 8, you can finally do the following instead:EAPI=8 inherit git-r3 PROPERTIES="live"Now the complete list of metadata variables accumulated across eclasses and ebuilds includes:
IUSE,REQUIRED_USE,*DEPEND,PROPERTIES,RESTRICT. Variables that are not treated this way are:EAPI,HOMEPAGE,SRC_URI,LICENSE,KEYWORDS.EAPIandKEYWORDSare not supposed to be set in eclasses; as for the others, there appears to be a valid use case for eclasses providing default values and the ebuilds being able to override them.
EAPI 8 phase functions
-
pkg_*phases now run in a dedicated empty directory -
Before EAPI 8, the initial working directory was specified for
src_*phases only. For other phases (i.e.pkg_*phases), ebuilds were not supposed to assume any particular directory. In EAPI 8, these phases are guaranteed to be started in a dedicated empty directory.The idea of using an empty directory is pretty simple — if there are no files in it, the risk of unexpected and hard to predict interactions of tools with their current working directory is minimized.
-
PATCHESno longer permits options -
The
eapplyinvocation in the defaultsrc_prepareimplementation has been changed to:eapply -- "${PATCHES[@]}"This ensures that all items in the
PATCHESvariable are treated as path names. As a side effect, it is no longer possible to specifypatchoptions via thePATCHESvariable. Such hacks were never used in the Gentoo repository but they have been spotted in user-contributed ebuilds. The following will no longer work:PATCHES=( -p0 "${FILESDIR}"/${P}-foo.patch )Instead, you will need to invoke
eapplyexplicitly, see the example below. Alternatively, rebase the patch level.src_prepare() { eapply -p0 "${FILESDIR}"/${P}-foo.patch eapply_user }
EAPI 8 commands
- New econf-passed options
-
The
econfhelper has been modified to pass two more options to the configure script if the--helptext indicates that they are supported. These are:--datarootdir="${EPREFIX}"/usr/share--disable-static
The former option defines the base directory that is used to determine locations for system/desktop-specific data files, e.g. .desktop files and various kinds of documentation. This is necessary for ebuilds that override
--prefix, as the default path is relative to it.The latter option disables building static libraries by default. This is part of the ongoing effort to disable unconditional install of static libraries ( Gentoo Policy Guide, Installation of static libraries).
-
dosym -rto create relative symlinks -
Relative symlink targets tend to be more reliable. Consider the two following examples:
dosym "${EPREFIX}"/usr/lib/frobnicate/frobnicate /usr/bin/frobnicate dosym ../lib/frobnicate/frobnicate /usr/bin/frobnicateThe first line creates a symlink using an absolute path. The problem with that is if you mount your Gentoo system in a subdirectory of your root filesystem (e.g. for recovery), the symlink will point at the wrong location. Using relative symlinks (as demonstrated on the second line) guarantees that the symlink will work independently of where the filesystem is mounted.
There is also fact that you need to explicitly prepend
${EPREFIX}to the absolute paths passed as the first argument ofdosym. Using a relative target avoids the problem altogether and makes it less likely to forget about the prefix.However, in some instances, determining the relative path could be hard or inconvenient. This is especially the case if one (or both) of the paths comes from an external tool. To make it easier, EAPI 8 adds a new
-roption that makesdosymcreate a relative symlink to the specified path (similarly toln -r):dosym -r /usr/lib/frobnicate/frobnicate /usr/bin/frobnicateNote that in this case, you do not pass
${EPREFIX}. The helper determines the logical relative path to the first argument and creates the appropriate relative symlink. It is very important here to understand that this function does not handle physical paths, i.e. it will work only if there are no directory symlinks along the way that would result in..resolving to a different path. For example, the following will not work:dosym bar/foo /usr/lib/foo dosym -r /usr/lib/zomg /usr/lib/foo/zomgThe logical path from
/usr/lib/foo/zomgto/usr/lib/zomgis../zomg. However, since/usr/lib/foois actually a symlink to/usr/lib/bar/foo,/usr/lib/foo/..resolves to/usr/lib/bar. If you need to account for such directory symlinks, you need to specify the correct path explicitly:dosym bar/foo /usr/lib/foo dosym ../../zomg /usr/lib/foo/zomg -
insoptsandexeoptsnow apply todoinsanddoexeonly -
In previous EAPIs, there was an inconsistency in how
insoptsandexeoptsapplied to various helpers. In particular, the majority of helpers (e.g.dobin,dodocand so on) ignored the options specified via these helpers but a few did not.EAPI 8 changes the behaviour of the following helpers that used to respect
insoptsorexeopts:doconfddoenvddoheaderdoinitd
In EAPI 8, they always use the default options. As a result,
insoptsnow only affectsdoins/newins, andexeoptsonly affectsdoexe/newexe. Furthermore,diroptsdoes not affect the directories implicitly created by these helpers. -
usevnow accepts a second argument -
The
usevhelper was introduced to provide the historical Portage behaviour of outputting the USE flag name on match. In EAPI 8, it has been extended, in order to provide an alternative to three-argumentusexwith an empty third argument (the two-argumentusexvariant uses a default ofnofor the false branch).In other words, the following two calls are now equivalent:
$(usex foo --enable-foo '') $(usev foo --enable-foo)This is particularly useful with custom build systems that accept individual
--enableor--disableoptions but not their counterparts.As a result,
usevandusexcan now be used to achieve all the common (and less common) output needs as summarized in the following table.Variant True False usev flag flag usev flag true true usex flag yesnousex flag true true nousex flag true false true false -
hasq,hasvanduseqfunctions have been banned -
In its early days, the
usehelper would print the USE flag name if it matched, in addition to its boolean exit status. Later, a quietuseqand a verboseusevhelper were added, andusewas made quiet by default. The same changes were applied tohas.Fast forward to EAPI 7, there are still three variants of
useand three variants ofhas. The base variant that is quiet, theuseq/hasqvariant that is equivalent to the base variant, and the verboseusev/hasvvariant.Obviously, adding a second argument to
hasvwas not possible, so its behaviour would have become inconsistent withusevin EAPI 8. Sincehasvwas not used in the Gentoo repository, it has been removed, along withhasqanduseqwhich were considered deprecated since 2011. - unpack removes support for 7-Zip, LHA and RAR formats
-
Support for the least commonly used archive formats from
unpackhas been removed:- 7-Zip (.7z)
- LHA (.lha, .lzh)
- RAR (.rar)
Packages using these format for distfiles must now unpack them manually. Using
unpacker.eclassis recommended for this.
EAPI 9
EAPI 9 profiles
- Default EAPI for profiles
-
Profile directories that don't contain their own
eapifile no longer default to EAPI 0, but to the EAPI specified in the top-levelprofilesdirectory. -
use.stableandpackage.use.stable -
Profile dirs may contain two new files
use.stableandpackage.use.stable. They can be used to override the USE flags specified bymake.defaults, but act only on packages that would be merged due to a stable keyword. The format of theuse.stablefile is simple: Each line contains a USE flag to enable; a minus sign before the flag indicates that the flag should be disabled instead. Thepackage.use.stablefile has the same format aspackage.use(see make.conf(5)).USE_EXPANDvalues may be enabled or disabled by usingexpand_name_value. Flags listed inpackage.use.stabletake precedence over those listed inpackage.use, which in turn take precedence overuse.stable.
EAPI 9 ebuild format
- Bash version is now 5.3
-
Ebuilds can use Bash version 5.3 (was 5.0 before). Some notable new features are:
-
declareandlocalnow have a-Ioption that inherits the attributes and value of any existing variable with the same name at a surrounding scope. For example, an ebuild could dolocal -Ix ROOTto export the
ROOTvariable to the environment, from where an external command could get its value. -
There is a new form of command substitution:
${ command; }or${|command;}to capture the output ofcommandwithout forking a child process and using pipes.${ command; }executescommandin the current execution environment and captures its output. Any side effects ofcommandtake effect immediately in the current execution environment and persist after the command completes. This type of command substitution superficially resembles executing an unnamed shell function: local variables are created as when a shell function is executing, and thereturnbuiltin forcescommandto complete; however, the rest of the execution environment is shared with the caller.The
${|command;}construct expands to the value of theREPLYshell variable aftercommandexecutes, without removing any trailing newlines, and the standard output ofcommandremains the same as in the calling shell. Bash createsREPLYas an initially-unset local variable whencommandexecutes, and restoresREPLYto its old value aftercommandcompletes, as with any local variable.For example, this construct expands to
12345, and leaves the shell variableXunchanged in the current execution environment:${ local X=12345; echo $X; }while the following construct does not require any output to expand to
12345and restoresREPLYto the value it had before the command substitution:${| REPLY=12345; } -
Pattern substitution now duplicates a common
sedidiom: If thepatsub_replacementshell option is enabled (which it is by default), any unquoted&in the replacement string is replaced with the portion of the string that matched the pattern.For instance:
var="foobar" echo "${var/foo/&123}"will print
foo123bar, i.e. the&in the replacement string is replaced with the stringfoothat matched the pattern.A backslash can be used to escape the
&and insert a literal&in the replacement string.
-
EAPI 9 variables
- Variables are no longer exported
-
The package manager no longer exports its defined shell variables to the environment but keeps them as unexported shell variables, namely:
-
CATEGORY,P,PF,PN,PV,PR,PVR A-
FILESDIR,DISTDIR,WORKDIR,S,ROOT,EROOT,T,EPREFIX,D,ED USE-
EBUILD_PHASE,EBUILD_PHASE_FUNC MERGE_TYPE-
REPLACING_VERSIONS,REPLACED_BY_VERSION -
ECLASS,INHERITED,DEFINED_PHASES
Important: Exceptions areSYSROOT,ESYSROOT,BROOT,TMPDIRandHOME, which are always exported.The same applies to variables set in
make.defaultsthat have specific meanings or special handling. These are no longer exported to the environment of an EAPI 9 ebuild:ARCH-
CONFIG_PROTECT,CONFIG_PROTECT_MASK -
USE,USE_EXPAND,USE_EXPAND_UNPREFIXED,USE_EXPAND_HIDDEN,USE_EXPAND_IMPLICIT,IUSE_IMPLICIT ENV_UNSET-
All variables named in
USE_EXPANDandUSE_EXPAND_UNPREFIXED -
All
USE_EXPAND_VALUES_${v}variables, where${v}is a value inUSE_EXPAND_IMPLICIT
All other variables set in
make.defaultswill still be exported to the environment.As a consequence, external commands can no longer rely on the value of the
ROOTvariable in the environment. For example,eselectmust be called with the--rootoption in EAPI 9:pkg_postinst() { eselect --root="${ROOT}" emacs update ifunset } -
EAPI 9 commands
-
asserthas been banned -
Our definition of
assertdeviated from the semantics of the command in mainstream programming languages and was a source of confusion. Therefore, use of theassertcommand in ebuilds is no longer allowed.pipestatus(see below) should be used as replacement. -
domohas been banned -
The operation of the
domocommand was unintuitive. It also hardcoded${PN}as the gettext domain, which is often not what is wanted. Therefore, thedomocommand has been banned.insintoplusnewinsshould be used as replacement. pipestatus-
Checks if all commands in the last executed pipeline have returned an exit
status of zero.
foo | bar | baz pipestatus || die -
When passed the
-voption, it also prints the pipeline's exit status values, i.e. Bash'sPIPESTATUSarray. This makes it easier to identify which stage of the pipeline failed. Note that declaring and assigning in a single statement likelocal status=$(pipestatus -v)masks the exit status ofpipestatusand must be avoided when combined with|| die.local status foo | bar | baz | qux status=$(pipestatus -v) || die "qux pipe failed (pipestatus: ${status})" -
ver_replacingop v2 -
Checks if the relation v1 op v2 is true for at least one element v1 of
REPLACING_VERSIONS. op can be any operator that is accepted byver_test. Returns shell true (0) if the specified relation holds for at least one element, shell false (1) otherwise. In particular, shell false is returned whenREPLACING_VERSIONSis empty.Obviously,
ver_replacingis only meaningful in phases whereREPLACING_VERSIONSis defined, i.e. inpkg_preinstandpkg_postinst.pkg_postinst() { if ver_replacing -lt 3; then elog "This is a major upgrade and may break your existing setup!" fi } edo- Outputs its entire argument list as an informational message, then executes it as a simple shell command, with standard failure behaviour.
EAPI 9 merging and unmerging
- Merging of symlinks
-
When merging
DtoROOT, absolute symlinks are now merged as-is. The package manager will no longer strip a leadingDfrom their link targets.In previous EAPIs, any absolute symlink whose link target started with
${D}was rewritten with the leading${D}removed. That behaviour was ill-defined becauseDis not guaranteed to have the same value that it had in thesrc_*phases. For example, when a binary package was merged on a target host withPORTAGE_TMPDIRdifferent from the build host'sPORTAGE_TMPDIR, symlink rewriting would have failed.