Writing ebuilds is easy!

By Matija Šuklje

Recently I have written (and submitted to the Gentoo bugzilla) a few ebuilds that I think were lacking in the portage tree and decided to write a small post to encourage others to do so as well. Namely the net-im/kopete-antispam (Antispam plugin for Kopete 3.x), app-pda/libopensync-plugin-moto (Motorola plugin for OpenSync) and games-action/wordwarvi (awesome retro-style scrolling shoot'em up).

If you were (like me) afraid of all the headers and variables needed to even start — fear not! There is a nice little trick to make you start easier.

I will demonstrate just how easy it is to write a basic ebuild based on my experience with net-im/kopete-antispam.

First tip is to not to work directly on your local overlay, but have a separate directory in your home to play with (e.g. ~/Code/ebuilds/) and then move what works to the overlay (e.g. /usr/local/portage/). This way you can avoid running a buggy system.

The next few steps are quite self-explanatory:

cd ~/Code/ebuilds
mkdir -p net-im/kopete-antispam
vim net-im/kopete-antispam/kopete-antispam-0.3.ebuild

In the last line lies another trick — if you start Vim with trailing $GROUP/$PACKAGE/$EBUILD it will automatically apply not only the generic ebuild template (with the appropriate KEYWORDS="" entry), but also inherit the appropriate eclasses (e.g. the 'games' eclass when writing ebuilds that fit under games-* groups).

In our example this is what Vim generates:

# Copyright 1999-2008 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $
 
DESCRIPTION=""
HOMEPAGE=""
SRC_URI=""
 
LICENSE=""
SLOT="0"
KEYWORDS="~amd64"
IUSE=""
 
DEPEND=""
RDEPEND=""

What we need to add now is basic information like the description, homepage, its USE flags, dependancies and so on. As you can see when downloading from SourceForge you can use the elegant mirror:// protocol to include all SF.net mirrors.

In our case we need no special USE flags (except those that come with the 'kde' eclass). But the package does depend on Kopete being installed either from the monolithic or the atomic KDE ebuilds on which the package depends both on compiling ($DEPEND) and on running ($RDEPEND)

Also note that since this is a KDE3 application (or actually plugin) we will inherit the 'kde' eclass and add the 'need-kde 3'.

# Copyright 1999-2008 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $
 
inherit kde
 
DESCRIPTION="This plugin enables anti-spam properties in the KDE instant
messenger Kopete."
HOMEPAGE="http://kopeteantispam.sourceforge.net"
SRC_URI="mirror://sourceforge/kopeteantispam/${P}.tar.gz"
 
LICENSE="GPL-2"
SLOT="0"
KEYWORDS="~amd64"
IUSE=""
 
DEPEND="|| ( =kde-base/kdenetwork-3.5* =kde-base/kopete-3.5* )"
RDEPEND="${DEPEND}"
 
need-kde 3

The above ebuild is already usable, but to make it even cleaner and more practical, we can tweak it a bit.

One thing that you might notice is that when you inherit the 'kde' class you currently automatically get 'arts' (amongst others) in the package's USE flags. Since the Kopete Antispam plugin has no need whatsoever for sound, we can safely remove the 'arts' USE flag.

Also the RDEPEND variable by default replicates the DEPEND, so it is cleaner (and desired) to just omit it when they are identical.

And while we are at it, we might as well add a nice message for the user to know how to enable the plugin. This can be easily done with the pkg_postinst() function.

# Copyright 1999-2008 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $
 
ARTS_REQUIRED="never"
inherit kde
 
DESCRIPTION="This plugin enables simple anti-spam properties in the KDE instant
messenger Kopete."
HOMEPAGE="http://kopeteantispam.sourceforge.net"
SRC_URI="mirror://sourceforge/kopeteantispam/${P}.tar.gz"
 
LICENSE="GPL-2"
SLOT="0"
KEYWORDS="~amd64"
IUSE=""
 
DEPEND="|| ( =kde-base/kopete-3.5* =kde-base/kdenetwork-3.5* )"
 
need-kde 3.5
 
pkg_postinst() {
    elog "You can now enable and set up the Antispam plugin in Kopete."
    elog "It can be reached in the Kopete Plugin dialog."
}

Of course this is a quite simple ebuild without any needed patching to the 'Makefile' or source or other complications (e.g. non-standard uncompressing, compiling etc.). But there are a lot of packages out there that need nothing more then someone to write such a small ebuild for them and to maintain it. So be a sport and adopt an ebuild!

Further reading:
Gentoo official ebuild writing manual
FlameEyes' post on when to use DEPEND and when RDEPEND
/usr/portage/eclass/* for which functions and variables each eclass introduces

edit:
* added the '.ebuild' extention which I initially forgot
* changed 'mkdirhier' to 'mkdir -p', because it is more common (thank you, John Alberts)

11 comments

By Matija Šuklje
2 years 13 hours ago

Vim and Emacs .ebuild templates

For the .ebuild templates in Vim you need the 'app-vim/gentoo-syntax' package — but it already gets pulled by the 'app-editors/vim-core' (unless you have USE="livecd").

Similarly for Emacs or XEmacs you need 'app-emacs/gentoo-syntax' or 'app-xemacs/gentoo-syntax' respectively (as Aron hinted).

By John Alberts (not verified)
2 years 23 hours ago

A quick question

You're article has:
"mkdirhier net-im/kopete-antispam"

was this just a typo? I assumed it was, but "vim net-im/kopete-antispam/kopete-antispam-0.3" doesn't start me off with a nice template like you show.

By Matija Šuklje
2 years 23 hours ago

The mkdirhier

The mkdirhier net-im/kopete-antispam bit is there to make the directory 'net-im' and inside it a subdirectory 'kopete-antispam'.

I took the template for granted, but now that you mention it, you probably need to emerge app-vim/gentoo-syntax — I hope that helps. Terribly sorry that I missed it.

By John Alberts (not verified)
2 years 23 hours ago

So "mkdirhier" is actually a

So "mkdirhier" is actually a command? I don't have that on my system. I do have app-vim/gentoo-syntax installed. I just re-emerged and it still doesn't generate any type of template for me. Is there some config setting in vim that I need to enable maybe?

By Matija Šuklje
2 years 22 hours ago

More Vim trouble + where is mkdirhier

Yes, it is.
mkdirhier net-im/kopete-antispam/files
just equals
mkdir net-im; cd net-im; mkdir kopete-antispam; cd kopete-antispam; mkdir files.

It's quite useful. Again, I thought that mkdirhier fits under system (e.g. binutils). A quick equery belongs mkdirhier shows that it belongs to the 'x11-misc/imake' package though.

Oh, crud! I made a typo in the "vim [...]" line! Of course the ebuild should have the .ebuild file extention! I'll fix it right away! Sorry about that.

By tante (not verified)
1 year 52 weeks ago

Just as an addition: Instead

Just as an addition: Instead of mkdirhier you can just use mkdir -p /path/of/dirs/and/subdirs to create all nonexisisting directories along the way.

By John Alberts (not verified)
2 years 23 hours ago

Ok, I got the template thing

Ok, I got the template thing to work in vim.

You have "vim net-im/kopete-antispam/kopete-antispam-0.3" as the command, but you need ".ebuild" at the end.
So, it should be "vim net-im/kopete-antispam/kopete-antispam-0.3.ebuild"

I still have no idea what mkdirhier is, but "mkdir -p net-im/kopete-antispam" works fine.

BTW, thanks for this article. I didn't know there were nice templates for vim.

By Matija Šuklje
2 years 22 hours ago

Thanks

Sorry for the '.ebuild' typo and thanks for the 'mkdir -p' tip — I've changed both in the article, so it is hopefully more accurate now.

Truth be told, I stumbled upon this nice template by accident as well and thought it would be nice to share ;)

By Matija Šuklje
2 years 1 day ago

Emacs

Of course, I imagine Emacs has a similar template system as Vim!

By aron (not verified)
2 years 1 day ago

Lovely

Thanks for this. Good stuff.
I will check out http://www.gentoo.org/proj/en/lisp/emacs/ =)

By Matija Šuklje
2 years 22 hours ago

You're welcome. I really

You're welcome. I really have no Emacs experience whatsoever, so I cannot tell if that's the right project or not.