Package Source (pkgsrc): An Introduction

Package source, AKA pkgsrc, is an interesting project that is developed primarily by the NetBSD team. But, why would you even use it, and honestly, what even is it?

Package Source (pkgsrc): An Introduction
Photo by Fotis Fotopoulos / Unsplash

(Editor's Note: If you are unfamiliar with NetBSD (or any of the BSDs),  this article was written to give you a gentle introduction, and we recommend reading that one first.)

Package source is an interesting project that is developed primarily by the NetBSD team. It is based on a ports tree system. If you are a Linux user, then know that Gentoo is based on the FreeBSD ports tree system. For the rest of you who are not Linux users or not familiar with FreeBSD or Gentoo, a ports tree is a collection of makefiles. However, they are a little bit more than just makefiles (or in Gentoo, ebuild scripts). The Makefiles not only will specify how to compile the software but also where to download the software and list dependencies, and they often are not the greatest at dependency resolution. The central idea is that you have a directory in a tree structure. In that directory, there are folders for categories of software like game/ or x11/ or editors/. In those directories are directories for software. Inside of a directory for a specific piece of software is a Makefile. That makefile will download the source and compile, along with call all dependencies.

There are many examples of ports trees, however, package source (pkgsrc) is interesting in that it falls in line with the overall NetBSD project goal of being portable. Not only is NetBSD itself portable, but so is the main package management system (pkgsrc). pkgsrc can be bootstrapped on most Unix or Unix-like systems, such as FreeBSD, Linux, macOS, Solaris, and more. One could say it was aiming to solve the universal package management before snaps, flatpaks, and appimages.

The documentation of pkgsrc is fairly easy to follow and works well across systems. I myself use it in place of brew or Macports on my macOS partition and am currently testing its usefulness on FreeBSD for a project. As I write this, I have done a bulk build of 10290 packages on FreeBSD and am continuing to build more.

pkgsrc comprises of just under 22,000 packages and contains most of the common tools on most Unix systems. While it is not perfect, it works pretty well as its branches across multiple binary-incompatible UNIX and UNIX-like operating systems.


The question is, why would someone use this? First, if someone is a NetBSD user, this is the primary source of package management. But, why others? A project I am working on is looking to work across multiple systems using multiple different UNIX and UNIX-like operating systems. While it can be done using each operating system's own package management scheme, keeping packages consistent can be more difficult. While Fedora and Debian may ship a very similar stack of software, they are likely to be at different major and minor versions. The same could be said if maintaining an infrastructure that consists of FreeBSD servers and Linux servers. And lastly, a quest for one tool to simplify across systems if one uses multiple different systems.

Next is flexibility. Learning how to make files work, changing the configuration of the software is easier. Suppose a server needs to run Apache, but we do not need for some reason, Apache support for OpenSSL built-in, this option can be removed from the compile and linking options in the Makefile allowing for a more customized build. A great example would be compiling libraries and they both can be compiled with GTK and/or QT support. Suppose the target system(s) don't need either as in a server or only need GTK support? This can be adjusted. While we do spend time compiling and building packages, we can achieve a more curated selection of software. Users of Gentoo will be familiar with this in the usage of USE flags. Sometimes default compiled software are fine, such as the ones included in most pre-compiled repositories on Linux distributions, pkg from FreeBSD, or pkgin from NetBSD, but they may pull in unneeded dependencies. Think of the earlier example. If we are going to be using GNOME, do we really need to pull down KDE and QT software to satisfy the generic requirement in a generically built package? This gives great power to someone managing infrastructure to cut down on un-needed bits. However, this is not exclusive to pkgsrc. Gentoo's portage and any of the BSD port tree systems are capable of this flexibility.

In a follow-up article, I intend to cover setting up pkgsrc on NetBSD and then potentially another article of bootstrapping it onto either a Linux or FreeBSD system.

Useful Links:
https://www.pkgsrc.org/
https://www.netbsd.org/docs/software/packages.html
https://www.pkgsrc.se/
https://pkgsrc.joyent.com/