Chapter 8. Advanced pkg-plist Practices

8.1. Changing pkg-plist Based on Make Variables

Some ports, particularly the p5- ports, need to change their pkg-plist depending on what options they are configured with (or version of perl, in the case of p5- ports). To make this easy, any instances in pkg-plist of %%OSREL%%, %%PERL_VER%%, and %%PERL_VERSION%% will be substituted appropriately. The value of %%OSREL%% is the numeric revision of the operating system (for example, 4.9). %%PERL_VERSION%% and %%PERL_VER%% is the full version number of perl (for example, 5.8.9). Several other %%VARS%% related to port’s documentation files are described in the relevant section.

To make other substitutions, set PLIST_SUB with a list of VAR=VALUE pairs and instances of %%VAR%% will be substituted with VALUE in pkg-plist.

For instance, if a port installs many files in a version-specific subdirectory, use a placeholder for the version so that pkg-plist does not have to be regenerated every time the port is updated. For example, set:

OCTAVE_VERSION=	${PORTREVISION}
PLIST_SUB=	OCTAVE_VERSION=${OCTAVE_VERSION}

in the Makefile and use %%OCTAVE_VERSION%% wherever the version shows up in pkg-plist. When the port is upgraded, it will not be necessary to edit dozens (or in some cases, hundreds) of lines in pkg-plist.

If files are installed conditionally on the options set in the port, the usual way of handling it is prefixing pkg-plist lines with a %%OPT%% for lines needed when the option is enabled, or %%NO_OPT%% when the option is disabled, and adding OPTIONS_SUB=yes to the Makefile. See OPTIONS_SUB for more information.

For instance, if there are files that are only installed when the X11 option is enabled, and Makefile has:

OPTIONS_DEFINE=	X11
OPTIONS_SUB=	yes

In pkg-plist, put %%X11%% in front of the lines only being installed when the option is enabled, like this :

%%X11%%bin/foo-gui

This substitution will be done between the pre-install and do-install targets, by reading from PLIST and writing to TMPPLIST (default: WRKDIR/.PLIST.mktmp). So if the port builds PLIST on the fly, do so in or before pre-install. Also, if the port needs to edit the resulting file, do so in post-install to a file named TMPPLIST.

Another way of modifying a port’s packing list is based on setting the variables PLIST_FILES and PLIST_DIRS. The value of each variable is regarded as a list of pathnames to write to TMPPLIST along with PLIST contents. While names listed in PLIST_FILES and PLIST_DIRS are subject to %%VAR%% substitution as described above, it is better to use the ${VAR} directly. Except for that, names from PLIST_FILES will appear in the final packing list unchanged, while @dir will be prepended to names from PLIST_DIRS. To take effect, PLIST_FILES and PLIST_DIRS must be set before TMPPLIST is written, that is, in pre-install or earlier.

From time to time, using OPTIONS_SUB is not enough. In those cases, adding a specific TAG to PLIST_SUB inside the Makefile with a special value of @comment, makes package tools to ignore the line. For instance, if some files are only installed when the X11 option is on and the architecture is i386:

.include <bsd.port.pre.mk>

.if ${PORT_OPTIONS:MX11} && ${ARCH} == "i386"
PLIST_SUB+=	X11I386=""
.else
PLIST_SUB+=	X11I386="@comment "
.endif

8.2. Empty Directories

8.2.1. Cleaning Up Empty Directories

When being de-installed, a port has to remove empty directories it created. Most of these directories are removed automatically by pkg(8), but for directories created outside of ${PREFIX}, or empty directories, some more work needs to be done. This is usually accomplished by adding @dir lines for those directories. Subdirectories must be deleted before deleting parent directories.

[...]
@dir /var/games/oneko/saved-games
@dir /var/games/oneko

8.2.2. Creating Empty Directories

Empty directories created during port installation need special attention. They must be present when the package is created. If they are not created by the port code, create them in the Makefile:

post-install:
	${MKDIR} ${STAGEDIR}${PREFIX}/some/directory

Add the directory to pkg-plist like any other. For example:

@dir some/directory

8.3. Configuration Files

If the port installs configuration files to PREFIX/etc (or elsewhere) do not list them in pkg-plist. That will cause pkg delete to remove files that have been carefully edited by the user, and a re-installation will wipe them out.

Instead, install sample files with a filename.sample extension. The @sample macro automates this, see @sample file [file] for what it does exactly. For each sample file, add a line to pkg-plist:

@sample etc/orbit.conf.sample

If there is a very good reason not to install a working configuration file by default, only list the sample filename in pkg-plist, without the @sample followed by a space part, and add a message pointing out that the user must copy and edit the file before the software will work.

When a port installs its configuration in a subdirectory of ${PREFIX}/etc, use ETCDIR, which defaults to ${PREFIX}/etc/${PORTNAME}, it can be overridden in the ports Makefile if there is a convention for the port to use some other directory. The %%ETCDIR%% macro will be used in its stead in pkg-plist.

The sample configuration files should always have the .sample suffix. If for some historical reason using the standard suffix is not possible, or if the sample files come from some other directory, use this construct:

@sample etc/orbit.conf-dist etc/orbit.conf

or

@sample %%EXAMPLESDIR%%/orbit.conf etc/orbit.conf

The format is @sample sample-file actual-config-file.

8.4. Dynamic Versus Static Package List

A static package list is a package list which is available in the Ports Collection either as pkg-plist (with or without variable substitution), or embedded into the Makefile via PLIST_FILES and PLIST_DIRS. Even if the contents are auto-generated by a tool or a target in the Makefile before the inclusion into the Ports Collection by a committer (for example, using make makeplist), this is still considered a static list, since it is possible to examine it without having to download or compile the distfile.

A dynamic package list is a package list which is generated at the time the port is compiled based upon the files and directories which are installed. It is not possible to examine it before the source code of the ported application is downloaded and compiled, or after running a make clean.

While the use of dynamic package lists is not forbidden, maintainers should use static package lists wherever possible, as it enables users to grep(1) through available ports to discover, for example, which port installs a certain file. Dynamic lists should be primarily used for complex ports where the package list changes drastically based upon optional features of the port (and thus maintaining a static package list is infeasible), or ports which change the package list based upon the version of dependent software used. For example, ports which generate docs with Javadoc.

8.5. Automated Package List Creation

First, make sure the port is almost complete, with only pkg-plist missing. Running make makeplist will show an example for pkg-plist. The output of makeplist must be double checked for correctness as it tries to automatically guess a few things, and can get it wrong.

User configuration files should be installed as filename.sample, as it is described in Configuration Files. info/dir must not be listed and appropriate install-info lines must be added as noted in the info files section. Any libraries installed by the port must be listed as specified in the shared libraries section.

8.5.1. Expanding PLIST_SUB with Regular Expressions

Strings to be replaced sometimes need to be very specific to avoid undesired replacements. This is a common problem with shorter values.

To address this problem, for each PLACEHOLDER=value, a PLACEHOLDER_regex=regex can be set, with the regex part matching value more precisely.

Example 1. Using PLIST_SUB with Regular Expressions

Perl ports can install architecture dependent files in a specific tree. On FreeBSD to ease porting, this tree is called mach. For example, a port that installs a file whose path contains mach could have that part of the path string replaced with the wrong values. Consider this Makefile:

PORTNAME=	Machine-Build
DISTVERSION=	1
CATEGORIES=	devel perl5
MASTER_SITES=	CPAN
PKGNAMEPREFIX=	p5-

MAINTAINER=	perl@FreeBSD.org
COMMENT=	Building machine
WWW=		https://search.cpan.org/dist/Machine-Build

USES=		perl5
USE_PERL5=	configure

PLIST_SUB=	PERL_ARCH=mach

The files installed by the port are:

/usr/local/bin/machine-build
/usr/local/lib/perl5/site_perl/man/man1/machine-build.1.gz
/usr/local/lib/perl5/site_perl/man/man3/Machine::Build.3.gz
/usr/local/lib/perl5/site_perl/Machine/Build.pm
/usr/local/lib/perl5/site_perl/mach/5.20/Machine/Build/Build.so

Running make makeplist wrongly generates:

bin/%%PERL_ARCH%%ine-build
%%PERL5_MAN1%%/%%PERL_ARCH%%ine-build.1.gz
%%PERL5_MAN3%%/Machine::Build.3.gz
%%SITE_PERL%%/Machine/Build.pm
%%SITE_PERL%%/%%PERL_ARCH%%/%%PERL_VER%%/Machine/Build/Build.so

Change the PLIST_SUB line from the Makefile to:

PLIST_SUB=	PERL_ARCH=mach \
		PERL_ARCH_regex=\bmach\b

Now make makeplist correctly generates:

bin/machine-build
%%PERL5_MAN1%%/machine-build.1.gz
%%PERL5_MAN3%%/Machine::Build.3.gz
%%SITE_PERL%%/Machine/Build.pm
%%SITE_PERL%%/%%PERL_ARCH%%/%%PERL_VER%%/Machine/Build/Build.so

8.6. Expanding Package List with Keywords

All keywords can also take optional arguments in parentheses. The arguments are owner, group, and mode. This argument is used on the file or directory referenced. To change the owner, group, and mode of a configuration file, use:

@sample(games,games,640) etc/config.sample

The arguments are optional. If only the group and mode need to be changed, use:

@sample(,games,660) etc/config.sample

If a keyword is used on an optional entry, it must to be added after the helper:

%%FOO%%@sample etc/orbit.conf.sample

This is because the options plist helpers are used to comment out the line, so they need to be put first. See OPTIONS_SUB for more information.

8.6.1. @desktop-file-utils

Will run update-desktop-database -q after installation and deinstallation. Never use directly, add USES=desktop-file-utils to the Makefile.

8.6.2. @fc directory

Add a @dir entry for the directory passed as an argument, and run fc-cache -fs on that directory after installation and deinstallation.

8.6.3. @fontsdir directory

Add a @dir entry for the directory passed as an argument, and run mkfontscale and mkfontdir on that directory after installation and deinstallation. Additionally, on deinstallation, it removes the fonts.scale and fonts.dir cache files if they are empty.

8.6.4. @info file

Add the file passed as argument to the plist, and updates the info document index on installation and deinstallation. Additionally, it removes the index if empty on deinstallation. This should never be used manually, but always through INFO. See Info Files for more information.

8.6.5. @kld directory

Runs kldxref on the directory on installation and deinstallation. Additionally, on deinstallation, it will remove the directory if empty.

8.6.6. @rmtry file

Will remove the file on deinstallation, and not give an error if the file is not there.

8.6.7. @sample file [file]

This is used to handle installation of configuration files, through example files bundled with the package. The "actual", non-sample, file is either the second filename, if present, or the first filename without the .sample extension.

This does three things. First, add the first file passed as argument, the sample file, to the plist. Then, on installation, if the actual file is not found, copy the sample file to the actual file. And finally, on deinstallation, remove the actual file if it has not been modified. See Configuration Files for more information.

8.6.8. @shared-mime-info directory

Runs update-mime-database on the directory on installation and deinstallation.

8.6.9. @shell file

Add the file passed as argument to the plist.

On installation, add the full path to file to /etc/shells, while making sure it is not added twice. On deinstallation, remove it from /etc/shells.

8.6.10. @terminfo

Do not use by itself. If the port installs *.terminfo files, add to its Makefile.

On installation and deinstallation, if tic is present, refresh ${PREFIX}/shared/misc/terminfo.db from the *.terminfo files in ${PREFIX}/shared/misc.

8.6.11. Base Keywords

There are a few keywords that are hardcoded, and documented in pkg-create(8). For the sake of completeness, they are also documented here.

8.6.11.1. @ [file]

The empty keyword is a placeholder to use when the file’s owner, group, or mode need to be changed. For example, to set the group of the file to games and add the setgid bit, add:

@(,games,2755) sbin/daemon

8.6.11.2. @preexec command, @postexec command, @preunexec command, @postunexec command

Execute command as part of the package installation or deinstallation process.

@preexec command

Execute command as part of the pre-install scripts.

@postexec command

Execute command as part of the post-install scripts.

@preunexec command

Execute command as part of the pre-deinstall scripts.

@postunexec command

Execute command as part of the post-deinstall scripts.

If command contains any of these sequences somewhere in it, they are expanded inline. For these examples, assume that @cwd is set to /usr/local and the last extracted file was bin/emacs.

%F

Expand to the last filename extracted (as specified). In the example case bin/emacs.

%D

Expand to the current directory prefix, as set with @cwd. In the example case /usr/local.

%B

Expand to the basename of the fully qualified filename, that is, the current directory prefix plus the last filespec, minus the trailing filename. In the example case, that would be /usr/local/bin.

%f

Expand to the filename part of the fully qualified name, or the converse of %B. In the example case, emacs.

These keywords are here to help you set up the package so that it is as ready to use as possible. They must not be abused to start services, stop services, or run any other commands that will modify the currently running system.

8.6.11.3. @mode mode

Set default permission for all subsequently extracted files to mode. Format is the same as that used by chmod(1). Use without an arg to set back to default permissions (mode of the file while being packed).

This must be a numeric mode, like 644, 4755, or 600. It cannot be a relative mode like u+s.

8.6.11.4. @owner user

Set default ownership for all subsequent files to user. Use without an argument to set back to default ownership (root).

8.6.11.5. @group group

Set default group ownership for all subsequent files to group. Use without an arg to set back to default group ownership (wheel).

8.6.11.6. @comment string

This line is ignored when packing.

8.6.11.7. @dir directory

Declare directory name. By default, directories created under PREFIX by a package installation are automatically removed. Use this when an empty directory under PREFIX needs to be created, or when the directory needs to have non default owner, group, or mode. Directories outside of PREFIX need to be registered. For example, /var/db/${PORTNAME} needs to have a @dir entry whereas ${PREFIX}/shared/${PORTNAME} does not if it contains files or uses the default owner, group, and mode.

8.6.11.8. @exec command, @unexec command (Deprecated)

Execute command as part of the installation or deinstallation process. Please use @preexec command, @postexec command, @preunexec command, @postunexec command instead.

8.6.11.9. @dirrm directory (Deprecated)

Declare directory name to be deleted at deinstall time. By default, directories created under PREFIX by a package installation are deleted when the package is deinstalled.

8.6.11.10. @dirrmtry directory (Deprecated)

Declare directory name to be removed, as for @dirrm, but does not issue a warning if the directory cannot be removed.

8.6.12. Creating New Keywords

Package list files can be extended by keywords that are defined in the ${PORTSDIR}/Keywords directory. The settings for each keyword are stored in a UCL file named keyword.ucl. The file must contain at least one of these sections:

  • attributes

  • action

  • pre-install

  • post-install

  • pre-deinstall

  • post-deinstall

  • pre-upgrade

  • post-upgrade

8.6.12.1. attributes

Changes the owner, group, or mode used by the keyword. Contains an associative array where the possible keys are owner, group, and mode. The values are, respectively, a user name, a group name, and a file mode. For example:

attributes: { owner: "games", group: "games", mode: 0555 }

8.6.12.2. action

Defines what happens to the keyword’s parameter. Contains an array where the possible values are:

setprefix

Set the prefix for the next plist entries.

dir

Register a directory to be created on install and removed on deinstall.

dirrm

Register a directory to be deleted on deinstall. Deprecated.

dirrmtry

Register a directory to try and deleted on deinstall. Deprecated.

file

Register a file.

setmode

Set the mode for the next plist entries.

setowner

Set the owner for the next plist entries.

setgroup

Set the group for the next plist entries.

comment

Does not do anything, equivalent to not entering an action section.

ignore_next

Ignore the next entry in the plist.

8.6.12.3. arguments

If set to true, adds argument handling, splitting the whole line, %@, into numbered arguments, %1, %2, and so on. For example, for this line:

@foo some.content other.content

%1 and %2 will contain:

some.content
other.content

It also affects how the action entry works. When there is more than one argument, the argument number must be specified. For example:

actions: [file(1)]

8.6.12.4. pre-install, post-install, pre-deinstall, post-deinstall, pre-upgrade, post-upgrade

These keywords contains a sh(1) script to be executed before or after installation, deinstallation, or upgrade of the package. In addition to the usual @exec %foo placeholders described in @preexec command, @postexec command, @preunexec command, @postunexec command, there is a new one, %@, which represents the argument of the keyword.

8.6.12.5. Custom Keyword Examples

Example 2. Example of a @dirrmtryecho Keyword

This keyword does two things, it adds a @dirrmtry directory line to the packing list, and echoes the fact that the directory is removed when deinstalling the package.

actions: [dirrmtry]
post-deinstall: <<EOD
  echo "Directory %D/%@ removed."
EOD
Example 3. Real Life Example, How @sample is Implemented

This keyword does three things. It adds the first filename passed as an argument to @sample to the packing list, it adds to the post-install script instructions to copy the sample to the actual configuration file if it does not already exist, and it adds to the post-deinstall instructions to remove the configuration file if it has not been modified.

actions: [file(1)]
arguments: true
post-install: <<EOD
  case "%1" in
  /*) sample_file="%1" ;;
  *) sample_file="%D/%1" ;;
  esac
  target_file="${sample_file%.sample}"
  set -- %@
  if [ $# -eq 2 ]; then
      target_file=${2}
  fi
  case "${target_file}" in
  /*) target_file="${target_file}" ;;
  *) target_file="%D/${target_file}" ;;
  esac
  if ! [ -f "${target_file}" ]; then
    /bin/cp -p "${sample_file}" "${target_file}" && \
      /bin/chmod u+w "${target_file}"
  fi
EOD
pre-deinstall: <<EOD
  case "%1" in
  /*) sample_file="%1" ;;
  *) sample_file="%D/%1" ;;
  esac
  target_file="${sample_file%.sample}"
  set -- %@
  if [ $# -eq 2 ]; then
      set -- %@
      target_file=${2}
  fi
  case "${target_file}" in
  /*) target_file="${target_file}" ;;
  *) target_file="%D/${target_file}" ;;
  esac
  if cmp -s "${target_file}" "${sample_file}"; then
    rm -f "${target_file}"
  else
    echo "You may need to manually remove ${target_file} if it is no longer needed."
  fi
EOD

Last modified on: September 7, 2022 by Stefan Eßer