Debian package patch HowTo
Debian GNU/Linux is a great distribution and my favorite for server systems. But once in a while a great package misses some functionality. Debian often heavily patches upstream software to integrate it into the distribution seamlessly. So compiling a upstream package from source is usually not the best option if a debian package is available and just misses a patch. Here is my small howto for patching existing packages "the debian way".
First /etc/apt/sources.list has to be configured to also index the source tree (deb-src) of a mirror. Example of a typical etch setup (as root):
# stable etch mirror
deb http ://ftp.uni-koeln.de/debian/ etch main contrib non-free
deb-src http ://ftp.uni-koeln.de/debian/ etch main contrib non-free
# security
deb http ://security.debian.org/ etch/updates main contrib non-free
deb-src http ://security.debian.org/ etch/updates main contrib non-free
After changing your sources.list you need to tell apt to get the new tree (as root):
apt-get update
Next you have to install any dependencies required for a successful compile run of your package. Debian implements a nice way to fetch and install all dependencies (as root):
apt-get build-dep <package>
You should now switch to an unprivileged user and fetch the source of a package:
mkdir <package>
cd <package>
apt-get source <package>
Several files and directories will be created. One of them is the native upstream package directory without any patches applied. Change dir to it and test if your unified patch applies with -p1 to it:
cd <package>-<version>
patch -p1 --dry-run < /path/to/my-patch.diff
If this test is ok, you can use debian to handle the patch before compile time. There is a special directory called "debian/patches" inside the source containing all patches a package should apply to the upstream source. A special header is needed for your patch which can be generated with dpatch:
cd <package-version>/debian/patches
dpatch patch-template > 99-my-patch.dpatch
cat /path/to/my-patch >> 99-my-patch.dpatch
After that you have your new patch in debian/patches and just need to tell dpkg to apply it before compiling. There is a special file called 00list, that references all patches, so you just add your patch there:
echo 99-my-patch.dpatch >> 00list
Go back to your package directory and create a new minor version of your hand crafted package with some meaningful information:
dch -n "Locally added patch with functionality foo."
All patching done for now. So lets build the package. You should see that your new patch is applied to the source.
dpkg-buildpackage -b -rfakeroot
If everything went ok, you should have one or more .deb files, that just need to be installed with dpkg (as root):
dpkg -i <package>-<newversion>.deb
All fine. You now have installed your patched package with a new minor version. To prevent debian to displace your new shiny package with a new one in case of security updates you need some pinning in apt. Be aware of security updates of your package and always recompile it with new source if debian comes up with a security update!
vi /etc/apt/preferences
Package: <package>
Pin: version <newversion>
Pin-Priority: 990