WOP: Where OpenBSD Ports

Nowadays when I work with a new program at my job, I use the weekends to check whether it exists in OpenBSD ports(7) so I can work from my machine when needed or port it to OpenBSD.

When I have a machine nearby it is easy to search ports(7), I can for example run the command:

cd /usr/ports
make search name=curl

For the command above to work you need to install sqlports, which is nothing more than a dump of ports(7) in SQLite.

The problem is when I only have my phone. In that case I use web options to search ports(7) such as:

All three are excellent projects, but I wanted to know whether I could get close to the result of these websites without having to install a database, without having to run make dump-vars on ports(7) since on my machine it "takes a good few minutes", and simply be able to search up-to-date ports(7) from the snapshot branch.

That is why I created wop.

About WOP

WOP is a static HTML site generator for browsing OpenBSD ports(7). A single Rust binary that currently operates in two modes:

  1. Static generator - reads the ports(7) tree and generates the whole HTML site.
  2. CGI - when the QUERY_STRING variable is set, it acts as a CGI program for search.

No database needed, no web framework needed, no JavaScript. It works in lynx or any terminal browser.

Processing in a nutshell

WOP has two ways of loading ports(7) data:

Parsing the Makefiles directly

The walk_ports function walks the /usr/ports directory looking for category/port/Makefile. It skips directories like infrastructure, tests and .git.

For each Makefile the parse_makefile function extracts the variables, handling the assignment operators of make:

It also handles line continuation with \.

This was the most annoying part =/

After parsing, WOP expands the variables with expand_vars. This function resolves references like ${DISTNAME} recursively with a limit of 8 iterations to avoid infinite loops. It supports make modifiers such as:

It also resolves MODULES by loading .port.mk files from the ports(7) infrastructure to fill in module variables (e.g. lang/python).

Via dump file

Alternatively you can pass a dump file with -d in the format:

category/port.VARIABLE=value

This option is useful if you already have a generated dump.

Challenges

The biggest challenge was implementing Makefile variable expansion. The OpenBSD ports(7) system uses chained variables, modifiers and modules that inject new variables. Reimplementing a subset of make(1) good enough to extract COMMENT, HOMEPAGE, MAINTAINER and dependencies of each port took several iterations.

Another point was correctly handling the dependency format, which can appear as category/port, pkg:category/port, category/port,subpkg or category/port>=version.

Differences

Some of the links mentioned show a count of ~10578 packages while WOP has ~8K. This difference happens because sqlports generates all flavors of OpenBSD, and for my use case that is not needed since I only need to know whether the package exists or not.

Features

CGI with httpd

WOP works as CGI through OpenBSD slowcgi. The configuration in httpd.conf is simple:

server "ports.example.com" {
    listen on * port 80
    root "/htdocs/ports"
    location "/search.cgi" {
        fastcgi socket "/run/slowcgi.sock"
        root "/"
    }
}

You need to copy the shared libraries into the httpd chroot:

mkdir -p /var/www/usr/lib /var/www/usr/libexec
cp /usr/lib/libc.so.* /var/www/usr/lib/
cp /usr/lib/libm.so.* /var/www/usr/lib/
cp /usr/lib/libpthread.so.* /var/www/usr/lib/
cp /usr/lib/libc++abi.so.* /var/www/usr/lib/
cp /usr/libexec/ld.so /var/www/usr/libexec/

Update script

The update.sh script automates updating the site:

  1. Uses a lock file at /tmp/wop-update.lock to avoid concurrent runs.
  2. Runs git pull --ff-only on the ports(7) tree.
  3. Regenerates the site with WOP.
  4. Injects the search form into index.html via sed.
  5. Copies INDEX.txt to /var/www/ with doas.

Just put it in cron to keep the site updated automatically.

Result

The site is available at ports.ijanc.org.

No JavaScript, you can search via lynx or any terminal browser.