Using pkgsrc on MacOS
Recently, I have been wanting to get back into using a more traditional package manager on my macOS machine. For the past couple of years, I have mostly been using brew. But its time for a bit of a change. I've used mac ports before and I do enjoy it, but pkgsrc has been calling out to me. I have also been feeling the itch to be getting back into the BSDs. Unfortunatley, I've been busy chasing other projects. So to dip my toes back in, I am going to get back into pkgsrc and maybe try to port a few packages over.
Most of my packaging experience has been on Ravenports, as mentioned in a few earlier posts. I have also packages comtrya and one other package for homebrew. So I know a little bit about packaging. However, before going down that road, I need to get pkgrc set up.
So this post is for anyone on mac interested in using pkgsrc and pkgin as their package manager. But more importantly, for me as a future reference for when I need to set this up again.
A little intro on pkgsrc
First, I should probably introduce it to people who are not me. Pkgsrc is a ports tree that is primarily used by the NetBSD folks. It comes out of NetBSD. However, in true NetBSD fashion, it is meant to be portable. You can use pkgsrc for pretty much any unix based system. So it works on Linux, Solaris, FreeBSD, and yes, macOS (as it is a unixed based system).
A ports tree is a collection of recipes pretty much. These recipies are organizaed and have the build and install instructions for all the software it contains. In the BSDs, these are mainly through Makefile and various other tools.
Pkgsrc provides the tree, or collection. All of these software packages are built from source. However, there is also a nifty tool called pkgin, which can handle pulling packages from a repository that are already built. It is essentially brew, or apt, or dnf, or pacman. Saving you the hassle of the compile time and system resources to build the packages.
Getting the source
I decided to use CVS. If I do end up contributing, I want to use this as a time to learn an older source control version and the primary one that the NetBSD project uses.
env CVS_RSH=ssh cvs -d anoncvs@anoncvs.NetBSD.org:/cvsroot checkout -P pkgsrc
This will essentially checkout the sources to your machine, using the anonymous account.
Bootstrapping
Now that the sources are gotten with CVS, I need to bootstrap. The bootstrapping process is going to set up the machine in a way that pkgin and pkgsrc finds suitable. This is mimicking the layout that NetBSD uses for user installed packages. However, I am going to change a few things.
sudo ./bootstrap \
--prefix=/opt/pkg \
--pkgdbdir=/opt/pkg/pkgdb \
--varbase=/opt/pkg/var \
The bootstrap is in pkgsrc/bootstrap and is a shell script to execute. It will build a few things, but mostly it sets up a folder structure that pkgsrc and pkgin can work with.
I also override the prefix and location where things install because where it expects to install, isn't exactly ideal for macOS. If I recall, it will try to install in /usr which is a protected directory on macOS that should only be touched by the system. This is a change that occured for when macOS changed to do a more immutable OS style layout many moons ago now.
So I choose to do /opt since that is where spellbook I had install and also where homebrew installs because this is not a strictly managed directory. From there, the other overrides should be pretty self explanatory after the prefix.
The bootstrap will also compile, build, and install a few packages. These will go in /opt/pkg/bin and /opt/pkg/sbin.
Setting the environment
After running the bootstrap, will need to change some environment variables in ~/.zshrc. This will allow binaries to be found in the terminal shell. Also, might be nice to also have access to the man pages for things installed with pkgsrc/pkgin.
export PKG_BIN_PATH="/opt/pkg/bin:/opt/pkg/sbin"
export PKG_MAN_PATH="/opt/pkg/man"
export PATH="$PKG_BIN_PATH:$PATH"
export MANPATH="$PKG_MAN_PATH:$MANPATH"
After reseting the shell, everything should be good to go from here.
Although it is always good to check.
~/ whereis bmake
bmake: /opt/pkg/bin/bmake /opt/pkg/man/man1/bmake.1
That is what we should expect. Using whereis to tell us the location of bmake should result in a binary and man page in the /opt/pkg/bin and /opt/pkg/man` directories.
One note, this bmake is the NetBSD's variant of make that pkgsrc expects to be used.
Building the first package
The next step is to actually build a package. So, navigating to where I pulled the sources for pkg, there is a handy shell script in there that will help me locate where in the hierarchy of files that package is that I want.
~/sources/pkgsrc/ [pkgsrc] ./pkglocate pkgin
pkgtools/pkgin/DESCR:pkgin is aimed at being an apt / yum like tool for managing
pkglocate will locate where the given package (in this case it is pkgin) lives in the file hierarchy. Pkgin, mentioned above, is the binary package installer for pkgsrc that is recommended. There is technically also pkg_add and a few others, but the recommendation is to usually use pkgin. pkgin is the "high level" package manager, while pkg_add is the low level. One also important mentions, pkgin uses pkg_add.
So, now go to that location and run the following:
sudo bmake install
Everything should install and now I should be able to invoke the pkgin program and see the following.
~/sources/pkgsrc/pkgtools/pkgin/ [pkgtools/pkgin] pkgin
Usage: pkgin [-46cdfhlnPtvVy] command [package ...]
Commands and shortcuts:
list (ls ) - List installed local packages
avail (av ) - List all available remote packages
search (se ) - Search for a remote package
install (in ) - Install or upgrade packages
update (up ) - Refresh local and remote package lists
upgrade (ug ) - Upgrade all packages
full-upgrade (fug ) - Upgrade all packages (deprecated)
remove (rm ) - Remove packages and any dependent packages
keep (ke ) - Mark packages that should be kept
unkeep (uk ) - Mark packages that can be autoremoved
export (ex ) - Display PKGPATH for all keep packages
import (im ) - Import keep package list from file
show-keep (sk ) - Display keep packages
show-no-keep (snk ) - Display autoremovable packages
autoremove (ar ) - Remove orphaned dependencies
clean (cl ) - Remove downloaded package files
show-deps (sd ) - List remote package direct dependencies
show-full-deps (sfd ) - List remote package full dependencies
show-rev-deps (srd ) - List local package reverse dependencies
provides (prov) - Show which shared libraries a package provides
requires (req ) - Show which shared libraries a package requires
show-category (sc ) - List all packages belonging to a category
show-pkg-category (spc ) - Show categories a package belongs to
show-all-categories (sac ) - List all known categories
pkg-content (pc ) - Show remote package content
pkg-descr (pd ) - Show remote package long-description
pkg-build-defs (pbd ) - Show remote package build definitions
stats (st ) - Show local and remote package statistics
Looks like everything is good to go!
Not so fast though, need to set the repository that pkgin is going to use. This is set in /opt/pkg/etc/pkgin/repositories.conf