nanoBSD: cust_pkgng() fetches ports-mgmt/pkg regardless of required package

From: Michael <embhd_at_posteo.de>
Date: Thu, 09 Feb 2023 19:30:41 UTC
Good evening,

I noticed that in cust_pkng(), pkg(7) (/usr/sbin/pkg) fetches pkg(8) 
(ports-mgmt/pkg) from remote even though the function itself requires a 
binary package of pkg(8) for bootstrapping. This also seems to be the 
reason why _.cust.cust_pkng notes that "the most recent version of 
pkg-X.Y.Z is already installed" - first, pkg(7) fetches and builds 
pkg(8), which then tries to install pkg(8) once more.
At the same time, I would guess that if the nanoBSD-image is for an 
entirely different plattform/architecture, the just bootstrapped pkg(8) 
would not be able to run during later operation, since I don't think it 
has been compiled for the later host system.

My current solution is to change the command set in ${PKGCMD} to use the 
build system's pkg(8), which chroots itself to ${NANO_WORLDDIR}. This 
also makes the separate calls to chroot(8) by means of CR() and CR0() 
unnecessary. Since the latter does not seem to be used anywhere else and 
the former only once in clean_build(), they can be removed altogether if 
the remaining call to CR() in clean_build() is replaced with a direct 
call to chroot(8).

I suppose this is also somewhat related to 
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=260467, since the 
corresponding line in cust_pkgng() is `CR "${PKGCMD} add 
/_.p/${_NANO_PKG_PACKAGE}"`. With the solution above, pkg(7) would never 
be invoked in the first place and the bug wouldn't pop up, at least in 
the default function.

Am I missing anything?

Regards,
Michael

PS: I originally posted this (minus some re-wording) to freebsd-embedded 
about two weeks ago, but never received any response.