Manually registering dependencies for ports

b. f. bf1783 at
Mon Jun 7 01:30:42 UTC 2010

>I've been wondering about something: When I write a script or webapp that
>needs some port to run, like a perl module, I install the needed port and
>life is good (tm). A year later when I've completely forgotten about the
>script I go do some spring-cleaning of the ports on the server, and I see
>some perl module that doesn't have any dependencies, and delete it. Fast
>forward a few days when I discover the script doesn't work anymore, cue
>face-palm, remove bullet from foot, etc.
>Is there some way I can register a dependency to prevent this ? Like
>adding a flag to an installed port to say "something outside of the ports
>system depends on this" along with a user specified comment string. A
>system like that could result in something like this:
>pkg_delete -x p5-something
>pkg_delete: p5-something cant be uninstalled because: " uses
>this module, for the love of everything good do not delete it"

Yeah, there is.  If you don't want to make a local port for your
script/program with the module listed as a dependency, then don't muck
about +REQUIRED_BY and friends -- that's a pain.  Instead, you can use
a "package requirements procedure" -- a script or program that is
executed before installation/deinstallation, and if it returns
non-zero exit status, the installation/deinstallation aborts.  You can
read about it in pkg_create(1) (look at the -r flag), and
pkg_delete(1), of course.  For example, say you have a script called
/home/foo that requires a port /usr/ports/devel/bar.  Add something
like the following script as /usr/ports/devel/bar/pkg-req:


if [ "x$1" = "x" ]; then
        exit 1;

if [ "x$2" = "xINSTALL" ]; then
	exit 0;
elif [ "x$2" = "xDEINSTALL" ]; then
	if [ -f /home/foo ] ; then
		echo "/home/foo requires this package; aborting deinstallation; use
-f to override"
		exit 1
		exit 0
	exit 1

When you rebuild and reinstall devel/bar, this script will be added as
/var/db/pkg/bar-1.0/+REQUIRE, for example.  You can add it manually if
bar-1.0 is already installed, and it will take effect when pkg_delete
acts on bar-1.0, even without rebuilding.
Obviously, you can make this more elaborate: it could examine a list
of dependent programs contained in a file that you can edit, and check
for each one of them in your PATH, or in specified places.  You could
edit this list at any time, and the +REQUIRE script would still work.
You can keep the script in a location other than
/usr/ports/devel/bar/pkg-req, just by using something like:

PKGREQ="insert full path to script here"

in /etc/make.conf, because /usr/ports/Mk/ will use PKGREQ
along with the -r flag in the "do-package" target.

You are always free to tie a string around your finger.  Just don't
tie it too tightly.


More information about the freebsd-ports mailing list