pkgsrc is NetBSD's ports system. But it's also cross-platform, available on Mac and Linux. It also support unprivileged use. This is a quick reference for it.

The document is awesome

First let's make a few decisions about the installation locations:

1
2
3
4
5
# Location of the pkgsrc
PKGSRC_DIR= /data/user/<u>/pkgsrc

# Location of the built package installation site
PKG_DIR= /data/user/<u>/pkg

BOOTSTRAPPING

First download pkgsrc - it's a collection of configurations instructing how to download and build different packages.

1
2
wget https://ftp.netbsd.org/pub/pkgsrc/stable/pkgsrc.tar.gz 
tar xf pkgsrc.tar.gz -C $PKGSRC_DIR

pkgsrc needs to be bootstrapped on the target machine where packages are to be built. The bootstrapping process builds the necessary tools pkgsrc will be using later on. We will focus on unprivileged use, since that's more fun and less covered by documents everywhere else:

1
2
3
cd $PKGSRC_DIR/bootstrap
./bootstrap --help
./bootstrap --unprivileged --prefix $PKGSRC_DIR --make-jobs <num_cpus>

Finally we need to export PATH and MANPATH to include $PKG_DIR.

PARALLEL BUILD

1
MAKE_JOBS=<num_cpus> bmake

USEFUL TARGETS, GETTING HELP

1
2
3
4
5
6
7
8
9
bmake help
bmake help topic=:index # prints all helps
bmake help topic=<target|option>

bmake show-all
bmake show-depends # seems only incl. direct deps
bmake show-depends-dirs # include transient deps

bmake show-var VARNAME=SOME_VAR_NAME

See pkgsrc targets The following targets may be useful to invoke from keyboard:

  • depends to build and install dependencies
  • fetch to fetch distribution file(s)
  • checksum to fetch and check distribution file(s)
  • extract to look at unmodified source
  • patch to look at initial source
  • configure to stop after configure stage
  • all or build to stop after build stage
  • stage-install to install under stage directory
  • test to run package's self-tests, if any exist and supported
  • package to create binary package before installing it
  • replace to change (upgrade, downgrade, or just replace) installed package in-place
  • deinstall to deinstall previous package
  • package-install to install package and build binary package
  • install to install package
  • bin-install to attempt to skip building from source and use pre-built binary package
  • show-depends print dependencies for building
  • show-options print available options from options.mk

Cleanup targets (in separate section because of importance):

  • clean-depends to remove work directories for dependencies
  • clean to remove work directory
  • distclean to remove distribution file(s)
  • package-clean to remove binary package

The following targets are useful in development and thus may be useful for an advanced user:

  • makesum to fetch and generate checksum for distributed file(s)
  • makepatchsum to (re)generate checksum for patches
  • makedistinfo to (re)generate distinfo file (creating checksums for distributed file and patches)
  • mps short for makepatchsum
  • mdi short for makedistinfo
  • print-PLIST to attempt to generate correct packaging list (NB! It helps, but it doesn't eliminate manual work.)

USEFUL PKG TOOLS

1
2
3
4
(cd pkgtools && bmake install)

man pkg_info
pkg_info -L <pkg_name> # list package contents

DISTFILES, FETCHING ETC

1
2
3
4
5
# generate sh script which downloads distfiles when run
bmake fetch-list

bmake checksum # fetch distfile and do checksum
bmake depends-checksum # also for deps

BUILD OPTIONS

$PKG_DIR/etc/mk.conf is where to set build options. For specific package, add line:

1
PKG_OPTIONS.<pkg_name>+=       <option>

bmake show-all is helpful in discovering available build options, and what are enabled/disabled.

USEFUL ENVIRONMENT VARS

These can be specified in mk.conf, or on each build instance:

1
DISTDIR=... bmake
  • DISTDIR: directory where to look for distfiles
  • FETCH_USING: the program for fetching (can set to wget)

TROUBLESHOOTING

1
2
# Prints detailed build command
PKG_DEBUG_LEVEL=1 bmake

BUILD PHASES

https://www.netbsd.org/docs/pkgsrc/build.html

  • The fetch phase
  • The checksum phase
  • The extract phase
  • The patch phase
  • The tools phase
  • The wrapper phase
  • The configure phase
  • The build phase
  • The test phase
  • The install phase
  • The package phase

Use pkgsrc along side Joyent pkgsrc binary distribution

Sat May 28 14:58:51 PDT 2022

  1. Install Joyent's pkgsrc binary distribution (this must be installed at the pre-determined location. E.g., for MacOS, at /opt/pkg)
  2. At any location, git clone pkgsrc source repo (e.g., clone into ~/work/pkgsrc)
  3. To build from source, just cd ~/work/pkgsrc/net/sshping && bmake install
    • bmake is from /opt/pkg/bin. Installed package will also go there.

Since this installs to system location, there maybe frequent prompt for root password. Use this patch can fix it. Note that it assumes you have sudo already installed at /usr/bin/sudo. Otherwise, follow the pkgsrc FAQ (use pkgsrc to install sudo first):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
--- /tmp/mk.conf	2022-05-28 15:03:49.000000000 -0700
+++ /opt/pkg/etc/mk.conf 2022-05-28 11:48:43.000000000 -0700
@@ -61,6 +61,12 @@

MAKE_JOBS= 6

+# Prevent bmake keep asking for root password
+# https://www.netbsd.org/docs/pkgsrc/faq.html
+.if exists(/usr/bin/sudo)
+SU_CMD= /usr/bin/sudo /bin/sh -c
+.endif
+
#
# All local changes should go into this file, as mk.conf could
# be replaced on upgrades.

Rebuild only changed files

There's the $WRKDIR/.build_done cookie that skips build. So:

1
2
3
4
5
6
bmake rebuild
# or...
bmake build-clean && bmake
# or...
rm "$(bmake show-var VARNAME=WRKDIR)"/.build_done
bmake build