My system is Debian Buster. I would like to track the list of installed packages with git.
When I list installed packages with dpkg -l
, where does the list come from?
I found some package info in /var/lib/dpkg/status
, but this file has more information than I am interested in. Is there some other place where the package list is stored ?
What is the best file to track, so that I can have overview of installed packages, their versions, or uninstalled packages?
UPDATE
I have tried tracking /var/lib/dpkg/status with git, but the output is very unclear and confusing. There is simply too much information in status. I just need to track the list of installed packages, and their versions. Something like output of dpkg -l.
Is the list of packages, as shown by dpkg -l, stored in some file, or is it generated each time on the fly?
Could I create a git repository in /var/lib/dpkg/ and create some filter in git, so that basically only the output of dpkg -l is beineg tracked?
Or perhaps that each time I run git status`, the list is created dynamically ? Or any other solution, I am not sure what possibilities git offers.
Best Answer
The list of installed packages is in
/var/lib/dpkg/status
; that is the canonical reference. Installed packages are signalled in that file by their “install ok installed” status.dpkg -l
processes this file every time it’s run, and uses the information stored therein to produce its output.If you want a simpler set of data to track, simplifying comparisons, you’ll have to generate it whenever necessary.
If you only want to track a list of installed packages, you can run
periodically and store its output in a file tracked with
git
; since you also want versions,might be more suitable.
As pointed out by Martin Konrad, if you want to be able to use the information generated here to restore the state of the system at a later date, you should also track the manually-installed markers, and I’d add the holds too:
You could add all the above to a
dpkg
hook, to track all changes to your system; for example, using/etc/packages/
to hold the files (rather than/var/lib/dpkg
, which is “owned” bydpkg
and should be left as-is), create a file named/etc/dpkg/dpkg.cfg.d/package-history
, containingand a file named
/usr/local/bin/package-history
containingThe latter needs to be executable:
The outputs of all the commands above are sorted, so there’s no need to post-process them. With those files, you’ll be able to restore the installed package states exactly, track version changes, and also see packages which have been removed but not purged.
You can either add
git commit
(checking for changes first) to thepackage-history
script, or useetckeeper
to track changes to the files in/etc/packages
, or even make/etc/packages
a git repository itself. Using adpkg
hook ensures that the files will be updated with any package change, whether driven byapt
ordpkg
or any other tool piggy-backing on top ofdpkg
. If you commit in thepackage-history
script itself, then the commit granularity will correspond todpkg
executions; if you rely onetckeeper
, it will correspond to actions involvingetckeeper
.To handle the commit in the script, add
to the end of the script above; you should then run it once manually, as root, to initialise the git history (after
mkdir /etc/packages; git init /etc/packages
).