Title: Reuse of OpenBSD packages for trying runtime
Author: Solène
Date: 19 September 2021
Tags: openbsd unix
Description: 

# Introduction

So, I'm currently playing with OpenBSD trying each end user package
(providing binaries) and see if they work when installed alone.  I
needed a simple way to keep packages downloaded and I didn't want to go
the hard way by using rsync on a package mirror because it would waste
too much bandwidth and would take too much time.

The most efficient way I found rely on a cache and ordering the source
of packages.

# pkg_add mastery

pkg_add has a special variable named PKG_CACHE that when it's set,
downloaded packages are copied in this directory.  This is handy
because every time I will install a package, all the packages
downloaded by will kept in that directory.

The other variable that interests us for the job is PKG_PATH because we
want pkg_add to first look up in $PKG_CACHE and if not found, in the
usual mirror.

I've set this in my /root/.profile

```shell file
export PKG_CACHE=/home/packages/
export PKG_PATH=${PKG_CACHE}:http://ftp.fr.openbsd.org/pub/OpenBSD/snapshots/packages/amd64/
```

Every time pkg_add will have to get a package, it will first look in
the cache, if not there it will download it in the mirror and then
store it in the cache.

# Saving time removing packages

Because I try packages one by one, installing and removing dependencies
takes a lot of time (I'm using old hardware for the job).  Instead of
installing a package, deleting it and removing its dependencies, it's
easier to work with manually installed packages and once done, remove
dependencies, this way you will keep already installed dependencies
that will be required for the next package.

```script shell
#!/bin/sh

# prepare the packages passed as parameter as a regex for grep
KEEP=$(echo $* | awk '{ gsub(" ","|",$0); printf("(%s)", $0) }')

# iterate among the manually installed packages
# but skip the packages passed as parameter
for pkg in $(pkg_info -mz | grep -vE "$KEEP")
do
        # instead of deleting the package
        # mark it installed automatically
        pkg_add -aa $pkg
done

# install the packages given as parameter
pkg_add $*

# remove packages not required anymore
pkg_delete -a
```

This way, I can use this script (named add.sh) "./add.sh gnome" and
then reuse it with "./add.sh xfce", the common dependencies between
gnome and xfce packages won't be removed and reinstalled, they will be
kept in place.

# Conclusion

There are always tricks to make bandwidth and storage more efficient,
it's not complicated and it's always a good opportunity to understand
simple mechanisms available in our daily tools.