Debconf in Debian Packages
- 3 minsAs part of my GSoC project, I had to find a way to ask users questions and install the packages depending on their given answers, my specific case was to ask the users to select an entry from multiple choices and download an archive from the chosen one, to achieve this, I used debconf.
So what is debconf ?
Debconf is a backend database, with a frontend that talks to it and presents an interface to the user. There can be many different types of frontends, from plain text to a web frontend. The frontend also talks to a special config script in the control section of a Debian package, and it can talk to postinst scripts and other scripts as well, all using a special protocol. These scripts tell the frontend what values they need from the database, and the frontend asks the user questions to get those values if they aren’t set.
Even better, we can make sure that the users get the questions in their own languages by using po-debconf
.
Steps:
- Install
debconf
andpo-debconf
:
$ apt install debconf po-debconf
- Create
debian/templates
file. An underscore before a field name indicates that this field is translatable. Example:
Template: packagename/something
Type: select
Choices: choice1, choice2
Default: choice1
_Description: A short description here
A longer description here about the quastion.
- Create debian/config file (
packagename/somthing
is the same as indebian/templates
):
#!/bin/sh
set -e
# Source debconf library.
. /usr/share/debconf/confmodule
# Run template
db_input high packagename/something || true
db_go
#DEBHELPER#
exit 0
- Create the folder
debian/po
$ cd debian && mkdir po
- Create the file
debian/po/PORFILES.in
:
$ echo "[type: gettext/rfc822deb] templates" > po/POTFILES.in
- Generate the
debian/po/templates.pot
:
$ debconf-updatepo
- Add
debconf
andpo-debconf
todebconf/control
as dependenties:
Depends: debconf,
po-debconf,
${misc:Depends}
- Get and use the result (in
debian/postinit
for example with the variable$RET
):
#!/bin/sh
set -e
# Source debconf library.
. /usr/share/debconf/confmodule
db_get packagename/something
case "$1" in
configure)
make -C /var/cache/packagename/ DL_MIRROR="$RET" install
;;
abort-upgrade|abort-remove|abort-deconfigure)
;;
*)
echo "postinst called with unknown argument \`$1'" >&2
exit 1
;;
esac
#DEBHELPER#
exit 0
- We can purge the database when purging the package with the command
db_purge
command indebian/postrm
, example:
#!/bin/sh
set -e
case "$1" in
purge)
rm -rf /var/cache/packagename
if [ -e /usr/share/debconf/confmodule ]
then
# Source debconf library and purge db
. /usr/share/debconf/confmodule
db_purge
fi
;;
remove|upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)
;;
*)
echo "postrm called with unknown argument \`$1'" >&2
exit 1
;;
esac
#DEBHELPER#
exit 0
- Finaly, We can add the entry
packagename.postrm.debhelper
to.gitignore
.