Categories
SysOps

How to query APT configuration

Query APT configuration to inspect specific options or create universal Debian utilities.

Query all APT configuration options

Query and display all APT configuration options.

$ apt-config dump 
APT "";
APT::Architecture "amd64";
APT::Build-Essential "";
APT::Build-Essential:: "build-essential";
APT::Install-Recommends "1";
APT::Install-Suggests "0";
[...]
Dir "/";
Dir::State "var/lib/apt";
Dir::State::lists "lists/";
Dir::State::cdroms "cdroms.list";
Dir::State::mirrors "mirrors/";
Dir::State::extended_states "extended_states";
Dir::State::status "/var/lib/dpkg/status";
[...]
Acquire::AllowInsecureRepositories "0";
Acquire::AllowWeakRepositories "0";
Acquire::AllowDowngradeToInsecureRepositories "0";
[...]
CommandLine::AsString "apt-config dump";

Query all APT configuration options and display cache settings.

$ apt-config dump | grep "^Dir\( \|::Cache\)"
Dir "/";
Dir::Cache "var/cache/apt";
Dir::Cache::archives "archives/";
Dir::Cache::srcpkgcache "srcpkgcache.bin";
Dir::Cache::pkgcache "pkgcache.bin";
Dir::Cache::Backup "backup/";

Use the specific format and additional utilities like grep and cut to query and display regular expressions matching packages that will not be automatically removed.

$ apt-config dump --no-empty --format '%f,%v%n' | grep NeverAutoRemove | grep -v  "^$" | cut -d, -f2 
^firmware-linux.*
^linux-firmware$
^linux-image-4\.9\.0-3-amd64$
^linux-headers-4\.9\.0-3-amd64$
^linux-image-extra-4\.9\.0-3-amd64$
^linux-signed-image-4\.9\.0-3-amd64$
^kfreebsd-image-4\.9\.0-3-amd64$
^kfreebsd-headers-4\.9\.0-3-amd64$
^gnumach-image-4\.9\.0-3-amd64$
^.*-modules-4\.9\.0-3-amd64$
^.*-kernel-4\.9\.0-3-amd64$
^linux-backports-modules-.*-4\.9\.0-3-amd64$
^linux-tools-4\.9\.0-3-amd64$

Investigate the potential impact on the APT configuration.

$ diff  <(apt-config dump | grep -v CommandLine::AsString) <(apt-config -o APT::Install-Suggests=1 dump | grep -v CommandLine::AsString)
6c6
< APT::Install-Suggests "0";
---
> APT::Install-Suggests "1";
$ diff <(apt-config dump | grep -v CommandLine::AsString) <(apt-config -o Acquire::http::Proxy="http://192.168.33.10:3142" dump | grep -v CommandLine::AsString)
188a189,190
> Acquire::http "";
> Acquire::http::Proxy "http://192.168.33.10:3142";

Query individual APT configuration options

Query and display individual APT configuration options.

$ eval $(apt-config shell Dir Dir)
$ eval $(apt-config shell CacheDir Dir::Cache)
$ echo ${Dir}${CacheDir}
/var/cache/apt

It is especially useful inside shell scripts to create universal configuration utilities.

# bash function
# return source list file path for provided name
function source_list_file_path() {
  local name dir etc src

  if [ -n "${1}" ]; then
    name="${1}"

    # set defaults
    dir="/"
    etc="etc/apt"
    src="sources.list.d"

    # query configuration options
    eval $(apt-config shell dir Dir)
    eval $(apt-config shell etc Dir::Etc)
    eval $(apt-config shell src Dir::Etc::sourceparts)

    # return path for source list file
    echo "${dir}${etc}/${src}/${name}.list"
  fi
}
$ source_list_file_path "jenkins"
/etc/apt/sources.list.d/jenkins.list

Alternatively, you can use APT configuration to store and query options to set up a custom application, but there are probably better ways to achieve this goal.

Read apt-config manual page.