Gentoo Portage: Clean up Your Mess

Gentoo Linux

By default, Gentoo’s emerge downloads and saves a lot of distfiles. And by a lot I mean … A LOT!
And they are not removed automatically when the emerge process has finished successfully.

Over time this will accumulate quite a collection of files in /vat/cache/distfiles that are no longer needed and just collecting dust there. Not to mention the space wasted by those files.

Of course, you can remove these files manually occasionally, but wouldn’t it be nice if portage would take care of this?

And portage can!
With a little tweak that I found quite some time ago.

Save the following as /etc/portage/bashrc:

# darkelf-features-0.1::darkelf
# created for ::darkelf overlay.
# by Simon the Sorcerer - http://homepages.uni-paderborn.de/neuron/gentoo/darkelf/
#
# This enables cleaning the distfiles after every emerge,
# to enable this feature set
# DARKELF_FEATURES="postmerge_distclean"
# in /etc/portage/make.conf or per command basis e.g.:
# DARKELF_FEATURES="postmerge_distclean" emerge ...

darkelf_postmerge_distclean() {
    echo cleaning distfiles for ${CATEGORY}/${PF} ...

    if [ -z $DISTDIR ] ; then
        echo ERROR: DISTDIR is empty!
        exit 1
    fi

    for f in $DISTDIR/* ; do
        echo deleting "`readlink -f $f`" ...
        rm -r "`readlink -f $f`"
    done
}

post_pkg_postinst() {
    if grep -q "postmerge_distclean" <<< $DARKELF_FEATURES ; then
        darkelf_postmerge_distclean
    fi
}

And now add the following to your /etc/portage/make.conf file:

# Portage clean up
# Remove downloaded distfiles from ${DISTDIR}
# see /etc/portage/bashrc
DARKELF_FEATURES="postmerge_distclean"

From now on, emerge will remove the downloaded distfile for the package that just got merged successfully from /var/cache/distfiles and clean up after itself this way. All you have to do is to clean out that directory one last time.

One thing though, please don’t change the code, it’s not mine. So keep the copyright/creator header in the bash script.

2 Comments

    1. In general, yes.

      But as mentioned in the troubleshooting section of the page you linked, it seems to have issues with packages no longer in portage. And this command is a one-time that should be run after the whole emerge process to clean up. (At least as far as I understand it.)

      The solution here is running during emerge, after every package that has been successfully merged.

Leave a Reply

Your email address will not be published. Required fields are marked *