Gentoo one-liners for a rainy day

Published: June 4th, 2025

Prerequisites

Install the necessary Portage utilities and GNU Parallel:

emerge -av app-portage/portage-utils app-portage/gentoolkit sys-process/parallel

Finding packages with the most dependencies

Show which packages pull in the greatest number of dependencies:

qlist -CI | sort -u | parallel 'echo $(qdepends -CqqQ {} | wc -l) {}' | sort -n > pkgs_with_most_deps.txt

Here’s what each part does:

The output shows dependency count followed by package name, from packages with the least dependencies to greatest.

Finding the largest packages

Find which packages consume the most storage:

qlist -CI | sort -u | parallel qsize -C | awk '{print $NF,$1}' | cut -d ':' -f 1 | sort -h > biggest_pkgs.txt

Breaking this down:

This arranges packages from smallest to largest.

Combining package size and dependency count

Merge dependency and size data in one file for analysis:

join -j 2 -o 1.1,2.1,0 <(sort -k2 pkgs_with_most_deps.txt) <(sort -k2 biggest_pkgs.txt) | sort -n | tr ' ' '\t' > combined.txt

The join command joins lines of two files on a common field:

Print packages without a package.env entry

Print out packages that haven’t been customized in /etc/portage/package.env:

comm -13 <(awk 'match($1, /^[a-zA-Z0-9_+-]+\/[a-zA-Z0-9_+-]+$/) {print $1}' /etc/portage/package.env/* | sort -u) <(qlist -CI | sort -u)

This comm usage compares sorted lists:

As above, but only larger packages

Focus on packages large enough to warrant optimization attention:

comm -13 <(awk 'match($1, /^[a-zA-Z0-9_+-]+\/[a-zA-Z0-9_+-]+$/) {print $1}' /etc/portage/package.env/* | sort -u) <(qlist -CI | sort -u) | grep -f - combined.txt | awk 'match($2, /^([0-9]{4}\.[0-9]K)|([0-9]+\.[0-9][MG])$/)' | sort -n

This adds size filtering:

Analyzing upgradable packages

Find packages that aren’t customized in /etc/portage/package.env and are already due for an upgrade anyway:

comm -13 <(awk 'match($1, /^[a-zA-Z0-9_+-]+\/[a-zA-Z0-9_+-]+$/) {print $1}' /etc/portage/package.env/* | sort -u) <(EIX_LIMIT=0 eix -u -# | sort -u)

This targets upgradable packages:

Multi-column sorting

Use multiple keys for different analysis priorities:

sort -k1,1n -k2,2h combined.txt
sort -k2,2h -k1,1n combined.txt

The first command prioritizes dependency count, using size as a tiebreaker. The second makes size the primary criterion. The h and n are for human-readable and numeric sorting.

Practical applications

These commands help to identify large packages that could be replaced with lightweight alternatives, which can be useful for a number of reasons:

When I was using these commands, it was to guide CFLAGS tweaking to better optimize Gentoo in a VM, since virtual machines have limited resources. I use a setup where video playback cannot benefit from hardware acceleration at the time of writing (Qubes OS), so I was seeing if this approach could make a difference. I think I did manage to gain a few FPS from this, but as I didn’t benchmark it beyond comparing frame timings in mpv and the gain is relatively minor (<=10fps), I have no solid proof.

Though with that said, I should mention that these days I really believe in benchmarking and profiling, as they make a huge difference. The gains from those techniques is typically far greater than CFLAGS tweaking.