git: 6871285f194d - main - sysconf: Add ucl and pkg targets

From: Faraz Vahedi <kfv_at_FreeBSD.org>
Date: Sun, 15 Nov 2026 11:02:38 UTC
The branch main has been updated by kfv:

URL: https://cgit.FreeBSD.org/src/commit/?id=6871285f194d3f83fd270b4b5d5783503c2b86d6

commit 6871285f194d3f83fd270b4b5d5783503c2b86d6
Author:     Faraz Vahedi <kfv@FreeBSD.org>
AuthorDate: 2026-11-15 10:53:54 +0000
Commit:     Faraz Vahedi <kfv@FreeBSD.org>
CommitDate: 2026-11-15 10:53:54 +0000

    sysconf: Add ucl and pkg targets
    
    ucl reads and rewrites Universal Configuration Language files with
    libucl(3), the private library pkg(8) links, so what sysconf reads is
    what pkg reads. Each file is parsed on its own and every scalar is
    addressed by its dotted path (FreeBSD-ports.enabled); the last file
    to define a path is authoritative. A write changes only the one
    statement, in the file's own separator, terminator, quoting, and
    boolean style, then re-parses the result with libucl and refuses the
    write unless the parsed contents differ in exactly the requested key.
    Removing the last key of an object removes the object.
    
    Arrays are leaves, read back as compact JSON. name+=word and
    name-=word on an array add and strike elements. Under an array a
    path component is an index, as uclcmd(1) spells it: name.i reaches
    one element (name.i=value sets it, name.i+=word edits it, -x name.i
    strikes it), name.i.j reaches into a nested array, and name.# is the
    count (name.#=value appends). A negative index counts from the end.
    Under an object the same component is a key, so a key named 404 or #
    or * is addressed as written. A key the library cannot take bare is
    written quoted; a file that leaves such a key bare, as uclcmd(1) may,
    is still read. An element is added or taken out on its own line in
    the style of its neighbours, a trailing comment going with a struck
    line as a sysrc(8) removal would take it.
    
    pkg is ucl over the files pkg(8) reads: /etc/pkg/*.conf,
    /usr/local/etc/pkg.conf, and /usr/local/etc/pkg/repos/*.conf. Files
    under /etc/pkg are read-only; a repository key found there is
    overridden in /usr/local/etc/pkg/repos/FreeBSD.conf, created in the
    base file's style, and a global key goes to pkg.conf. `pkg config`
    only reads, and `pkg repositories` enable/disable records a side file
    under the package database, so neither covers `sysconf pkg` ability.
    
    The pkg keyword makes `p' ambiguous with poudriere; pk and po select
    them.
    
    Co-authored-by: Devin Teske <dteske@FreeBSD.org>
    Reviewed by:    dteske
    Approved by:    dteske (mentor)
    Differential Revision:  https://reviews.freebsd.org/D61064
---
 usr.sbin/sysconf/Makefile              |  19 +-
 usr.sbin/sysconf/Makefile.depend       |   2 +
 usr.sbin/sysconf/sysconf-pkg.8         | 190 +++++++++++
 usr.sbin/sysconf/sysconf-poudriere.8   |  16 +-
 usr.sbin/sysconf/sysconf-targets.8     |   8 +-
 usr.sbin/sysconf/sysconf-targets.c     |   5 +-
 usr.sbin/sysconf/sysconf-ucl.8         | 249 +++++++++++++++
 usr.sbin/sysconf/sysconf.8             |  51 ++-
 usr.sbin/sysconf/sysconf.c             |  12 +
 usr.sbin/sysconf/sysconf_edit_assign.c |  12 +-
 usr.sbin/sysconf/sysconf_edit_write.c  |   4 +
 usr.sbin/sysconf/sysconf_pkg.c         | 178 +++++++++++
 usr.sbin/sysconf/sysconf_priv.h        |  50 ++-
 usr.sbin/sysconf/sysconf_resolve.c     |   9 +
 usr.sbin/sysconf/sysconf_scan.c        |  51 ++-
 usr.sbin/sysconf/sysconf_ucl.c         | 266 ++++++++++++++++
 usr.sbin/sysconf/sysconf_ucl.h         | 152 +++++++++
 usr.sbin/sysconf/sysconf_ucl_array.c   | 458 +++++++++++++++++++++++++++
 usr.sbin/sysconf/sysconf_ucl_file.c    | 316 +++++++++++++++++++
 usr.sbin/sysconf/sysconf_ucl_index.c   | 271 ++++++++++++++++
 usr.sbin/sysconf/sysconf_ucl_lex.c     | 338 ++++++++++++++++++++
 usr.sbin/sysconf/sysconf_ucl_list.c    | 150 +++++++++
 usr.sbin/sysconf/sysconf_ucl_style.c   | 414 ++++++++++++++++++++++++
 usr.sbin/sysconf/sysconf_ucl_text.c    | 447 ++++++++++++++++++++++++++
 usr.sbin/sysconf/sysconf_ucl_write.c   | 442 ++++++++++++++++++++++++++
 usr.sbin/sysconf/tests/sysconf_test.sh | 553 +++++++++++++++++++++++++++++++--
 26 files changed, 4614 insertions(+), 49 deletions(-)

diff --git a/usr.sbin/sysconf/Makefile b/usr.sbin/sysconf/Makefile
index cbe5440d6da7..214da07c433f 100644
--- a/usr.sbin/sysconf/Makefile
+++ b/usr.sbin/sysconf/Makefile
@@ -6,28 +6,41 @@ MAN=		sysconf.8			\
 		sysconf-generic.8		\
 		sysconf-loader.8		\
 		sysconf-make.8			\
+		sysconf-pkg.8			\
 		sysconf-poudriere.8		\
 		sysconf-rc.8			\
 		sysconf-src.8			\
 		sysconf-sysctl.8		\
-		sysconf-targets.8
+		sysconf-targets.8		\
+		sysconf-ucl.8
 
 SRCS=		sysconf.c			\
 		sysconf-targets.c		\
 		sysconf_edit_assign.c		\
 		sysconf_edit_make.c		\
 		sysconf_edit_write.c		\
+		sysconf_pkg.c			\
 		sysconf_poudriere.c		\
 		sysconf_print.c			\
 		sysconf_query_desc.c		\
 		sysconf_query_sysctl.c		\
 		sysconf_query_value.c		\
 		sysconf_resolve.c		\
-		sysconf_scan.c
+		sysconf_scan.c			\
+		sysconf_ucl.c			\
+		sysconf_ucl_array.c		\
+		sysconf_ucl_file.c		\
+		sysconf_ucl_index.c		\
+		sysconf_ucl_lex.c		\
+		sysconf_ucl_list.c		\
+		sysconf_ucl_style.c		\
+		sysconf_ucl_text.c		\
+		sysconf_ucl_write.c
 
 CFLAGS+=	-I${SRCTOP}/lib/libbsdconf -I${.CURDIR}
+CFLAGS+=	-I${SRCTOP}/contrib/libucl/include
 
-LIBADD=		bsdconf jail
+LIBADD=		bsdconf jail ucl
 
 HAS_TESTS=
 SUBDIR.${MK_TESTS}+=	tests
diff --git a/usr.sbin/sysconf/Makefile.depend b/usr.sbin/sysconf/Makefile.depend
index 1a3f452df3f2..32f7ab8b662f 100644
--- a/usr.sbin/sysconf/Makefile.depend
+++ b/usr.sbin/sysconf/Makefile.depend
@@ -8,6 +8,8 @@ DIRDEPS = \
 	lib/libc \
 	lib/libcompiler_rt \
 	lib/libjail \
+	lib/libucl \
+	lib/msun \
 
 
 .include <dirdeps.mk>
diff --git a/usr.sbin/sysconf/sysconf-pkg.8 b/usr.sbin/sysconf/sysconf-pkg.8
new file mode 100644
index 000000000000..c0c91fb5b00b
--- /dev/null
+++ b/usr.sbin/sysconf/sysconf-pkg.8
@@ -0,0 +1,190 @@
+.\" Copyright (c) 2013-2026 Devin Teske <dteske@FreeBSD.org>
+.\" Copyright (c) 2021-2026 Faraz Vahedi <kfv@FreeBSD.org>
+.\"
+.\" SPDX-License-Identifier: BSD-2-Clause
+.\"
+.Dd October 25, 2026
+.Dt SYSCONF-PKG 8
+.Os
+.Sh NAME
+.Nm "sysconf pkg"
+.Nd read and modify pkg.conf and repository files
+.Sh SYNOPSIS
+.Nm sysconf
+.Cm pkg
+.Op Fl ceFinNqvx
+.Op Fl j Ar jail | Fl R Ar dir
+.Op Fl f Ar file
+.Ar name Ns Op Ns = Ns Ar value
+.Ar ...
+.Nm sysconf
+.Cm pkg
+.Op Fl eFnNqvV
+.Op Fl j Ar jail | Fl R Ar dir
+.Op Fl f Ar file
+.Fl a
+.Nm sysconf
+.Cm pkg
+.Op Fl E
+.Op Fl j Ar jail | Fl R Ar dir
+.Fl l | L
+.Sh DESCRIPTION
+The
+.Cm pkg
+target of
+.Xr sysconf 8
+is the
+.Cm ucl
+target
+.Pq see Xr sysconf-ucl 8
+with the collection of files
+.Xr pkg 8
+reads by default.
+The syntax, the typing of values, and the rewrite are those of
+.Xr libucl 3 ,
+the library
+.Xr pkg 8
+itself links;
+this target adds only the paths and the rules below.
+.Pp
+.Xr pkg 8
+has no setter of its own for these files:
+.Xr pkg-config 8
+prints one merged value without saying which file supplied it, and
+.Xr pkg-repositories 8
+records an enable or disable override under the package database
+rather than editing a repository file.
+That override is
+.Xr pkg 8 Ns 's
+and is never written here.
+.Pp
+Files are consulted in this order:
+.Bl -enum -compact
+.It
+.Pa /etc/pkg/*.conf
+.It
+.Pa /usr/local/etc/pkg.conf
+.It
+.Pa /usr/local/etc/pkg/repos/*.conf
+.El
+.Pp
+A later assignment of the same key wins.
+A repository file is a set of repository tags, which is
+.Xr pkg.conf 5
+language for a top-level group:
+.Bd -literal -offset indent
+FreeBSD-ports: {
+  url: "pkg+https://pkg.FreeBSD.org/${ABI}/latest",
+  enabled: yes
+}
+.Ed
+.Pp
+The tag is the object name.
+Address the inner key as
+.Ql FreeBSD-ports.enabled
+.Pq and Ql FreeBSD-ports.url , and so on .
+Global options in
+.Pa pkg.conf
+have no dot
+.Pq Ql PKG_ENABLE_PLUGINS .
+Booleans read back as
+.Ql true
+or
+.Ql false
+whatever spelling the file used, and are written in the spelling the
+file already uses;
+.Ql ${ABI}
+is not expanded.
+.Pp
+Files under
+.Pa /etc/pkg
+are the base repository definitions shipped with the system.
+They are read, and
+.Fl F
+names them, but they are not modified.
+A write of a repository key whose current value comes from one of those
+files goes to
+.Pa /usr/local/etc/pkg/repos/FreeBSD.conf
+instead, which is what the comment in
+.Pa /etc/pkg/FreeBSD.conf
+asks for;
+the file is created if need be.
+A key already set in a file under
+.Pa /usr/local/etc/pkg/repos
+is rewritten in that file.
+A new global key is written to
+.Pa /usr/local/etc/pkg.conf .
+Removing a key deletes it from the local files only;
+a definition that exists solely under
+.Pa /etc/pkg
+is left in place and the removal reports it as unknown.
+A write changes only the statement concerned, in the file's own style;
+a repository override file created here takes the style of the base
+file it overrides, so the two read alike.
+See
+.Xr sysconf-ucl 8 .
+.Pp
+.Ev REPOS_DIR
+is not chased.
+The directories above are the ones
+.Xr pkg 8
+uses when that list is left at its default.
+Shared options are in
+.Xr sysconf 8 .
+Cross-file write rules are in
+.Xr sysconf-targets 8 .
+.Sh FILES
+.Bl -tag -width "/usr/local/etc/pkg/repos/FreeBSD.conf" -compact
+.It Pa /etc/pkg/*.conf
+Base repository definitions.
+Read-only to
+.Nm .
+.It Pa /usr/local/etc/pkg.conf
+Global
+.Xr pkg 8
+options.
+Default write path for a key with no dot.
+.It Pa /usr/local/etc/pkg/repos/*.conf
+Local repository overrides.
+.It Pa /usr/local/etc/pkg/repos/FreeBSD.conf
+Default write path for a repository key.
+.El
+.Sh EXAMPLES
+Read and disable the ports repository without editing the base file:
+.Pp
+.Dl sysconf pkg FreeBSD-ports.enabled
+.Dl sysconf pkg FreeBSD-ports.enabled=no
+.Pp
+The second command adds
+.Bd -literal -offset indent
+FreeBSD-ports: {
+  enabled: no
+}
+.Ed
+.Pp
+to
+.Pa /usr/local/etc/pkg/repos/FreeBSD.conf
+when that tag is not already overridden there;
+.Pa /etc/pkg/FreeBSD.conf
+is untouched.
+.Pp
+Show which file supplies the repository URL:
+.Pp
+.Dl sysconf pkg -F FreeBSD-ports.url
+.Sh SEE ALSO
+.Xr libucl 3 ,
+.Xr pkg.conf 5 ,
+.Xr pkg 8 ,
+.Xr pkg-config 8 ,
+.Xr pkg-repositories 8 ,
+.Xr sysconf 8 ,
+.Xr sysconf-targets 8 ,
+.Xr sysconf-ucl 8
+.Sh HISTORY
+The
+.Nm sysconf
+utility first appeared in
+.Fx 16.0 .
+.Sh AUTHORS
+.An Devin Teske Aq Mt dteske@FreeBSD.org
+.An Faraz Vahedi Aq Mt kfv@FreeBSD.org
diff --git a/usr.sbin/sysconf/sysconf-poudriere.8 b/usr.sbin/sysconf/sysconf-poudriere.8
index a5a61fea4421..55ed32d4db33 100644
--- a/usr.sbin/sysconf/sysconf-poudriere.8
+++ b/usr.sbin/sysconf/sysconf-poudriere.8
@@ -3,7 +3,7 @@
 .\"
 .\" SPDX-License-Identifier: BSD-2-Clause
 .\"
-.Dd September 23, 2026
+.Dd October 25, 2026
 .Dt SYSCONF-POUDRIERE 8
 .Os
 .Sh NAME
@@ -79,7 +79,11 @@ handled by the same engine as
 A unique prefix of the
 .Cm poudriere
 keyword is accepted
-.Pq Ql p .
+.Pq Ql po .
+.Ql p
+also matches
+.Cm pkg
+and is rejected as ambiguous.
 A unique prefix of a sub-target is accepted the same way
 .Po
 .Ql m
@@ -342,12 +346,12 @@ Overlay directory
 .Sh EXAMPLES
 .Dl sysconf poudriere ZPOOL=zroot
 .Dl sysconf poudriere --jail 131amd64 PARALLEL_JOBS=8
-.Dl sysconf p ZPOOL
-.Dl sysconf p -- src
+.Dl sysconf po ZPOOL
+.Dl sysconf po -- src
 .Dl sysconf poudriere make --jail 131amd64 CFLAGS
-.Dl sysconf p m --jail 131amd64 CFLAGS+=-O2
+.Dl sysconf po m --jail 131amd64 CFLAGS+=-O2
 .Dl sysconf poudriere src --set dev WITHOUT_LLVM_BINUTILS=
-.Dl sysconf p src-env --jail 131amd64 SRC_ENV_VAR
+.Dl sysconf po src-env --jail 131amd64 SRC_ENV_VAR
 .Sh SEE ALSO
 .Xr poudriere 8 ,
 .Xr sysconf 8 ,
diff --git a/usr.sbin/sysconf/sysconf-targets.8 b/usr.sbin/sysconf/sysconf-targets.8
index e87992852677..4b5e3e9d7f56 100644
--- a/usr.sbin/sysconf/sysconf-targets.8
+++ b/usr.sbin/sysconf/sysconf-targets.8
@@ -3,7 +3,7 @@
 .\"
 .\" SPDX-License-Identifier: BSD-2-Clause
 .\"
-.Dd September 22, 2026
+.Dd October 25, 2026
 .Dt SYSCONF-TARGETS 8
 .Os
 .Sh NAME
@@ -25,6 +25,8 @@ Per-target file lists and syntax quirks are in
 .Xr sysconf-src 8 ,
 .Xr sysconf-rc 8 ,
 .Xr sysconf-poudriere 8 ,
+.Xr sysconf-pkg 8 ,
+.Xr sysconf-ucl 8 ,
 and
 .Xr sysconf-generic 8 .
 Shared options remain in
@@ -161,10 +163,12 @@ but may differ when the value depends on conditionals or mk defaults.
 .Xr sysconf-generic 8 ,
 .Xr sysconf-loader 8 ,
 .Xr sysconf-make 8 ,
+.Xr sysconf-pkg 8 ,
 .Xr sysconf-poudriere 8 ,
 .Xr sysconf-rc 8 ,
 .Xr sysconf-src 8 ,
-.Xr sysconf-sysctl 8
+.Xr sysconf-sysctl 8 ,
+.Xr sysconf-ucl 8
 .Sh HISTORY
 The
 .Nm sysconf
diff --git a/usr.sbin/sysconf/sysconf-targets.c b/usr.sbin/sysconf/sysconf-targets.c
index 94b616db9b93..922ddc5bcdf2 100644
--- a/usr.sbin/sysconf/sysconf-targets.c
+++ b/usr.sbin/sysconf/sysconf-targets.c
@@ -15,16 +15,19 @@
 /*
  * Unified target keywords: libbsdconf formats plus pass-throughs. Exact
  * match wins; otherwise a unique prefix is accepted. Keep alphabetical so
- * ambiguous-match lists read naturally (e.g. `s' -> src, sysctl).
+ * ambiguous-match lists read naturally (e.g. `s' -> src, sysctl;
+ * `p' -> pkg, poudriere).
  */
 static const char *const target_keywords[] = {
 	"generic",
 	"loader",
 	"make",
+	"pkg",
 	"poudriere",
 	"rc",
 	"src",
 	"sysctl",
+	"ucl",
 	NULL
 };
 
diff --git a/usr.sbin/sysconf/sysconf-ucl.8 b/usr.sbin/sysconf/sysconf-ucl.8
new file mode 100644
index 000000000000..ac2288ea9102
--- /dev/null
+++ b/usr.sbin/sysconf/sysconf-ucl.8
@@ -0,0 +1,249 @@
+.\" Copyright (c) 2013-2026 Devin Teske <dteske@FreeBSD.org>
+.\" Copyright (c) 2021-2026 Faraz Vahedi <kfv@FreeBSD.org>
+.\"
+.\" SPDX-License-Identifier: BSD-2-Clause
+.\"
+.Dd October 25, 2026
+.Dt SYSCONF-UCL 8
+.Os
+.Sh NAME
+.Nm "sysconf ucl"
+.Nd read and modify a UCL file
+.Sh SYNOPSIS
+.Nm sysconf
+.Cm ucl
+.Fl f Ar file
+.Op Fl ceinNqvx
+.Op Fl j Ar jail | Fl R Ar dir
+.Ar name Ns Op Ns = Ns Ar value
+.Ar ...
+.Sh DESCRIPTION
+The
+.Cm ucl
+target of
+.Xr sysconf 8
+reads and rewrites files in the Universal Configuration Language,
+the syntax
+.Xr pkg.conf 5
+documents.
+The files are parsed and emitted by
+.Xr libucl 3 ,
+the same private library
+.Xr pkg 8
+links, so what this target reads is what
+.Xr pkg 8
+reads, including any defect the two then share.
+Like
+.Cm generic ,
+the target has no default files of its own and always requires
+.Fl f .
+The
+.Cm pkg
+target is this format with the
+.Xr pkg 8
+file collection bolted on;
+see
+.Xr sysconf-pkg 8 .
+.Pp
+Each file is parsed on its own, in order, and every scalar is reported
+under its dotted path:
+the key
+.Ql enabled
+inside the object
+.Ql FreeBSD-ports
+is
+.Ql FreeBSD-ports.enabled ,
+and objects nest as
+.Ql a.b.c .
+The last file to define a path is authoritative, as for every other
+target.
+Arrays are leaves and read back as compact JSON.
+On an array,
+.Ar name Ns += Ns Ar word
+and
+.Ar name Ns -= Ns Ar word
+add and strike elements
+.Pq each word parsed as a value, so Ql 3 is a number
+rather than words of a string;
+the survivors keep their order and newcomers follow the last of them,
+one per line when the array is laid out that way.
+On a string they are the word edits of
+.Xr sysconf 8 .
+.Pp
+Under an array a path component is an index rather than a key, as
+.Xr uclcmd 1
+spells it:
+.Ar name Ns \&. Ns Ar i
+is one element, counting from zero
+.Po or from the end when
+.Ar i
+is negative, so
+.Ql -1
+is the last
+.Pc ,
+and
+.Ar name Ns \&.#
+the number of elements
+.Pq reported under the name of the array counted .
+A negative index is reported as the index it resolved to.
+Under an object the same components are keys, so
+.Ql errors.404
+reads the key
+.Ql 404 .
+Indices chain into nested arrays:
+.Ar name Ns \&. Ns Ar i Ns \&. Ns Ar j
+is an element of the array that is element
+.Ar i ,
+and
+.Ar name Ns \&. Ns Ar i Ns \&.#
+its count.
+.Ar name Ns \&. Ns Ar i Ns = Ns Ar value
+changes that element where it stands, or appends when
+.Ar i
+is the count;
+.Ar name Ns \&.#= Ns Ar value
+appends likewise, and reports the index the new element took.
+.Ar name Ns \&. Ns Ar i Ns += Ns Ar word
+and
+.Ar name Ns \&. Ns Ar i Ns -= Ns Ar word
+edit the element as they would a value of its kind,
+words on a string and elements on an array;
+.Fl x Ar name Ns \&. Ns Ar i
+strikes it and, like
+.Ql -= ,
+reports the array before and after.
+Requests in one run see one another's edits, though a path into an
+array is only read as indices once the array exists in a file.
+An index past either end reads as an unknown directive, a value that is
+not an array cannot be indexed, and the count can be neither removed
+nor edited with
+.Ql +=
+or
+.Ql -= .
+A key that itself contains
+.Ql \&.
+cannot be addressed.
+A key the library cannot take bare
+.Po such as
+.Ql *
+or
+.Ql [1m
+.Pc
+is written quoted;
+a file that leaves such a key bare
+.Po as
+.Xr uclcmd 1
+may write
+.Ql *
+.Pc
+is still read.
+Values are shown as the library renders them:
+a boolean written
+.Ql yes
+reads back
+.Ql true ,
+and
+.Ql 1kb
+reads back
+.Ql 1024 .
+.Pp
+A value given on the command line is parsed as UCL,
+so
+.Ql yes ,
+.Ql 10 ,
+.Ql "[ a, b ]" ,
+and
+.Ql "{ k = v }"
+land with the types
+.Xr pkg 8
+would read.
+A value that does not parse to exactly the text given
+.Pq a Ql # the grammar would take for a comment, for instance
+is stored as that literal string, quoted; so is one holding a
+comment, a newline, or a
+.Ql \&.
+where a statement could begin, lest it carry a second statement or a
+macro into the file.
+Missing intermediate objects are created on a write;
+a write through a key that is not an object is an error.
+.Pp
+A write changes only the bytes it must.
+The statement is located in the file and its value replaced in place,
+in the file's own style:
+the separator
+.Pq Ql \&: or Ql = ,
+the terminator
+.Pq Ql \&, or Ql \&; ,
+quoting, indentation, and even the spelling of a boolean
+.Pq a Ql yes becomes Ql no , a Ql true becomes Ql false
+are taken from what is already there.
+A value is written as typed
+.Pq Ql 1kb stays Ql 1kb ;
+a string is quoted when the file quoted it or when it could not stand bare;
+a key that cannot stand bare is quoted even when its neighbours are not.
+A new key is added after its siblings in their style;
+a new object is appended in the style of the objects already in the file.
+An array that only gains or loses elements has those elements added or
+taken out one at a time, in the style of the elements around them;
+one that is reordered is replaced whole.
+Removing a key takes its own lines, a comment trailing it on the last of
+them included, and, when that leaves the enclosing object empty, the
+object too;
+striking an array element does the same with its line.
+A file that nothing changed is not rewritten;
+one that is, is replaced atomically with its mode kept,
+and its owner and group where the caller may set them.
+A symbolic link is followed:
+the file it names is replaced, and the link stays.
+A dangling symbolic link is left in place.
+Anything other than a regular file is refused.
+.Pp
+The library has the last word.
+After an edit the result is parsed again with
+.Xr libucl 3
+and the write is refused, leaving the file untouched,
+unless the parsed contents differ from the original in exactly the
+requested key.
+Macros such as
+.Ql .include
+are not expanded, so a parse never pulls in another file,
+and no variables are defined, so
+.Ql ${ABI}
+stays literal in both directions.
+A file the library rejects, or one holding a null character,
+is an error on a read as on a write.
+A key repeated in one file is an implicit array to the library,
+which presents its first element;
+that is the value reported and the statement a write edits.
+A removal takes every repetition, so none surfaces in its place.
+.Pp
+.Xr libucl 3
+keeps no line numbers; the trails shown by
+.Fl V
+report line 0.
+Shared options are in
+.Xr sysconf 8 .
+.Sh EXAMPLES
+.Dl sysconf ucl -f /tmp/app.conf server.port
+.Dl sysconf ucl -f /tmp/app.conf server.port=8080
+.Dl sysconf ucl -f /tmp/app.conf 'server.hosts=[ a, b ]'
+.Dl sysconf ucl -f /tmp/app.conf server.hosts+=c 'server.hosts.#'
+.Dl sysconf ucl -f /tmp/app.conf 'server.hosts.#=d' server.hosts.-1
+.Dl sysconf ucl -f /tmp/app.conf -x server.hosts.0
+.Dl sysconf ucl -f /tmp/app.conf matrix.1.0=30 'matrix.1.#'
+.Sh SEE ALSO
+.Xr uclcmd 1 Pq Pa ports/textproc/uclcmd ,
+.Xr libucl 3 ,
+.Xr pkg.conf 5 ,
+.Xr sysconf 8 ,
+.Xr sysconf-generic 8 ,
+.Xr sysconf-pkg 8 ,
+.Xr sysconf-targets 8
+.Sh HISTORY
+The
+.Nm sysconf
+utility first appeared in
+.Fx 16.0 .
+.Sh AUTHORS
+.An Devin Teske Aq Mt dteske@FreeBSD.org
+.An Faraz Vahedi Aq Mt kfv@FreeBSD.org
diff --git a/usr.sbin/sysconf/sysconf.8 b/usr.sbin/sysconf/sysconf.8
index 6d8428b5f72a..704279908602 100644
--- a/usr.sbin/sysconf/sysconf.8
+++ b/usr.sbin/sysconf/sysconf.8
@@ -3,7 +3,7 @@
 .\"
 .\" SPDX-License-Identifier: BSD-2-Clause
 .\"
-.Dd September 23, 2026
+.Dd October 25, 2026
 .Dt SYSCONF 8
 .Os
 .Sh NAME
@@ -259,6 +259,18 @@ is instead the
 append operator and
 .Ql -=
 strikes words from the last matching assignment fragment.
+For the UCL targets
+.Po
+.Cm ucl
+and
+.Cm pkg ;
+see
+.Xr sysconf-ucl 8
+.Pc ,
+the same operators on a value that is an array add and strike elements
+rather than words, and
+.Ar name Ns \&. Ns Ar i
+reaches one element.
 .Pp
 Files are never modified in place:
 updates are streamed to a temporary file which is flushed to stable storage
@@ -312,6 +324,27 @@ overlays under
 .Pa poudriere.d .
 See
 .Xr sysconf-poudriere 8 .
+.It Cm pkg
+.Xr pkg.conf 5
+and the repository files under
+.Pa /etc/pkg
+and
+.Pa /usr/local/etc/pkg/repos ,
+parsed with
+.Xr libucl 3
+as
+.Xr pkg 8
+does; a repository tag is addressed as
+.Ql tag.key .
+See
+.Xr sysconf-pkg 8 .
+.It Cm ucl
+UCL, via
+.Xr libucl 3 ,
+applied to a file named with
+.Fl f .
+See
+.Xr sysconf-ucl 8 .
 .It Cm generic
 Foreign or unknown format;
 requires
@@ -330,6 +363,18 @@ selects
 .Cm src .
 .Ql s
 matches both and is rejected with a list of matches.
+.Ql p
+likewise matches both
+.Cm pkg
+and
+.Cm poudriere ;
+.Ql pk
+selects
+.Cm pkg
+and
+.Ql po
+selects
+.Cm poudriere .
 An exact keyword always wins.
 Prefix matching is case-sensitive and applies to the start of the keyword
 only, so
@@ -736,6 +781,8 @@ Further examples for each target appear in
 .Xr sysconf-src 8 ,
 .Xr sysconf-rc 8 ,
 .Xr sysconf-poudriere 8 ,
+.Xr sysconf-pkg 8 ,
+.Xr sysconf-ucl 8 ,
 and
 .Xr sysconf-generic 8 .
 .Sh SEE ALSO
@@ -749,11 +796,13 @@ and
 .Xr sysconf-generic 8 ,
 .Xr sysconf-loader 8 ,
 .Xr sysconf-make 8 ,
+.Xr sysconf-pkg 8 ,
 .Xr sysconf-poudriere 8 ,
 .Xr sysconf-rc 8 ,
 .Xr sysconf-src 8 ,
 .Xr sysconf-sysctl 8 ,
 .Xr sysconf-targets 8 ,
+.Xr sysconf-ucl 8 ,
 .Xr sysctl 8 ,
 .Xr sysrc 8
 .Sh HISTORY
diff --git a/usr.sbin/sysconf/sysconf.c b/usr.sbin/sysconf/sysconf.c
index 41091aa2ed58..460e1a41b8d2 100644
--- a/usr.sbin/sysconf/sysconf.c
+++ b/usr.sbin/sysconf/sysconf.c
@@ -27,6 +27,10 @@ size_t		conf_scanidx;	/* file being parsed (scan_pass) */
 size_t		write_idx;	/* default write target index */
 int		defaults_idx = -1; /* defaults file index in conf_files
 		                    * (read-only, never listed); -1 if none */
+unsigned char	*conf_ro;	/* per-file read-only (pkg base repos) */
+bool		pkg_target;	/* pkg derivative is the active target */
+size_t		pkg_conf_idx;	/* /usr/local/etc/pkg.conf in conf_files */
+size_t		pkg_repos_idx;	/* local repos/FreeBSD.conf in conf_files */
 enum bsdconf_format format = BSDCONF_FORMAT_GENERIC;
 
 /* Extra display information */
@@ -606,6 +610,14 @@ help(void)
 	fprintf(stderr, TGTFMT, "rc",
 	    "pass-through: all other arguments go to sysrc(8) verbatim");
 	poudriere_help();
+	fprintf(stderr, TGTFMT, "pkg",
+	    "pkg.conf(5) plus /etc/pkg and repos/*.conf via libucl(3);");
+	fprintf(stderr, TGTFMT, "",
+	    "/etc/pkg is read-only; repository keys are overridden in");
+	fprintf(stderr, TGTFMT, "",
+	    "/usr/local/etc/pkg/repos/FreeBSD.conf");
+	fprintf(stderr, TGTFMT, "ucl",
+	    "UCL via libucl(3); no default files; requires -f file");
 	fprintf(stderr, TGTFMT, "generic",
 	    "no default files; requires -f file");
 	fprintf(stderr,
diff --git a/usr.sbin/sysconf/sysconf_edit_assign.c b/usr.sbin/sysconf/sysconf_edit_assign.c
index fc595c27c3cd..eb2963570efc 100644
--- a/usr.sbin/sysconf/sysconf_edit_assign.c
+++ b/usr.sbin/sysconf/sysconf_edit_assign.c
@@ -33,6 +33,12 @@ split_request(char *arg, struct request *req, int remove)
 
 	req->name = arg;
 	req->newvalue = NULL;
+	req->rawvalue = NULL;
+	req->ucl_base = NULL;
+	req->ucl_label = NULL;
+	req->ucl_idx = NULL;
+	req->ucl_res = NULL;
+	req->ucl_nidx = 0;
 	req->op = BSDCONF_OP_DEFAULT;
 	req->listop = '\0';
 	req->value = NULL;
@@ -184,7 +190,8 @@ list_merge(const char *current, const char *words, int op)
  * `name-=word ...') against the effective values recorded by scan_pass(),
  * rewriting each request's pending value in place so the ordinary write,
  * check, and echo machinery sees a plain assignment. Make/src `name-=word'
- * strikes are handled separately by do_make_strikes(). Appending to an
+ * strikes are handled separately by do_make_strikes(), and a UCL value
+ * that is an array by ucl_list_merge(). Appending to an
  * unset directive creates it; striking words from one is reported like any
  * other unknown directive (there is nothing to remove) and the request is
  * neutralized. Returns EXIT_SUCCESS or EXIT_FAILURE.
@@ -213,6 +220,9 @@ merge_list_requests(void)
 			reqs[n].name = "";
 			continue;
 		}
+		/* A UCL array takes elements, not words */
+		if (ucl_target && ucl_list_merge(&reqs[n]))
+			continue;
 		reqs[n].newvalue = list_merge(reqs[n].value,
 		    reqs[n].newvalue, reqs[n].listop);
 	}
diff --git a/usr.sbin/sysconf/sysconf_edit_write.c b/usr.sbin/sysconf/sysconf_edit_write.c
index e7a5c0fc831f..fd5f255d5c11 100644
--- a/usr.sbin/sysconf/sysconf_edit_write.c
+++ b/usr.sbin/sysconf/sysconf_edit_write.c
@@ -70,6 +70,10 @@ do_writes(void)
 	struct bsdconf_option *options;
 	struct request **owner;
 
+	/* UCL files are rewritten by libucl, not bsdconf_put() */
+	if (ucl_target)
+		return (ucl_do_writes());
+
 	if ((options = calloc(nreqs + 1, sizeof(*options))) == NULL ||
 	    (owner = calloc(nreqs, sizeof(*owner))) == NULL)
 		err(EXIT_FAILURE, NULL);
diff --git a/usr.sbin/sysconf/sysconf_pkg.c b/usr.sbin/sysconf/sysconf_pkg.c
new file mode 100644
index 000000000000..05d1fe3fc9f5
--- /dev/null
+++ b/usr.sbin/sysconf/sysconf_pkg.c
@@ -0,0 +1,178 @@
+/*
+ * Copyright (c) 2013-2026 Devin Teske <dteske@FreeBSD.org>
+ * Copyright (c) 2021-2026 Faraz Vahedi <kfv@FreeBSD.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+/*
+ * The pkg target: the collection of files that influence pkg(8), as a
+ * derivative of the ucl target. The syntax is libucl's (see
+ * sysconf_ucl.c); this file is policy only: the paths, the sourcing
+ * order, the read-only base repository files, and where a write lands.
+ *
+ * Sourcing order matches pkg's defaults (REPOS_DIR is not re-read):
+ *   each .conf file in /etc/pkg
+ *   /usr/local/etc/pkg.conf
+ *   each .conf file in /usr/local/etc/pkg/repos
+ * Files under /etc/pkg are the base repository definitions. They are
+ * consulted, and -F names them, but a write is never applied there: a
+ * repository key (a dotted path, the repository tag plus the inner key)
+ * is overridden in /usr/local/etc/pkg/repos/FreeBSD.conf, and a global
+ * key is written to /usr/local/etc/pkg.conf. That is the split the
+ * comment in /etc/pkg/FreeBSD.conf asks for.
+ *
+ * pkg(8) itself offers no setter for these files: `pkg config' reads one
+ * merged value, and `pkg repositories enable' records a side override
+ * under the package database rather than editing a repository file.
+ * That switch stays pkg's own; this target never writes repos_state.
+ */
+
+#include "sysconf_priv.h"
+
+static const struct bsdconf_source pkg_sources[] = {
+	{ BSDCONF_SOURCE_DIR,	"/etc/pkg" },
+	{ BSDCONF_SOURCE_FILE,	"/usr/local/etc/pkg.conf" },
+	{ BSDCONF_SOURCE_DIR,	"/usr/local/etc/pkg/repos" },
+	{ BSDCONF_SOURCE_FILE,	NULL },
+};
+
+#define PKG_CONF_REL	"/usr/local/etc/pkg.conf"
+#define PKG_REPOS_REL	"/usr/local/etc/pkg/repos/FreeBSD.conf"
+#define PKG_REPOS_DIR	"/usr/local/etc/pkg/repos/"
+#define PKG_BASE_DIR	"/etc/pkg/"
+
+enum bsdconf_format	pkg_format;
+
+/*
+ * Register pkg as a derivative of the (already registered) ucl target.
+ * Called from ucl_register(); idempotent.
+ */
+int
+pkg_register(void)
+{
+	static bool done;
+	struct bsdconf_format_def def;
+
+	if (done)
+		return (0);
+	if (bsdconf_format_derive(ucl_format, &def) != 0)
+		return (-1);
+	def.keyword = "pkg";
+	def.path = PKG_REPOS_REL;
+	def.sources = pkg_sources;
+	if (bsdconf_format_register(&def, &pkg_format) != 0)
+		return (-1);
+	done = true;
+	return (0);
+}
+
+static const char *
+rel_of(const char *path)
+{
+	size_t n;
+
+	n = strlen(rootdir);
+	if (n > 0 && strncmp(path, rootdir, n) == 0)
+		return (path + n);
+	return (path);
+}
+
+static char *
+join_root(const char *rel)
+{
+	char *path;
+	char buf[PATH_MAX];
+
+	if (snprintf(buf, sizeof(buf), "%s%s", rootdir, rel) >=
+	    (int)sizeof(buf))
+		errx(EXIT_FAILURE, "%s%s: %s", rootdir, rel,
+		    strerror(ENAMETOOLONG));
+	if ((path = strdup(buf)) == NULL)
+		err(EXIT_FAILURE, NULL);
+	return (path);
+}
+
+static int
+index_of(const char *path)
+{
+	size_t n;
+
+	for (n = 0; n < nconf_files; n++) {
*** 4278 LINES SKIPPED ***