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:
- ports.su (source) - does not support search, redirects to Google
- openbsd.app (source) - I can't get visibility into the categories
- openports.pl (source) - created by Marc Espie, who completely rewrote pkg_add(1).
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:
- Static generator - reads the ports(7) tree and generates the whole HTML site.
- CGI - when the
QUERY_STRINGvariable 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:
=assignment?=conditional assignment (only if not already set)+=append
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:
:R- root (removes extension):L- lowercase:U- uppercase:S/old/new/- substitution
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
- Built-in CGI - search without needing anything besides the binary itself and an
INDEX.txt. - No SQLite - parses Makefiles directly or uses a dump file.
- Fast - generates the whole site in ~0.4s on my machine.
- Single binary - to build it you only need rustc installed, no cargo, no dependencies.
- pledge /
unveil - in CGI mode it does
unveilonly onINDEX.txtwith read permission andpledgewithstdio rpath. In generation mode it allows reading the ports(7) tree and writing to the output directory. - No JavaScript - plain HTML with inline CSS, works in any browser.
- Dark mode - supports
prefers-color-scheme. - Embedded assets - favicon and logo compiled into the binary with
include_bytes!.
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:
- Uses a lock file at
/tmp/wop-update.lockto avoid concurrent runs. - Runs
git pull --ff-onlyon the ports(7) tree. - Regenerates the site with WOP.
- Injects the search form into
index.htmlviased. - Copies
INDEX.txtto/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.