Creating Gentoo ebuilds is easy as fuck!
Ah shit… Here we go again manually downloading binary version of libspotify
from archive on GitHub. But wait 🤔, don’t we have package manager already? Let’s fix that right now!
What is ebuild anyway?
According to Gentoo wiki:
An ebuild file is a text file, usually stored in a repository, which identifies a specific software package and tells the Gentoo package manager how to handle it. Ebuilds use a bash-like syntax style and are standardized through the Package Manager Specification, by adhering to a specific EAPI version.
Basically it’s just a file which contains a set of instructions to Portage on how to work with your package. If you are trying to install a non-standard package (e.g not from the official Gentoo tree), chances are there’s already an ebuild for it in someone’s overlay (= ebuild repository) on the internet.
Creating your own overlay
There are multiple reasons for creating your own overlay with custom ebuilds, here are some of them:
- You want Portage to manage installation of your custom packages
- You want to share package across your machines
- You want to distribute a package for your own program
The process of creating an overlay is as easy as initializing a git repo and is well documented on the wiki. Here’s an example ebuild for raylib, which is a simple and easy-to-use library to enjoy videogames programming, that I’m using for my Carcassonne game engine:
DESCRIPTION="simple and easy-to-use library to enjoy videogames programming"
HOMEPAGE="http://www.raylib.com/"
SRC_URI="https://github.com/raysan5/raylib/archive/refs/tags/4.0.0.tar.gz"
S="${WORKDIR}/${P}/src"
LICENSE="zlib"
SLOT="0"
KEYWORDS="~amd64"
src_compile() {
emake PLATFORM=PLATFORM_DESKTOP RAYLIB_LIBTYPE=SHARED
}
src_install() {
emake install RAYLIB_LIBTYPE=SHARED RAYLIB_INSTALL_PATH=${D}/usr/lib64 RAYLIB_H_INSTALL_PATH=${D}/usr/include
}
I’m often switching machines when developing my program, so I want to make sure that all dependencies are easily installable. Since I’ve recently switched my last workstation to Gentoo, sharing packages can’t be easier for me! As you can see, I’m just passing a link to a release tarball, specifying stuff like license and keywords and some metadata. All magic is of course happening inside the functions, which are as easy as calling make install
with some environmental overrides. There’s also a default implementation for these functions, and in many cases you don’t even need to override them. Now I can install my package as usual emerge -av media-libs/raylib
, and Portage will do the job. Everything’s as it should be - a package manager is managing a package.
Another example would be libspotify
, which is a proprietary program that enables other programs to use Spotify API:
EAPI=8
HOMEPAGE="https://github.com/mopidy/libspotify-archive"
SRC_URI="https://github.com/mopidy/libspotify-archive/raw/master/${P}-Linux-x86_64-release.tar.gz"
S="${WORKDIR}/${P}-Linux-x86_64-release"
SLOT="0"
KEYWORDS="amd64"
PATCHES=(
"${FILESDIR}/destdir.patch"
)
src_compile() {
true
}
src_install() {
emake install DESTDIR=${D}/usr LIB=lib64
}
src_uninstall() {
emake install DESTDIR=${D}/usr LIB=lib64
}
It’s a bit trickier: we don’t need to compile it, so we explicitly say that we are doing nothing in src_compile
function. We also apply a patch so that we can more precisely specify destination directory. It’s also important to match the keywords - since this binary is precompiled for amd64
architecture I specify the corresponding keyword and install it under /usr/lib64
. Now I can stream my Spotify playlists from my mopidy
server!
These examples are not perfect, and they are not trying to be! I’ve created them for personal use, but they can give you an idea about the whole process. But you can of course get them from my overlay. There’s also a package for openring and my own build of dwmblocks
.
Footnotes
If you are planning to be maintainer of a public overlay, you should of course pay more attention to the ebuild writing guidelines, perform some quality assurance, probably create some patches, and test multiple versions.