How to modify source code on Gentoo
The cool thing about Gentoo is that you can modify a package’s source code as you see fit and Portage will still manage that package for you. This is done using patches. Modifying the sources directly won’t work, as emerge will re-fetch the package.
For this example, I’ll modify sys-process/btop-1.1.2.
Preparing and modifying the sources
- Fetch the sources with
emerge --fetchonly btop
-
Extract them to a/ and b/ (don’t use any other names) in the current directory:
tar xf /var/cache/distfiles/btop-1.1.2.tar.gz -C a tar xf /var/cache/distfiles/btop-1.1.2.tar.gz -C b
- Modify the sources in b/ as you see fit. Don’t touch a/.
Generating a patch
A patch is just the diff -ru
between the original package and the modified
version. The patch may be called anything, but must have a .patch
or .diff
extension
Generate the patch:
mkdir -p /etc/portage/patches/sys-process/btop-1.1.2
touch /etc/portage/patches/sys-process/btop-1.1.2/lowerdelay.patch
diff -ru a b | sudo tee /etc/portage/patches/sys-process/btop-1.1.2/lowerdelay.patch >/dev/null
Applying the patch
Re-emerge the package: emerge -1 btop
. You should see something like:
* Messages for package sys-process/btop-1.1.2:
* User patches applied.