I’ve been aware of epatch_user for some time, but I always thought it had to be used by ebuilds explicitly until I read epatch_user in bashrc.

Basically, you just add a /etc/portage/bashrc file with the following hook:

post_src_prepare() {
    if type epatch_user &> /dev/null ; then
        cd ${S}
        epatch_user
    fi
}

This will invoke epatch_user whatever packages are emerged. x11-dwm is the only packages I actually need to apply patches, but I managed to use savedconfig to apply the patches I got from dwm website without actually using patch. Now, since I knew I don’t need to have ebuild to invoke that, I decided to try it out.

I want to limit the use of bashrc, I don’t want any packages to have that hook, so /etc/portage/env is the way to go. Here is the commands to create the hook only for dwm package:

mkdir -p /etc/portage/env/x11-wm
vi /etc/portage/env/x11-wm/dwm
# copy and paste the hook code above

There are stricter ways to set up, such as limiting to specific version, read portage(1) for more detail. Next step is to add the patches I need, which should be store at /etc/portage/patches/x11-wm/dwm in this case.

Final step is emerging dwm, here is an example output of emerge:

# emerge -1 dwm
Calculating dependencies... done!

>>> Verifying ebuild manifests

>>> Emerging (1 of 1) x11-wm/dwm-6.0
 * dwm-6.0.tar.gz SHA256 SHA512 WHIRLPOOL size ;-) ...    [ ok ]
>>> Unpacking source...
>>> Unpacking dwm-6.0.tar.gz to /var/tmp/portage/x11-wm/dw[snip]
>>> Source unpacked in /var/tmp/portage/x11-wm/dwm-6.0/work
>>> Preparing source in /var/tmp/portage/x11-wm/dwm-6.0/wo[snip]
 * Checking existence of //etc/portage/savedconfig//x11-wm[snip]
 * found //etc/portage/savedconfig//x11-wm/dwm-6.0
 * Building using saved configfile //etc/portage/savedconf[snip]
 * Applying user patches from /etc/portage/patches//x11-wm[snip]
 *   dwm-5.8.2-gridmode.patch ...                         [ ok ]
 *   dwm-6.0-push.patch ...                               [ ok ]
 * Done with patching
>>> Source prepared.
>>> Configuring source in /var/tmp/portage/x11-wm/dwm-6.0/[snip]
>>> Source configured.
>>> Compiling source in /var/tmp/portage/x11-wm/dwm-6.0/wo[snip]
make -j2 CC=x86_64-pc-linux-gnu-gcc dwm
x86_64-pc-linux-gnu-gcc -c [snip] dwm.c
In file included from dwm.c:288:0:
[snip]
>>> Installing (1 of 1) x11-wm/dwm-6.0
[snip]
>>> Auto-cleaning packages...

>>> No outdated packages were found on your system.

 * GNU info directory index is up-to-date.

You can see two patches are applied. Note that the patches have to use .patch extension, you may need to rename the extension. If any patch is failed to apply, then emerge will not continue, and you can see an error message about that.

Like env, you can have stricter ways to patch:

/etc/portage/patches/<category>/<package>[-<version>[-<revision>]]

Since it will fail anyway, because my dwm config uses the code from the patches, doesn’t seem to have any points to create directories for specific versions.