User:Tastytea/Drafts/Writing go Ebuilds

From Gentoo Wiki
Jump to:navigation Jump to:search

This is a short reference, intended to be read alongside Basic guide to write Gentoo Ebuilds.

go-module eclass

If the software you are packaging has a file named go.mod in its top level directory, it uses modules and your ebuild should inherit this eclass.

Packaging the dependencies

Ebuilds using the go-module eclass are different from most other ebuilds, because they have to include every go dependency. If you are lucky, the project has a vendor/ directory and you don't have to do anything special. If it doesn't, you have to package the dependencies yourself.

Previously go-module packages often used EGO_SUM, but that is deprecated and will not be covered here. Instead we will create a vendor- or dependency-tarball.

Write the ebuild like normal, inherit go-module and add the upstream tarball to SRC_URI. Then unpack the package and cd to the build directory:

user $cd "$(portageq get_repo_path / <your repo name>)"/app-misc/foo
user $nano foo-1.ebuild
user $ebuild foo-1.ebuild unpack
user $cd /path/to/the/unpacked/source/foo-1

Vendor tarball

This method produces far smaller tarballs, but might not include everything needed to compile the package, especially if the dependencies compile C or C++ code.

user $go mod vendor
user $cd ..
user $tar --create --auto-compress --file foo-1-vendor.tar.xz foo-1/vendor

Upload the tarball somewhere and add it to SRC_URI.

Dependency tarball

If the package won't compile because the vendor tarball has not all dependencies, a dependency tarball is needed.

user $GOMODCACHE="${PWD}"/go-mod go mod download -modcacherw
user $tar --create --auto-compress --file tar-1-deps.tar.xz go-mod

Upload the tarball somewhere and add it to SRC_URI.

Uploading the tarball

If you're a Gentoo developer you can put the generated tarballs into your devspace, but what if you're not?

  • If you have access to a web or FTP server, or a file hosting service, put it there. Make sure the file can be downloaded without having to solve a captcha or having a cookie set. The easiest way to test this is by downloading it with wget.
  • If you have access to a git forge such as GitLab, Gitea, GitHub, … you can create an empty repository, add a new tag for each new version (named ${P}) and upload the tarballs to these ”releases“. Make sure that the hoster of the forge allows this usage.

Compiling and installing

Since the go-module eclass doesn't implement src_compile() and src_install(), you have to do that yourself.

FILE foo-1.ebuildSimple implementation of the compile and install phases
src_compile() {
    ego build

src_install() {
    dobin foo


TODO: Unbundling


Since Go programs are statically linked, it is important that your ebuild's LICENSE setting includes the licenses of all statically linked dependencies. So please make sure it is accurate. You can use a utility like dev-go/golicense or dev-go/go-licenses to extract this information.

See also