about summary refs log tree commit diff
path: root/pkgs/os-specific/bsd
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/os-specific/bsd')
-rw-r--r--pkgs/os-specific/bsd/freebsd/default.nix112
-rw-r--r--pkgs/os-specific/bsd/freebsd/evdev-proto/default.nix11
-rw-r--r--pkgs/os-specific/bsd/freebsd/lib/default.nix20
-rw-r--r--pkgs/os-specific/bsd/freebsd/package-set.nix96
-rw-r--r--pkgs/os-specific/bsd/freebsd/patches/13.1/compat-fix-typedefs-locations.patch (renamed from pkgs/os-specific/bsd/freebsd/pkgs/compat/compat-fix-typedefs-locations.patch)0
-rw-r--r--pkgs/os-specific/bsd/freebsd/patches/13.1/compat-install-dirs.patch (renamed from pkgs/os-specific/bsd/freebsd/pkgs/compat/compat-install-dirs.patch)4
-rw-r--r--pkgs/os-specific/bsd/freebsd/patches/13.1/libc-msun-arch-subdir.patch (renamed from pkgs/os-specific/bsd/freebsd/pkgs/libc/libc-msun-arch-subdir.patch)0
-rw-r--r--pkgs/os-specific/bsd/freebsd/patches/13.1/libc-no-force--lcompiler-rt.patch (renamed from pkgs/os-specific/bsd/freebsd/pkgs/libc/libc-no-force--lcompiler-rt.patch)0
-rw-r--r--pkgs/os-specific/bsd/freebsd/patches/13.1/libnetbsd-do-install.patch (renamed from pkgs/os-specific/bsd/freebsd/pkgs/libnetbsd/libnetbsd-do-install.patch)0
-rw-r--r--pkgs/os-specific/bsd/freebsd/patches/13.1/librpcsvc-include-subdir.patch (renamed from pkgs/os-specific/bsd/freebsd/pkgs/libc/librpcsvc-include-subdir.patch)0
-rw-r--r--pkgs/os-specific/bsd/freebsd/patches/13.1/mtree-Makefile.patch13
-rw-r--r--pkgs/os-specific/bsd/freebsd/patches/13.1/no-perms-BSD.include.dist.patch (renamed from pkgs/os-specific/bsd/freebsd/pkgs/include/no-perms-BSD.include.dist.patch)0
-rw-r--r--pkgs/os-specific/bsd/freebsd/patches/13.1/sys-gnu-date.patch (renamed from pkgs/os-specific/bsd/freebsd/pkgs/sys/sys-gnu-date.patch)0
-rw-r--r--pkgs/os-specific/bsd/freebsd/patches/13.1/sys-no-explicit-intrinsics-dep.patch (renamed from pkgs/os-specific/bsd/freebsd/pkgs/sys/sys-no-explicit-intrinsics-dep.patch)0
-rw-r--r--pkgs/os-specific/bsd/freebsd/patches/14.0/bmake-no-compiler-rt.patch11
-rw-r--r--pkgs/os-specific/bsd/freebsd/patches/14.0/compat-fix-typedefs-locations.patch32
-rw-r--r--pkgs/os-specific/bsd/freebsd/patches/14.0/compat-install-dirs.patch40
-rw-r--r--pkgs/os-specific/bsd/freebsd/patches/14.0/install-bootstrap-Makefile.patch11
-rw-r--r--pkgs/os-specific/bsd/freebsd/patches/14.0/libc-msun-arch-subdir.patch11
-rw-r--r--pkgs/os-specific/bsd/freebsd/patches/14.0/libc-no-force--lcompiler-rt.patch10
-rw-r--r--pkgs/os-specific/bsd/freebsd/patches/14.0/libcxxrt-headers.patch11
-rw-r--r--pkgs/os-specific/bsd/freebsd/patches/14.0/libnetbsd-do-install.patch32
-rw-r--r--pkgs/os-specific/bsd/freebsd/patches/14.0/librpcsvc-include-subdir.patch11
-rw-r--r--pkgs/os-specific/bsd/freebsd/patches/14.0/localedef.patch158
-rw-r--r--pkgs/os-specific/bsd/freebsd/patches/14.0/mtree-Makefile.patch13
-rw-r--r--pkgs/os-specific/bsd/freebsd/patches/14.0/no-perms-BSD.include.dist.patch11
-rw-r--r--pkgs/os-specific/bsd/freebsd/patches/14.0/rtld-no-force--lcompiler-rt.patch10
-rw-r--r--pkgs/os-specific/bsd/freebsd/patches/14.0/sys-gnu-date.patch13
-rw-r--r--pkgs/os-specific/bsd/freebsd/patches/14.0/sys-no-explicit-intrinsics-dep.patch42
-rw-r--r--pkgs/os-specific/bsd/freebsd/patches/14.0/tinfo-host-cc.patch15
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/bin.nix92
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/boot-install.nix24
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/compat/package.nix241
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/config.nix36
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/cp.nix10
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/csu.nix25
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/ctfconvert.nix25
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/filterSource.nix34
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/freebsdSetupHook/package.nix4
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/freebsdSetupHook/setup-hook.sh11
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/gencat.nix4
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/iconv.nix12
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/include/package.nix33
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/install.nix76
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/ldd.nix21
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/libc/package.nix315
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/libcapsicum.nix1
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/libcasper.nix26
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/libcxxrt.nix18
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/libdl.nix9
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/libdwarf.nix21
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/libedit.nix7
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/libelf.nix29
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/libjail.nix5
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/libmd.nix49
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/libncurses-tinfo.nix15
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/libncurses.nix25
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/libnetbsd/package.nix32
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/libsbuf.nix4
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/libspl.nix23
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/libutil.nix9
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/libxo.nix6
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/locale.nix7
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/localedef.nix39
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/locales.nix51
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/lorder.nix19
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/make.nix27
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/makeMinimal.nix21
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/mkDerivation.nix231
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/mkcsmapper.nix22
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/mkesdb.nix19
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/mknod.nix4
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/mtree.nix37
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/rpcgen/package.nix11
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/sed.nix3
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/source.nix1
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/stat.nix19
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/statHook.nix16
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/sys/package.nix148
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/tsort.nix28
-rwxr-xr-xpkgs/os-specific/bsd/freebsd/update.py22
-rw-r--r--pkgs/os-specific/bsd/freebsd/versions.json59
-rw-r--r--pkgs/os-specific/bsd/lib/install-wrapper.sh (renamed from pkgs/os-specific/bsd/freebsd/lib/install-wrapper.sh)1
-rw-r--r--pkgs/os-specific/bsd/netbsd/default.nix310
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/cksum.nix2
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/column.nix6
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/common.nix3
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/compat/libbsd-overlay.pc2
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/compat/package.nix181
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/config.nix29
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/csu.nix50
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/dict.nix2
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/fts/package.nix20
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/genassym.nix6
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/gencat.nix6
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/getconf.nix6
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/getent/package.nix2
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/headers.nix8
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/i18n_module.nix19
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/include.nix34
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/install/package.nix89
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/ld_elf_so.nix26
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/libarch.nix2
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/libc.nix116
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/libcMinimal.nix85
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/libcrypt.nix10
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/libcurses.nix26
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/libedit.nix50
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/libm.nix16
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/libossaudio.nix2
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/libpci.nix10
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/libpthread/base.nix6
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/libpthread/headers.nix5
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/libpthread/package.nix42
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/libresolv.nix14
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/librpcsvc.nix44
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/librt.nix25
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/libterminfo.nix36
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/libutil.nix55
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/locale/package.nix2
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/lorder.nix21
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/make-rules.nix70
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/make.nix64
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/makeMinimal.nix24
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/man.nix2
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/misc.nix2
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/mkDerivation.nix194
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/mknod.nix6
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/mtree.nix4
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/nbperf.nix7
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/netbsdSetupHook/package.nix4
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/netbsdSetupHook/setup-hook.sh11
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/rpcgen.nix6
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/source.nix12
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/stat/hook.nix17
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/stat/package.nix21
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/sys/base.nix56
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/sys/headers.nix46
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/sys/package.nix46
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/tic.nix36
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/tsort.nix21
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/uudecode.nix9
-rw-r--r--pkgs/os-specific/bsd/openbsd/default.nix50
-rw-r--r--pkgs/os-specific/bsd/openbsd/pkgs/csu.nix23
-rw-r--r--pkgs/os-specific/bsd/openbsd/pkgs/include/package.nix57
-rw-r--r--pkgs/os-specific/bsd/openbsd/pkgs/libc/disable-librebuild.patch12
-rw-r--r--pkgs/os-specific/bsd/openbsd/pkgs/libc/netbsd-make-to-lower.patch16
-rw-r--r--pkgs/os-specific/bsd/openbsd/pkgs/libc/package.nix121
-rw-r--r--pkgs/os-specific/bsd/openbsd/pkgs/lorder.nix21
-rw-r--r--pkgs/os-specific/bsd/openbsd/pkgs/make-rules/netbsd-make-sinclude.patch15
-rw-r--r--pkgs/os-specific/bsd/openbsd/pkgs/make-rules/package.nix35
-rw-r--r--pkgs/os-specific/bsd/openbsd/pkgs/mkDerivation.nix98
-rw-r--r--pkgs/os-specific/bsd/openbsd/pkgs/openbsdSetupHook/package.nix3
-rw-r--r--pkgs/os-specific/bsd/openbsd/pkgs/openbsdSetupHook/setup-hook.sh21
-rw-r--r--pkgs/os-specific/bsd/openbsd/pkgs/source.nix8
-rw-r--r--pkgs/os-specific/bsd/setup-hook.sh1
156 files changed, 3818 insertions, 1403 deletions
diff --git a/pkgs/os-specific/bsd/freebsd/default.nix b/pkgs/os-specific/bsd/freebsd/default.nix
index b7cf5484c9e94..cfe6080b020a0 100644
--- a/pkgs/os-specific/bsd/freebsd/default.nix
+++ b/pkgs/os-specific/bsd/freebsd/default.nix
@@ -1,72 +1,52 @@
-{ stdenv, lib, stdenvNoCC
-, makeScopeWithSplicing', generateSplicesForMkScope
-, buildPackages
-, fetchgit, fetchzip
+{
+  lib,
+  makeScopeWithSplicing',
+  generateSplicesForMkScope,
+  callPackage,
+  attributePathToSplice ? [ "freebsd" ],
+  branch ? "release/14.0.0",
 }:
 
 let
-  inherit (buildPackages.buildPackages) rsync;
-
   versions = builtins.fromJSON (builtins.readFile ./versions.json);
 
-  version = "13.1.0";
-  branch = "release/${version}";
-
-in makeScopeWithSplicing' {
-  otherSplices = generateSplicesForMkScope "freebsd";
-  f = (self: lib.packagesFromDirectoryRecursive {
-    callPackage = self.callPackage;
-    directory = ./pkgs;
-  } // {
-    sourceData = versions.${branch};
-
-    ports = fetchzip {
-      url = "https://cgit.freebsd.org/ports/snapshot/ports-dde3b2b456c3a4bdd217d0bf3684231cc3724a0a.tar.gz";
-      sha256 = "BpHqJfnGOeTE7tkFJBx0Wk8ryalmf4KNTit/Coh026E=";
-    };
-
-    compatIfNeeded = lib.optional (!stdenvNoCC.hostPlatform.isFreeBSD) self.compat;
-    freebsd-lib = import ./lib { inherit version; };
-
-    # The manual callPackages below should in principle be unnecessary, but are
-    # necessary. See note in ../netbsd/default.nix
-
-    compat = self.callPackage ./pkgs/compat/package.nix {
-      inherit stdenv;
-      inherit (buildPackages.freebsd) makeMinimal boot-install;
-    };
-
-    csu = self.callPackage ./pkgs/csu.nix {
-      inherit (buildPackages.freebsd) makeMinimal install gencat;
-      inherit (self) include;
-    };
-
-    include = self.callPackage ./pkgs/include/package.nix {
-      inherit (buildPackages.freebsd) makeMinimal install rpcgen;
-    };
-
-    install = self.callPackage ./pkgs/install.nix {
-      inherit (buildPackages.freebsd) makeMinimal;
-      inherit (self) mtree libnetbsd;
-    };
-
-    libc = self.callPackage ./pkgs/libc/package.nix {
-      inherit (buildPackages.freebsd) makeMinimal install gencat rpcgen;
-      inherit (self) csu include;
-    };
-
-    libnetbsd = self.callPackage ./pkgs/libnetbsd/package.nix {
-      inherit (buildPackages.freebsd) makeMinimal;
-    };
-
-    mkDerivation = self.callPackage ./pkgs/mkDerivation.nix {
-      inherit stdenv;
-      inherit (buildPackages.freebsd) makeMinimal install tsort;
-    };
-
-    makeMinimal = self.callPackage ./pkgs/makeMinimal.nix {
-      inherit (self) make;
-    };
-
-  });
+  badBranchError =
+    branch:
+    throw ''
+      Unknown FreeBSD branch ${branch}!
+      FreeBSD branches normally look like one of:
+      * `release/<major>.<minor>.0` for tagged releases without security updates
+      * `releng/<major>.<minor>` for release update branches with security updates
+      * `stable/<major>` for stable versions working towards the next minor release
+      * `main` for the latest development version
+
+      Branches can be selected by overriding the `branch` attribute on the freebsd package set.
+    '';
+
+  # we do not include the branch in the splice here because the branch
+  # parameter to this file will only ever take on one value - more values
+  # are provided through overrides.
+  otherSplices = generateSplicesForMkScope attributePathToSplice;
+in
+# `./package-set.nix` should never know the name of the package set we
+# are constructing; just this function is allowed to know that. This
+# is why we:
+#
+#  - do the splicing for cross compilation here
+#
+#  - construct the *anonymized* `buildFreebsd` attribute to be passed
+#    to `./package-set.nix`.
+makeScopeWithSplicing' {
+  inherit otherSplices;
+  f =
+    self:
+    {
+      inherit branch;
+    }
+    // callPackage ./package-set.nix ({
+      sourceData = versions.${self.branch} or (throw (badBranchError self.branch));
+      versionData = self.sourceData.version;
+      buildFreebsd = otherSplices.selfBuildHost;
+      patchesRoot = ./patches + "/${self.versionData.revision}";
+    }) self;
 }
diff --git a/pkgs/os-specific/bsd/freebsd/evdev-proto/default.nix b/pkgs/os-specific/bsd/freebsd/evdev-proto/default.nix
index d62c01f5eb887..6696d12912e68 100644
--- a/pkgs/os-specific/bsd/freebsd/evdev-proto/default.nix
+++ b/pkgs/os-specific/bsd/freebsd/evdev-proto/default.nix
@@ -1,4 +1,11 @@
-{ lib, stdenv, linuxHeaders, freebsd, runCommandCC, buildPackages }:
+{
+  lib,
+  stdenv,
+  linuxHeaders,
+  freebsd,
+  runCommandCC,
+  buildPackages,
+}:
 
 stdenv.mkDerivation {
   pname = "evdev-proto";
@@ -28,7 +35,7 @@ stdenv.mkDerivation {
   TOUCH = "touch";
   XARGS = "xargs";
 
-  ABI_FILE = runCommandCC "abifile" {} "$CC -shared -o $out";
+  ABI_FILE = runCommandCC "abifile" { } "$CC -shared -o $out";
   CLEAN_FETCH_ENV = true;
   INSTALL_AS_USER = true;
   NO_CHECKSUM = true;
diff --git a/pkgs/os-specific/bsd/freebsd/lib/default.nix b/pkgs/os-specific/bsd/freebsd/lib/default.nix
index 30fea7e17dce6..d022f7cfa14c6 100644
--- a/pkgs/os-specific/bsd/freebsd/lib/default.nix
+++ b/pkgs/os-specific/bsd/freebsd/lib/default.nix
@@ -3,14 +3,16 @@
 {
   inherit version;
 
-  mkBsdArch = stdenv':  {
-    x86_64 = "amd64";
-    aarch64 = "arm64";
-    i486 = "i386";
-    i586 = "i386";
-    i686 = "i386";
-  }.${stdenv'.hostPlatform.parsed.cpu.name}
-    or stdenv'.hostPlatform.parsed.cpu.name;
+  mkBsdArch =
+    stdenv':
+    {
+      x86_64 = "amd64";
+      aarch64 = "arm64";
+      i486 = "i386";
+      i586 = "i386";
+      i686 = "i386";
+    }
+    .${stdenv'.hostPlatform.parsed.cpu.name} or stdenv'.hostPlatform.parsed.cpu.name;
 
-  install-wrapper = builtins.readFile ./install-wrapper.sh;
+  install-wrapper = builtins.readFile ../../lib/install-wrapper.sh;
 }
diff --git a/pkgs/os-specific/bsd/freebsd/package-set.nix b/pkgs/os-specific/bsd/freebsd/package-set.nix
new file mode 100644
index 0000000000000..4ff6cb2102490
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/package-set.nix
@@ -0,0 +1,96 @@
+{
+  stdenv,
+  lib,
+  stdenvNoCC,
+  fetchzip,
+  sourceData,
+  versionData,
+  buildFreebsd,
+  patchesRoot,
+}:
+
+self:
+
+lib.packagesFromDirectoryRecursive {
+  callPackage = self.callPackage;
+  directory = ./pkgs;
+}
+// {
+  inherit sourceData patchesRoot versionData;
+  patches = ./patches + "/${self.versionData.revision}";
+
+  # Keep the crawled portion of Nixpkgs finite.
+  buildFreebsd = lib.dontRecurseIntoAttrs buildFreebsd;
+
+  ports = fetchzip {
+    url = "https://cgit.freebsd.org/ports/snapshot/ports-dde3b2b456c3a4bdd217d0bf3684231cc3724a0a.tar.gz";
+    sha256 = "BpHqJfnGOeTE7tkFJBx0Wk8ryalmf4KNTit/Coh026E=";
+  };
+
+  compatIsNeeded = !stdenvNoCC.hostPlatform.isFreeBSD;
+  compatIfNeeded = lib.optional self.compatIsNeeded self.compat;
+  freebsd-lib = import ./lib {
+    version = lib.concatStringsSep "." (
+      map toString (
+        lib.filter (x: x != null) [
+          self.versionData.major
+          self.versionData.minor
+          self.versionData.patch or null
+        ]
+      )
+    );
+  };
+
+  # The manual callPackages below should in principle be unnecessary, but are
+  # necessary. See note in ../netbsd/default.nix
+
+  compat = self.callPackage ./pkgs/compat/package.nix {
+    inherit stdenv;
+    inherit (buildFreebsd) makeMinimal;
+  };
+
+  csu = self.callPackage ./pkgs/csu.nix {
+    inherit (buildFreebsd) makeMinimal install gencat;
+    inherit (self) include;
+  };
+
+  include = self.callPackage ./pkgs/include/package.nix { inherit (buildFreebsd) rpcgen mtree; };
+
+  install = self.callPackage ./pkgs/install.nix {
+    inherit (buildFreebsd) makeMinimal;
+    inherit (self) libmd libnetbsd;
+  };
+
+  libc = self.callPackage ./pkgs/libc/package.nix {
+    inherit (buildFreebsd)
+      makeMinimal
+      install
+      gencat
+      rpcgen
+      mkcsmapper
+      mkesdb
+      ;
+    inherit (self) csu include;
+  };
+
+  libnetbsd = self.callPackage ./pkgs/libnetbsd/package.nix { inherit (buildFreebsd) makeMinimal; };
+
+  libmd = self.callPackage ./pkgs/libmd.nix { inherit (buildFreebsd) makeMinimal; };
+
+  mkDerivation = self.callPackage ./pkgs/mkDerivation.nix {
+    inherit stdenv;
+    inherit (buildFreebsd)
+      freebsdSetupHook
+      makeMinimal
+      install
+      tsort
+      lorder
+      ;
+  };
+
+  makeMinimal = self.callPackage ./pkgs/makeMinimal.nix { inherit (self) make; };
+
+  mtree = self.callPackage ./pkgs/mtree.nix { inherit (self) libnetbsd libmd; };
+
+  tsort = self.callPackage ./pkgs/tsort.nix { inherit (buildFreebsd) makeMinimal install; };
+}
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/compat/compat-fix-typedefs-locations.patch b/pkgs/os-specific/bsd/freebsd/patches/13.1/compat-fix-typedefs-locations.patch
index 3336a2504e584..3336a2504e584 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/compat/compat-fix-typedefs-locations.patch
+++ b/pkgs/os-specific/bsd/freebsd/patches/13.1/compat-fix-typedefs-locations.patch
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/compat/compat-install-dirs.patch b/pkgs/os-specific/bsd/freebsd/patches/13.1/compat-install-dirs.patch
index 9bb2bea32ee98..4bc21cf8eb147 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/compat/compat-install-dirs.patch
+++ b/pkgs/os-specific/bsd/freebsd/patches/13.1/compat-install-dirs.patch
@@ -2,12 +2,11 @@ diff --git a/tools/build/Makefile b/tools/build/Makefile
 index 948a5f9dfdb..592af84eeae 100644
 --- a/tools/build/Makefile
 +++ b/tools/build/Makefile
-@@ -327,15 +327,15 @@ host-symlinks:
+@@ -327,14 +327,14 @@ host-symlinks:
  # and cross-tools stages. We do this here using mkdir since mtree may not exist
  # yet (this happens if we are crossbuilding from Linux/Mac).
  INSTALLDIR_LIST= \
 -	bin \
--	lib/casper \
 -	lib/geom \
 -	usr/include/casper \
 -	usr/include/private/ucl \
@@ -16,7 +15,6 @@ index 948a5f9dfdb..592af84eeae 100644
 -	usr/libdata/pkgconfig \
 -	usr/libexec
 +	${BINDIR} \
-+	${LIBDIR}/casper \
 +	${LIBDIR}/geom \
 +	${INCLUDEDIR}/casper \
 +	${INCLUDEDIR}/private/ucl \
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/libc/libc-msun-arch-subdir.patch b/pkgs/os-specific/bsd/freebsd/patches/13.1/libc-msun-arch-subdir.patch
index 4a69e85a986a0..4a69e85a986a0 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/libc/libc-msun-arch-subdir.patch
+++ b/pkgs/os-specific/bsd/freebsd/patches/13.1/libc-msun-arch-subdir.patch
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/libc/libc-no-force--lcompiler-rt.patch b/pkgs/os-specific/bsd/freebsd/patches/13.1/libc-no-force--lcompiler-rt.patch
index 60176fb73cf76..60176fb73cf76 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/libc/libc-no-force--lcompiler-rt.patch
+++ b/pkgs/os-specific/bsd/freebsd/patches/13.1/libc-no-force--lcompiler-rt.patch
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/libnetbsd/libnetbsd-do-install.patch b/pkgs/os-specific/bsd/freebsd/patches/13.1/libnetbsd-do-install.patch
index a7bd032d2be54..a7bd032d2be54 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/libnetbsd/libnetbsd-do-install.patch
+++ b/pkgs/os-specific/bsd/freebsd/patches/13.1/libnetbsd-do-install.patch
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/libc/librpcsvc-include-subdir.patch b/pkgs/os-specific/bsd/freebsd/patches/13.1/librpcsvc-include-subdir.patch
index 38e06682869fb..38e06682869fb 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/libc/librpcsvc-include-subdir.patch
+++ b/pkgs/os-specific/bsd/freebsd/patches/13.1/librpcsvc-include-subdir.patch
diff --git a/pkgs/os-specific/bsd/freebsd/patches/13.1/mtree-Makefile.patch b/pkgs/os-specific/bsd/freebsd/patches/13.1/mtree-Makefile.patch
new file mode 100644
index 0000000000000..2a6e560d1d287
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/patches/13.1/mtree-Makefile.patch
@@ -0,0 +1,13 @@
+--- a/contrib/mtree/Makefile	2023-12-04 23:02:13.919144141 -0700
++++ b/contrib/mtree/Makefile		2023-12-04 23:02:58.371810109 -0700
+@@ -10,8 +10,8 @@
+ SRCS=  compare.c crc.c create.c excludes.c misc.c mtree.c spec.c specspec.c \
+        verify.c getid.c pack_dev.c only.c
+ .if (${HOSTPROG:U} == "")
+-DPADD+= ${LIBUTIL}
+-LDADD+= -lutil
++LIBADD+= ${LIBUTIL}
++#LIBADD+= -lutil
+ .endif
+
+ CPPFLAGS+=	-I${NETBSDSRCDIR}/sbin/mknod
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/include/no-perms-BSD.include.dist.patch b/pkgs/os-specific/bsd/freebsd/patches/13.1/no-perms-BSD.include.dist.patch
index 985617ee0f457..985617ee0f457 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/include/no-perms-BSD.include.dist.patch
+++ b/pkgs/os-specific/bsd/freebsd/patches/13.1/no-perms-BSD.include.dist.patch
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/sys/sys-gnu-date.patch b/pkgs/os-specific/bsd/freebsd/patches/13.1/sys-gnu-date.patch
index 2356446baf853..2356446baf853 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/sys/sys-gnu-date.patch
+++ b/pkgs/os-specific/bsd/freebsd/patches/13.1/sys-gnu-date.patch
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/sys/sys-no-explicit-intrinsics-dep.patch b/pkgs/os-specific/bsd/freebsd/patches/13.1/sys-no-explicit-intrinsics-dep.patch
index edf44de5bb0d7..edf44de5bb0d7 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/sys/sys-no-explicit-intrinsics-dep.patch
+++ b/pkgs/os-specific/bsd/freebsd/patches/13.1/sys-no-explicit-intrinsics-dep.patch
diff --git a/pkgs/os-specific/bsd/freebsd/patches/14.0/bmake-no-compiler-rt.patch b/pkgs/os-specific/bsd/freebsd/patches/14.0/bmake-no-compiler-rt.patch
new file mode 100644
index 0000000000000..f43d87c01e549
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/patches/14.0/bmake-no-compiler-rt.patch
@@ -0,0 +1,11 @@
+--- a/share/mk/src.libnames.mk	2023-12-21 23:56:50.767042385 -0800
++++ b/share/mk/src.libnames.mk	2023-12-21 23:56:39.671089506 -0800
+@@ -392,7 +392,7 @@
+ _DP_ztest=	geom m nvpair umem zpool pthread avl zfs_core spl zutil zfs uutil icp
+ # The libc dependencies are not strictly needed but are defined to make the
+ # assert happy.
+-_DP_c=		compiler_rt
++_DP_c=		
+ # Use libssp_nonshared only on i386 and power*.  Other archs emit direct calls
+ # to __stack_chk_fail, not __stack_chk_fail_local provided by libssp_nonshared.
+ .if ${MK_SSP} != "no" && \
diff --git a/pkgs/os-specific/bsd/freebsd/patches/14.0/compat-fix-typedefs-locations.patch b/pkgs/os-specific/bsd/freebsd/patches/14.0/compat-fix-typedefs-locations.patch
new file mode 100644
index 0000000000000..3336a2504e584
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/patches/14.0/compat-fix-typedefs-locations.patch
@@ -0,0 +1,32 @@
+--- a/tools/build/cross-build/include/common/sys/_types.h
++++ b/tools/build/cross-build/include/common/sys/_types.h
+@@ -47,3 +47,6 @@
+  * Neither GLibc nor macOS define __va_list but many FreeBSD headers require it.
+  */
+ typedef __builtin_va_list __va_list;
++
++typedef __UINTPTR_TYPE__ __uintptr_t;
++typedef __INTPTR_TYPE__ __intptr_t;
+--- a/tools/build/cross-build/include/common/sys/types.h
++++ b/tools/build/cross-build/include/common/sys/types.h
+@@ -49,9 +49,6 @@
+ #include <sys/sysmacros.h>
+ #endif
+ 
+-typedef __UINTPTR_TYPE__ __uintptr_t;
+-typedef __INTPTR_TYPE__ __intptr_t;
+-
+ /* needed for gencat */
+ typedef int __nl_item;
+ 
+--- a/tools/build/cross-build/include/linux/sys/types.h
++++ b/tools/build/cross-build/include/linux/sys/types.h
+@@ -39,6 +39,8 @@
+ 
+ #include_next <sys/types.h>
+ 
++#include <sys/_types.h>
++
+ #ifndef __size_t
+ typedef __SIZE_TYPE__ __size_t;
+ #endif
diff --git a/pkgs/os-specific/bsd/freebsd/patches/14.0/compat-install-dirs.patch b/pkgs/os-specific/bsd/freebsd/patches/14.0/compat-install-dirs.patch
new file mode 100644
index 0000000000000..4bc21cf8eb147
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/patches/14.0/compat-install-dirs.patch
@@ -0,0 +1,40 @@
+diff --git a/tools/build/Makefile b/tools/build/Makefile
+index 948a5f9dfdb..592af84eeae 100644
+--- a/tools/build/Makefile
++++ b/tools/build/Makefile
+@@ -327,14 +327,14 @@ host-symlinks:
+ # and cross-tools stages. We do this here using mkdir since mtree may not exist
+ # yet (this happens if we are crossbuilding from Linux/Mac).
+ INSTALLDIR_LIST= \
+-	bin \
+-	lib/geom \
+-	usr/include/casper \
+-	usr/include/private/ucl \
+-	usr/include/private/zstd \
+-	usr/lib \
+-	usr/libdata/pkgconfig \
+-	usr/libexec
++	${BINDIR} \
++	${LIBDIR}/geom \
++	${INCLUDEDIR}/casper \
++	${INCLUDEDIR}/private/ucl \
++	${INCLUDEDIR}/private/zstd \
++	${LIBDIR} \
++	${LIBDIR}/libdata/pkgconfig \
++	${LIBEXECDIR}
+ 
+ installdirs:
+ 	mkdir -p ${INSTALLDIR_LIST:S,^,${DESTDIR}/,}
+@@ -352,9 +352,9 @@ installdirs:
+ 	    rm -rf "${DESTDIR}/${_dir}"; \
+ 	fi
+ .endfor
+-	ln -sfn bin ${DESTDIR}/sbin
+-	ln -sfn ../bin ${DESTDIR}/usr/bin
+-	ln -sfn ../bin ${DESTDIR}/usr/sbin
++	ln -sfn bin ${DESTDIR}/${SBINDIR}
++	ln -sfn ../bin ${DESTDIR}/${BINDIR}
++	ln -sfn ../bin ${DESTDIR}/${SBINDIR}
+ .for _group in ${INCSGROUPS:NINCS}
+ 	mkdir -p "${DESTDIR}/${${_group}DIR}"
+ .endfor
diff --git a/pkgs/os-specific/bsd/freebsd/patches/14.0/install-bootstrap-Makefile.patch b/pkgs/os-specific/bsd/freebsd/patches/14.0/install-bootstrap-Makefile.patch
new file mode 100644
index 0000000000000..a69c5501ddd16
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/patches/14.0/install-bootstrap-Makefile.patch
@@ -0,0 +1,11 @@
+--- a/usr.bin/xinstall/Makefile	2023-09-23 19:18:49.165192183 -0700
++++ b/usr.bin/xinstall/Makefile	2023-12-06 17:06:57.836888028 -0700
+@@ -14,7 +14,7 @@
+ CFLAGS+=	-I${SRCTOP}/lib/libnetbsd
+ 
+ LIBADD=		md
+-CFLAGS+=	-DWITH_MD5 -DWITH_RIPEMD160
++CFLAGS+=		-I${BSDSRCDIR}/contrib/libc-vis -I${BSDSRCDIR}/lib/libnetbsd
+ 
+ .ifdef BOOTSTRAPPING
+ # For the bootstrap we disable copy_file_range()
diff --git a/pkgs/os-specific/bsd/freebsd/patches/14.0/libc-msun-arch-subdir.patch b/pkgs/os-specific/bsd/freebsd/patches/14.0/libc-msun-arch-subdir.patch
new file mode 100644
index 0000000000000..4a69e85a986a0
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/patches/14.0/libc-msun-arch-subdir.patch
@@ -0,0 +1,11 @@
+--- a/lib/libc/Makefile
++++ b/lib/libc/Makefile
+@@ -194,7 +194,7 @@ SUBDIR.${MK_TESTS}+= tests
+ # recording a build dependency
+ CFLAGS+= -I${SRCTOP}/lib/libutil
+ # Same issue with libm
+-MSUN_ARCH_SUBDIR != ${MAKE} -B -C ${SRCTOP}/lib/msun -V ARCH_SUBDIR
++MSUN_ARCH_SUBDIR = ${MACHINE_CPUARCH:S/i386/i387/}
+ # unfortunately msun/src contains both private and public headers
+ CFLAGS+= -I${SRCTOP}/lib/msun/${MSUN_ARCH_SUBDIR}
+ .if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
diff --git a/pkgs/os-specific/bsd/freebsd/patches/14.0/libc-no-force--lcompiler-rt.patch b/pkgs/os-specific/bsd/freebsd/patches/14.0/libc-no-force--lcompiler-rt.patch
new file mode 100644
index 0000000000000..60176fb73cf76
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/patches/14.0/libc-no-force--lcompiler-rt.patch
@@ -0,0 +1,10 @@
+--- a/lib/libc/Makefile
++++ b/lib/libc/Makefile
+@@ -58,7 +58,6 @@ CFLAGS+=${CANCELPOINTS_CFLAGS}
+ # Link with static libcompiler_rt.a.
+ #
+ LDFLAGS+= -nodefaultlibs
+-LIBADD+=	compiler_rt
+ 
+ .if ${MK_SSP} != "no" && \
+     (${LIBC_ARCH} == "i386" || ${MACHINE_ARCH:Mpower*} != "")
diff --git a/pkgs/os-specific/bsd/freebsd/patches/14.0/libcxxrt-headers.patch b/pkgs/os-specific/bsd/freebsd/patches/14.0/libcxxrt-headers.patch
new file mode 100644
index 0000000000000..256db9e2d9cf4
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/patches/14.0/libcxxrt-headers.patch
@@ -0,0 +1,11 @@
+--- freebsd/lib/libcxxrt/Makefile	2024-05-30 14:27:42.328086005 -0700
++++ freebsd/lib/libcxxrt/Makefile.mod	2024-05-30 14:27:48.048014581 -0700
+@@ -19,6 +19,8 @@
+ SRCS+=		terminate.cc
+ SRCS+=		typeinfo.cc
+ 
++INCS+=cxxabi.h unwind.h unwind-arm.h unwind-itanium.h
++
+ WARNS?=		0
+ CFLAGS+=	-isystem ${SRCDIR} -nostdinc++
+ CXXSTD?=	c++14
diff --git a/pkgs/os-specific/bsd/freebsd/patches/14.0/libnetbsd-do-install.patch b/pkgs/os-specific/bsd/freebsd/patches/14.0/libnetbsd-do-install.patch
new file mode 100644
index 0000000000000..a7bd032d2be54
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/patches/14.0/libnetbsd-do-install.patch
@@ -0,0 +1,32 @@
+diff --git a/Makefile b/Makefile
+index 22710f3d933..22effc848cf 100644
+--- a/lib/libnetbsd/Makefile
++++ b/lib/libnetbsd/Makefile
+@@ -9,6 +9,26 @@ CFLAGS+=	-I${.CURDIR}
+ 
+ SRCS+=	efun.c sockaddr_snprintf.c strsuftoll.c util.c util.h
+ 
+-INTERNALLIB=
++INCSGROUPS= INCS SYSINCS NETINETINCS
++
++INCS+= \
++	glob.h \
++	pthread.h \
++	rmd160.h \
++	sha1.h \
++	sha2.h \
++	stdlib.h \
++	util.h
++
++SYSINCSDIR= ${INCLUDEDIR}/sys
++SYSINCS+= \
++	sys/cdefs.h \
++	sys/event.h \
++	sys/types.h \
++	sys/wait.h
++
++NETINETINCSDIR= ${INCLUDEDIR}/netinet
++NETINETINCS+= \
++	netinet/in.h
+ 
+ .include <bsd.lib.mk>
diff --git a/pkgs/os-specific/bsd/freebsd/patches/14.0/librpcsvc-include-subdir.patch b/pkgs/os-specific/bsd/freebsd/patches/14.0/librpcsvc-include-subdir.patch
new file mode 100644
index 0000000000000..38e06682869fb
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/patches/14.0/librpcsvc-include-subdir.patch
@@ -0,0 +1,11 @@
+--- a/lib/librpcsvc/Makefile
++++ b/lib/librpcsvc/Makefile
+@@ -20,7 +20,7 @@ OTHERSRCS+= yp_passwd.c yp_update.c
+ 
+ RPCCOM=	RPCGEN_CPP=${CPP:Q} rpcgen -C
+ 
+-INCDIRS= -I${SYSROOT:U${DESTDIR}}/usr/include/rpcsvc
++INCDIRS= -I${INCLUDEDIR}/rpcsvc
+ 
+ CFLAGS+= -DYP ${INCDIRS}
+ 
diff --git a/pkgs/os-specific/bsd/freebsd/patches/14.0/localedef.patch b/pkgs/os-specific/bsd/freebsd/patches/14.0/localedef.patch
new file mode 100644
index 0000000000000..73a0341bd3240
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/patches/14.0/localedef.patch
@@ -0,0 +1,158 @@
+diff --git a/include/_ctype.h b/include/_ctype.h
+index 91e6b1d14f6b..a6896b598da3 100644
+--- a/include/_ctype.h
++++ b/include/_ctype.h
+@@ -44,7 +44,7 @@
+ #define	__CTYPE_H_
+ 
+ #include <sys/cdefs.h>
+-#include <sys/_types.h>
++#include <sys/types.h>
+ 
+ #define	_CTYPE_A	0x00000100L		/* Alpha */
+ #define	_CTYPE_C	0x00000200L		/* Control */
+diff --git a/lib/libc/locale/collate.h b/lib/libc/locale/collate.h
+index 2d3723b49f5b..6bbff732b9d7 100644
+--- a/lib/libc/locale/collate.h
++++ b/lib/libc/locale/collate.h
+@@ -36,6 +36,7 @@
+ #ifndef _COLLATE_H_
+ #define	_COLLATE_H_
+ 
++#include <stdint.h>
+ #include <sys/cdefs.h>
+ #include <sys/types.h>
+ #include <limits.h>
+diff --git a/usr.bin/localedef/charmap.c b/usr.bin/localedef/charmap.c
+index 44b7e3292eae..79c30b7cf372 100644
+--- a/usr.bin/localedef/charmap.c
++++ b/usr.bin/localedef/charmap.c
+@@ -31,6 +31,7 @@
+ /*
+  * CHARMAP file handling for localedef.
+  */
++#include <stdint.h>
+ #include <sys/cdefs.h>
+ #include <sys/types.h>
+ #include <sys/tree.h>
+diff --git a/usr.bin/localedef/collate.c b/usr.bin/localedef/collate.c
+index 2a080773a95e..3f0030c638f5 100644
+--- a/usr.bin/localedef/collate.c
++++ b/usr.bin/localedef/collate.c
+@@ -31,6 +31,7 @@
+ /*
+  * LC_COLLATE database generation routines for localedef.
+  */
++#include <stdint.h>
+ #include <sys/cdefs.h>
+ #include <sys/types.h>
+ #include <sys/tree.h>
+diff --git a/usr.bin/localedef/ctype.c b/usr.bin/localedef/ctype.c
+index ab7b76e57b2d..846c6d6480a8 100644
+--- a/usr.bin/localedef/ctype.c
++++ b/usr.bin/localedef/ctype.c
+@@ -32,6 +32,7 @@
+ /*
+  * LC_CTYPE database generation routines for localedef.
+  */
++#include <stdint.h>
+ #include <sys/cdefs.h>
+ #include <sys/tree.h>
+ 
+diff --git a/usr.bin/localedef/localedef.c b/usr.bin/localedef/localedef.c
+index 5ff146d6f655..ed69aa1f0c0e 100644
+--- a/usr.bin/localedef/localedef.c
++++ b/usr.bin/localedef/localedef.c
+@@ -32,7 +32,7 @@
+  * POSIX localedef.
+  */
+ #include <sys/cdefs.h>
+-#include <sys/endian.h>
++#include <endian.h>
+ #include <sys/stat.h>
+ #include <sys/types.h>
+ 
+diff --git a/usr.bin/localedef/messages.c b/usr.bin/localedef/messages.c
+index 6b8eb9d684dd..0155821d0e56 100644
+--- a/usr.bin/localedef/messages.c
++++ b/usr.bin/localedef/messages.c
+@@ -31,6 +31,7 @@
+ /*
+  * LC_MESSAGES database generation routines for localedef.
+  */
++#include <stdint.h>
+ #include <sys/cdefs.h>
+ #include <stdio.h>
+ #include <stdlib.h>
+diff --git a/usr.bin/localedef/monetary.c b/usr.bin/localedef/monetary.c
+index 7a77ac7e256c..7636c4deca1f 100644
+--- a/usr.bin/localedef/monetary.c
++++ b/usr.bin/localedef/monetary.c
+@@ -31,6 +31,7 @@
+ /*
+  * LC_MONETARY database generation routines for localedef.
+  */
++#include <stdint.h>
+ #include <sys/cdefs.h>
+ #include <stdio.h>
+ #include <stdlib.h>
+diff --git a/usr.bin/localedef/numeric.c b/usr.bin/localedef/numeric.c
+index 5533b7c10e1a..9c47494f815c 100644
+--- a/usr.bin/localedef/numeric.c
++++ b/usr.bin/localedef/numeric.c
+@@ -31,6 +31,7 @@
+ /*
+  * LC_NUMERIC database generation routines for localedef.
+  */
++#include <stdint.h>
+ #include <sys/cdefs.h>
+ #include <stdio.h>
+ #include <stdlib.h>
+diff --git a/usr.bin/localedef/parser.y b/usr.bin/localedef/parser.y
+index 23b3b54f8a6e..e01330f0152d 100644
+--- a/usr.bin/localedef/parser.y
++++ b/usr.bin/localedef/parser.y
+@@ -33,6 +33,7 @@
+  * POSIX localedef grammar.
+  */
+ 
++#include <stdint.h>
+ #include <wchar.h>
+ #include <stdio.h>
+ #include <limits.h>
+diff --git a/usr.bin/localedef/scanner.c b/usr.bin/localedef/scanner.c
+index c6d45a993f28..b17670ef4b4a 100644
+--- a/usr.bin/localedef/scanner.c
++++ b/usr.bin/localedef/scanner.c
+@@ -32,6 +32,7 @@
+  * This file contains the "scanner", which tokenizes the input files
+  * for localedef for processing by the higher level grammar processor.
+  */
++#include <stdint.h>
+ #include <sys/cdefs.h>
+ #include <stdio.h>
+ #include <stdlib.h>
+diff --git a/usr.bin/localedef/time.c b/usr.bin/localedef/time.c
+index 7a56e244c921..0e409a742d0a 100644
+--- a/usr.bin/localedef/time.c
++++ b/usr.bin/localedef/time.c
+@@ -31,6 +31,7 @@
+ /*
+  * LC_TIME database generation routines for localedef.
+  */
++#include <stdint.h>
+ #include <sys/cdefs.h>
+ #include <stdio.h>
+ #include <stdlib.h>
+diff --git a/usr.bin/localedef/wide.c b/usr.bin/localedef/wide.c
+index 062e120e6912..a199cddb198d 100644
+--- a/usr.bin/localedef/wide.c
++++ b/usr.bin/localedef/wide.c
+@@ -34,6 +34,7 @@
+  * to the wide character forms used internally by libc.  Unfortunately,
+  * this approach means that we need a method for each and every encoding.
+  */
++#include <stdint.h>
+ #include <sys/cdefs.h>
+ #include <ctype.h>
+ #include <stdlib.h>
diff --git a/pkgs/os-specific/bsd/freebsd/patches/14.0/mtree-Makefile.patch b/pkgs/os-specific/bsd/freebsd/patches/14.0/mtree-Makefile.patch
new file mode 100644
index 0000000000000..2a6e560d1d287
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/patches/14.0/mtree-Makefile.patch
@@ -0,0 +1,13 @@
+--- a/contrib/mtree/Makefile	2023-12-04 23:02:13.919144141 -0700
++++ b/contrib/mtree/Makefile		2023-12-04 23:02:58.371810109 -0700
+@@ -10,8 +10,8 @@
+ SRCS=  compare.c crc.c create.c excludes.c misc.c mtree.c spec.c specspec.c \
+        verify.c getid.c pack_dev.c only.c
+ .if (${HOSTPROG:U} == "")
+-DPADD+= ${LIBUTIL}
+-LDADD+= -lutil
++LIBADD+= ${LIBUTIL}
++#LIBADD+= -lutil
+ .endif
+
+ CPPFLAGS+=	-I${NETBSDSRCDIR}/sbin/mknod
diff --git a/pkgs/os-specific/bsd/freebsd/patches/14.0/no-perms-BSD.include.dist.patch b/pkgs/os-specific/bsd/freebsd/patches/14.0/no-perms-BSD.include.dist.patch
new file mode 100644
index 0000000000000..985617ee0f457
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/patches/14.0/no-perms-BSD.include.dist.patch
@@ -0,0 +1,11 @@
+--- a/etc/mtree/BSD.include.dist
++++ b/etc/mtree/BSD.include.dist
+@@ -3,7 +3,7 @@
+ # Please see the file src/etc/mtree/README before making changes to this file.
+ #
+ 
+-/set type=dir uname=root gname=wheel mode=0755
++/set type=dir
+ .
+     arpa
+     ..
diff --git a/pkgs/os-specific/bsd/freebsd/patches/14.0/rtld-no-force--lcompiler-rt.patch b/pkgs/os-specific/bsd/freebsd/patches/14.0/rtld-no-force--lcompiler-rt.patch
new file mode 100644
index 0000000000000..45f0d0c51eec0
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/patches/14.0/rtld-no-force--lcompiler-rt.patch
@@ -0,0 +1,10 @@
+--- a/libexec/rtld-elf/Makefile
++++ b/libexec/rtld-elf/Makefile
+@@ -86,7 +86,6 @@ 
+ 
+ # Some of the required math functions (div & mod) are implemented in
+ # libcompiler_rt on some architectures.
+-LIBADD+=	compiler_rt
+ 
+ .include <bsd.prog.mk>
+ ${PROG_FULL}:  ${VERSION_MAP}
diff --git a/pkgs/os-specific/bsd/freebsd/patches/14.0/sys-gnu-date.patch b/pkgs/os-specific/bsd/freebsd/patches/14.0/sys-gnu-date.patch
new file mode 100644
index 0000000000000..2356446baf853
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/patches/14.0/sys-gnu-date.patch
@@ -0,0 +1,13 @@
+diff --git a/sys/conf/newvers.sh b/sys/conf/newvers.sh
+index c594724d814..d5287c7b992 100644
+--- a/sys/conf/newvers.sh
++++ b/sys/conf/newvers.sh
+@@ -177,7 +177,7 @@ u=${USER:-root}
+ d=$(pwd)
+ h=${HOSTNAME:-$(hostname)}
+ if [ -n "$SOURCE_DATE_EPOCH" ]; then
+-	if ! t=$(date -r $SOURCE_DATE_EPOCH 2>/dev/null); then
++	if ! t=$(date -d @$SOURCE_DATE_EPOCH 2>/dev/null); then
+ 		echo "Invalid SOURCE_DATE_EPOCH" >&2
+ 		exit 1
+ 	fi
diff --git a/pkgs/os-specific/bsd/freebsd/patches/14.0/sys-no-explicit-intrinsics-dep.patch b/pkgs/os-specific/bsd/freebsd/patches/14.0/sys-no-explicit-intrinsics-dep.patch
new file mode 100644
index 0000000000000..5cf926d4519b2
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/patches/14.0/sys-no-explicit-intrinsics-dep.patch
@@ -0,0 +1,42 @@
+--- a/sys/modules/aesni/Makefile	2023-12-16 09:19:28.454892154 -0700
++++ b/sys/Modules/aesni/Makefile	2023-12-16 09:19:41.975047684 -0700
+@@ -1,6 +1,5 @@
+ 
+ .PATH: ${SRCTOP}/sys/crypto/aesni
+-.PATH: ${SRCTOP}/contrib/llvm-project/clang/lib/Headers
+ 
+ KMOD=	aesni
+ SRCS=	aesni.c
+@@ -39,8 +38,8 @@
+ aesni_ghash.o: aesni.h
+ aesni_wrap.o: aesni.h
+ aesni_ccm.o: aesni.h
+-intel_sha1.o: sha_sse.h immintrin.h shaintrin.h tmmintrin.h xmmintrin.h
+-intel_sha256.o: sha_sse.h immintrin.h shaintrin.h tmmintrin.h xmmintrin.h
++intel_sha1.o: sha_sse.h
++intel_sha256.o: sha_sse.h
+ 
+ .include <bsd.kmod.mk>
+ 
+diff --git a/sys/modules/blake2/Makefile b/sys/modules/blake2/Makefile
+index e4b3fb9f126..5bfd9c2ae02 100644
+--- a/sys/modules/blake2/Makefile
++++ b/sys/modules/blake2/Makefile
+@@ -3,7 +3,6 @@
+ .PATH:	${SRCTOP}/sys/contrib/libb2
+ .PATH:	${SRCTOP}/sys/crypto/blake2
+ .PATH:	${SRCTOP}/sys/opencrypto
+-.PATH:	${SRCTOP}/contrib/llvm-project/clang/lib/Headers
+ 
+ KMOD	= blake2
+ 
+@@ -64,8 +63,7 @@ ${src:S/.c/.o/}: ${src}
+ 	    -D_MM_MALLOC_H_INCLUDED -Wno-unused-function ${.IMPSRC}
+ 	${CTFCONVERT_CMD}
+ 
+-${src:S/.c/.o/}: intrin.h emmintrin.h tmmintrin.h smmintrin.h immintrin.h \
+-    x86intrin.h ${SRCS:M*.h}
++${src:S/.c/.o/}: ${SRCS:M*.h}
+ .endfor
+ 
+ # FreeBSD-specific sources:
diff --git a/pkgs/os-specific/bsd/freebsd/patches/14.0/tinfo-host-cc.patch b/pkgs/os-specific/bsd/freebsd/patches/14.0/tinfo-host-cc.patch
new file mode 100644
index 0000000000000..6446322a8a0a3
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/patches/14.0/tinfo-host-cc.patch
@@ -0,0 +1,15 @@
+--- a/lib/ncurses/tinfo/Makefile	2023-12-26 23:02:07.827892619 -0800
++++ b/lib/ncurses/tinfo/Makefile	2023-12-26 23:01:24.175546100 -0800
+@@ -282,10 +282,10 @@
+ build-tools: make_hash make_keys
+ 
+ make_keys: make_keys.c names.c ncurses_def.h ${HEADERS} ${BUILD_TOOLS_META}
+-	${CC:N${CCACHE_BIN}} -o $@ ${CFLAGS} ${NCURSES_DIR}/ncurses/tinfo/make_keys.c
++	${CC_HOST:N${CCACHE_BIN}} -o $@ ${CFLAGS} ${NCURSES_DIR}/ncurses/tinfo/make_keys.c
+ 
+ make_hash: make_hash.c hashsize.h ncurses_def.h ${HEADERS} ${BUILD_TOOLS_META}
+-	${CC:N${CCACHE_BIN}} -o $@ ${CFLAGS} -DMAIN_PROGRAM \
++	${CC_HOST:N${CCACHE_BIN}} -o $@ ${CFLAGS} -DMAIN_PROGRAM \
+ 		${NCURSES_DIR}/ncurses/tinfo/make_hash.c
+ .endif
+ .if ${MK_DIRDEPS_BUILD} == "yes" && ${MACHINE} != "host"
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/bin.nix b/pkgs/os-specific/bsd/freebsd/pkgs/bin.nix
new file mode 100644
index 0000000000000..2b2738ec5794a
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/bin.nix
@@ -0,0 +1,92 @@
+{
+  mkDerivation,
+  pkgsBuildBuild,
+  libjail,
+  libmd,
+  libnetbsd,
+  libcapsicum,
+  libcasper,
+  libelf,
+  libxo,
+  libncurses-tinfo,
+  libedit,
+  lib,
+  stdenv,
+  bsdSetupHook,
+  freebsdSetupHook,
+  makeMinimal,
+  install,
+  tsort,
+  lorder,
+  mandoc,
+  groff,
+  byacc,
+  gencat,
+}:
+mkDerivation {
+  pname = "bins";
+  path = "bin";
+  extraPaths = [
+    "sys/conf"
+    "sys/sys/param.h"
+    "contrib/sendmail"
+    "contrib/tcsh"
+    "usr.bin/printf"
+    "lib/libsm"
+  ];
+  buildInputs = [
+    libjail
+    libmd
+    libnetbsd
+    libcapsicum
+    libcasper
+    libelf
+    libxo
+    libncurses-tinfo
+    libedit
+  ];
+  nativeBuildInputs = [
+    bsdSetupHook
+    freebsdSetupHook
+    makeMinimal
+    install
+    tsort
+    lorder
+    mandoc
+    groff
+
+    byacc
+    gencat
+  ];
+
+  MK_TESTS = "no";
+
+  postPatch = ''
+    sed -E -i -e '/#define\tBSD.*/d' $BSDSRCDIR/sys/sys/param.h
+    sed -E -i -e '/^SYMLINKS.*/d' $BSDSRCDIR/bin/*/Makefile
+    sed -E -i -e 's/mktemp -t ka/mktemp -t kaXXXXXX/' $BSDSRCDIR/bin/sh/mkbuiltins $BSDSRCDIR/bin/sh/mktokens
+  '';
+
+  preBuild = ''
+    export NIX_CFLAGS_COMPILE="-I$BSDSRCDIR/sys $NIX_CFLAGS_COMPILE"
+
+    make -C $BSDSRCDIR/lib/libsm $makeFlags
+
+    make -C $BSDSRCDIR/bin/sh $makeFlags "CC=${pkgsBuildBuild.stdenv.cc}/bin/cc" CFLAGS="-D__unused= -D__printf0like\(a,b\)= -D__dead2=" ${
+      lib.optionalString (!stdenv.buildPlatform.isFreeBSD) "MK_PIE=no "
+    }mkbuiltins mksyntax mktokens mknodes
+    make -C $BSDSRCDIR/bin/csh $makeFlags "CC=${pkgsBuildBuild.stdenv.cc}/bin/cc" CFLAGS="-D__unused= -D__printf0like\(a,b\)= -D__dead2= -I$BSDSRCDIR/contrib/tcsh -I." ${
+      lib.optionalString (!stdenv.buildPlatform.isFreeBSD) "MK_PIE=no "
+    }gethost
+  '';
+
+  preInstall = ''
+    makeFlags="$makeFlags ROOTDIR=$out/root"
+  '';
+
+  outputs = [
+    "out"
+    "man"
+    "debug"
+  ];
+}
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/boot-install.nix b/pkgs/os-specific/bsd/freebsd/pkgs/boot-install.nix
index 71ecef1bcae47..79dab282e3f3f 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/boot-install.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/boot-install.nix
@@ -1,7 +1,23 @@
 { buildPackages, freebsd-lib }:
 
-# Wrap NetBSD's install
-buildPackages.writeShellScriptBin "boot-install" (freebsd-lib.install-wrapper + ''
+# Wrap GNU coreutils' install
+# The -l flag causes a symlink instead of a copy to be installed, so
+# it is safe to discard during bootstrap since coreutils does not support it.
 
-  ${buildPackages.netbsd.install}/bin/xinstall "''${args[@]}"
-'')
+buildPackages.writeShellScriptBin "boot-install" (
+  freebsd-lib.install-wrapper
+  + ''
+    fixed_args=()
+    while [[ ''${#args[0]} > 0 ]]; do
+      case "''${args[0]}" in
+        -l)
+          args=("''${args[@]:2}")
+          continue
+      esac
+      fixed_args+=("''${args[0]}")
+      args=("''${args[@]:1}")
+    done
+
+    ${buildPackages.coreutils}/bin/install "''${fixed_args[@]}"
+  ''
+)
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/compat/package.nix b/pkgs/os-specific/bsd/freebsd/pkgs/compat/package.nix
index 5e4528fbf46a1..f597d6e3705b4 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/compat/package.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/compat/package.nix
@@ -1,102 +1,129 @@
-{ lib, stdenv, mkDerivation
-, bsdSetupHook, freebsdSetupHook
-, makeMinimal, boot-install
-, which
-, freebsd-lib
-, expat, zlib,
+{
+  lib,
+  stdenv,
+  mkDerivation,
+  versionData,
+  bsdSetupHook,
+  freebsdSetupHook,
+  makeMinimal,
+  boot-install,
+  which,
+  freebsd-lib,
+  expat,
+  zlib,
 }:
 
 let
   inherit (freebsd-lib) mkBsdArch;
 in
 
-mkDerivation rec {
+mkDerivation {
   pname = "compat";
   path = "tools/build";
-  extraPaths = [
-    "lib/libc/db"
-    "lib/libc/stdlib" # getopt
-    "lib/libc/gen" # getcap
-    "lib/libc/locale" # rpmatch
-  ] ++ lib.optionals stdenv.hostPlatform.isLinux [
-    "lib/libc/string" # strlcpy
-    "lib/libutil"
-  ] ++ [
-    "contrib/libc-pwcache"
-    "contrib/libc-vis"
-    "sys/libkern"
-    "sys/kern/subr_capability.c"
-
-    # Take only individual headers, or else we will clobber native libc, etc.
-
-    "sys/rpc/types.h"
-
-    # Listed in Makekfile as INC
-    "include/mpool.h"
-    "include/ndbm.h"
-    "include/err.h"
-    "include/stringlist.h"
-    "include/a.out.h"
-    "include/nlist.h"
-    "include/db.h"
-    "include/getopt.h"
-    "include/nl_types.h"
-    "include/elf.h"
-    "sys/sys/ctf.h"
-
-    # Listed in Makekfile as SYSINC
-
-    "sys/sys/capsicum.h"
-    "sys/sys/caprights.h"
-    "sys/sys/imgact_aout.h"
-    "sys/sys/nlist_aout.h"
-    "sys/sys/nv.h"
-    "sys/sys/dnv.h"
-    "sys/sys/cnv.h"
-
-    "sys/sys/elf32.h"
-    "sys/sys/elf64.h"
-    "sys/sys/elf_common.h"
-    "sys/sys/elf_generic.h"
-    "sys/${mkBsdArch stdenv}/include"
-  ] ++ lib.optionals stdenv.hostPlatform.isx86 [
-    "sys/x86/include"
-  ] ++ [
-
-    "sys/sys/queue.h"
-    "sys/sys/md5.h"
-    "sys/sys/sbuf.h"
-    "sys/sys/tree.h"
-    "sys/sys/font.h"
-    "sys/sys/consio.h"
-    "sys/sys/fnv_hash.h"
-
-    "sys/crypto/chacha20/_chacha.h"
-    "sys/crypto/chacha20/chacha.h"
-    # included too, despite ".c"
-    "sys/crypto/chacha20/chacha.c"
-
-    "sys/fs"
-    "sys/ufs"
-    "sys/sys/disk"
-
-    "lib/libcapsicum"
-    "lib/libcasper"
-  ];
-
-  patches = [
-    ./compat-install-dirs.patch
-    ./compat-fix-typedefs-locations.patch
-  ];
-
-  preBuild = ''
-    NIX_CFLAGS_COMPILE+=' -I../../include -I../../sys'
-
-    cp ../../sys/${mkBsdArch stdenv}/include/elf.h ../../sys/sys
-    cp ../../sys/${mkBsdArch stdenv}/include/elf.h ../../sys/sys/${mkBsdArch stdenv}
-  '' + lib.optionalString stdenv.hostPlatform.isx86 ''
-    cp ../../sys/x86/include/elf.h ../../sys/x86
-  '';
+  extraPaths =
+    [
+      "lib/libc/db"
+      "lib/libc/stdlib" # getopt
+      "lib/libc/gen" # getcap
+      "lib/libc/locale" # rpmatch
+    ]
+    ++ lib.optionals stdenv.hostPlatform.isLinux [
+      "lib/libc/string" # strlcpy
+      "lib/libutil"
+    ]
+    ++ [
+      "contrib/libc-pwcache"
+      "contrib/libc-vis"
+      "sys/libkern"
+      "sys/kern/subr_capability.c"
+
+      # Take only individual headers, or else we will clobber native libc, etc.
+
+      "sys/rpc/types.h"
+    ]
+    ++ lib.optionals (versionData.major == 14) [ "sys/sys/bitcount.h" ]
+    ++ [
+
+      # Listed in Makekfile as INC
+      "include/mpool.h"
+      "include/ndbm.h"
+      "include/err.h"
+      "include/stringlist.h"
+      "include/a.out.h"
+      "include/nlist.h"
+      "include/db.h"
+      "include/getopt.h"
+      "include/nl_types.h"
+      "include/elf.h"
+      "sys/sys/ctf.h"
+    ]
+    ++ lib.optionals (versionData.major == 14) [
+      "include/bitstring.h"
+      "sys/sys/bitstring.h"
+      "sys/sys/nv_namespace.h"
+    ]
+    ++ [
+
+      # Listed in Makekfile as SYSINC
+
+      "sys/sys/capsicum.h"
+      "sys/sys/caprights.h"
+      "sys/sys/imgact_aout.h"
+      "sys/sys/nlist_aout.h"
+      "sys/sys/nv.h"
+      "sys/sys/dnv.h"
+      "sys/sys/cnv.h"
+
+      "sys/sys/elf32.h"
+      "sys/sys/elf64.h"
+      "sys/sys/elf_common.h"
+      "sys/sys/elf_generic.h"
+      "sys/${mkBsdArch stdenv}/include"
+    ]
+    ++ lib.optionals stdenv.hostPlatform.isx86 [ "sys/x86/include" ]
+    ++ [
+
+      "sys/sys/queue.h"
+      "sys/sys/md5.h"
+      "sys/sys/sbuf.h"
+      "sys/sys/tree.h"
+      "sys/sys/font.h"
+      "sys/sys/consio.h"
+      "sys/sys/fnv_hash.h"
+      #"sys/sys/cdefs.h"
+      #"sys/sys/param.h"
+      "sys/sys/_null.h"
+      #"sys/sys/types.h"
+      "sys/sys/_pthreadtypes.h"
+      "sys/sys/_stdint.h"
+
+      "sys/crypto/chacha20/_chacha.h"
+      "sys/crypto/chacha20/chacha.h"
+      # included too, despite ".c"
+      "sys/crypto/chacha20/chacha.c"
+
+      "sys/fs"
+      "sys/ufs"
+      "sys/sys/disk"
+
+      "lib/libcapsicum"
+      "lib/libcasper"
+      "lib/libmd"
+
+      # idk bro
+      "sys/sys/kbio.h"
+    ];
+
+  preBuild =
+    ''
+      NIX_CFLAGS_COMPILE+=' -I../../include -I../../sys'
+
+      cp ../../sys/${mkBsdArch stdenv}/include/elf.h ../../sys/sys
+      cp ../../sys/${mkBsdArch stdenv}/include/elf.h ../../sys/sys/${mkBsdArch stdenv}
+    ''
+    + lib.optionalString stdenv.hostPlatform.isx86 ''
+      cp ../../sys/x86/include/elf.h ../../sys/x86
+    '';
 
   setupHooks = [
     ../../../../../build-support/setup-hooks/role.bash
@@ -109,13 +136,17 @@ mkDerivation rec {
   '';
 
   nativeBuildInputs = [
-    bsdSetupHook freebsdSetupHook
+    bsdSetupHook
+    freebsdSetupHook
     makeMinimal
     boot-install
 
     which
   ];
-  buildInputs = [ expat zlib ];
+  buildInputs = [
+    expat
+    zlib
+  ];
 
   makeFlags = [
     "STRIP=-s" # flag to install, not command
@@ -124,12 +155,20 @@ mkDerivation rec {
     "INSTALL=boot-install"
   ];
 
-  preIncludes = ''
-    mkdir -p $out/{0,1}-include
-    cp --no-preserve=mode -r cross-build/include/common/* $out/0-include
-  '' + lib.optionalString stdenv.hostPlatform.isLinux ''
-    cp --no-preserve=mode -r cross-build/include/linux/* $out/1-include
-  '' + lib.optionalString stdenv.hostPlatform.isDarwin ''
-    cp --no-preserve=mode -r cross-build/include/darwin/* $out/1-include
-  '';
+  preIncludes =
+    ''
+      mkdir -p $out/{0,1}-include
+      cp --no-preserve=mode -r cross-build/include/common/* $out/0-include
+    ''
+    + lib.optionalString stdenv.hostPlatform.isLinux ''
+      cp --no-preserve=mode -r cross-build/include/linux/* $out/1-include
+    ''
+    + lib.optionalString stdenv.hostPlatform.isDarwin ''
+      cp --no-preserve=mode -r cross-build/include/darwin/* $out/1-include
+    '';
+
+  # Compat is for making other platforms look like FreeBSD (e.g. to
+  # build build-time dependencies for building FreeBSD packages). It is
+  # not needed when building for FreeBSD.
+  meta.broken = stdenv.hostPlatform.isFreeBSD;
 }
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/config.nix b/pkgs/os-specific/bsd/freebsd/pkgs/config.nix
index 641cfc46b4e9b..f7ba273ed5583 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/config.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/config.nix
@@ -1,17 +1,35 @@
-{ mkDerivation
-, bsdSetupHook, freebsdSetupHook
-, makeMinimal, install, mandoc, groff
-, flex, byacc, file2c
-, compatIfNeeded, libnv, libsbuf
+{
+  mkDerivation,
+  bsdSetupHook,
+  freebsdSetupHook,
+  makeMinimal,
+  install,
+  mandoc,
+  groff,
+  flex,
+  byacc,
+  file2c,
+  compatIfNeeded,
+  libnv,
+  libsbuf,
 }:
 
 mkDerivation {
   path = "usr.sbin/config";
   nativeBuildInputs = [
-    bsdSetupHook freebsdSetupHook
-    makeMinimal install mandoc groff
+    bsdSetupHook
+    freebsdSetupHook
+    makeMinimal
+    install
+    mandoc
+    groff
 
-    flex byacc file2c
+    flex
+    byacc
+    file2c
+  ];
+  buildInputs = compatIfNeeded ++ [
+    libnv
+    libsbuf
   ];
-  buildInputs = compatIfNeeded ++ [ libnv libsbuf ];
 }
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/cp.nix b/pkgs/os-specific/bsd/freebsd/pkgs/cp.nix
new file mode 100644
index 0000000000000..3ebfd91a4e153
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/cp.nix
@@ -0,0 +1,10 @@
+{ mkDerivation }:
+mkDerivation {
+  path = "bin/cp";
+
+  extraPaths = [ "sys" ];
+
+  postPatch = ''
+    substituteInPlace $BSDSRCDIR/bin/cp/Makefile --replace 'tests' ""
+  '';
+}
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/csu.nix b/pkgs/os-specific/bsd/freebsd/pkgs/csu.nix
index 0b17cb1c8481d..020a08c1d01ef 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/csu.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/csu.nix
@@ -1,23 +1,32 @@
-{ lib, mkDerivation
-, bsdSetupHook, freebsdSetupHook
-, makeMinimal
-, install
-, flex, byacc, gencat
-, include
+{
+  lib,
+  mkDerivation,
+  bsdSetupHook,
+  freebsdSetupHook,
+  makeMinimal,
+  install,
+  flex,
+  byacc,
+  gencat,
+  include,
 }:
 
 mkDerivation {
+  noLibc = true;
   path = "lib/csu";
   extraPaths = [
     "lib/Makefile.inc"
     "lib/libc/include/libc_private.h"
   ];
   nativeBuildInputs = [
-    bsdSetupHook freebsdSetupHook
+    bsdSetupHook
+    freebsdSetupHook
     makeMinimal
     install
 
-    flex byacc gencat
+    flex
+    byacc
+    gencat
   ];
   buildInputs = [ include ];
   MK_TESTS = "no";
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/ctfconvert.nix b/pkgs/os-specific/bsd/freebsd/pkgs/ctfconvert.nix
index 0aebc9b3d0b93..9e653bb4f51ef 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/ctfconvert.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/ctfconvert.nix
@@ -1,7 +1,10 @@
-{ lib, stdenv, mkDerivation
-, bsdSetupHook, freebsdSetupHook
-, makeMinimal, install, mandoc, groff
-, compatIfNeeded, libelf, libdwarf, zlib, libspl
+{
+  lib,
+  mkDerivation,
+  compatIfNeeded,
+  libdwarf,
+  zlib,
+  libspl,
 }:
 
 mkDerivation {
@@ -15,14 +18,18 @@ mkDerivation {
   ];
   OPENSOLARIS_USR_DISTDIR = "$(SRCTOP)/cddl/contrib/opensolaris";
   OPENSOLARIS_SYS_DISTDIR = "$(SRCTOP)/sys/cddl/contrib/opensolaris";
-  nativeBuildInputs = [
-    bsdSetupHook freebsdSetupHook
-    makeMinimal install mandoc groff
 
-    # flex byacc file2c
+  makeFlags = [
+    "STRIP=-s"
+    "MK_WERROR=no"
+    "MK_TESTS=no"
   ];
+
   buildInputs = compatIfNeeded ++ [
-    libelf libdwarf zlib libspl
+    libdwarf
+    zlib
+    libspl
   ];
+
   meta.license = lib.licenses.cddl;
 }
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/filterSource.nix b/pkgs/os-specific/bsd/freebsd/pkgs/filterSource.nix
new file mode 100644
index 0000000000000..09674056d9cd8
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/filterSource.nix
@@ -0,0 +1,34 @@
+{
+  lib,
+  pkgsBuildBuild,
+  runCommand,
+  writeText,
+  source,
+}:
+
+{
+  pname,
+  path,
+  extraPaths ? [ ],
+}:
+
+let
+  sortedPaths = lib.naturalSort ([ path ] ++ extraPaths);
+  filterText = writeText "${pname}-src-include" (
+    lib.concatMapStringsSep "\n" (path: "/${path}") sortedPaths
+  );
+in
+runCommand "${pname}-filtered-src"
+  {
+    nativeBuildInputs = [
+      (pkgsBuildBuild.rsync.override {
+        enableZstd = false;
+        enableXXHash = false;
+        enableOpenSSL = false;
+        enableLZ4 = false;
+      })
+    ];
+  }
+  ''
+    rsync -a -r --files-from=${filterText} ${source}/ $out
+  ''
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/freebsdSetupHook/package.nix b/pkgs/os-specific/bsd/freebsd/pkgs/freebsdSetupHook/package.nix
index 430c4c5c43ac1..ef4a14ccf4aa9 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/freebsdSetupHook/package.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/freebsdSetupHook/package.nix
@@ -1,5 +1,3 @@
 { makeSetupHook }:
 
-makeSetupHook {
-  name = "freebsd-setup-hook";
-} ./setup-hook.sh
+makeSetupHook { name = "freebsd-setup-hook"; } ./setup-hook.sh
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/freebsdSetupHook/setup-hook.sh b/pkgs/os-specific/bsd/freebsd/pkgs/freebsdSetupHook/setup-hook.sh
index 929782954ba77..96a3d14c80f7b 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/freebsdSetupHook/setup-hook.sh
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/freebsdSetupHook/setup-hook.sh
@@ -5,7 +5,18 @@ setFreeBSDSrcTop() {
 addFreeBSDMakeFlags() {
   makeFlags="SBINDIR=${!outputBin}/bin $makeFlags"
   makeFlags="LIBEXECDIR=${!outputLib}/libexec $makeFlags"
+  makeFlags="LIBDATADIR=${!outputLib}/data $makeFlags"
   makeFlags="INCLUDEDIR=${!outputDev}/include $makeFlags"
+  makeFlags="CONFDIR=${!outputBin}/etc $makeFlags"
+  makeFlags="MANDIR=${!outputMan}/share/man/man $makeFlags"
+
+  if [ -n "$debug" ]; then
+    makeFlags="DEBUGFILEDIR=${debug}/lib/debug $makeFlags"
+  else
+    makeFlags="DEBUGFILEDIR=${out}/lib/debug $makeFlags"
+  fi
+
+  echo $makeFlags
 }
 
 postUnpackHooks+=(setFreeBSDSrcTop)
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/gencat.nix b/pkgs/os-specific/bsd/freebsd/pkgs/gencat.nix
index e9ae9f27cc05b..ea701b6404ecc 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/gencat.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/gencat.nix
@@ -1,5 +1,3 @@
 { mkDerivation }:
 
-mkDerivation {
-  path = "usr.bin/gencat";
-}
+mkDerivation { path = "usr.bin/gencat"; }
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/iconv.nix b/pkgs/os-specific/bsd/freebsd/pkgs/iconv.nix
new file mode 100644
index 0000000000000..9c1ba1835c145
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/iconv.nix
@@ -0,0 +1,12 @@
+{
+  mkDerivation,
+  libcapsicum,
+  libcasper,
+}:
+mkDerivation {
+  path = "usr.bin/iconv";
+  buildInputs = [
+    libcapsicum
+    libcasper
+  ];
+}
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/include/package.nix b/pkgs/os-specific/bsd/freebsd/pkgs/include/package.nix
index 73fa887c51231..a7ceb0cdf384f 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/include/package.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/include/package.nix
@@ -1,12 +1,13 @@
-{ lib, mkDerivation
-, buildPackages
-, bsdSetupHook, freebsdSetupHook
-, makeMinimal
-, install
-, mandoc, groff, rsync /*, nbperf*/, rpcgen
+{
+  lib,
+  mkDerivation,
+  buildPackages,
+  rpcgen,
+  mtree,
 }:
 
 mkDerivation {
+  noLibc = true;
   path = "include";
 
   extraPaths = [
@@ -15,18 +16,9 @@ mkDerivation {
     "sys"
   ];
 
-  nativeBuildInputs =  [
-    bsdSetupHook freebsdSetupHook
-    makeMinimal
-    install
-    mandoc groff rsync /*nbperf*/ rpcgen
-
-    # HACK use NetBSD's for now
-    buildPackages.netbsd.mtree
-  ];
-
-  patches = [
-    ./no-perms-BSD.include.dist.patch
+  extraNativeBuildInputs = [
+    rpcgen
+    mtree
   ];
 
   # The makefiles define INCSDIR per subdirectory, so we have to set
@@ -37,11 +29,10 @@ mkDerivation {
       sed -i -E \
         -e 's_/usr/include_''${INCSDIR0}_' \
         {} \;
+    sed -E -i -e "/_PATH_LOGIN/d" $BSDSRCDIR/include/paths.h
   '';
 
-  makeFlags = [
-    "RPCGEN_CPP=${buildPackages.stdenv.cc.cc}/bin/cpp"
-  ];
+  makeFlags = [ "RPCGEN_CPP=${buildPackages.stdenv.cc.cc}/bin/cpp" ];
 
   # multiple header dirs, see above
   postConfigure = ''
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/install.nix b/pkgs/os-specific/bsd/freebsd/pkgs/install.nix
index 3db6cd3633d49..b8e59adb09c5f 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/install.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/install.nix
@@ -1,41 +1,67 @@
-{ lib, stdenv, mkDerivation, writeShellScript
-, freebsd-lib
-, mtree
-, bsdSetupHook, freebsdSetupHook
-, makeMinimal, mandoc, groff
-, boot-install, install
-, compatIfNeeded, libmd, libnetbsd
+{
+  lib,
+  stdenv,
+  mkDerivation,
+  writeShellScript,
+  freebsd-lib,
+  bsdSetupHook,
+  freebsdSetupHook,
+  makeMinimal,
+  mandoc,
+  groff,
+  boot-install,
+  install,
+  compatIfNeeded,
+  libmd,
+  libnetbsd,
 }:
 
 # HACK: to ensure parent directories exist. This emulates GNU
 # install’s -D option. No alternative seems to exist in BSD install.
 let
-  binstall = writeShellScript "binstall" (freebsd-lib.install-wrapper + ''
+  binstall = writeShellScript "binstall" (
+    freebsd-lib.install-wrapper
+    + ''
 
-    @out@/bin/xinstall "''${args[@]}"
-  '');
-in mkDerivation {
+      @out@/bin/xinstall "''${args[@]}"
+    ''
+  );
+in
+mkDerivation {
   path = "usr.bin/xinstall";
-  extraPaths = [ mtree.path ];
+  extraPaths = [ "contrib/mtree" ];
   nativeBuildInputs = [
-    bsdSetupHook freebsdSetupHook
-    makeMinimal mandoc groff
-    (if stdenv.hostPlatform == stdenv.buildPlatform
-     then boot-install
-     else install)
+    bsdSetupHook
+    freebsdSetupHook
+    makeMinimal
+    mandoc
+    groff
+    (if stdenv.hostPlatform == stdenv.buildPlatform then boot-install else install)
   ];
   skipIncludesPhase = true;
-  buildInputs = compatIfNeeded ++ [ libmd libnetbsd ];
-  makeFlags = [
-    "STRIP=-s" # flag to install, not command
-    "MK_WERROR=no"
-    "TESTSDIR=${builtins.placeholder "test"}"
-  ] ++ lib.optional (stdenv.hostPlatform == stdenv.buildPlatform) "INSTALL=boot-install";
+  buildInputs = compatIfNeeded ++ [
+    libmd
+    libnetbsd
+  ];
+  makeFlags =
+    [
+      "STRIP=-s" # flag to install, not command
+      "MK_WERROR=no"
+      "TESTSDIR=${builtins.placeholder "test"}"
+    ]
+    ++ lib.optionals (stdenv.hostPlatform == stdenv.buildPlatform) [
+      "BOOTSTRAPPING=1"
+      "INSTALL=boot-install"
+    ];
   postInstall = ''
-    install -D -m 0550 ${binstall} $out/bin/binstall
+    install -C -m 0550 ${binstall} $out/bin/binstall
     substituteInPlace $out/bin/binstall --subst-var out
     mv $out/bin/install $out/bin/xinstall
     ln -s ./binstall $out/bin/install
   '';
-  outputs = [ "out" "man" "test" ];
+  outputs = [
+    "out"
+    "man"
+    "test"
+  ];
 }
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/ldd.nix b/pkgs/os-specific/bsd/freebsd/pkgs/ldd.nix
new file mode 100644
index 0000000000000..406e37402b2bb
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/ldd.nix
@@ -0,0 +1,21 @@
+{
+  lib,
+  stdenv,
+  mkDerivation,
+  libelf,
+}:
+mkDerivation {
+  path = "usr.bin/ldd";
+  extraPaths = [
+    "libexec/rtld-elf"
+    "contrib/elftoolchain/libelf"
+  ];
+
+  buildInputs = [ libelf ];
+
+  env = {
+    NIX_CFLAGS_COMPILE = "-D_RTLD_PATH=${lib.getLib stdenv.cc.libc}/libexec/ld-elf.so.1";
+  };
+
+  meta.platforms = lib.platforms.freebsd;
+}
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/libc/package.nix b/pkgs/os-specific/bsd/freebsd/pkgs/libc/package.nix
index 0225d44be4c3b..a991e69265823 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/libc/package.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/libc/package.nix
@@ -1,79 +1,145 @@
-{ lib, stdenv, mkDerivation
+{
+  lib,
+  buildPackages,
+  stdenv,
+  mkDerivation,
 
-, bsdSetupHook, freebsdSetupHook
-, makeMinimal
-, install
-, flex, byacc, gencat, rpcgen
+  bsdSetupHook,
+  freebsdSetupHook,
+  makeMinimal,
+  install,
+  flex,
+  byacc,
+  gencat,
+  rpcgen,
+  mkcsmapper,
+  mkesdb,
 
-, csu, include
+  csu,
+  include,
+  versionData,
 }:
 
-mkDerivation rec {
+mkDerivation {
+  noLibc = true;
   pname = "libc";
   path = "lib/libc";
-  extraPaths = [
-    "etc/group"
-    "etc/master.passwd"
-    "etc/shells"
-    "lib/libmd"
-    "lib/libutil"
-    "lib/msun"
-    "sys/kern"
-    "sys/libkern"
-    "sys/sys"
-    "sys/crypto/chacha20"
-    "include/rpcsvc"
-    "contrib/jemalloc"
-    "contrib/gdtoa"
-    "contrib/libc-pwcache"
-    "contrib/libc-vis"
-    "contrib/tzcode/stdtime"
-
-    # libthr
-    "lib/libthr"
-    "lib/libthread_db"
-    "libexec/rtld-elf"
-
-    # librpcsvc
-    "lib/librpcsvc"
-
-    # librt
-    "lib/librt"
-
-    # libcrypt
-    "lib/libcrypt"
-    "lib/libmd"
-    "sys/crypto/sha2"
-  ];
+  extraPaths =
+    [
+      "lib/libc_nonshared"
+      "etc/group"
+      "etc/master.passwd"
+      "etc/shells"
+      "lib/libmd"
+      "lib/libutil"
+      "lib/msun"
+      "sys/kern"
+      "sys/libkern"
+      "sys/sys"
+      "sys/crypto/chacha20"
+      "include/rpcsvc"
+      "contrib/jemalloc"
+      "contrib/gdtoa"
+      "contrib/libc-pwcache"
+      "contrib/libc-vis"
+    ]
+    ++ lib.optionals (versionData.major == 13) [ "contrib/tzcode/stdtime" ]
+    ++ lib.optionals (versionData.major == 14) [ "contrib/tzcode" ]
+    ++ [
 
-  patches = [
-    # Hack around broken propogating MAKEFLAGS to submake, just inline logic
-    ./libc-msun-arch-subdir.patch
+      # libthr
+      "lib/libthr"
+      "lib/libthread_db"
+      "libexec/rtld-elf"
+      "lib/csu/common/crtbrand.S"
+      "lib/csu/common/notes.h"
 
-    # Don't force -lcompiler-rt, we don't actually call it that
-    ./libc-no-force--lcompiler-rt.patch
+      # librpcsvc
+      "lib/librpcsvc"
 
-    # Fix extra include dir to get rpcsvc headers.
-    ./librpcsvc-include-subdir.patch
-  ];
+      # librt
+      "lib/librt"
+
+      # libcrypt
+      "lib/libcrypt"
+      "lib/libmd"
+      "sys/crypto/sha2"
+      "sys/crypto/skein"
+
+      # libgcc and friends
+      "lib/libgcc_eh"
+      "lib/libgcc_s"
+      "lib/libcompiler_rt"
+      "contrib/llvm-project/libunwind"
+      "contrib/llvm-project/compiler-rt"
+      #"contrib/llvm-project/libcxx"
+
+      # terminfo
+      "lib/ncurses"
+      "contrib/ncurses"
+      "lib/Makefile.inc"
+    ]
+    ++ lib.optionals (stdenv.hostPlatform.isx86_32) [ "lib/libssp_nonshared" ]
+    ++ [
+      "lib/libexecinfo"
+      "contrib/libexecinfo"
+
+      "lib/libkvm"
+      "sys" # ummmmmmmmmm libkvm wants arch-specific headers from the kernel tree
+
+      "lib/libmemstat"
+
+      "lib/libprocstat"
+      "sys/contrib/openzfs"
+      "sys/contrib/pcg-c"
+      "sys/opencrypto"
+      "sys/contrib/ck"
+      "sys/crypto"
+
+      "lib/libdevstat"
+
+      "lib/libelf"
+      "contrib/elftoolchain"
+
+      "lib/libiconv_modules"
+      "share/i18n"
+      "include/paths.h"
+
+      "lib/libdl"
+    ];
 
   postPatch = ''
     substituteInPlace $COMPONENT_PATH/Makefile --replace '.include <src.opts.mk>' ""
+
+    substituteInPlace $BSDSRCDIR/include/paths.h \
+        --replace '/usr/lib/i18n' '${builtins.placeholder "out"}/lib/i18n' \
+        --replace '/usr/share/i18n' '${builtins.placeholder "out"}/share/i18n'
   '';
 
   nativeBuildInputs = [
-    bsdSetupHook freebsdSetupHook
+    bsdSetupHook
+    freebsdSetupHook
     makeMinimal
     install
 
-    flex byacc gencat rpcgen
+    flex
+    byacc
+    gencat
+    rpcgen
+    mkcsmapper
+    mkesdb
+  ];
+  buildInputs = [
+    include
+    csu
+  ];
+  env.NIX_CFLAGS_COMPILE = toString [
+    "-B${csu}/lib"
+    # These are supposed to have _RTLD_COMPAT_LIB_SUFFIX so we can get things like "lib32"
+    # but that's unnecessary
+    "-DSTANDARD_LIBRARY_PATH=\"${builtins.placeholder "out"}/lib\""
+    "-D_PATH_RTLD=\"${builtins.placeholder "out"}/libexec/ld-elf.so.1\""
   ];
-  buildInputs = [ include csu ];
-  env.NIX_CFLAGS_COMPILE = "-B${csu}/lib";
-
-  # Suppress lld >= 16 undefined version errors
-  # https://github.com/freebsd/freebsd-src/commit/2ba84b4bcdd6012e8cfbf8a0d060a4438623a638
-  env.NIX_LDFLAGS = lib.optionalString (stdenv.targetPlatform.linker == "lld") "--undefined-version";
 
   makeFlags = [
     "STRIP=-s" # flag to install, not command
@@ -84,7 +150,7 @@ mkDerivation rec {
   MK_SYMVER = "yes";
   MK_SSP = "yes";
   MK_NLS = "yes";
-  MK_ICONV = "no"; # TODO make srctop
+  MK_ICONV = "yes";
   MK_NS_CACHING = "yes";
   MK_INET6_SUPPORT = "yes";
   MK_HESIOD = "yes";
@@ -96,44 +162,125 @@ mkDerivation rec {
   MK_MALLOC_PRODUCTION = "yes";
 
   MK_TESTS = "no";
+  MACHINE_ABI = "";
+  MK_DETECT_TZ_CHANGES = "no";
+  MK_MACHDEP_OPTIMIZATIONS = "yes";
+  MK_ASAN = "no";
+  MK_UBSAN = "no";
+
+  NO_FSCHG = "yes";
+
+  preBuild = lib.optionalString (stdenv.hostPlatform.isx86_32) ''
+    make -C $BSDSRCDIR/lib/libssp_nonshared $makeFlags
+    make -C $BSDSRCDIR/lib/libssp_nonshared $makeFlags install
+  '';
+
+  postInstall =
+    ''
+      pushd ${include}
+      find . -type d -exec mkdir -p $out/\{} \;
+      find . \( -type f -o -type l \) -exec cp -pr \{} $out/\{} \;
+      popd
+
+      pushd ${csu}
+      find . -type d -exec mkdir -p $out/\{} \;
+      find . \( -type f -o -type l \) -exec cp -pr \{} $out/\{} \;
+      popd
+
+      mkdir $BSDSRCDIR/lib/libcompiler_rt/i386
+      make -C $BSDSRCDIR/lib/libcompiler_rt $makeFlags
+      make -C $BSDSRCDIR/lib/libcompiler_rt $makeFlags install
+
+      make -C $BSDSRCDIR/lib/libgcc_eh $makeFlags
+      make -C $BSDSRCDIR/lib/libgcc_eh $makeFlags install
+
+      ln -s $BSDSRCDIR/lib/libc/libc.so.7 $BSDSRCDIR/lib/libc/libc.so  # not sure
+      mkdir $BSDSRCDIR/lib/libgcc_s/i386
+      make -C $BSDSRCDIR/lib/libgcc_s $makeFlags
+      make -C $BSDSRCDIR/lib/libgcc_s $makeFlags install
+
+      NIX_CFLAGS_COMPILE+=" -B$out/lib"
+      NIX_CFLAGS_COMPILE+=" -I$out/include"
+      NIX_LDFLAGS+=" -L$out/lib"
+
+      make -C $BSDSRCDIR/lib/libc_nonshared $makeFlags
+      make -C $BSDSRCDIR/lib/libc_nonshared $makeFlags install
+
+      mkdir $BSDSRCDIR/lib/libmd/sys
+      make -C $BSDSRCDIR/lib/libmd $makeFlags
+      make -C $BSDSRCDIR/lib/libmd $makeFlags install
 
-  postInstall = ''
-    pushd ${include}
-    find . -type d -exec mkdir -p $out/\{} \;
-    find . \( -type f -o -type l \) -exec cp -pr \{} $out/\{} \;
-    popd
+      make -C $BSDSRCDIR/lib/libthr $makeFlags
+      make -C $BSDSRCDIR/lib/libthr $makeFlags install
 
-    pushd ${csu}
-    find . -type d -exec mkdir -p $out/\{} \;
-    find . \( -type f -o -type l \) -exec cp -pr \{} $out/\{} \;
-    popd
+      make -C $BSDSRCDIR/lib/msun $makeFlags
+      make -C $BSDSRCDIR/lib/msun $makeFlags install
 
-    sed -i -e 's| [^ ]*/libc_nonshared.a||' $out/lib/libc.so
+      make -C $BSDSRCDIR/lib/librpcsvc $makeFlags
+      make -C $BSDSRCDIR/lib/librpcsvc $makeFlags install
 
-    $CC -nodefaultlibs -lgcc -shared -o $out/lib/libgcc_s.so
+      make -C $BSDSRCDIR/lib/libutil $makeFlags
+      make -C $BSDSRCDIR/lib/libutil $makeFlags install
 
-    NIX_CFLAGS_COMPILE+=" -B$out/lib"
-    NIX_CFLAGS_COMPILE+=" -I$out/include"
-    NIX_LDFLAGS+=" -L$out/lib"
+      make -C $BSDSRCDIR/lib/librt $makeFlags
+      make -C $BSDSRCDIR/lib/librt $makeFlags install
 
-    make -C $BSDSRCDIR/lib/libthr $makeFlags
-    make -C $BSDSRCDIR/lib/libthr $makeFlags install
+      make -C $BSDSRCDIR/lib/libcrypt $makeFlags
+      make -C $BSDSRCDIR/lib/libcrypt $makeFlags install
 
-    make -C $BSDSRCDIR/lib/msun $makeFlags
-    make -C $BSDSRCDIR/lib/msun $makeFlags install
+      make -C $BSDSRCDIR/lib/libelf $makeFlags
+      make -C $BSDSRCDIR/lib/libelf $makeFlags install
 
-    make -C $BSDSRCDIR/lib/librpcsvc $makeFlags
-    make -C $BSDSRCDIR/lib/librpcsvc $makeFlags install
+      make -C $BSDSRCDIR/lib/libexecinfo $makeFlags
+      make -C $BSDSRCDIR/lib/libexecinfo $makeFlags install
 
-    make -C $BSDSRCDIR/lib/libutil $makeFlags
-    make -C $BSDSRCDIR/lib/libutil $makeFlags install
+      make -C $BSDSRCDIR/lib/libkvm $makeFlags
+      make -C $BSDSRCDIR/lib/libkvm $makeFlags install
 
-    make -C $BSDSRCDIR/lib/librt $makeFlags
-    make -C $BSDSRCDIR/lib/librt $makeFlags install
+      make -C $BSDSRCDIR/lib/libmemstat $makeFlags
+      make -C $BSDSRCDIR/lib/libmemstat $makeFlags install
 
-    make -C $BSDSRCDIR/lib/libcrypt $makeFlags
-    make -C $BSDSRCDIR/lib/libcrypt $makeFlags install
+      make -C $BSDSRCDIR/lib/libprocstat $makeFlags
+      make -C $BSDSRCDIR/lib/libprocstat $makeFlags install
+
+      make -C $BSDSRCDIR/lib/libdevstat $makeFlags
+      make -C $BSDSRCDIR/lib/libdevstat $makeFlags install
+
+      make -C $BSDSRCDIR/lib/libiconv_modules $makeFlags
+      make -C $BSDSRCDIR/lib/libiconv_modules $makeFlags SHLIBDIR=${builtins.placeholder "out"}/lib/i18n install
+
+      make -C $BSDSRCDIR/lib/libdl $makeFlags
+      make -C $BSDSRCDIR/lib/libdl $makeFlags install
+
+      make -C $BSDSRCDIR/share/i18n $makeFlags
+      make -C $BSDSRCDIR/share/i18n $makeFlags ESDBDIR=${builtins.placeholder "out"}/share/i18n/esdb CSMAPPERDIR=${builtins.placeholder "out"}/share/i18n/csmapper install
+
+    ''
+    + lib.optionalString stdenv.hostPlatform.isx86_32 ''
+      $CC -c $BSDSRCDIR/contrib/llvm-project/compiler-rt/lib/builtins/udivdi3.c -o $BSDSRCDIR/contrib/llvm-project/compiler-rt/lib/builtins/udivdi3.o
+      ORIG_NIX_LDFLAGS="$NIX_LDFLAGS"
+      NIX_LDFLAGS+=" $BSDSRCDIR/contrib/llvm-project/compiler-rt/lib/builtins/udivdi3.o"
+    ''
+    + ''
+      make -C $BSDSRCDIR/libexec/rtld-elf $makeFlags
+      make -C $BSDSRCDIR/libexec/rtld-elf $makeFlags install
+      rm -f $out/libexec/ld-elf.so.1
+      mv $out/bin/ld-elf.so.1 $out/libexec
+    '';
+
+  # libc should not be allowed to refer to anything other than itself
+  postFixup = ''
+    find $out -type f | xargs -n1 ${buildPackages.patchelf}/bin/patchelf --shrink-rpath --allowed-rpath-prefixes $out || true
   '';
 
   meta.platforms = lib.platforms.freebsd;
+
+  # definitely a bad idea to enable stack protection on the stack protection initializers
+  hardeningDisable = [ "stackprotector" ];
+
+  outputs = [
+    "out"
+    "man"
+    "debug"
+  ];
 }
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/libcapsicum.nix b/pkgs/os-specific/bsd/freebsd/pkgs/libcapsicum.nix
new file mode 100644
index 0000000000000..3cb1560df77aa
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/libcapsicum.nix
@@ -0,0 +1 @@
+{ mkDerivation }: mkDerivation { path = "lib/libcapsicum"; }
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/libcasper.nix b/pkgs/os-specific/bsd/freebsd/pkgs/libcasper.nix
new file mode 100644
index 0000000000000..df27f72bc8cb0
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/libcasper.nix
@@ -0,0 +1,26 @@
+{
+  lib,
+  stdenv,
+  mkDerivation,
+  libnv,
+}:
+mkDerivation {
+  path = "lib/libcasper/libcasper";
+  extraPaths = [
+    "lib/Makefile.inc"
+    "lib/libcasper"
+  ];
+  buildInputs = [ libnv ];
+
+  MK_TESTS = "no";
+
+  makeFlags = [
+    "STRIP=-s" # flag to install, not command
+    "CFLAGS=-DWITH_CASPER"
+  ] ++ lib.optional (!stdenv.hostPlatform.isFreeBSD) "MK_WERROR=no";
+
+  postInstall = ''
+    make -C $BSDSRCDIR/lib/libcasper/services $makeFlags CFLAGS="-DWITH_CASPER -I$out/include"
+    make -C $BSDSRCDIR/lib/libcasper/services $makeFlags install
+  '';
+}
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/libcxxrt.nix b/pkgs/os-specific/bsd/freebsd/pkgs/libcxxrt.nix
new file mode 100644
index 0000000000000..fd5591c0ffb8a
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/libcxxrt.nix
@@ -0,0 +1,18 @@
+{ mkDerivation }:
+# this package is quite different from stock libcxxrt.
+# as of FreeBSD 14.0, it is vendored from APPROXIMATELY libcxxrt
+# 5d8a15823a103bbc27f1bfdcf2b5aa008fab57dd, though the vendoring mechanism is
+# extremely ad-hoc. Moreover, the build mechanism is totally custom, and adds
+# symbol versions not specified on any version of libcxxrt.
+mkDerivation {
+  pname = "libcxxrt";
+  path = "lib/libcxxrt";
+  extraPaths = [ "contrib/libcxxrt" ];
+  outputs = [
+    "out"
+    "dev"
+    "debug"
+  ];
+  noLibcxx = true;
+  libName = "cxxrt";
+}
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/libdl.nix b/pkgs/os-specific/bsd/freebsd/pkgs/libdl.nix
new file mode 100644
index 0000000000000..2b77a0f716624
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/libdl.nix
@@ -0,0 +1,9 @@
+{ mkDerivation, ... }:
+mkDerivation {
+  path = "lib/libdl";
+  extraPaths = [
+    "lib/libc"
+    "libexec/rtld-elf"
+  ];
+  buildInputs = [ ];
+}
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/libdwarf.nix b/pkgs/os-specific/bsd/freebsd/pkgs/libdwarf.nix
index 9fc8fc5f9a624..ee4d57b021b1d 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/libdwarf.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/libdwarf.nix
@@ -1,8 +1,8 @@
-{ lib, stdenv, mkDerivation
-, bsdSetupHook, freebsdSetupHook
-, makeMinimal, install, mandoc, groff
-, m4
-, compatIfNeeded, libelf
+{
+  mkDerivation,
+  m4,
+  compatIfNeeded,
+  zlib,
 }:
 
 mkDerivation {
@@ -14,14 +14,7 @@ mkDerivation {
     "sys/sys/elf64.h"
     "sys/sys/elf_common.h"
   ];
-  nativeBuildInputs = [
-    bsdSetupHook freebsdSetupHook
-    makeMinimal install mandoc groff
-
-    m4
-  ];
-  buildInputs = compatIfNeeded ++ [
-    libelf
-  ];
+  extraNativeBuildInputs = [ m4 ];
+  buildInputs = compatIfNeeded ++ [ zlib ];
   MK_TESTS = "no";
 }
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/libedit.nix b/pkgs/os-specific/bsd/freebsd/pkgs/libedit.nix
new file mode 100644
index 0000000000000..50b1c9f58333f
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/libedit.nix
@@ -0,0 +1,7 @@
+{ mkDerivation, libncurses-tinfo }:
+mkDerivation {
+  path = "lib/libedit";
+  extraPaths = [ "contrib/libedit" ];
+  buildInputs = [ libncurses-tinfo ];
+  MK_TESTS = "no";
+}
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/libelf.nix b/pkgs/os-specific/bsd/freebsd/pkgs/libelf.nix
index a44ce1685e57d..a116aff81f397 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/libelf.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/libelf.nix
@@ -1,24 +1,29 @@
-{ lib, stdenv, mkDerivation
-, bsdSetupHook, freebsdSetupHook
-, makeMinimal, install, mandoc, groff
-, m4
+{
+  mkDerivation,
+  lib,
+  bsdSetupHook,
+  freebsdSetupHook,
+  makeMinimal,
+  install,
+  m4,
 }:
-
 mkDerivation {
   path = "lib/libelf";
   extraPaths = [
-    "contrib/elftoolchain/libelf"
-    "contrib/elftoolchain/common"
+    "lib/libc"
+    "contrib/elftoolchain"
     "sys/sys/elf32.h"
     "sys/sys/elf64.h"
     "sys/sys/elf_common.h"
   ];
-  BOOTSTRAPPING = !stdenv.isFreeBSD;
+  buildInputs = [ ];
   nativeBuildInputs = [
-    bsdSetupHook freebsdSetupHook
-    makeMinimal install mandoc groff
-
+    bsdSetupHook
+    freebsdSetupHook
+    makeMinimal
+    install
     m4
   ];
-  MK_TESTS = "no";
+
+  meta.platforms = lib.platforms.freebsd;
 }
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/libjail.nix b/pkgs/os-specific/bsd/freebsd/pkgs/libjail.nix
new file mode 100644
index 0000000000000..9150be559c650
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/libjail.nix
@@ -0,0 +1,5 @@
+{ mkDerivation }:
+mkDerivation {
+  path = "lib/libjail";
+  MK_TESTS = "no";
+}
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/libmd.nix b/pkgs/os-specific/bsd/freebsd/pkgs/libmd.nix
new file mode 100644
index 0000000000000..71d0c1e50d5ea
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/libmd.nix
@@ -0,0 +1,49 @@
+{
+  lib,
+  stdenv,
+  mkDerivation,
+  freebsdSetupHook,
+  bsdSetupHook,
+  makeMinimal,
+}:
+mkDerivation {
+  path = "lib/libmd";
+  extraPaths = [
+    "sys/sys/md5.h"
+    "sys/crypto/sha2"
+    "sys/crypto/skein"
+  ];
+  nativeBuildInputs = [
+    makeMinimal
+    bsdSetupHook
+    freebsdSetupHook
+  ];
+
+  makeFlags = [
+    "STRIP=-s" # flag to install, not command
+    "RELDIR=."
+  ] ++ lib.optional (!stdenv.hostPlatform.isFreeBSD) "MK_WERROR=no";
+
+  preBuild = ''
+    mkdir sys
+  '';
+
+  installPhase = ''
+    # libmd is used by install. do it yourself!
+    mkdir -p $out/include $out/lib $man/share/man
+    cp libmd.a $out/lib/libmd.a
+    for f in $(make $makeFlags -V INCS); do
+      if [ -e "$f" ]; then cp "$f" "$out/include/$f"; fi
+      if [ -e "$BSDSRCDIR/sys/crypto/sha2/$f" ]; then cp "$BSDSRCDIR/sys/crypto/sha2/$f" "$out/include/$f"; fi
+      if [ -e "$BSDSRCDIR/sys/crypto/skein/$f" ]; then cp "$BSDSRCDIR/sys/crypto/skein/$f" "$out/include/$f"; fi
+    done
+    for f in $(make $makeFlags -V MAN); do
+      cp "$f" "$man/share/man/$f"
+    done
+  '';
+
+  outputs = [
+    "out"
+    "man"
+  ];
+}
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/libncurses-tinfo.nix b/pkgs/os-specific/bsd/freebsd/pkgs/libncurses-tinfo.nix
new file mode 100644
index 0000000000000..f86f78dbc2914
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/libncurses-tinfo.nix
@@ -0,0 +1,15 @@
+{ mkDerivation, pkgsBuildBuild }:
+mkDerivation {
+  path = "lib/ncurses/tinfo";
+  extraPaths = [
+    "lib/ncurses"
+    "contrib/ncurses"
+    "lib/Makefile.inc"
+  ];
+  CC_HOST = "${pkgsBuildBuild.stdenv.cc}/bin/cc";
+  MK_TESTS = "no";
+  preBuild = ''
+    export NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE -D_VA_LIST -D_VA_LIST_DECLARED -Dva_list=__builtin_va_list -D_SIZE_T -D_WCHAR_T"
+    make $makeFlags "CFLAGS=-D_VA_LIST -D_VA_LIST_DECLARED -Dva_list=__builtin_va_list -I$BSDSRCDIR/contrib/ncurses/ncurses -I$BSDSRCDIR/contrib/ncurses/include -I." ncurses_dll.h make_hash make_keys
+  '';
+}
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/libncurses.nix b/pkgs/os-specific/bsd/freebsd/pkgs/libncurses.nix
new file mode 100644
index 0000000000000..2dd1ee2fe57de
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/libncurses.nix
@@ -0,0 +1,25 @@
+{
+  lib,
+  versionData,
+  mkDerivation,
+  libncurses-tinfo,
+  ...
+}:
+mkDerivation {
+  path = "lib/ncurses/ncurses";
+  extraPaths = [
+    "lib/ncurses"
+    "contrib/ncurses"
+    "lib/Makefile.inc"
+  ];
+  MK_TESTS = "no";
+  preBuild = lib.optionalString (versionData.major == 14) ''
+    make -C ../tinfo $makeFlags curses.h ncurses_dll.h ncurses_def.h
+  '';
+  buildInputs = lib.optionals (versionData.major == 14) [ libncurses-tinfo ];
+
+  # some packages depend on libncursesw.so.8
+  postInstall = ''
+    ln -s $out/lib/libncursesw.so.9 $out/lib/libncursesw.so.8
+  '';
+}
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/libnetbsd/package.nix b/pkgs/os-specific/bsd/freebsd/pkgs/libnetbsd/package.nix
index 4011e4d8a6490..82a9e140102f6 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/libnetbsd/package.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/libnetbsd/package.nix
@@ -1,26 +1,28 @@
-{ lib, stdenv
-, mkDerivation
-, bsdSetupHook, freebsdSetupHook, makeMinimal, mandoc, groff
-, boot-install, install
-, compatIfNeeded
+{
+  lib,
+  stdenv,
+  mkDerivation,
+  bsdSetupHook,
+  freebsdSetupHook,
+  makeMinimal,
+  mandoc,
+  groff,
+  boot-install,
+  install,
 }:
 
 mkDerivation {
   path = "lib/libnetbsd";
   nativeBuildInputs = [
-    bsdSetupHook freebsdSetupHook
-    makeMinimal mandoc groff
-    (if stdenv.hostPlatform == stdenv.buildPlatform
-     then boot-install
-     else install)
-  ];
-  patches = lib.optionals (!stdenv.hostPlatform.isFreeBSD) [
-    ./libnetbsd-do-install.patch
-    #./libnetbsd-define-__va_list.patch
+    bsdSetupHook
+    freebsdSetupHook
+    makeMinimal
+    mandoc
+    groff
+    (if stdenv.hostPlatform == stdenv.buildPlatform then boot-install else install)
   ];
   makeFlags = [
     "STRIP=-s" # flag to install, not command
     "MK_WERROR=no"
   ] ++ lib.optional (stdenv.hostPlatform == stdenv.buildPlatform) "INSTALL=boot-install";
-  buildInputs = compatIfNeeded;
 }
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/libsbuf.nix b/pkgs/os-specific/bsd/freebsd/pkgs/libsbuf.nix
index 719474dbb11ac..242492a3f2f23 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/libsbuf.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/libsbuf.nix
@@ -2,8 +2,6 @@
 
 mkDerivation {
   path = "lib/libsbuf";
-  extraPaths = [
-    "sys/kern"
-  ];
+  extraPaths = [ "sys/kern" ];
   MK_TESTS = "no";
 }
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/libspl.nix b/pkgs/os-specific/bsd/freebsd/pkgs/libspl.nix
index da5445a09fcda..c444975549ad6 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/libspl.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/libspl.nix
@@ -3,19 +3,20 @@
 mkDerivation {
   path = "cddl/lib/libspl";
   extraPaths = [
-    "sys/contrib/openzfs/lib/libspl"
-    "sys/contrib/openzfs/include"
-
     "cddl/compat/opensolaris/include"
+    "sys/contrib/openzfs/include"
+    "sys/contrib/openzfs/lib/libspl"
     "sys/contrib/openzfs/module/icp/include"
-    "sys/modules/zfs"
+    "sys/modules/zfs/zfs_config.h"
   ];
-  # nativeBuildInputs = [
-  #   bsdSetupHook freebsdSetupHook
-  #   makeMinimal install mandoc groff
 
-  #   flex byacc file2c
-  # ];
-  # buildInputs = compatIfNeeded ++ [ libnv libsbuf ];
-  meta.license = lib.licenses.cddl;
+  # Without a prefix it will try to put object files in nonexistant directories
+  preBuild = ''
+    export MAKEOBJDIRPREFIX=$TMP/obj
+  '';
+
+  meta = with lib; {
+    platform = platforms.freebsd;
+    license = licenses.cddl;
+  };
 }
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/libutil.nix b/pkgs/os-specific/bsd/freebsd/pkgs/libutil.nix
index c420d0daf8520..6df6fa740f45e 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/libutil.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/libutil.nix
@@ -1,7 +1,10 @@
-{ mkDerivation, lib, stdenv }:
+{
+  mkDerivation,
+  lib,
+  stdenv,
+}:
 mkDerivation {
   path = "lib/libutil";
-  extraPaths = ["lib/libc/gen"];
-  clangFixup = true;
+  extraPaths = [ "lib/libc/gen" ];
   MK_TESTS = "no";
 }
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/libxo.nix b/pkgs/os-specific/bsd/freebsd/pkgs/libxo.nix
new file mode 100644
index 0000000000000..a4882ea1970c3
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/libxo.nix
@@ -0,0 +1,6 @@
+{ mkDerivation }:
+mkDerivation {
+  path = "lib/libxo";
+  extraPaths = [ "contrib/libxo" ];
+  MK_TESTS = "no";
+}
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/locale.nix b/pkgs/os-specific/bsd/freebsd/pkgs/locale.nix
new file mode 100644
index 0000000000000..b661a7cee853f
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/locale.nix
@@ -0,0 +1,7 @@
+{ mkDerivation, libsbuf }:
+mkDerivation {
+  path = "usr.bin/locale";
+  buildInputs = [ libsbuf ];
+  extraPaths = [ "lib/libc/locale" ];
+  MK_TESTS = "no";
+}
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/localedef.nix b/pkgs/os-specific/bsd/freebsd/pkgs/localedef.nix
new file mode 100644
index 0000000000000..2e4902860c8d9
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/localedef.nix
@@ -0,0 +1,39 @@
+{
+  mkDerivation,
+  lib,
+  stdenv,
+  compat,
+  bsdSetupHook,
+  byacc,
+  freebsdSetupHook,
+  makeMinimal,
+  install,
+}:
+mkDerivation (
+  {
+    path = "usr.bin/localedef";
+
+    extraPaths = [
+      "lib/libc/locale"
+      "lib/libc/stdtime"
+    ] ++ lib.optionals (!stdenv.hostPlatform.isFreeBSD) [ "." ];
+
+    nativeBuildInputs = [
+      bsdSetupHook
+      byacc
+      freebsdSetupHook
+      makeMinimal
+      install
+    ];
+
+    buildInputs = [ ];
+
+    preBuild = lib.optionalString (!stdenv.hostPlatform.isFreeBSD) ''
+      export NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE -I${compat}/include -D__unused= -D__pure= -Wno-strict-aliasing"
+      export NIX_LDFLAGS="$NIX_LDFLAGS -L${compat}/lib"
+    '';
+
+    MK_TESTS = "no";
+  }
+  // lib.optionalAttrs (!stdenv.hostPlatform.isFreeBSD) { BOOTSTRAPPING = 1; }
+)
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/locales.nix b/pkgs/os-specific/bsd/freebsd/pkgs/locales.nix
new file mode 100644
index 0000000000000..d8d3de93d5ec7
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/locales.nix
@@ -0,0 +1,51 @@
+{
+  mkDerivation,
+  lib,
+  symlinkJoin,
+  bsdSetupHook,
+  freebsdSetupHook,
+  makeMinimal,
+  install,
+  tsort,
+  lorder,
+  mandoc,
+  groff,
+  localedef,
+  allLocales ? true,
+  locales ? [ "en_US.UTF-8" ],
+}:
+let
+  build =
+    name: needsLocaledef:
+    mkDerivation {
+      path = "share/${name}";
+
+      extraPaths = lib.optional needsLocaledef "tools/tools/locale/etc/final-maps";
+      nativeBuildInputs = [
+        bsdSetupHook
+        freebsdSetupHook
+        makeMinimal
+        install
+        tsort
+        lorder
+        mandoc
+        groff
+      ] ++ lib.optional needsLocaledef localedef;
+    };
+  directories = {
+    colldef = true;
+    colldef_unicode = true;
+    ctypedef = true;
+    monetdef = false;
+    monetdef_unicode = false;
+    msgdef = false;
+    msgdef_unicode = false;
+    numericdef = false;
+    numericdef_unicode = false;
+    timedef = false;
+  };
+in
+symlinkJoin {
+  name = "freebsd-locales";
+  paths = lib.mapAttrsToList build directories;
+}
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/lorder.nix b/pkgs/os-specific/bsd/freebsd/pkgs/lorder.nix
index d1fd86ab5e463..25e7f491a1c94 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/lorder.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/lorder.nix
@@ -1,8 +1,9 @@
-{ mkDerivation
-, bsdSetupHook, freebsdSetupHook
+{
+  mkDerivation,
+  bsdSetupHook,
+  freebsdSetupHook,
 }:
-
-mkDerivation rec {
+mkDerivation {
   path = "usr.bin/lorder";
   noCC = true;
   dontBuild = true;
@@ -13,8 +14,12 @@ mkDerivation rec {
     mv "lorder.1" "$man/share/man"
   '';
   nativeBuildInputs = [
-    bsdSetupHook freebsdSetupHook
+    bsdSetupHook
+    freebsdSetupHook
+  ];
+  buildInputs = [ ];
+  outputs = [
+    "out"
+    "man"
   ];
-  buildInputs = [];
-  outputs = [ "out" "man" ];
 }
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/make.nix b/pkgs/os-specific/bsd/freebsd/pkgs/make.nix
index fa1722cfec22c..ecf231c304145 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/make.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/make.nix
@@ -1,20 +1,25 @@
-{ lib, mkDerivation, stdenv }:
+{
+  lib,
+  mkDerivation,
+  stdenv,
+}:
 
 mkDerivation {
   path = "contrib/bmake";
   version = "9.2";
-  postPatch = ''
-    # make needs this to pick up our sys make files
-    export NIX_CFLAGS_COMPILE+=" -D_PATH_DEFSYSPATH=\"$out/share/mk\""
+  postPatch =
+    ''
+      # make needs this to pick up our sys make files
+      export NIX_CFLAGS_COMPILE+=" -D_PATH_DEFSYSPATH=\"$out/share/mk\""
 
-  '' + lib.optionalString stdenv.isDarwin ''
-    substituteInPlace $BSDSRCDIR/share/mk/bsd.sys.mk \
-      --replace '-Wl,--fatal-warnings' "" \
-      --replace '-Wl,--warn-shared-textrel' ""
-  '';
+    ''
+    + lib.optionalString stdenv.isDarwin ''
+      substituteInPlace $BSDSRCDIR/share/mk/bsd.sys.mk \
+        --replace '-Wl,--fatal-warnings' "" \
+        --replace '-Wl,--warn-shared-textrel' ""
+    '';
   postInstall = ''
     make -C $BSDSRCDIR/share/mk FILESDIR=$out/share/mk install
   '';
-  extraPaths = [ "share/mk" ]
-    ++ lib.optional (!stdenv.hostPlatform.isFreeBSD) "tools/build/mk";
+  extraPaths = [ "share/mk" ] ++ lib.optional (!stdenv.hostPlatform.isFreeBSD) "tools/build/mk";
 }
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/makeMinimal.nix b/pkgs/os-specific/bsd/freebsd/pkgs/makeMinimal.nix
index e6a8e38a4acec..069e74474852a 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/makeMinimal.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/makeMinimal.nix
@@ -1,25 +1,32 @@
-{ lib, stdenv, mkDerivation
-, make
-, bsdSetupHook, freebsdSetupHook
+{
+  lib,
+  stdenv,
+  mkDerivation,
+  make,
+  bsdSetupHook,
+  freebsdSetupHook,
 }:
 
-mkDerivation rec {
+mkDerivation {
   inherit (make) path;
 
-  buildInputs = [];
+  buildInputs = [ ];
   nativeBuildInputs = [
-    bsdSetupHook freebsdSetupHook
+    bsdSetupHook
+    freebsdSetupHook
   ];
 
   skipIncludesPhase = true;
 
-  makeFlags = [];
+  makeFlags = [ ];
 
   postPatch = ''
     patchShebangs configure
     ${make.postPatch}
   '';
 
+  configureFlags = [ "--with-filemon=no" ];
+
   buildPhase = ''
     runHook preBuild
 
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/mkDerivation.nix b/pkgs/os-specific/bsd/freebsd/pkgs/mkDerivation.nix
index 79428626b8ec2..a085367ac68e6 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/mkDerivation.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/mkDerivation.nix
@@ -1,78 +1,177 @@
-{ lib, stdenv, stdenvNoCC
-, compatIfNeeded
-, runCommand, rsync
-, freebsd-lib
-, source
-, bsdSetupHook, freebsdSetupHook
-, makeMinimal
-, install, tsort, lorder, mandoc, groff
+{
+  lib,
+  stdenv,
+  stdenvNoCC,
+  stdenvNoLibs,
+  overrideCC,
+  buildPackages,
+  versionData,
+  writeText,
+  patches,
+  compatIfNeeded,
+  freebsd-lib,
+  filterSource,
+  bsdSetupHook,
+  freebsdSetupHook,
+  makeMinimal,
+  install,
+  tsort,
+  lorder,
+  mandoc,
+  groff,
 }:
 
-lib.makeOverridable (attrs: let
-  stdenv' = if attrs.noCC or false then stdenvNoCC else stdenv;
-in stdenv'.mkDerivation (rec {
-  pname = "${attrs.pname or (baseNameOf attrs.path)}-freebsd";
-  inherit (freebsd-lib) version;
-  src = runCommand "${pname}-filtered-src" {
-    nativeBuildInputs = [ rsync ];
-  } ''
-    for p in ${lib.concatStringsSep " " ([ attrs.path ] ++ attrs.extraPaths or [])}; do
-      set -x
-      path="$out/$p"
-      mkdir -p "$(dirname "$path")"
-      src_path="${source}/$p"
-      if [[ -d "$src_path" ]]; then src_path+=/; fi
-      rsync --chmod="+w" -r "$src_path" "$path"
-      set +x
-    done
-  '';
+lib.makeOverridable (
+  attrs:
+  let
+    stdenv' =
+      if attrs.noCC or false then
+        stdenvNoCC
+      else if attrs.noLibc or false then
+        stdenvNoLibs
+      else if attrs.noLibcxx or false then
+        overrideCC stdenv buildPackages.llvmPackages.clangNoLibcxx
+      else
+        stdenv;
+  in
+  stdenv'.mkDerivation (
+    rec {
+      inherit (freebsd-lib) version;
+      pname = "${attrs.pname or (baseNameOf attrs.path)}";
+      src = filterSource {
+        inherit pname;
+        inherit (attrs) path;
+        extraPaths = attrs.extraPaths or [ ];
+      };
 
-  extraPaths = [ ];
+      nativeBuildInputs = [
+        bsdSetupHook
+        freebsdSetupHook
+        makeMinimal
+        install
+        tsort
+        lorder
+        mandoc
+        groff
+      ] ++ attrs.extraNativeBuildInputs or [ ];
+      buildInputs = compatIfNeeded;
 
-  nativeBuildInputs = [
-    bsdSetupHook freebsdSetupHook
-    makeMinimal
-    install tsort lorder mandoc groff #statHook
-  ];
-  buildInputs = compatIfNeeded;
+      HOST_SH = stdenv'.shell;
 
-  HOST_SH = stdenv'.shell;
+      makeFlags = [
+        "STRIP=-s" # flag to install, not command
+      ] ++ lib.optional (!stdenv'.hostPlatform.isFreeBSD) "MK_WERROR=no";
 
-  # Since STRIP below is the flag
-  STRIPBIN = "${stdenv.cc.bintools.targetPrefix}strip";
+      # amd64 not x86_64 for this on unlike NetBSD
+      MACHINE_ARCH = freebsd-lib.mkBsdArch stdenv';
 
-  makeFlags = [
-    "STRIP=-s" # flag to install, not command
-  ] ++ lib.optional (!stdenv.hostPlatform.isFreeBSD) "MK_WERROR=no";
+      MACHINE = freebsd-lib.mkBsdArch stdenv';
 
-  # amd64 not x86_64 for this on unlike NetBSD
-  MACHINE_ARCH = freebsd-lib.mkBsdArch stdenv';
+      MACHINE_CPUARCH = MACHINE_ARCH;
 
-  MACHINE = freebsd-lib.mkBsdArch stdenv';
+      COMPONENT_PATH = attrs.path or null;
 
-  MACHINE_CPUARCH = MACHINE_ARCH;
+      strictDeps = true;
 
-  COMPONENT_PATH = attrs.path or null;
+      meta =
+        with lib;
+        {
+          maintainers = with maintainers; [
+            rhelmot
+            artemist
+          ];
+          platforms = platforms.unix;
+          license = licenses.bsd2;
+        }
+        // attrs.meta or { };
+    }
+    // lib.optionalAttrs stdenv'.hasCC {
+      # TODO should CC wrapper set this?
+      CPP = "${stdenv'.cc.targetPrefix}cpp";
 
-  strictDeps = true;
-
-  meta = with lib; {
-    maintainers = with maintainers; [ ericson2314 ];
-    platforms = platforms.unix;
-    license = licenses.bsd2;
-  };
-} // lib.optionalAttrs stdenv'.hasCC {
-  # TODO should CC wrapper set this?
-  CPP = "${stdenv'.cc.targetPrefix}cpp";
-} // lib.optionalAttrs stdenv'.isDarwin {
-  MKRELRO = "no";
-} // lib.optionalAttrs (stdenv'.cc.isClang or false) {
-  HAVE_LLVM = lib.versions.major (lib.getVersion stdenv'.cc.cc);
-} // lib.optionalAttrs (stdenv'.cc.isGNU or false) {
-  HAVE_GCC = lib.versions.major (lib.getVersion stdenv'.cc.cc);
-} // lib.optionalAttrs (stdenv'.isx86_32) {
-  USE_SSP = "no";
-} // lib.optionalAttrs (attrs.headersOnly or false) {
-  installPhase = "includesPhase";
-  dontBuild = true;
-} // attrs))
+      # Since STRIP in `makeFlags` has to be a flag, not the binary itself
+      STRIPBIN = "${stdenv'.cc.bintools.targetPrefix}strip";
+    }
+    // lib.optionalAttrs stdenv'.isDarwin { MKRELRO = "no"; }
+    // lib.optionalAttrs (stdenv'.cc.isClang or false) {
+      HAVE_LLVM = lib.versions.major (lib.getVersion stdenv'.cc.cc);
+    }
+    // lib.optionalAttrs (stdenv'.cc.isGNU or false) {
+      HAVE_GCC = lib.versions.major (lib.getVersion stdenv'.cc.cc);
+    }
+    // lib.optionalAttrs (stdenv'.isx86_32) { USE_SSP = "no"; }
+    // lib.optionalAttrs (attrs.headersOnly or false) {
+      installPhase = "includesPhase";
+      dontBuild = true;
+    }
+    // attrs
+    // lib.optionalAttrs (stdenv'.hasCC && stdenv'.cc.isClang or false && attrs.clangFixup or true) {
+      preBuild =
+        ''
+          export NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE -D_VA_LIST -D_VA_LIST_DECLARED -Dva_list=__builtin_va_list -D_SIZE_T_DECLARED -D_SIZE_T -Dsize_t=__SIZE_TYPE__ -D_WCHAR_T"
+        ''
+        + lib.optionalString (versionData.major == 13) ''
+          export NIX_LDFLAGS="$NIX_LDFLAGS --undefined-version"
+        ''
+        + (attrs.preBuild or "");
+    }
+    // {
+      patches =
+        let
+          isDir =
+            file:
+            let
+              base = baseNameOf file;
+              type = (builtins.readDir (dirOf file)).${base} or null;
+            in
+            file == /. || type == "directory";
+          consolidatePatches =
+            patches:
+            if (lib.isDerivation patches) then
+              [ patches ]
+            else if (builtins.isPath patches) then
+              (if (isDir patches) then (lib.filesystem.listFilesRecursive patches) else [ patches ])
+            else if (builtins.isList patches) then
+              (lib.flatten (builtins.map consolidatePatches patches))
+            else
+              throw "Bad patches - must be path or derivation or list thereof";
+          consolidated = consolidatePatches patches;
+          splitPatch =
+            patchFile:
+            let
+              allLines' = lib.strings.splitString "\n" (builtins.readFile patchFile);
+              allLines = builtins.filter (
+                line: !((lib.strings.hasPrefix "diff --git" line) || (lib.strings.hasPrefix "index " line))
+              ) allLines';
+              foldFunc =
+                a: b:
+                if ((lib.strings.hasPrefix "--- " b) || (lib.strings.hasPrefix "diff --git " b)) then
+                  (a ++ [ [ b ] ])
+                else
+                  ((lib.lists.init a) ++ (lib.lists.singleton ((lib.lists.last a) ++ [ b ])));
+              partitionedPatches' = lib.lists.foldl foldFunc [ [ ] ] allLines;
+              partitionedPatches =
+                if (builtins.length partitionedPatches' > 1) then
+                  (lib.lists.drop 1 partitionedPatches')
+                else
+                  (throw "${patchFile} does not seem to be a unified patch (diff -u). this is required for FreeBSD.");
+              filterFunc =
+                patchLines:
+                let
+                  prefixedPath = builtins.elemAt (builtins.split " |\t" (builtins.elemAt patchLines 1)) 2;
+                  unfixedPath = lib.path.subpath.join (lib.lists.drop 1 (lib.path.subpath.components prefixedPath));
+                in
+                lib.lists.any (included: lib.path.hasPrefix (/. + ("/" + included)) (/. + ("/" + unfixedPath))) (
+                  (attrs.extraPaths or [ ]) ++ [ attrs.path ]
+                );
+              filteredLines = builtins.filter filterFunc partitionedPatches;
+              derive = patchLines: writeText "freebsd-patch" (lib.concatLines patchLines);
+              derivedPatches = builtins.map derive filteredLines;
+            in
+            derivedPatches;
+          picked = lib.lists.concatMap splitPatch consolidated;
+        in
+        picked ++ attrs.patches or [ ];
+    }
+  )
+)
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/mkcsmapper.nix b/pkgs/os-specific/bsd/freebsd/pkgs/mkcsmapper.nix
new file mode 100644
index 0000000000000..60cef347446ec
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/mkcsmapper.nix
@@ -0,0 +1,22 @@
+{
+  stdenv,
+  mkDerivation,
+  byacc,
+  flex,
+}:
+
+mkDerivation {
+  path = "usr.bin/mkcsmapper";
+
+  extraPaths = [
+    "lib/libc/iconv"
+    "lib/libiconv_modules/mapper_std"
+  ];
+
+  BOOTSTRAPPING = !stdenv.hostPlatform.isFreeBSD;
+
+  extraNativeBuildInputs = [
+    byacc
+    flex
+  ];
+}
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/mkesdb.nix b/pkgs/os-specific/bsd/freebsd/pkgs/mkesdb.nix
new file mode 100644
index 0000000000000..a503af529f274
--- /dev/null
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/mkesdb.nix
@@ -0,0 +1,19 @@
+{
+  stdenv,
+  mkDerivation,
+  byacc,
+  flex,
+}:
+
+mkDerivation {
+  path = "usr.bin/mkesdb";
+
+  extraPaths = [ "lib/libc/iconv" ];
+
+  BOOTSTRAPPING = !stdenv.hostPlatform.isFreeBSD;
+
+  extraNativeBuildInputs = [
+    byacc
+    flex
+  ];
+}
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/mknod.nix b/pkgs/os-specific/bsd/freebsd/pkgs/mknod.nix
index 56dff7d606bd9..bf7d8b7db689b 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/mknod.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/mknod.nix
@@ -1,5 +1,3 @@
 { mkDerivation }:
 
-mkDerivation {
-  path = "sbin/mknod";
-}
+mkDerivation { path = "sbin/mknod"; }
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/mtree.nix b/pkgs/os-specific/bsd/freebsd/pkgs/mtree.nix
index 23a4672069e6a..bbcaff3a1c6a8 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/mtree.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/mtree.nix
@@ -1,6 +1,39 @@
-{ mkDerivation, mknod }:
+{
+  lib,
+  stdenv,
+  mkDerivation,
+  compatIfNeeded,
+  compatIsNeeded,
+  libmd,
+  libnetbsd,
+  libutil,
+}:
 
 mkDerivation {
   path = "contrib/mtree";
-  extraPaths = [ mknod.path ];
+  extraPaths = [ "contrib/mknod" ];
+  buildInputs =
+    compatIfNeeded
+    ++ [
+      libmd
+      libnetbsd
+    ]
+    ++ lib.optional (stdenv.isFreeBSD) libutil;
+
+  postPatch = ''
+    ln -s $BSDSRCDIR/contrib/mknod/*.c $BSDSRCDIR/contrib/mknod/*.h $BSDSRCDIR/contrib/mtree
+  '';
+
+  preBuild = ''
+    export NIX_LDFLAGS="$NIX_LDFLAGS ${
+      toString (
+        [
+          "-lmd"
+          "-lnetbsd"
+        ]
+        ++ lib.optional compatIsNeeded "-legacy"
+        ++ lib.optional stdenv.isFreeBSD "-lutil"
+      )
+    }"
+  '';
 }
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/rpcgen/package.nix b/pkgs/os-specific/bsd/freebsd/pkgs/rpcgen/package.nix
index 56141255af5e1..e187cacbb0d05 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/rpcgen/package.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/rpcgen/package.nix
@@ -1,6 +1,11 @@
-{ lib, mkDerivation, stdenv }:
+{
+  lib,
+  mkDerivation,
+  stdenv,
+  patchesRoot,
+}:
 
-mkDerivation rec {
+mkDerivation {
   path = "usr.bin/rpcgen";
   patches = lib.optionals (stdenv.hostPlatform.libc == "glibc") [
     # `WUNTRACED` is defined privately `bits/waitflags.h` in glibc.
@@ -12,7 +17,7 @@ mkDerivation rec {
     # those headers ends up included other headers...which ends up
     # including the other one, this means by the first time we reach
     # `#include `<bits/waitflags.h>`, both `_SYS_WAIT_H` and
-    # `_STDLIB_H` are already defined! Thus, we never ned up including
+    # `_STDLIB_H` are already defined! Thus, we never end up including
     # `<bits/waitflags.h>` and defining `WUNTRACED`.
     #
     # This hacks around this by manually including `WUNTRACED` until
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/sed.nix b/pkgs/os-specific/bsd/freebsd/pkgs/sed.nix
index 07b55afe133fc..ec5bfee17d2c1 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/sed.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/sed.nix
@@ -1,7 +1,6 @@
-{ mkDerivation, source }:
+{ mkDerivation }:
 
 mkDerivation {
   path = "usr.bin/sed";
-  TESTSRC = "${source}/contrib/netbsd-tests";
   MK_TESTS = "no";
 }
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/source.nix b/pkgs/os-specific/bsd/freebsd/pkgs/source.nix
index 5e31f900e8216..c14d26559047b 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/source.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/source.nix
@@ -4,7 +4,6 @@
 # If you want you could fetchgit from "https://git.FreeBSD.org/src.git" instead.
 # The update script still pulls directly from git.freebsd.org
 fetchFromGitHub {
-  name = "src"; # Want to rename this next rebuild
   owner = "freebsd";
   repo = "freebsd-src";
   inherit (sourceData) rev hash;
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/stat.nix b/pkgs/os-specific/bsd/freebsd/pkgs/stat.nix
index c730f00869f8e..a801ab895441c 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/stat.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/stat.nix
@@ -1,13 +1,22 @@
-{ mkDerivation
-, bsdSetupHook, freebsdSetupHook
-, makeMinimal, install, mandoc, groff
+{
+  mkDerivation,
+  bsdSetupHook,
+  freebsdSetupHook,
+  makeMinimal,
+  install,
+  mandoc,
+  groff,
 }:
 
 # Don't add this to nativeBuildInputs directly.  Use statHook instead.
 mkDerivation {
   path = "usr.bin/stat";
   nativeBuildInputs = [
-    bsdSetupHook freebsdSetupHook
-    makeMinimal install mandoc groff
+    bsdSetupHook
+    freebsdSetupHook
+    makeMinimal
+    install
+    mandoc
+    groff
   ];
 }
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/statHook.nix b/pkgs/os-specific/bsd/freebsd/pkgs/statHook.nix
index 07129938b0953..4609c004e6f51 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/statHook.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/statHook.nix
@@ -1,12 +1,16 @@
-{ makeSetupHook, writeText, stat }:
+{
+  makeSetupHook,
+  writeText,
+  stat,
+}:
 
 # stat isn't in POSIX, and NetBSD stat supports a completely
 # different range of flags than GNU stat, so including it in PATH
 # breaks stdenv.  Work around that with a hook that will point
 # NetBSD's build system and NetBSD stat without including it in
 # PATH.
-makeSetupHook {
-  name = "netbsd-stat-hook";
-} (writeText "netbsd-stat-hook-impl" ''
-  makeFlagsArray+=(TOOL_STAT=${stat}/bin/stat)
-'')
+makeSetupHook { name = "netbsd-stat-hook"; } (
+  writeText "netbsd-stat-hook-impl" ''
+    makeFlagsArray+=(TOOL_STAT=${stat}/bin/stat)
+  ''
+)
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/sys/package.nix b/pkgs/os-specific/bsd/freebsd/pkgs/sys/package.nix
index 81cf4114e8737..86f847cbd45cf 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/sys/package.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/sys/package.nix
@@ -1,67 +1,85 @@
-{ lib, stdenv, mkDerivation, freebsd-lib
-, buildPackages
-, bsdSetupHook, freebsdSetupHook
-, makeMinimal, install, mandoc, groff
-, config, rpcgen, file2c, gawk, uudecode, xargs-j #, ctfconvert
+{
+  stdenv,
+  mkDerivation,
+  freebsd-lib,
+  buildPackages,
+  bsdSetupHook,
+  freebsdSetupHook,
+  makeMinimal,
+  install,
+  mandoc,
+  groff,
+  config,
+  rpcgen,
+  file2c,
+  gawk,
+  uudecode,
+  xargs-j,
 }:
 
-mkDerivation (let
-  cfg = "MINIMAL";
-in rec {
-  path = "sys";
-
-  nativeBuildInputs = [
-    bsdSetupHook freebsdSetupHook
-    makeMinimal install mandoc groff
-
-    config rpcgen file2c gawk uudecode xargs-j
-    #ctfconvert
-  ];
-
-  patches = [
-    ./sys-gnu-date.patch
-    ./sys-no-explicit-intrinsics-dep.patch
-  ];
-
-  # --dynamic-linker /red/herring is used when building the kernel.
-  NIX_ENFORCE_PURITY = 0;
-
-  AWK = "${buildPackages.gawk}/bin/awk";
-
-  CWARNEXTRA = "-Wno-error=shift-negative-value -Wno-address-of-packed-member";
-
-  MK_CTF = "no";
-
-  KODIR = "${builtins.placeholder "out"}/kernel";
-  KMODDIR = "${builtins.placeholder "out"}/kernel";
-  DTBDIR = "${builtins.placeholder"out"}/dbt";
-
-  KERN_DEBUGDIR = "${builtins.placeholder "out"}/debug";
-  KERN_DEBUGDIR_KODIR = "${KERN_DEBUGDIR}/kernel";
-  KERN_DEBUGDIR_KMODDIR = "${KERN_DEBUGDIR}/kernel";
-
-  skipIncludesPhase = true;
-
-  configurePhase = ''
-    runHook preConfigure
-
-    for f in conf/kmod.mk contrib/dev/acpica/acpica_prep.sh; do
-      substituteInPlace "$f" --replace 'xargs -J' 'xargs-j '
-    done
-
-    for f in conf/*.mk; do
-      substituteInPlace "$f" --replace 'KERN_DEBUGDIR}''${' 'KERN_DEBUGDIR_'
-    done
-
-    cd ${freebsd-lib.mkBsdArch stdenv}/conf
-    sed -i ${cfg} \
-      -e 's/WITH_CTF=1/WITH_CTF=0/' \
-      -e '/KDTRACE/d'
-    config ${cfg}
-
-    runHook postConfigure
-  '';
-  preBuild = ''
-    cd ../compile/${cfg}
-  '';
-})
+mkDerivation (
+  let
+    cfg = "MINIMAL";
+  in
+  rec {
+    path = "sys";
+
+    nativeBuildInputs = [
+      bsdSetupHook
+      freebsdSetupHook
+      makeMinimal
+      install
+      mandoc
+      groff
+
+      config
+      rpcgen
+      file2c
+      gawk
+      uudecode
+      xargs-j
+    ];
+
+    # --dynamic-linker /red/herring is used when building the kernel.
+    NIX_ENFORCE_PURITY = 0;
+
+    AWK = "${buildPackages.gawk}/bin/awk";
+
+    CWARNEXTRA = "-Wno-error=shift-negative-value -Wno-address-of-packed-member";
+
+    MK_CTF = "no";
+
+    KODIR = "${builtins.placeholder "out"}/kernel";
+    KMODDIR = "${builtins.placeholder "out"}/kernel";
+    DTBDIR = "${builtins.placeholder "out"}/dbt";
+
+    KERN_DEBUGDIR = "${builtins.placeholder "out"}/debug";
+    KERN_DEBUGDIR_KODIR = "${KERN_DEBUGDIR}/kernel";
+    KERN_DEBUGDIR_KMODDIR = "${KERN_DEBUGDIR}/kernel";
+
+    skipIncludesPhase = true;
+
+    configurePhase = ''
+      runHook preConfigure
+
+      for f in conf/kmod.mk contrib/dev/acpica/acpica_prep.sh; do
+        substituteInPlace "$f" --replace 'xargs -J' 'xargs-j '
+      done
+
+      for f in conf/*.mk; do
+        substituteInPlace "$f" --replace 'KERN_DEBUGDIR}''${' 'KERN_DEBUGDIR_'
+      done
+
+      cd ${freebsd-lib.mkBsdArch stdenv}/conf
+      sed -i ${cfg} \
+        -e 's/WITH_CTF=1/WITH_CTF=0/' \
+        -e '/KDTRACE/d'
+      config ${cfg}
+
+      runHook postConfigure
+    '';
+    preBuild = ''
+      cd ../compile/${cfg}
+    '';
+  }
+)
diff --git a/pkgs/os-specific/bsd/freebsd/pkgs/tsort.nix b/pkgs/os-specific/bsd/freebsd/pkgs/tsort.nix
index cdc1b27ce8fed..04a45ff980cba 100644
--- a/pkgs/os-specific/bsd/freebsd/pkgs/tsort.nix
+++ b/pkgs/os-specific/bsd/freebsd/pkgs/tsort.nix
@@ -1,12 +1,28 @@
-{ mkDerivation
-, bsdSetupHook, freebsdSetupHook
-, makeMinimal, install, mandoc, groff
+{
+  lib,
+  mkDerivation,
+  bsdSetupHook,
+  freebsdSetupHook,
+  makeMinimal,
+  install,
+  mandoc,
+  groff,
 }:
 
 mkDerivation {
   path = "usr.bin/tsort";
-  nativeBuildInputs =  [
-    bsdSetupHook freebsdSetupHook
-    makeMinimal install mandoc groff
+  extraPaths = [ ];
+  outputs = [ "out" ];
+  MK_TESTS = "no";
+  makeFlags = [
+    "STRIP=-s" # flag to install, not command
+  ];
+  nativeBuildInputs = [
+    bsdSetupHook
+    freebsdSetupHook
+    makeMinimal
+    install
+    mandoc
+    groff
   ];
 }
diff --git a/pkgs/os-specific/bsd/freebsd/update.py b/pkgs/os-specific/bsd/freebsd/update.py
index cd20f67148fa7..533a871a4b04d 100755
--- a/pkgs/os-specific/bsd/freebsd/update.py
+++ b/pkgs/os-specific/bsd/freebsd/update.py
@@ -16,6 +16,7 @@ import typing
 import urllib.request
 
 _QUERY_VERSION_PATTERN = re.compile('^([A-Z]+)="(.+)"$')
+_RELEASE_PATCH_PATTERN = re.compile('^RELEASE-p([0-9]+)$')
 BASE_DIR = os.path.dirname(os.path.abspath(__file__))
 MIN_VERSION = packaging.version.Version("13.0.0")
 MAIN_BRANCH = "main"
@@ -60,7 +61,16 @@ def query_version(repo: git.Repo) -> dict[str, typing.Any]:
             continue
         fields[m[1].lower()] = m[2]
 
-    fields["major"] = packaging.version.parse(fields["revision"]).major
+    parsed = packaging.version.parse(fields["revision"])
+    fields["major"] = parsed.major
+    fields["minor"] = parsed.minor
+
+    # Extract the patch number from `RELAESE-p<patch>`, which is used
+    # e.g. in the "releng" branches.
+    m = _RELEASE_PATCH_PATTERN.match(fields["branch"])
+    if m is not None:
+        fields["patch"] = m[1]
+
     return fields
 
 
@@ -95,7 +105,7 @@ def handle_commit(
         "ref": ref_name,
         "refType": ref_type,
         "supported": ref_name in supported_refs,
-        "version": query_version(repo),
+        "version": version,
     }
 
 
@@ -151,6 +161,14 @@ def main() -> None:
         result = handle_commit(
             repo, tag.commit, tag.name, "tag", supported_refs, old_versions
         )
+
+        # Hack in the patch version from parsing the tag, if we didn't
+        # get one from the "branch" field (from newvers). This is
+        # probably 0.
+        versionObj = result["version"]
+        if "patch" not in versionObj:
+            versionObj["patch"] = version.micro
+
         versions[tag.name] = result
 
     for branch in repo.remote("origin").refs:
diff --git a/pkgs/os-specific/bsd/freebsd/versions.json b/pkgs/os-specific/bsd/freebsd/versions.json
index 736c4c3a3e3c1..3f781b4eeaf23 100644
--- a/pkgs/os-specific/bsd/freebsd/versions.json
+++ b/pkgs/os-specific/bsd/freebsd/versions.json
@@ -1,14 +1,15 @@
 {
   "main": {
-    "hash": "sha256-C5ucT9BK/eK8a9HNSDDi8S1uhpPmiqV22XEooxAqbPw=",
+    "hash": "sha256-3aUsD2yRqVvb12z2XPmhE5/u4d9bqyD2ZHH3xNmwYwU=",
     "ref": "main",
     "refType": "branch",
-    "rev": "125c4560bc70971b950d035cfcd2255b89984011",
+    "rev": "aa34b1d20e44141749ffdecf16908fc1e5db4db6",
     "supported": false,
     "version": {
       "branch": "CURRENT",
       "major": 15,
-      "reldate": "1500017",
+      "minor": 0,
+      "reldate": "1500018",
       "release": "15.0-CURRENT",
       "revision": "15.0",
       "type": "FreeBSD",
@@ -24,6 +25,8 @@
     "version": {
       "branch": "RELEASE",
       "major": 13,
+      "minor": 0,
+      "patch": 0,
       "reldate": "1300139",
       "release": "13.0-RELEASE",
       "revision": "13.0",
@@ -40,6 +43,8 @@
     "version": {
       "branch": "RELEASE",
       "major": 13,
+      "minor": 1,
+      "patch": 0,
       "reldate": "1301000",
       "release": "13.1-RELEASE",
       "revision": "13.1",
@@ -56,6 +61,8 @@
     "version": {
       "branch": "RELEASE",
       "major": 13,
+      "minor": 2,
+      "patch": 0,
       "reldate": "1302001",
       "release": "13.2-RELEASE",
       "revision": "13.2",
@@ -72,6 +79,8 @@
     "version": {
       "branch": "RELEASE",
       "major": 13,
+      "minor": 3,
+      "patch": 0,
       "reldate": "1303001",
       "release": "13.3-RELEASE",
       "revision": "13.3",
@@ -88,6 +97,8 @@
     "version": {
       "branch": "RELEASE",
       "major": 14,
+      "minor": 0,
+      "patch": 0,
       "reldate": "1400097",
       "release": "14.0-RELEASE",
       "revision": "14.0",
@@ -104,6 +115,8 @@
     "version": {
       "branch": "RELEASE-p13",
       "major": 13,
+      "minor": 0,
+      "patch": "13",
       "reldate": "1300139",
       "release": "13.0-RELEASE-p13",
       "revision": "13.0",
@@ -120,6 +133,8 @@
     "version": {
       "branch": "RELEASE-p9",
       "major": 13,
+      "minor": 1,
+      "patch": "9",
       "reldate": "1301000",
       "release": "13.1-RELEASE-p9",
       "revision": "13.1",
@@ -136,6 +151,8 @@
     "version": {
       "branch": "RELEASE-p11",
       "major": 13,
+      "minor": 2,
+      "patch": "11",
       "reldate": "1302001",
       "release": "13.2-RELEASE-p11",
       "revision": "13.2",
@@ -144,19 +161,21 @@
     }
   },
   "releng/13.3": {
-    "hash": "sha256-huzUiMZHfyK/mgLD3hW+DaSGgAaTUIuM51xDp+IE3qE=",
+    "hash": "sha256-g3i9q9XihesdfQxGy3oC7IMGtbWaLNwFlNzbdvS/4ng=",
     "ref": "releng/13.3",
     "refType": "branch",
-    "rev": "7a0d63c9093222938f26cd63ff742e555168de77",
+    "rev": "be4f1894ef399f421bab451e8cf8557e27e5a948",
     "supported": true,
     "version": {
-      "branch": "RELEASE-p1",
+      "branch": "RELEASE-p2",
       "major": 13,
+      "minor": 3,
+      "patch": "2",
       "reldate": "1303001",
-      "release": "13.3-RELEASE-p1",
+      "release": "13.3-RELEASE-p2",
       "revision": "13.3",
       "type": "FreeBSD",
-      "version": "FreeBSD 13.3-RELEASE-p1"
+      "version": "FreeBSD 13.3-RELEASE-p2"
     }
   },
   "releng/14.0": {
@@ -168,6 +187,8 @@
     "version": {
       "branch": "RELEASE-p6",
       "major": 14,
+      "minor": 0,
+      "patch": "6",
       "reldate": "1400097",
       "release": "14.0-RELEASE-p6",
       "revision": "14.0",
@@ -176,15 +197,16 @@
     }
   },
   "stable/13": {
-    "hash": "sha256-XateLKKs2A/HCP9Lx/nBm1cybB3otrbeXQvyCL40S0M=",
+    "hash": "sha256-ItC8haDdxMSZt1thpCrn8p0xxvs7Uqh/uNo1OwMalj8=",
     "ref": "stable/13",
     "refType": "branch",
-    "rev": "e0a58ef24a3baf5ed4cc09a798b9fe2d85408052",
+    "rev": "825cb4c850f2b97cfd1b24ed421d7938bf37eee7",
     "supported": true,
     "version": {
       "branch": "STABLE",
       "major": 13,
-      "reldate": "1303502",
+      "minor": 3,
+      "reldate": "1303503",
       "release": "13.3-STABLE",
       "revision": "13.3",
       "type": "FreeBSD",
@@ -192,19 +214,20 @@
     }
   },
   "stable/14": {
-    "hash": "sha256-tIKnK/SYBDk9UnE5AfhjeDpqHnzspYbor0678ye/mrs=",
+    "hash": "sha256-iAj75IXJi4Oium6BqFvsyQipDP2crBZIGg0Dac8Zf1g=",
     "ref": "stable/14",
     "refType": "branch",
-    "rev": "ab872ab0bf195e872ed8d955aab3b2a537a230cd",
+    "rev": "a3b8266f5420601e231bc08c5402d9a4929fbdc0",
     "supported": true,
     "version": {
-      "branch": "STABLE",
+      "branch": "PRERELEASE",
       "major": 14,
-      "reldate": "1400510",
-      "release": "14.0-STABLE",
-      "revision": "14.0",
+      "minor": 1,
+      "reldate": "1400511",
+      "release": "14.1-PRERELEASE",
+      "revision": "14.1",
       "type": "FreeBSD",
-      "version": "FreeBSD 14.0-STABLE"
+      "version": "FreeBSD 14.1-PRERELEASE"
     }
   }
 }
diff --git a/pkgs/os-specific/bsd/freebsd/lib/install-wrapper.sh b/pkgs/os-specific/bsd/lib/install-wrapper.sh
index 91a7a2679f209..97a2aaab22c0e 100644
--- a/pkgs/os-specific/bsd/freebsd/lib/install-wrapper.sh
+++ b/pkgs/os-specific/bsd/lib/install-wrapper.sh
@@ -14,7 +14,6 @@ while (( $# )); do
   case $1 in
     -C) ;;
     -o | -g) shift ;;
-    -s) ;;
     -m | -l)
       # handle next arg so not counted as path arg
       args+=("$1" "$2")
diff --git a/pkgs/os-specific/bsd/netbsd/default.nix b/pkgs/os-specific/bsd/netbsd/default.nix
index fd5e24aa7102a..eeccd0fa07eab 100644
--- a/pkgs/os-specific/bsd/netbsd/default.nix
+++ b/pkgs/os-specific/bsd/netbsd/default.nix
@@ -1,137 +1,181 @@
-{ stdenv, lib, stdenvNoCC
-, makeScopeWithSplicing', generateSplicesForMkScope
-, buildPackages
-, fetchcvs
+{
+  lib,
+  crossLibcStdenv,
+  stdenvNoCC,
+  makeScopeWithSplicing',
+  generateSplicesForMkScope,
+  buildPackages,
+  fetchcvs,
 }:
 
-makeScopeWithSplicing' {
+let
   otherSplices = generateSplicesForMkScope "netbsd";
-  f = (self: lib.packagesFromDirectoryRecursive {
-    callPackage = self.callPackage;
-    directory = ./pkgs;
-  } // (let inherit (self) mkDerivation; in {
-
-    fetchNetBSD = path: version: sha256: fetchcvs {
-      cvsRoot = ":pserver:anoncvs@anoncvs.NetBSD.org:/cvsroot";
-      module = "src/${path}";
-      inherit sha256;
-      tag = "netbsd-${lib.replaceStrings ["."] ["-"] version}-RELEASE";
-    };
-
-    defaultMakeFlags = [
-      "MKSOFTFLOAT=${if stdenv.hostPlatform.gcc.float or (stdenv.hostPlatform.parsed.abi.float or "hard") == "soft"
-        then "yes"
-        else "no"}"
-    ];
-
-    compatIfNeeded = lib.optional (!stdenvNoCC.hostPlatform.isNetBSD) self.compat;
-
-    # The manual callPackages below should in principle be unnecessary because
-    # they're just selecting arguments that would be selected anyway. However,
-    # if we don't perform these manual calls, we get infinite recursion issues
-    # because of the splices.
-
-    mkDerivation = self.callPackage ./pkgs/mkDerivation.nix {
-      inherit stdenv stdenvNoCC;
-      inherit (buildPackages.netbsd) netbsdSetupHook makeMinimal install tsort lorder;
-      inherit (buildPackages) mandoc;
-      inherit (buildPackages.buildPackages) rsync;
-
-    };
-
-    makeMinimal = self.callPackage ./pkgs/makeMinimal.nix {
-      inherit (self) make;
-    };
-
-    compat = self.callPackage ./pkgs/compat/package.nix {
-      inherit (buildPackages) coreutils;
-      inherit (buildPackages.darwin) cctools-port;
-      inherit (buildPackages.buildPackages) rsync;
-      inherit (buildPackages.netbsd) makeMinimal;
-      inherit (self) install include libc libutil;
-    };
-
-    install = self.callPackage ./pkgs/install/package.nix {
-      inherit (self) fts mtree make compatIfNeeded;
-      inherit (buildPackages.buildPackages) rsync;
-      inherit (buildPackages.netbsd) makeMinimal;
-    };
-
-    # See note in pkgs/stat/package.nix
-    stat = self.callPackage ./pkgs/stat/package.nix {
-      inherit (buildPackages.netbsd) makeMinimal install;
-      inherit (buildPackages.buildPackages) rsync;
-    };
-
-    # See note in pkgs/stat/hook.nix
-    statHook = self.callPackage ./pkgs/stat/hook.nix {
-      inherit (self) stat;
-    };
-
-    tsort = self.callPackage ./pkgs/tsort.nix {
-      inherit (buildPackages.netbsd) makeMinimal install;
-      inherit (buildPackages.buildPackages) rsync;
-    };
-
-    lorder = self.callPackage ./pkgs/lorder.nix {
-      inherit (buildPackages.netbsd) makeMinimal install;
-      inherit (buildPackages.buildPackages) rsync;
-    };
-
-    config = self.callPackage ./pkgs/config.nix {
-      inherit (buildPackages.netbsd) makeMinimal install;
-      inherit (buildPackages.buildPackages) rsync;
-      inherit (self) cksum;
-    };
-
-    include = self.callPackage ./pkgs/include.nix {
-      inherit (buildPackages.netbsd)
-        makeMinimal install nbperf rpcgen;
-      inherit (buildPackages) stdenv;
-      inherit (buildPackages.buildPackages) rsync;
-    };
-
-    sys-headers = self.callPackage ./pkgs/sys/headers.nix {
-      inherit (buildPackages.netbsd)
-        makeMinimal install tsort lorder statHook uudecode config genassym;
-      inherit (buildPackages.buildPackages) rsync;
-    };
-
-    libutil = self.callPackage ./pkgs/libutil.nix {
-      inherit (self) libc sys;
-    };
-
-    libpthread-headers = self.callPackage ./pkgs/libpthread/headers.nix { };
-
-    csu = self.callPackage ./pkgs/csu.nix {
-      inherit (self) headers sys ld_elf_so;
-      inherit (buildPackages.netbsd)
-        netbsdSetupHook
-        makeMinimal
-        install
-        genassym gencat lorder tsort statHook;
-      inherit (buildPackages.buildPackages) rsync;
-    };
-
-    _mainLibcExtraPaths = with self; [
-        common i18n_module.src sys.src
-        ld_elf_so.src libpthread.src libm.src libresolv.src
-        librpcsvc.src libutil.src librt.src libcrypt.src
-    ];
-
-    libc = self.callPackage ./pkgs/libc.nix {
-      inherit (self) headers csu librt;
-      inherit (buildPackages.netbsd)
-        netbsdSetupHook
-        makeMinimal
-        install
-        genassym gencat lorder tsort statHook rpcgen;
-      inherit (buildPackages.buildPackages) rsync;
-    };
-
-    mtree = self.callPackage ./pkgs/mtree.nix {
-      inherit (self) mknod;
-    };
-
-  }));
+  buildNetbsd = otherSplices.selfBuildHost;
+in
+
+makeScopeWithSplicing' {
+  inherit otherSplices;
+  f = (
+    self:
+    lib.packagesFromDirectoryRecursive {
+      callPackage = self.callPackage;
+      directory = ./pkgs;
+    }
+    // {
+      version = "9.2";
+
+      defaultMakeFlags = [
+        "MKSOFTFLOAT=${
+          if
+            stdenvNoCC.hostPlatform.gcc.float or (stdenvNoCC.hostPlatform.parsed.abi.float or "hard") == "soft"
+          then
+            "yes"
+          else
+            "no"
+        }"
+      ];
+
+      compatIfNeeded = lib.optional (!stdenvNoCC.hostPlatform.isNetBSD) self.compat;
+
+      stdenvLibcMinimal = crossLibcStdenv.override (old: {
+        cc = old.cc.override {
+          libc = self.libcMinimal;
+          bintools = old.cc.bintools.override {
+            libc = self.libcMinimal;
+            sharedLibraryLoader = null;
+          };
+        };
+      });
+
+      # The manual callPackages below should in principle be unnecessary because
+      # they're just selecting arguments that would be selected anyway. However,
+      # if we don't perform these manual calls, we get infinite recursion issues
+      # because of the splices.
+
+      compat = self.callPackage ./pkgs/compat/package.nix {
+        inherit (buildPackages) coreutils;
+        inherit (buildPackages.darwin) cctools-port;
+        inherit (buildNetbsd) makeMinimal;
+        inherit (self) install;
+      };
+
+      config = self.callPackage ./pkgs/config.nix {
+        inherit (buildNetbsd) makeMinimal install;
+        inherit (self) cksum;
+      };
+
+      csu = self.callPackage ./pkgs/csu.nix {
+        inherit (self) headers sys-headers ld_elf_so;
+        inherit (buildNetbsd)
+          netbsdSetupHook
+          makeMinimal
+          install
+          genassym
+          gencat
+          lorder
+          tsort
+          statHook
+          ;
+      };
+
+      include = self.callPackage ./pkgs/include.nix {
+        inherit (buildNetbsd)
+          makeMinimal
+          install
+          nbperf
+          rpcgen
+          ;
+        inherit (buildPackages) stdenv;
+      };
+
+      install = self.callPackage ./pkgs/install/package.nix {
+        inherit (self)
+          fts
+          mtree
+          make
+          compatIfNeeded
+          ;
+        inherit (buildNetbsd) makeMinimal;
+      };
+
+      libcMinimal = self.callPackage ./pkgs/libcMinimal.nix {
+        inherit (self) headers csu;
+        inherit (buildNetbsd)
+          netbsdSetupHook
+          makeMinimal
+          install
+          genassym
+          gencat
+          lorder
+          tsort
+          statHook
+          rpcgen
+          ;
+      };
+
+      libpthread-headers = self.callPackage ./pkgs/libpthread/headers.nix { };
+
+      librpcsvc = self.callPackage ./pkgs/librpcsvc.nix {
+        inherit (buildNetbsd)
+          netbsdSetupHook
+          makeMinimal
+          install
+          lorder
+          tsort
+          statHook
+          rpcgen
+          ;
+      };
+
+      libutil = self.callPackage ./pkgs/libutil.nix {
+        inherit (buildNetbsd)
+          netbsdSetupHook
+          makeMinimal
+          install
+          lorder
+          tsort
+          statHook
+          ;
+      };
+
+      lorder = self.callPackage ./pkgs/lorder.nix { inherit (buildNetbsd) makeMinimal install; };
+
+      mtree = self.callPackage ./pkgs/mtree.nix { inherit (self) mknod; };
+
+      mkDerivation = self.callPackage ./pkgs/mkDerivation.nix {
+        inherit (buildNetbsd)
+          netbsdSetupHook
+          makeMinimal
+          install
+          tsort
+          lorder
+          ;
+        inherit (buildPackages) mandoc;
+        inherit (buildPackages.buildPackages) rsync;
+      };
+
+      makeMinimal = self.callPackage ./pkgs/makeMinimal.nix { inherit (self) make; };
+
+      # See note in pkgs/stat/package.nix
+      stat = self.callPackage ./pkgs/stat/package.nix { inherit (buildNetbsd) makeMinimal install; };
+
+      # See note in pkgs/stat/hook.nix
+      statHook = self.callPackage ./pkgs/stat/hook.nix { inherit (self) stat; };
+
+      sys-headers = self.callPackage ./pkgs/sys/headers.nix {
+        inherit (buildNetbsd)
+          makeMinimal
+          install
+          tsort
+          lorder
+          statHook
+          uudecode
+          config
+          genassym
+          ;
+      };
+
+      tsort = self.callPackage ./pkgs/tsort.nix { inherit (buildNetbsd) makeMinimal install; };
+    }
+  );
 }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/cksum.nix b/pkgs/os-specific/bsd/netbsd/pkgs/cksum.nix
index a2ec387501ae1..a95df55874e42 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/cksum.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/cksum.nix
@@ -2,7 +2,5 @@
 
 mkDerivation {
   path = "usr.bin/cksum";
-  version = "9.2";
-  sha256 = "0msfhgyvh5c2jmc6qjnf12c378dhw32ffsl864qz4rdb2b98rfcq";
   meta.platforms = lib.platforms.netbsd;
 }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/column.nix b/pkgs/os-specific/bsd/netbsd/pkgs/column.nix
index f3cebfa9ab261..a51369433f6c2 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/column.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/column.nix
@@ -1,7 +1,3 @@
 { mkDerivation }:
 
-mkDerivation {
-  path = "usr.bin/column";
-  version = "9.2";
-  sha256 = "0r6b0hjn5ls3j3sv6chibs44fs32yyk2cg8kh70kb4cwajs4ifyl";
-}
+mkDerivation { path = "usr.bin/column"; }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/common.nix b/pkgs/os-specific/bsd/netbsd/pkgs/common.nix
deleted file mode 100644
index 464fc1c9e0c26..0000000000000
--- a/pkgs/os-specific/bsd/netbsd/pkgs/common.nix
+++ /dev/null
@@ -1,3 +0,0 @@
-{ fetchNetBSD }:
-
-fetchNetBSD "common" "9.2" "1pfylz9r3ap5wnwwbwczbfjb1m5qdyspzbnmxmcdkpzz2zgj64b9"
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/compat/libbsd-overlay.pc b/pkgs/os-specific/bsd/netbsd/pkgs/compat/libbsd-overlay.pc
index 3aadabe508822..086dd62303e96 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/compat/libbsd-overlay.pc
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/compat/libbsd-overlay.pc
@@ -1,7 +1,7 @@
 prefix=@out@
 exec_prefix=${prefix}
 libdir=${exec_prefix}/lib
-includedir=${prefix}/include
+includedir=@includedir@
 
 Name: nbcompat
 Description: NetBSD compatibility framework
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/compat/package.nix b/pkgs/os-specific/bsd/netbsd/pkgs/compat/package.nix
index a1336ad52f6dc..0f7483ce55ad1 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/compat/package.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/compat/package.nix
@@ -1,24 +1,29 @@
-{ lib, mkDerivation, stdenv
-, zlib
-, defaultMakeFlags
-, coreutils
-, cctools-port
-, include, libc, libutil
-, install
-, bsdSetupHook, netbsdSetupHook
-, makeMinimal
-, rsync
-, fetchNetBSD
-, _mainLibcExtraPaths
+{
+  lib,
+  mkDerivation,
+  stdenv,
+  zlib,
+  defaultMakeFlags,
+  coreutils,
+  cctools-port,
+  install,
+  bsdSetupHook,
+  netbsdSetupHook,
+  makeMinimal,
+  version,
 }:
 
-mkDerivation (let
-    version = "9.2";
+mkDerivation (
+  let
     commonDeps = [ zlib ];
-  in {
+  in
+  {
     path = "tools/compat";
-    sha256 = "1vsxg7136nlhc72vpa664vs22874xh7ila95nkmsd8crn3z3cyn0";
-    inherit version;
+
+    outputs = [
+      "out"
+      "dev"
+    ];
 
     setupHooks = [
       ../../../../../build-support/setup-hooks/role.bash
@@ -29,43 +34,51 @@ mkDerivation (let
       make include/.stamp configure nbtool_config.h.in defs.mk.in
     '';
 
-    configurePlatforms = [ "build" "host" ];
-    configureFlags = [
-      "--cache-file=config.cache"
-    ] ++ lib.optionals stdenv.hostPlatform.isMusl [
-      # We include this header in our musl package only for legacy
-      # compatibility, and compat works fine without it (and having it
-      # know about sys/cdefs.h breaks packages like glib when built
-      # statically).
-      "ac_cv_header_sys_cdefs_h=no"
+    configurePlatforms = [
+      "build"
+      "host"
     ];
+    configureFlags =
+      [ "--cache-file=config.cache" ]
+      ++ lib.optionals stdenv.hostPlatform.isMusl [
+        # We include this header in our musl package only for legacy
+        # compatibility, and compat works fine without it (and having it
+        # know about sys/cdefs.h breaks packages like glib when built
+        # statically).
+        "ac_cv_header_sys_cdefs_h=no"
+      ];
 
     nativeBuildInputs = commonDeps ++ [
-      bsdSetupHook netbsdSetupHook
+      bsdSetupHook
+      netbsdSetupHook
       makeMinimal
-      rsync
     ];
 
     buildInputs = commonDeps;
 
     # temporarily use gnuinstall for bootstrapping
     # bsdinstall will be built later
-    makeFlags = defaultMakeFlags ++ [
-      "INSTALL=${coreutils}/bin/install"
-      "DATADIR=$(out)/share"
-      # Can't sort object files yet
-      "LORDER=echo"
-      "TSORT=cat"
-      # Can't process man pages yet
-      "MKSHARE=no"
-    ] ++ lib.optionals stdenv.hostPlatform.isDarwin [
-      # GNU objcopy produces broken .a libs which won't link into dependers.
-      # Makefiles only invoke `$OBJCOPY -x/-X`, so cctools strip works here.
-      "OBJCOPY=${cctools-port}/bin/strip"
-    ];
+    makeFlags =
+      defaultMakeFlags
+      ++ [
+        "INSTALL=${coreutils}/bin/install"
+        "DATADIR=$(dev)/share"
+        # Can't sort object files yet
+        "LORDER=echo"
+        "TSORT=cat"
+        # Can't process man pages yet
+        "MKSHARE=no"
+      ]
+      ++ lib.optionals stdenv.hostPlatform.isDarwin [
+        # GNU objcopy produces broken .a libs which won't link into dependers.
+        # Makefiles only invoke `$OBJCOPY -x/-X`, so cctools strip works here.
+        "OBJCOPY=${cctools-port}/bin/strip"
+      ];
     RENAME = "-D";
 
-    passthru.tests = { netbsd-install = install; };
+    passthru.tests = {
+      netbsd-install = install;
+    };
 
     patches = [
       ./compat-cxx-safe-header.patch
@@ -79,42 +92,50 @@ mkDerivation (let
       makeFlagsArray+=('INSTALL_SYMLINK=''${INSTALL} ''${SYMLINK} ''${RENAME}')
     '';
 
-    postInstall = ''
-      # why aren't these installed by netbsd?
-      install -D compat_defs.h $out/include/compat_defs.h
-      install -D $BSDSRCDIR/include/cdbw.h $out/include/cdbw.h
-      install -D $BSDSRCDIR/sys/sys/cdbr.h $out/include/cdbr.h
-      install -D $BSDSRCDIR/sys/sys/featuretest.h \
-                 $out/include/sys/featuretest.h
-      install -D $BSDSRCDIR/sys/sys/md5.h $out/include/md5.h
-      install -D $BSDSRCDIR/sys/sys/rmd160.h $out/include/rmd160.h
-      install -D $BSDSRCDIR/sys/sys/sha1.h $out/include/sha1.h
-      install -D $BSDSRCDIR/sys/sys/sha2.h $out/include/sha2.h
-      install -D $BSDSRCDIR/sys/sys/queue.h $out/include/sys/queue.h
-      install -D $BSDSRCDIR/include/vis.h $out/include/vis.h
-      install -D $BSDSRCDIR/include/db.h $out/include/db.h
-      install -D $BSDSRCDIR/include/netconfig.h $out/include/netconfig.h
-      install -D $BSDSRCDIR/include/utmpx.h $out/include/utmpx.h
-      install -D $BSDSRCDIR/include/tzfile.h $out/include/tzfile.h
-      install -D $BSDSRCDIR/sys/sys/tree.h $out/include/sys/tree.h
-      install -D $BSDSRCDIR/include/nl_types.h $out/include/nl_types.h
-      install -D $BSDSRCDIR/include/stringlist.h $out/include/stringlist.h
+    postInstall =
+      ''
+        # why aren't these installed by netbsd?
+        install -D compat_defs.h $dev/include/compat_defs.h
+        install -D $BSDSRCDIR/include/cdbw.h $dev/include/cdbw.h
+        install -D $BSDSRCDIR/sys/sys/cdbr.h $dev/include/cdbr.h
+        install -D $BSDSRCDIR/sys/sys/featuretest.h \
+                   $dev/include/sys/featuretest.h
+        install -D $BSDSRCDIR/sys/sys/md5.h $dev/include/md5.h
+        install -D $BSDSRCDIR/sys/sys/rmd160.h $dev/include/rmd160.h
+        install -D $BSDSRCDIR/sys/sys/sha1.h $dev/include/sha1.h
+        install -D $BSDSRCDIR/sys/sys/sha2.h $dev/include/sha2.h
+        install -D $BSDSRCDIR/sys/sys/queue.h $dev/include/sys/queue.h
+        install -D $BSDSRCDIR/include/vis.h $dev/include/vis.h
+        install -D $BSDSRCDIR/include/db.h $dev/include/db.h
+        install -D $BSDSRCDIR/include/netconfig.h $dev/include/netconfig.h
+        install -D $BSDSRCDIR/include/utmpx.h $dev/include/utmpx.h
+        install -D $BSDSRCDIR/include/tzfile.h $dev/include/tzfile.h
+        install -D $BSDSRCDIR/sys/sys/tree.h $dev/include/sys/tree.h
+        install -D $BSDSRCDIR/include/nl_types.h $dev/include/nl_types.h
+        install -D $BSDSRCDIR/include/stringlist.h $dev/include/stringlist.h
 
-      # Collapse includes slightly to fix dangling reference
-      install -D $BSDSRCDIR/common/include/rpc/types.h $out/include/rpc/types.h
-      sed -i '1s;^;#include "nbtool_config.h"\n;' $out/include/rpc/types.h
-   '' + lib.optionalString stdenv.isDarwin ''
-      mkdir -p $out/include/ssp
-      touch $out/include/ssp/ssp.h
-   '' + ''
-      mkdir -p $out/lib/pkgconfig
-      substitute ${./libbsd-overlay.pc} $out/lib/pkgconfig/libbsd-overlay.pc \
-        --subst-var-by out $out \
-        --subst-var-by version ${version}
-    '';
-    extraPaths = [ include.src libc.src libutil.src
-      (fetchNetBSD "external/bsd/flex" "9.2" "0h98jpfj7vx5zh7vd7bk6b1hmzgkcb757a8j6d9zgygxxv13v43m")
-      (fetchNetBSD "sys/sys" "9.2" "0zawhw51klaigqqwkx0lzrx3mim2jywrc24cm7c66qsf1im9awgd")
-      (fetchNetBSD "common/include/rpc/types.h" "9.2" "0n2df12mlc3cbc48jxq35yzl1y7ghgpykvy7jnfh898rdhac7m9a")
-    ] ++ libutil.extraPaths ++ _mainLibcExtraPaths;
-  })
+        # Collapse includes slightly to fix dangling reference
+        install -D $BSDSRCDIR/common/include/rpc/types.h $dev/include/rpc/types.h
+        sed -i '1s;^;#include "nbtool_config.h"\n;' $dev/include/rpc/types.h
+      ''
+      + lib.optionalString stdenv.isDarwin ''
+        mkdir -p $dev/include/ssp
+        touch $dev/include/ssp/ssp.h
+      ''
+      + ''
+        mkdir -p $dev/lib/pkgconfig
+        substitute ${./libbsd-overlay.pc} $dev/lib/pkgconfig/libbsd-overlay.pc \
+          --subst-var-by out "$out" \
+          --subst-var-by includedir "$dev/include" \
+          --subst-var-by version ${version}
+      '';
+    extraPaths = [
+      "common"
+      "include"
+      "lib/libc"
+      "lib/libutil"
+      "external/bsd/flex"
+      "sys/sys"
+    ];
+  }
+)
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/config.nix b/pkgs/os-specific/bsd/netbsd/pkgs/config.nix
index 277d70129e6f7..94a014a189cf4 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/config.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/config.nix
@@ -1,18 +1,27 @@
-{ mkDerivation
-, bsdSetupHook, netbsdSetupHook
-, makeMinimal, install, mandoc, byacc, flex, rsync
-, compatIfNeeded
-, cksum
+{
+  mkDerivation,
+  bsdSetupHook,
+  netbsdSetupHook,
+  makeMinimal,
+  install,
+  mandoc,
+  byacc,
+  flex,
+  compatIfNeeded,
+  cksum,
 }:
 mkDerivation {
   path = "usr.bin/config";
-  version = "9.2";
-  sha256 = "1yz3n4hncdkk6kp595fh2q5lg150vpqg8iw2dccydkyw4y3hgsjj";
   env.NIX_CFLAGS_COMPILE = toString [ "-DMAKE_BOOTSTRAP" ];
   nativeBuildInputs = [
-    bsdSetupHook netbsdSetupHook
-    makeMinimal install mandoc byacc flex rsync
+    bsdSetupHook
+    netbsdSetupHook
+    makeMinimal
+    install
+    mandoc
+    byacc
+    flex
   ];
   buildInputs = compatIfNeeded;
-  extraPaths = [ cksum.src ];
+  extraPaths = [ cksum.path ];
 }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/csu.nix b/pkgs/os-specific/bsd/netbsd/pkgs/csu.nix
index 2d13ced6d5ed5..c6e0de8edd117 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/csu.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/csu.nix
@@ -1,24 +1,46 @@
-{ lib, mkDerivation
-, bsdSetupHook, netbsdSetupHook
-, makeMinimal
-, install, mandoc, groff, flex
-, byacc, genassym, gencat, lorder, tsort, statHook, rsync
-, headers
-, sys
-, ld_elf_so
+{
+  lib,
+  mkDerivation,
+  bsdSetupHook,
+  netbsdSetupHook,
+  makeMinimal,
+  install,
+  mandoc,
+  groff,
+  flex,
+  byacc,
+  genassym,
+  gencat,
+  lorder,
+  tsort,
+  statHook,
+  headers,
+  sys-headers,
+  ld_elf_so,
 }:
 
 mkDerivation {
+  noLibc = true;
   path = "lib/csu";
-  version = "9.2";
-  sha256 = "0al5jfazvhlzn9hvmnrbchx4d0gm282hq5gp4xs2zmj9ycmf6d03";
   meta.platforms = lib.platforms.netbsd;
   nativeBuildInputs = [
-    bsdSetupHook netbsdSetupHook
+    bsdSetupHook
+    netbsdSetupHook
     makeMinimal
-    install mandoc groff flex
-    byacc genassym gencat lorder tsort statHook rsync
+    install
+    mandoc
+    groff
+    flex
+    byacc
+    genassym
+    gencat
+    lorder
+    tsort
+    statHook
   ];
   buildInputs = [ headers ];
-  extraPaths = [ sys.src ld_elf_so.src ];
+  extraPaths = [
+    sys-headers.path
+    ld_elf_so.path
+  ];
 }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/dict.nix b/pkgs/os-specific/bsd/netbsd/pkgs/dict.nix
index 0d7d026dd21d2..8d27f29a75e57 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/dict.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/dict.nix
@@ -3,7 +3,5 @@
 mkDerivation {
   path = "share/dict";
   noCC = true;
-  version = "9.2";
-  sha256 = "0svfc0byk59ri37pyjslv4c4rc7zw396r73mr593i78d39q5g3ad";
   makeFlags = defaultMakeFlags ++ [ "BINDIR=$(out)/share" ];
 }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/fts/package.nix b/pkgs/os-specific/bsd/netbsd/pkgs/fts/package.nix
index 0393d36357f6e..3201a1c0e082a 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/fts/package.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/fts/package.nix
@@ -1,22 +1,22 @@
-{ mkDerivation
-, bsdSetupHook, netbsdSetupHook, rsync
-, compatIfNeeded
-, fetchNetBSD
+{
+  mkDerivation,
+  bsdSetupHook,
+  netbsdSetupHook,
+  compatIfNeeded,
 }:
 
 mkDerivation {
   pname = "fts";
   path = "include/fts.h";
-  sha256 = "01d4fpxvz1pgzfk5xznz5dcm0x0gdzwcsfm1h3d0xc9kc6hj2q77";
-  version = "9.2";
   nativeBuildInputs = [
-    bsdSetupHook netbsdSetupHook rsync
+    bsdSetupHook
+    netbsdSetupHook
   ];
   propagatedBuildInputs = compatIfNeeded;
   extraPaths = [
-    (fetchNetBSD "lib/libc/gen/fts.c" "9.2" "1a8hmf26242nmv05ipn3ircxb0jqmmi66rh78kkyi9vjwkfl3qn7")
-    (fetchNetBSD "lib/libc/include/namespace.h" "9.2" "0kksr3pdwdc1cplqf5z12ih4cml6l11lqrz91f7hjjm64y7785kc")
-    (fetchNetBSD "lib/libc/gen/fts.3" "9.2" "1asxw0n3fhjdadwkkq3xplfgqgl3q32w1lyrvbakfa3gs0wz5zc1")
+    "lib/libc/gen/fts.c"
+    "lib/libc/include/namespace.h"
+    "lib/libc/gen/fts.3"
   ];
   skipIncludesPhase = true;
   buildPhase = ''
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/genassym.nix b/pkgs/os-specific/bsd/netbsd/pkgs/genassym.nix
index 7f81a77a02dd2..9eed584b29ae4 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/genassym.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/genassym.nix
@@ -1,7 +1,3 @@
 { mkDerivation }:
 
-mkDerivation {
-  path = "usr.bin/genassym";
-  version = "9.2";
-  sha256 = "1acl1dz5kvh9h5806vkz2ap95rdsz7phmynh5i3x5y7agbki030c";
-}
+mkDerivation { path = "usr.bin/genassym"; }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/gencat.nix b/pkgs/os-specific/bsd/netbsd/pkgs/gencat.nix
index 411be85e91e2f..ea701b6404ecc 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/gencat.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/gencat.nix
@@ -1,7 +1,3 @@
 { mkDerivation }:
 
-mkDerivation {
-  path = "usr.bin/gencat";
-  version = "9.2";
-  sha256 = "0gd463x1hg36bhr7y0xryb5jyxk0z0g7xvy8rgk82nlbnlnsbbwb";
-}
+mkDerivation { path = "usr.bin/gencat"; }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/getconf.nix b/pkgs/os-specific/bsd/netbsd/pkgs/getconf.nix
index c8483d454b877..2feb6495ccf86 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/getconf.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/getconf.nix
@@ -1,7 +1,3 @@
 { mkDerivation }:
 
-mkDerivation {
-  path = "usr.bin/getconf";
-  sha256 = "122vslz4j3h2mfs921nr2s6m078zcj697yrb75rwp2hnw3qz4s8q";
-  version = "9.2";
-}
+mkDerivation { path = "usr.bin/getconf"; }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/getent/package.nix b/pkgs/os-specific/bsd/netbsd/pkgs/getent/package.nix
index 7a9acb1e0a9e8..b6a4efad4ac9c 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/getent/package.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/getent/package.nix
@@ -2,7 +2,5 @@
 
 mkDerivation {
   path = "usr.bin/getent";
-  sha256 = "1qngywcmm0y7nl8h3n8brvkxq4jw63szbci3kc1q6a6ndhycbbvr";
-  version = "9.2";
   patches = [ ./getent.patch ];
 }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/headers.nix b/pkgs/os-specific/bsd/netbsd/pkgs/headers.nix
index 646e5675130bb..40615b2722d5d 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/headers.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/headers.nix
@@ -1,4 +1,10 @@
-{ lib, symlinkJoin, include, sys-headers, libpthread-headers }:
+{
+  lib,
+  symlinkJoin,
+  include,
+  sys-headers,
+  libpthread-headers,
+}:
 
 symlinkJoin {
   name = "netbsd-headers-9.2";
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/i18n_module.nix b/pkgs/os-specific/bsd/netbsd/pkgs/i18n_module.nix
index 60b091ef0b718..9504ac22a74ed 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/i18n_module.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/i18n_module.nix
@@ -1,9 +1,20 @@
-{ lib, mkDerivation, libc }:
+{
+  lib,
+  stdenvLibcMinimal,
+  mkDerivation,
+  libcMinimal,
+}:
 
 mkDerivation {
   path = "lib/i18n_module";
-  version = "9.2";
-  sha256 = "0w6y5v3binm7gf2kn7y9jja8k18rhnyl55cvvfnfipjqdxvxd9jd";
+
+  libcMinimal = true;
+
+  # Hack around GCC's limits.h missing the include_next we want See
+  # https://gcc.gnu.org/legacy-ml/gcc/2003-10/msg01278.html
+  NIX_CFLAGS_COMPILE_BEFORE = "-isystem ${stdenvLibcMinimal.cc.libc.dev}/include";
+
+  extraPaths = [ libcMinimal.path ];
+
   meta.platforms = lib.platforms.netbsd;
-  extraPaths = [ libc.src ];
 }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/include.nix b/pkgs/os-specific/bsd/netbsd/pkgs/include.nix
index 1ecdec90ae095..1048016b81e2f 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/include.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/include.nix
@@ -1,20 +1,30 @@
-{ lib, mkDerivation
-, bsdSetupHook, netbsdSetupHook
-, makeMinimal
-, install, mandoc, groff, rsync, nbperf, rpcgen
-, common
-, defaultMakeFlags
-, stdenv
+{
+  lib,
+  mkDerivation,
+  bsdSetupHook,
+  netbsdSetupHook,
+  makeMinimal,
+  install,
+  mandoc,
+  groff,
+  nbperf,
+  rpcgen,
+  defaultMakeFlags,
+  stdenv,
 }:
 
 mkDerivation {
+  noLibc = true;
   path = "include";
-  version = "9.2";
-  sha256 = "0nxnmj4c8s3hb9n3fpcmd0zl3l1nmhivqgi9a35sis943qvpgl9h";
   nativeBuildInputs = [
-    bsdSetupHook netbsdSetupHook
+    bsdSetupHook
+    netbsdSetupHook
     makeMinimal
-    install mandoc groff rsync nbperf rpcgen
+    install
+    mandoc
+    groff
+    nbperf
+    rpcgen
   ];
 
   # The makefiles define INCSDIR per subdirectory, so we have to set
@@ -32,7 +42,7 @@ mkDerivation {
     makeFlags=''${makeFlags/INCSDIR/INCSDIR0}
   '';
 
-  extraPaths = [ common ];
+  extraPaths = [ "common" ];
   headersOnly = true;
   noCC = true;
   meta.platforms = lib.platforms.netbsd;
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/install/package.nix b/pkgs/os-specific/bsd/netbsd/pkgs/install/package.nix
index 991517ac2d486..fa8bb690075f1 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/install/package.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/install/package.nix
@@ -1,49 +1,58 @@
-{ mkDerivation
-, writeShellScript
-, mtree, make
-, bsdSetupHook, netbsdSetupHook
-, makeMinimal
-, mandoc, groff, rsync
-, compatIfNeeded, fts
+{
+  mkDerivation,
+  writeShellScript,
+  mtree,
+  make,
+  bsdSetupHook,
+  netbsdSetupHook,
+  makeMinimal,
+  mandoc,
+  groff,
+  compatIfNeeded,
+  fts,
 
 }:
 
 # HACK: to ensure parent directories exist. This emulates GNU
 # install’s -D option. No alternative seems to exist in BSD install.
 let
-  binstall = writeShellScript "binstall" ''
-    set -eu
-    for last in "$@"; do true; done
-    mkdir -p $(dirname $last)
-    @out@/bin/xinstall "$@"
-  '';
+  binstall = writeShellScript "binstall" (
+    builtins.readFile ../../../lib/install-wrapper.sh
+    + ''
+      @out@/bin/xinstall "''${args[@]}"
+    ''
+  );
 in
-  mkDerivation {
-    path = "usr.bin/xinstall";
-    version = "9.2";
-    sha256 = "1f6pbz3qv1qcrchdxif8p5lbmnwl8b9nq615hsd3cyl4avd5bfqj";
-    extraPaths = [ mtree.src make.src ];
-    nativeBuildInputs = [
-      bsdSetupHook netbsdSetupHook
-      makeMinimal
-      mandoc groff rsync
-    ];
-    skipIncludesPhase = true;
-    buildInputs = compatIfNeeded
-      # fts header is needed. glibc already has this header, but musl doesn't,
-      # so make sure pkgsMusl.netbsd.install still builds in case you want to
-      # remove it!
-      ++ [ fts ];
-    installPhase = ''
-      runHook preInstall
+mkDerivation {
+  path = "usr.bin/xinstall";
+  extraPaths = [
+    mtree.path
+    make.path
+  ];
+  nativeBuildInputs = [
+    bsdSetupHook
+    netbsdSetupHook
+    makeMinimal
+    mandoc
+    groff
+  ];
+  skipIncludesPhase = true;
+  buildInputs =
+    compatIfNeeded
+    # fts header is needed. glibc already has this header, but musl doesn't,
+    # so make sure pkgsMusl.netbsd.install still builds in case you want to
+    # remove it!
+    ++ [ fts ];
+  installPhase = ''
+    runHook preInstall
 
-      install -D install.1 $out/share/man/man1/install.1
-      install -D xinstall $out/bin/xinstall
-      install -D -m 0550 ${binstall} $out/bin/binstall
-      substituteInPlace $out/bin/binstall --subst-var out
-      ln -s $out/bin/binstall $out/bin/install
+    install -D install.1 $out/share/man/man1/install.1
+    install -D xinstall $out/bin/xinstall
+    install -D -m 0550 ${binstall} $out/bin/binstall
+    substituteInPlace $out/bin/binstall --subst-var out
+    ln -s $out/bin/binstall $out/bin/install
 
-      runHook postInstall
-    '';
-    setupHook = ./install-setup-hook.sh;
-  }
+    runHook postInstall
+  '';
+  setupHook = ./install-setup-hook.sh;
+}
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/ld_elf_so.nix b/pkgs/os-specific/bsd/netbsd/pkgs/ld_elf_so.nix
index 1496ad6a0e884..900b87967927c 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/ld_elf_so.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/ld_elf_so.nix
@@ -1,18 +1,24 @@
-{ lib
-, mkDerivation
-, libc
-, defaultMakeFlags
+{
+  lib,
+  mkDerivation,
+  libcMinimal,
+  defaultMakeFlags,
 }:
 
 mkDerivation {
-  path  = "libexec/ld.elf_so";
-  version = "9.2";
-  sha256 = "0ia9mqzdljly0vqfwflm5mzz55k7qsr4rw2bzhivky6k30vgirqa";
+  noLibc = true;
+  path = "libexec/ld.elf_so";
   meta.platforms = lib.platforms.netbsd;
-  LIBC_PIC = "${libc}/lib/libc_pic.a";
+  LIBC_PIC = "${libcMinimal}/lib/libc_pic.a";
   # Hack to prevent a symlink being installed here for compatibility.
   SHLINKINSTALLDIR = "/usr/libexec";
   USE_FORT = "yes";
-  makeFlags = defaultMakeFlags ++ [ "BINDIR=$(out)/libexec" "CLIBOBJ=${libc}/lib" ];
-  extraPaths = [ libc.src ] ++ libc.extraPaths;
+  makeFlags = defaultMakeFlags ++ [
+    "BINDIR=$(out)/libexec"
+    "CLIBOBJ=${libcMinimal}/lib"
+  ];
+  extraPaths = [
+    libcMinimal.path
+    "sys"
+  ];
 }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/libarch.nix b/pkgs/os-specific/bsd/netbsd/pkgs/libarch.nix
index 93ea02f8a17c1..457ea6fa48d91 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/libarch.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/libarch.nix
@@ -2,7 +2,5 @@
 
 mkDerivation {
   path = "lib/libarch";
-  version = "9.2";
-  sha256 = "6ssenRhuSwp0Jn71ErT0PrEoCJ+cIYRztwdL4QTDZsQ=";
   meta.platforms = lib.platforms.netbsd;
 }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/libc.nix b/pkgs/os-specific/bsd/netbsd/pkgs/libc.nix
index ebc01a11668a6..665c9ce8ec8be 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/libc.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/libc.nix
@@ -1,76 +1,52 @@
-{ lib
-, mkDerivation
-, defaultMakeFlags
-, _mainLibcExtraPaths
-, fetchNetBSD
-, bsdSetupHook, netbsdSetupHook
-, makeMinimal
-, install, mandoc, groff, flex
-, byacc, genassym, gencat, lorder, tsort, statHook, rsync, rpcgen
-, csu, headers
-, librt
+{
+  lib,
+  symlinkJoin,
+  libcMinimal,
+  libpthread,
+  libm,
+  libresolv,
+  librpcsvc,
+  i18n_module,
+  libutil,
+  librt,
+  libcrypt,
+  version,
 }:
 
-mkDerivation {
-  path = "lib/libc";
-  version = "9.2";
-  sha256 = "1y9c13igg0kai07sqvf9cm6yqmd8lhfd8hq3q7biilbgs1l99as3";
-  USE_FORT = "yes";
-  MKPROFILE = "no";
-  extraPaths = _mainLibcExtraPaths ++ [
-    (fetchNetBSD "external/bsd/jemalloc" "9.2" "0cq704swa0h2yxv4gc79z2lwxibk9k7pxh3q5qfs7axx3jx3n8kb")
-  ];
-  nativeBuildInputs = [
-    bsdSetupHook netbsdSetupHook
-    makeMinimal
-    install mandoc groff flex
-    byacc genassym gencat lorder tsort statHook rsync rpcgen
-  ];
-  buildInputs = [ headers csu ];
-  env.NIX_CFLAGS_COMPILE = "-B${csu}/lib -fcommon";
-  meta.platforms = lib.platforms.netbsd;
-  SHLIBINSTALLDIR = "$(out)/lib";
-  MKPICINSTALL = "yes";
-  NLSDIR = "$(out)/share/nls";
-  makeFlags = defaultMakeFlags ++ [ "FILESDIR=$(out)/var/db"];
-  postInstall = ''
-    pushd ${headers}
-    find . -type d -exec mkdir -p $out/\{} \;
-    find . \( -type f -o -type l \) -exec cp -pr \{} $out/\{} \;
-    popd
-
-    pushd ${csu}
-    find . -type d -exec mkdir -p $out/\{} \;
-    find . \( -type f -o -type l \) -exec cp -pr \{} $out/\{} \;
-    popd
-
-    NIX_CFLAGS_COMPILE+=" -B$out/lib"
-    NIX_CFLAGS_COMPILE+=" -I$out/include"
-    NIX_LDFLAGS+=" -L$out/lib"
-
-    make -C $BSDSRCDIR/lib/libpthread $makeFlags
-    make -C $BSDSRCDIR/lib/libpthread $makeFlags install
-
-    make -C $BSDSRCDIR/lib/libm $makeFlags
-    make -C $BSDSRCDIR/lib/libm $makeFlags install
+symlinkJoin rec {
+  name = "${pname}-${version}";
+  pname = "libc-netbsd";
+  inherit version;
 
-    make -C $BSDSRCDIR/lib/libresolv $makeFlags
-    make -C $BSDSRCDIR/lib/libresolv $makeFlags install
-
-    make -C $BSDSRCDIR/lib/librpcsvc $makeFlags
-    make -C $BSDSRCDIR/lib/librpcsvc $makeFlags install
-
-    make -C $BSDSRCDIR/lib/i18n_module $makeFlags
-    make -C $BSDSRCDIR/lib/i18n_module $makeFlags install
-
-    make -C $BSDSRCDIR/lib/libutil $makeFlags
-    make -C $BSDSRCDIR/lib/libutil $makeFlags install
-
-    make -C $BSDSRCDIR/lib/librt $makeFlags
-    make -C $BSDSRCDIR/lib/librt $makeFlags install
+  outputs = [
+    "out"
+    "dev"
+    "man"
+  ];
 
-    make -C $BSDSRCDIR/lib/libcrypt $makeFlags
-    make -C $BSDSRCDIR/lib/libcrypt $makeFlags install
+  paths =
+    lib.concatMap
+      (p: [
+        (lib.getDev p)
+        (lib.getLib p)
+        (lib.getMan p)
+      ])
+      [
+        libcMinimal
+        libm
+        libpthread
+        libresolv
+        librpcsvc
+        i18n_module
+        libutil
+        librt
+        libcrypt
+      ];
+
+  postBuild = ''
+    rm -r "$out/nix-support"
+    fixupPhase
   '';
-  inherit (librt) postPatch;
+
+  meta.platforms = lib.platforms.netbsd;
 }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/libcMinimal.nix b/pkgs/os-specific/bsd/netbsd/pkgs/libcMinimal.nix
new file mode 100644
index 0000000000000..bbab20d53d51e
--- /dev/null
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/libcMinimal.nix
@@ -0,0 +1,85 @@
+{
+  lib,
+  mkDerivation,
+  defaultMakeFlags,
+  bsdSetupHook,
+  netbsdSetupHook,
+  makeMinimal,
+  install,
+  mandoc,
+  groff,
+  flex,
+  byacc,
+  genassym,
+  gencat,
+  lorder,
+  tsort,
+  statHook,
+  rpcgen,
+  csu,
+  headers,
+}:
+
+mkDerivation {
+  noLibc = true;
+  path = "lib/libc";
+  pname = "libcMinimal-netbsd";
+  outputs = [
+    "out"
+    "dev"
+    "man"
+    "tags"
+  ];
+  USE_FORT = "yes";
+  MKPROFILE = "no";
+  extraPaths = [
+    "common"
+    "lib/i18n_module"
+    "libexec/ld.elf_so"
+    "sys"
+    "external/bsd/jemalloc"
+  ];
+  nativeBuildInputs = [
+    bsdSetupHook
+    netbsdSetupHook
+    makeMinimal
+    install
+    mandoc
+    groff
+    flex
+    byacc
+    genassym
+    gencat
+    lorder
+    tsort
+    statHook
+    rpcgen
+  ];
+  buildInputs = [
+    headers
+    csu
+  ];
+  env.NIX_CFLAGS_COMPILE = "-B${csu}/lib -fcommon";
+  meta.platforms = lib.platforms.netbsd;
+  SHLIBINSTALLDIR = "$(out)/lib";
+  MKPICINSTALL = "yes";
+  NLSDIR = "$(out)/share/nls";
+  makeFlags = defaultMakeFlags ++ [ "FILESDIR=$(out)/var/db" ];
+  postInstall = ''
+    pushd ${headers}
+    find include -type d -exec mkdir -p "$dev/{}" ';'
+    find include '(' -type f -o -type l ')' -exec cp -pr "{}" "$dev/{}" ';'
+    popd
+
+    pushd ${csu}
+    find lib -type d -exec mkdir -p "$out/{}" ';'
+    find lib '(' -type f -o -type l ')' -exec cp -pr "{}" "$out/{}" ';'
+    popd
+
+    moveToOutput var/db/libc.tags "$tags"
+  '';
+
+  postPatch = ''
+    sed -i 's,/usr\(/include/sys/syscall.h\),${headers}\1,g' lib/lib*/sys/Makefile.inc
+  '';
+}
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/libcrypt.nix b/pkgs/os-specific/bsd/netbsd/pkgs/libcrypt.nix
index c5f9deff589c4..f51dec0f68af2 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/libcrypt.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/libcrypt.nix
@@ -2,8 +2,14 @@
 
 mkDerivation {
   path = "lib/libcrypt";
-  version = "9.2";
-  sha256 = "0siqan1wdqmmhchh2n8w6a8x1abbff8n4yb6jrqxap3hqn8ay54g";
+
+  libcMinimal = true;
+
+  outputs = [
+    "out"
+    "man"
+  ];
+
   SHLIBINSTALLDIR = "$(out)/lib";
   meta.platforms = lib.platforms.netbsd;
 }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/libcurses.nix b/pkgs/os-specific/bsd/netbsd/pkgs/libcurses.nix
index 2409879640a90..8d61bcfdf7d97 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/libcurses.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/libcurses.nix
@@ -1,19 +1,23 @@
-{ lib, mkDerivation, stdenv
-, libterminfo
-, compatIfNeeded
-, defaultMakeFlags
+{
+  lib,
+  mkDerivation,
+  stdenv,
+  libterminfo,
+  compatIfNeeded,
+  defaultMakeFlags,
 }:
 
 mkDerivation {
   path = "lib/libcurses";
-  version = "9.2";
-  sha256 = "0pd0dggl3w4bv5i5h0s1wrc8hr66n4hkv3zlklarwfdhc692fqal";
   buildInputs = [ libterminfo ];
-  env.NIX_CFLAGS_COMPILE = toString ([
-    "-D__scanflike(a,b)="
-    "-D__va_list=va_list"
-    "-D__warn_references(a,b)="
-  ] ++ lib.optional stdenv.isDarwin "-D__strong_alias(a,b)=");
+  env.NIX_CFLAGS_COMPILE = toString (
+    [
+      "-D__scanflike(a,b)="
+      "-D__va_list=va_list"
+      "-D__warn_references(a,b)="
+    ]
+    ++ lib.optional stdenv.isDarwin "-D__strong_alias(a,b)="
+  );
   propagatedBuildInputs = compatIfNeeded;
   MKDOC = "no"; # missing vfontedpr
   makeFlags = defaultMakeFlags ++ [ "LIBDO.terminfo=${libterminfo}/lib" ];
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/libedit.nix b/pkgs/os-specific/bsd/netbsd/pkgs/libedit.nix
index eb734f34c3d49..cb9951cb8cf10 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/libedit.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/libedit.nix
@@ -1,26 +1,30 @@
-{ lib, mkDerivation
-, libterminfo, libcurses
-, compatIfNeeded
-, defaultMakeFlags
+{
+  lib,
+  mkDerivation,
+  libterminfo,
+  libcurses,
+  compatIfNeeded,
+  defaultMakeFlags,
 }:
 
 mkDerivation {
-    path = "lib/libedit";
-    version = "9.2";
-    sha256 = "1wqhngraxwqk4jgrf5f18jy195yrp7c06n1gf31pbplq79mg1bcj";
-    buildInputs = [ libterminfo libcurses ];
-    propagatedBuildInputs = compatIfNeeded;
-    SHLIBINSTALLDIR = "$(out)/lib";
-    makeFlags = defaultMakeFlags ++ [ "LIBDO.terminfo=${libterminfo}/lib" ];
-    postPatch = ''
-      sed -i '1i #undef bool_t' $COMPONENT_PATH/el.h
-      substituteInPlace $COMPONENT_PATH/config.h \
-        --replace "#define HAVE_STRUCT_DIRENT_D_NAMLEN 1" ""
-      substituteInPlace $COMPONENT_PATH/readline/Makefile --replace /usr/include "$out/include"
-    '';
-    env.NIX_CFLAGS_COMPILE = toString [
-      "-D__noinline="
-      "-D__scanflike(a,b)="
-      "-D__va_list=va_list"
-    ];
-  }
+  path = "lib/libedit";
+  buildInputs = [
+    libterminfo
+    libcurses
+  ];
+  propagatedBuildInputs = compatIfNeeded;
+  SHLIBINSTALLDIR = "$(out)/lib";
+  makeFlags = defaultMakeFlags ++ [ "LIBDO.terminfo=${libterminfo}/lib" ];
+  postPatch = ''
+    sed -i '1i #undef bool_t' $COMPONENT_PATH/el.h
+    substituteInPlace $COMPONENT_PATH/config.h \
+      --replace "#define HAVE_STRUCT_DIRENT_D_NAMLEN 1" ""
+    substituteInPlace $COMPONENT_PATH/readline/Makefile --replace /usr/include "$out/include"
+  '';
+  env.NIX_CFLAGS_COMPILE = toString [
+    "-D__noinline="
+    "-D__scanflike(a,b)="
+    "-D__va_list=va_list"
+  ];
+}
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/libm.nix b/pkgs/os-specific/bsd/netbsd/pkgs/libm.nix
index 86990088adf28..f010c808c4843 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/libm.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/libm.nix
@@ -1,10 +1,18 @@
-{ lib, mkDerivation, sys }:
+{ lib, mkDerivation }:
 
 mkDerivation {
   path = "lib/libm";
-  version = "9.2";
-  sha256 = "1apwfr26shdmbqqnmg7hxf7bkfxw44ynqnnnghrww9bnhqdnsy92";
+
+  libcMinimal = true;
+
+  outputs = [
+    "out"
+    "man"
+  ];
+
   SHLIBINSTALLDIR = "$(out)/lib";
+
+  extraPaths = [ "sys" ];
+
   meta.platforms = lib.platforms.netbsd;
-  extraPaths = [ sys.src ];
 }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/libossaudio.nix b/pkgs/os-specific/bsd/netbsd/pkgs/libossaudio.nix
index 51bbc216e2046..a74176db68cdf 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/libossaudio.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/libossaudio.nix
@@ -2,7 +2,5 @@
 
 mkDerivation {
   path = "lib/libossaudio";
-  version = "9.2";
-  sha256 = "16l3bfy6dcwqnklvh3x0ps8ld1y504vf57v9rx8f9adzhb797jh0";
   meta.platforms = lib.platforms.netbsd;
 }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/libpci.nix b/pkgs/os-specific/bsd/netbsd/pkgs/libpci.nix
index 21ca0f62fd4e4..69d1ff5accca4 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/libpci.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/libpci.nix
@@ -1,11 +1,13 @@
-{ lib, mkDerivation, sys }:
+{
+  lib,
+  mkDerivation,
+  sys,
+}:
 
 mkDerivation {
   pname = "libpci";
   path = "lib/libpci";
-  version = "9.2";
-  sha256 = "+IOEO1Bw3/H3iCp3uk3bwsFZbvCqN5Ciz70irnPl8E8=";
   env.NIX_CFLAGS_COMPILE = toString [ "-I." ];
   meta.platforms = lib.platforms.netbsd;
-  extraPaths = [ sys.src ];
+  extraPaths = [ sys.path ];
 }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/libpthread/base.nix b/pkgs/os-specific/bsd/netbsd/pkgs/libpthread/base.nix
deleted file mode 100644
index 485a12837bcb4..0000000000000
--- a/pkgs/os-specific/bsd/netbsd/pkgs/libpthread/base.nix
+++ /dev/null
@@ -1,6 +0,0 @@
-{
-  path = "lib/libpthread";
-  version = "9.2";
-  sha256 = "0mlmc31k509dwfmx5s2x010wxjc44mr6y0cbmk30cfipqh8c962h";
-}
-
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/libpthread/headers.nix b/pkgs/os-specific/bsd/netbsd/pkgs/libpthread/headers.nix
index 3896562501fc5..41a447e2b4083 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/libpthread/headers.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/libpthread/headers.nix
@@ -1,9 +1,10 @@
 { lib, mkDerivation }:
 
-mkDerivation (import ./base.nix // {
+mkDerivation {
+  path = "lib/libpthread";
   pname = "libpthread-headers";
   installPhase = "includesPhase";
   dontBuild = true;
   noCC = true;
   meta.platforms = lib.platforms.netbsd;
-})
+}
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/libpthread/package.nix b/pkgs/os-specific/bsd/netbsd/pkgs/libpthread/package.nix
index c2a0d407718e9..8e0ef5fd1b74e 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/libpthread/package.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/libpthread/package.nix
@@ -1,15 +1,35 @@
-{ lib, mkDerivation
-, headers
-, common, libc, librt, sys
+{
+  lib,
+  stdenvLibcMinimal,
+  mkDerivation,
+  headers,
+  libcMinimal,
+  librt,
 }:
 
-mkDerivation (import ./base.nix // {
-  pname = "libpthread";
-  installPhase = null;
-  noCC = false;
-  dontBuild = false;
-  buildInputs = [ headers ];
+mkDerivation {
+  path = "lib/libpthread";
+
+  libcMinimal = true;
+
+  outputs = [
+    "out"
+    "dev"
+    "man"
+  ];
+
   SHLIBINSTALLDIR = "$(out)/lib";
-  extraPaths = [ common libc.src librt.src sys.src ];
+
+  # Hack around GCC's limits.h missing the include_next we want See
+  # https://gcc.gnu.org/legacy-ml/gcc/2003-10/msg01278.html
+  NIX_CFLAGS_COMPILE_BEFORE = "-isystem ${stdenvLibcMinimal.cc.libc.dev}/include";
+
+  extraPaths = [
+    "common"
+    libcMinimal.path
+    librt.path
+    "sys"
+  ];
+
   meta.platforms = lib.platforms.netbsd;
-})
+}
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/libresolv.nix b/pkgs/os-specific/bsd/netbsd/pkgs/libresolv.nix
index 2427592e4aa9a..63e95de850c09 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/libresolv.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/libresolv.nix
@@ -1,9 +1,15 @@
-{ lib, mkDerivation, libc }:
+{
+  lib,
+  mkDerivation,
+  libcMinimal,
+}:
 
 mkDerivation {
   path = "lib/libresolv";
-  version = "9.2";
-  sha256 = "1am74s74mf1ynwz3p4ncjkg63f78a1zjm983q166x4sgzps15626";
+
+  libcMinimal = true;
+
+  extraPaths = [ libcMinimal.path ];
+
   meta.platforms = lib.platforms.netbsd;
-  extraPaths = [ libc.src ];
 }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/librpcsvc.nix b/pkgs/os-specific/bsd/netbsd/pkgs/librpcsvc.nix
index 87524339e268f..60609ed134fb7 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/librpcsvc.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/librpcsvc.nix
@@ -1,21 +1,39 @@
-{ lib
-, mkDerivation
-, defaultMakeFlags
-, bsdSetupHook, netbsdSetupHook
-, makeMinimal
-, install, tsort, lorder, rpcgen, statHook
+{
+  lib,
+  mkDerivation,
+  defaultMakeFlags,
+  bsdSetupHook,
+  netbsdSetupHook,
+  makeMinimal,
+  install,
+  tsort,
+  lorder,
+  rpcgen,
+  statHook,
 }:
 
 mkDerivation {
   path = "lib/librpcsvc";
-  version = "9.2";
-  sha256 = "1q34pfiyjbrgrdqm46jwrsqms49ly6z3b0xh1wg331zga900vq5n";
-  makeFlags = defaultMakeFlags ++ [ "INCSDIR=$(out)/include/rpcsvc" ];
-  meta.platforms = lib.platforms.netbsd;
+
+  libcMinimal = true;
+
+  outputs = [
+    "out"
+    "dev"
+  ];
+
   nativeBuildInputs = [
-    bsdSetupHook netbsdSetupHook
+    bsdSetupHook
+    netbsdSetupHook
     makeMinimal
-    install tsort lorder rpcgen statHook
+    install
+    tsort
+    lorder
+    rpcgen
+    statHook
   ];
-}
 
+  makeFlags = defaultMakeFlags ++ [ "INCSDIR=$(dev)/include/rpcsvc" ];
+
+  meta.platforms = lib.platforms.netbsd;
+}
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/librt.nix b/pkgs/os-specific/bsd/netbsd/pkgs/librt.nix
index 2ffe0787004a5..c69062d9e87df 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/librt.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/librt.nix
@@ -1,13 +1,22 @@
-{ lib, mkDerivation, libc, headers }:
+{
+  lib,
+  mkDerivation,
+  libcMinimal,
+}:
 
 mkDerivation {
   path = "lib/librt";
-  version = "9.2";
-  sha256 = "07f8mpjcqh5kig5z5sp97fg55mc4dz6aa1x5g01nv2pvbmqczxc6";
+
+  libcMinimal = true;
+
+  outputs = [
+    "out"
+    "man"
+  ];
+
+  extraPaths = [ libcMinimal.path ] ++ libcMinimal.extraPaths;
+
+  inherit (libcMinimal) postPatch;
+
   meta.platforms = lib.platforms.netbsd;
-  extraPaths = [ libc.src ] ++ libc.extraPaths;
-  postPatch = ''
-    sed -i 's,/usr\(/include/sys/syscall.h\),${headers}\1,g' \
-      $BSDSRCDIR/lib/{libc,librt}/sys/Makefile.inc
-  '';
 }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/libterminfo.nix b/pkgs/os-specific/bsd/netbsd/pkgs/libterminfo.nix
index b9268cc7ba333..c02410a52ed2a 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/libterminfo.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/libterminfo.nix
@@ -1,17 +1,31 @@
-{ mkDerivation
-, bsdSetupHook, netbsdSetupHook
-, makeMinimal, install, tsort, lorder, mandoc, statHook, nbperf, tic, rsync
-, compatIfNeeded
-, fetchNetBSD
+{
+  mkDerivation,
+  bsdSetupHook,
+  netbsdSetupHook,
+  makeMinimal,
+  install,
+  tsort,
+  lorder,
+  mandoc,
+  statHook,
+  nbperf,
+  tic,
+  compatIfNeeded,
 }:
 
 mkDerivation {
   path = "lib/libterminfo";
-  version = "9.2";
-  sha256 = "0pq05k3dj0dfsczv07frnnji92mazmy2qqngqbx2zgqc1x251414";
   nativeBuildInputs = [
-    bsdSetupHook netbsdSetupHook
-    makeMinimal install tsort lorder mandoc statHook nbperf tic rsync
+    bsdSetupHook
+    netbsdSetupHook
+    makeMinimal
+    install
+    tsort
+    lorder
+    mandoc
+    statHook
+    nbperf
+    tic
   ];
   buildInputs = compatIfNeeded;
   SHLIBINSTALLDIR = "$(out)/lib";
@@ -26,7 +40,5 @@ mkDerivation {
   postInstall = ''
     make -C $BSDSRCDIR/share/terminfo $makeFlags BINDIR=$out/share install
   '';
-  extraPaths = [
-    (fetchNetBSD "share/terminfo" "9.2" "1vh9rl4w8118a9qdpblfxmv1wkpm83rm9gb4rzz5bpm56i6d7kk7")
-  ];
+  extraPaths = [ "share/terminfo" ];
 }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/libutil.nix b/pkgs/os-specific/bsd/netbsd/pkgs/libutil.nix
index 4aaf8f0e7ab63..c217f6a7ce465 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/libutil.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/libutil.nix
@@ -1,21 +1,52 @@
-{ mkDerivation
-, common, libc, sys
-, bsdSetupHook, netbsdSetupHook
-, makeMinimal
-, byacc, install, tsort, lorder, mandoc, statHook, rsync
-, headers
+{
+  lib,
+  stdenvLibcMinimal,
+  mkDerivation,
+  bsdSetupHook,
+  netbsdSetupHook,
+  makeMinimal,
+  byacc,
+  install,
+  tsort,
+  lorder,
+  mandoc,
+  statHook,
+  headers,
 }:
 
 mkDerivation {
   path = "lib/libutil";
-  version = "9.2";
-  sha256 = "02gm5a5zhh8qp5r5q5r7x8x6x50ir1i0ncgsnfwh1vnrz6mxbq7z";
-  extraPaths = [ common libc.src sys.src ];
+
+  libcMinimal = true;
+
+  outputs = [
+    "out"
+    "man"
+  ];
+
   nativeBuildInputs = [
-    bsdSetupHook netbsdSetupHook
+    bsdSetupHook
+    netbsdSetupHook
     makeMinimal
-    byacc install tsort lorder mandoc statHook rsync
+    byacc
+    install
+    tsort
+    lorder
+    mandoc
+    statHook
   ];
-  buildInputs = [ headers ];
+
   SHLIBINSTALLDIR = "$(out)/lib";
+
+  # Hack around GCC's limits.h missing the include_next we want See
+  # https://gcc.gnu.org/legacy-ml/gcc/2003-10/msg01278.html
+  NIX_CFLAGS_COMPILE_BEFORE = "-isystem ${stdenvLibcMinimal.cc.libc.dev}/include";
+
+  extraPaths = [
+    "common"
+    "lib/libc"
+    "sys"
+  ];
+
+  meta.platforms = lib.platforms.netbsd;
 }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/locale/package.nix b/pkgs/os-specific/bsd/netbsd/pkgs/locale/package.nix
index 3c61e8517b35b..e3e06ba1a152c 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/locale/package.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/locale/package.nix
@@ -2,8 +2,6 @@
 
 mkDerivation {
   path = "usr.bin/locale";
-  version = "9.2";
-  sha256 = "0kk6v9k2bygq0wf9gbinliqzqpzs9bgxn0ndyl2wcv3hh2bmsr9p";
   patches = [ ./locale.patch ];
   env.NIX_CFLAGS_COMPILE = "-DYESSTR=__YESSTR -DNOSTR=__NOSTR";
 }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/lorder.nix b/pkgs/os-specific/bsd/netbsd/pkgs/lorder.nix
index d3109601382a0..b10b8caee2ff2 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/lorder.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/lorder.nix
@@ -1,16 +1,21 @@
-{ mkDerivation
-, bsdSetupHook, netbsdSetupHook
-, makeMinimal
-, install, mandoc, groff, rsync
+{
+  mkDerivation,
+  bsdSetupHook,
+  netbsdSetupHook,
+  makeMinimal,
+  install,
+  mandoc,
+  groff,
 }:
 
 mkDerivation {
   path = "usr.bin/lorder";
-  version = "9.2";
-  sha256 = "0rjf9blihhm0n699vr2bg88m4yjhkbxh6fxliaay3wxkgnydjwn2";
   nativeBuildInputs = [
-    bsdSetupHook netbsdSetupHook
+    bsdSetupHook
+    netbsdSetupHook
     makeMinimal
-    install mandoc groff rsync
+    install
+    mandoc
+    groff
   ];
 }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/make-rules.nix b/pkgs/os-specific/bsd/netbsd/pkgs/make-rules.nix
new file mode 100644
index 0000000000000..e16027b535fca
--- /dev/null
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/make-rules.nix
@@ -0,0 +1,70 @@
+{
+  lib,
+  mkDerivation,
+  stdenv,
+  bsdSetupHook,
+  netbsdSetupHook,
+}:
+
+mkDerivation {
+  path = "share/mk";
+  noCC = true;
+
+  buildInputs = [ ];
+  nativeBuildInputs = [
+    bsdSetupHook
+    netbsdSetupHook
+  ];
+
+  dontBuild = true;
+
+  postPatch =
+    ''
+      substituteInPlace $BSDSRCDIR/share/mk/bsd.doc.mk \
+        --replace '-o ''${DOCOWN}' "" \
+        --replace '-g ''${DOCGRP}' ""
+      for mk in $BSDSRCDIR/share/mk/bsd.inc.mk $BSDSRCDIR/share/mk/bsd.kinc.mk; do
+        substituteInPlace $mk \
+          --replace '-o ''${BINOWN}' "" \
+          --replace '-g ''${BINGRP}' ""
+      done
+      substituteInPlace $BSDSRCDIR/share/mk/bsd.kmodule.mk \
+        --replace '-o ''${KMODULEOWN}' "" \
+        --replace '-g ''${KMODULEGRP}' ""
+      substituteInPlace $BSDSRCDIR/share/mk/bsd.lib.mk \
+        --replace '-o ''${LIBOWN}' "" \
+        --replace '-g ''${LIBGRP}' "" \
+        --replace '-o ''${DEBUGOWN}' "" \
+        --replace '-g ''${DEBUGGRP}' ""
+      substituteInPlace $BSDSRCDIR/share/mk/bsd.lua.mk \
+        --replace '-o ''${LIBOWN}' "" \
+        --replace '-g ''${LIBGRP}' ""
+      substituteInPlace $BSDSRCDIR/share/mk/bsd.man.mk \
+        --replace '-o ''${MANOWN}' "" \
+        --replace '-g ''${MANGRP}' ""
+      substituteInPlace $BSDSRCDIR/share/mk/bsd.nls.mk \
+        --replace '-o ''${NLSOWN}' "" \
+        --replace '-g ''${NLSGRP}' ""
+      substituteInPlace $BSDSRCDIR/share/mk/bsd.prog.mk \
+        --replace '-o ''${BINOWN}' "" \
+        --replace '-g ''${BINGRP}' "" \
+        --replace '-o ''${RUMPBINOWN}' "" \
+        --replace '-g ''${RUMPBINGRP}' "" \
+        --replace '-o ''${DEBUGOWN}' "" \
+        --replace '-g ''${DEBUGGRP}' ""
+
+       substituteInPlace $BSDSRCDIR/share/mk/bsd.lib.mk \
+         --replace '_INSTRANLIB=''${empty(PRESERVE):?-a "''${RANLIB} -t":}' '_INSTRANLIB='
+       substituteInPlace $BSDSRCDIR/share/mk/bsd.kinc.mk \
+         --replace /bin/rm rm
+    ''
+    + lib.optionalString stdenv.targetPlatform.isDarwin ''
+      substituteInPlace $BSDSRCDIR/share/mk/bsd.sys.mk \
+        --replace '-Wl,--fatal-warnings' "" \
+        --replace '-Wl,--warn-shared-textrel' ""
+    '';
+
+  installPhase = ''
+    cp -r . $out
+  '';
+}
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/make.nix b/pkgs/os-specific/bsd/netbsd/pkgs/make.nix
index dcd80ef004678..9612ca7ff4893 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/make.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/make.nix
@@ -1,60 +1,22 @@
-{ lib, mkDerivation, fetchNetBSD, stdenv }:
+{
+  lib,
+  mkDerivation,
+  stdenv,
+  make-rules,
+}:
 
 mkDerivation {
   path = "usr.bin/make";
-  sha256 = "0vi73yicbmbp522qzqvd979cx6zm5jakhy77xh73c1kygf8klccs";
-  version = "9.2";
 
- postPatch = ''
-   substituteInPlace $BSDSRCDIR/share/mk/bsd.doc.mk \
-     --replace '-o ''${DOCOWN}' "" \
-     --replace '-g ''${DOCGRP}' ""
-   for mk in $BSDSRCDIR/share/mk/bsd.inc.mk $BSDSRCDIR/share/mk/bsd.kinc.mk; do
-     substituteInPlace $mk \
-       --replace '-o ''${BINOWN}' "" \
-       --replace '-g ''${BINGRP}' ""
-   done
-   substituteInPlace $BSDSRCDIR/share/mk/bsd.kmodule.mk \
-     --replace '-o ''${KMODULEOWN}' "" \
-     --replace '-g ''${KMODULEGRP}' ""
-   substituteInPlace $BSDSRCDIR/share/mk/bsd.lib.mk \
-     --replace '-o ''${LIBOWN}' "" \
-     --replace '-g ''${LIBGRP}' "" \
-     --replace '-o ''${DEBUGOWN}' "" \
-     --replace '-g ''${DEBUGGRP}' ""
-   substituteInPlace $BSDSRCDIR/share/mk/bsd.lua.mk \
-     --replace '-o ''${LIBOWN}' "" \
-     --replace '-g ''${LIBGRP}' ""
-   substituteInPlace $BSDSRCDIR/share/mk/bsd.man.mk \
-     --replace '-o ''${MANOWN}' "" \
-     --replace '-g ''${MANGRP}' ""
-   substituteInPlace $BSDSRCDIR/share/mk/bsd.nls.mk \
-     --replace '-o ''${NLSOWN}' "" \
-     --replace '-g ''${NLSGRP}' ""
-   substituteInPlace $BSDSRCDIR/share/mk/bsd.prog.mk \
-     --replace '-o ''${BINOWN}' "" \
-     --replace '-g ''${BINGRP}' "" \
-     --replace '-o ''${RUMPBINOWN}' "" \
-     --replace '-g ''${RUMPBINGRP}' "" \
-     --replace '-o ''${DEBUGOWN}' "" \
-     --replace '-g ''${DEBUGGRP}' ""
+  postPatch =
+    make-rules.postPatch
+    + ''
+      # make needs this to pick up our sys make files
+      appendToVar NIX_CFLAGS_COMPILE "-D_PATH_DEFSYSPATH=\"$out/share/mk\""
+    '';
 
-    # make needs this to pick up our sys make files
-    export NIX_CFLAGS_COMPILE+=" -D_PATH_DEFSYSPATH=\"$out/share/mk\""
-
-    substituteInPlace $BSDSRCDIR/share/mk/bsd.lib.mk \
-      --replace '_INSTRANLIB=''${empty(PRESERVE):?-a "''${RANLIB} -t":}' '_INSTRANLIB='
-    substituteInPlace $BSDSRCDIR/share/mk/bsd.kinc.mk \
-      --replace /bin/rm rm
-  '' + lib.optionalString stdenv.isDarwin ''
-    substituteInPlace $BSDSRCDIR/share/mk/bsd.sys.mk \
-      --replace '-Wl,--fatal-warnings' "" \
-      --replace '-Wl,--warn-shared-textrel' ""
-  '';
   postInstall = ''
     make -C $BSDSRCDIR/share/mk FILESDIR=$out/share/mk install
   '';
-  extraPaths = [
-    (fetchNetBSD "share/mk" "9.2" "0w9x77cfnm6zwy40slradzi0ip9gz80x6lk7pvnlxzsr2m5ra5sy")
-  ];
+  extraPaths = [ "share/mk" ];
 }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/makeMinimal.nix b/pkgs/os-specific/bsd/netbsd/pkgs/makeMinimal.nix
index 18655b3bf863e..aab6e7f2b0dfe 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/makeMinimal.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/makeMinimal.nix
@@ -1,23 +1,27 @@
-{ mkDerivation
-, bsdSetupHook, netbsdSetupHook, rsync
-, make
+{
+  mkDerivation,
+  bsdSetupHook,
+  netbsdSetupHook,
+  make,
+  make-rules,
 }:
 
 mkDerivation {
   path = "tools/make";
-  sha256 = "0fh0nrnk18m613m5blrliq2aydciv51qhc0ihsj4k63incwbk90n";
-  version = "9.2";
 
-  buildInputs = [];
+  buildInputs = [ ];
   nativeBuildInputs = [
-    bsdSetupHook netbsdSetupHook rsync
+    bsdSetupHook
+    netbsdSetupHook
   ];
 
   skipIncludesPhase = true;
 
   postPatch = ''
     patchShebangs $COMPONENT_PATH/configure
-    ${make.postPatch}
+
+    # make needs this to pick up our sys make files
+    appendToVar NIX_CFLAGS_COMPILE "-D_PATH_DEFSYSPATH=\"$out/share/mk\""
   '';
 
   buildPhase = ''
@@ -34,10 +38,10 @@ mkDerivation {
     install -D nbmake $out/bin/nbmake
     ln -s $out/bin/nbmake $out/bin/make
     mkdir -p $out/share
-    cp -r $BSDSRCDIR/share/mk $out/share/mk
+    cp -r ${make-rules} $out/share/mk
 
     runHook postInstall
   '';
 
-  extraPaths = [ make.src ] ++ make.extraPaths;
+  extraPaths = [ make.path ];
 }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/man.nix b/pkgs/os-specific/bsd/netbsd/pkgs/man.nix
index bce5bcee66946..527c547fdaad0 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/man.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/man.nix
@@ -3,8 +3,6 @@
 mkDerivation {
   path = "share/man";
   noCC = true;
-  version = "9.2";
-  sha256 = "1l4lmj4kmg8dl86x94sr45w0xdnkz8dn4zjx0ipgr9bnq98663zl";
   # man0 generates a man.pdf using ps2pdf, but doesn't install it later,
   # so we can avoid the dependency on ghostscript
   postPatch = ''
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/misc.nix b/pkgs/os-specific/bsd/netbsd/pkgs/misc.nix
index 7f96f4d1495ad..726acf27d50b0 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/misc.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/misc.nix
@@ -3,7 +3,5 @@
 mkDerivation {
   path = "share/misc";
   noCC = true;
-  version = "9.2";
-  sha256 = "1j2cdssdx6nncv8ffj7f7ybl7m9hadjj8vm8611skqdvxnjg6nbc";
   makeFlags = defaultMakeFlags ++ [ "BINDIR=$(out)/share" ];
 }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/mkDerivation.nix b/pkgs/os-specific/bsd/netbsd/pkgs/mkDerivation.nix
index 82e8889f6cf4d..8605bfbfcebe2 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/mkDerivation.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/mkDerivation.nix
@@ -1,82 +1,134 @@
-{ lib
-, stdenvNoCC, stdenv
-, fetchNetBSD
-, bsdSetupHook, netbsdSetupHook
-, makeMinimal
-, install, tsort, lorder
-, mandoc, groff, statHook, rsync
-, compatIfNeeded
-, defaultMakeFlags
+{
+  lib,
+  stdenv,
+  stdenvNoCC,
+  crossLibcStdenv,
+  stdenvLibcMinimal,
+  runCommand,
+  rsync,
+  source,
+  bsdSetupHook,
+  netbsdSetupHook,
+  makeMinimal,
+  install,
+  tsort,
+  lorder,
+  mandoc,
+  groff,
+  statHook,
+  compatIfNeeded,
+  defaultMakeFlags,
+  version,
 }:
 
-lib.makeOverridable (attrs: let
-  stdenv' = if attrs.noCC or false then stdenvNoCC else stdenv;
-in stdenv'.mkDerivation ({
-  pname = "${attrs.pname or (baseNameOf attrs.path)}-netbsd";
-  inherit (attrs) version;
-  src = fetchNetBSD attrs.path attrs.version attrs.sha256;
+lib.makeOverridable (
+  attrs:
+  let
+    stdenv' =
+      if attrs.noCC or false then
+        stdenvNoCC
+      else if attrs.noLibc or false then
+        crossLibcStdenv
+      else if attrs.libcMinimal or false then
+        stdenvLibcMinimal
+      else
+        stdenv;
+  in
+  stdenv'.mkDerivation (
+    rec {
+      pname = "${attrs.pname or (baseNameOf attrs.path)}-netbsd";
+      inherit version;
+      src = runCommand "${pname}-filtered-src" { nativeBuildInputs = [ rsync ]; } ''
+        for p in ${lib.concatStringsSep " " ([ attrs.path ] ++ attrs.extraPaths or [ ])}; do
+          set -x
+          path="$out/$p"
+          mkdir -p "$(dirname "$path")"
+          src_path="${source}/$p"
+          if [[ -d "$src_path" ]]; then src_path+=/; fi
+          rsync --chmod="+w" -r "$src_path" "$path"
+          set +x
+        done
+      '';
 
-  extraPaths = [ ];
+      extraPaths = [ ];
 
-  nativeBuildInputs = [
-    bsdSetupHook netbsdSetupHook
-    makeMinimal
-    install tsort lorder mandoc groff statHook rsync
-  ];
-  buildInputs = compatIfNeeded;
+      nativeBuildInputs = [
+        bsdSetupHook
+        netbsdSetupHook
+        makeMinimal
+        install
+        tsort
+        lorder
+        mandoc
+        groff
+        statHook
+      ];
+      buildInputs = compatIfNeeded;
 
-  HOST_SH = stdenv'.shell;
+      HOST_SH = stdenv'.shell;
 
-  MACHINE_ARCH = {
-    i486 = "i386";
-    i586 = "i386";
-    i686 = "i386";
-  }.${stdenv'.hostPlatform.parsed.cpu.name}
-    or stdenv'.hostPlatform.parsed.cpu.name;
+      MACHINE_ARCH =
+        {
+          i486 = "i386";
+          i586 = "i386";
+          i686 = "i386";
+        }
+        .${stdenv'.hostPlatform.parsed.cpu.name} or stdenv'.hostPlatform.parsed.cpu.name;
 
-  MACHINE = {
-    x86_64 = "amd64";
-    aarch64 = "evbarm64";
-    i486 = "i386";
-    i586 = "i386";
-    i686 = "i386";
-  }.${stdenv'.hostPlatform.parsed.cpu.name}
-    or stdenv'.hostPlatform.parsed.cpu.name;
+      MACHINE =
+        {
+          x86_64 = "amd64";
+          aarch64 = "evbarm64";
+          i486 = "i386";
+          i586 = "i386";
+          i686 = "i386";
+        }
+        .${stdenv'.hostPlatform.parsed.cpu.name} or stdenv'.hostPlatform.parsed.cpu.name;
 
-  COMPONENT_PATH = attrs.path;
+      COMPONENT_PATH = attrs.path;
 
-  makeFlags = defaultMakeFlags;
+      makeFlags = defaultMakeFlags;
 
-  strictDeps = true;
+      strictDeps = true;
 
-  meta = with lib; {
-    maintainers = with maintainers; [ matthewbauer qyliss ];
-    platforms = platforms.unix;
-    license = licenses.bsd2;
-  };
-
-} // lib.optionalAttrs stdenv'.hasCC {
-  # TODO should CC wrapper set this?
-  CPP = "${stdenv'.cc.targetPrefix}cpp";
-} // lib.optionalAttrs stdenv'.isDarwin {
-  MKRELRO = "no";
-} // lib.optionalAttrs (stdenv'.cc.isClang or false) {
-  HAVE_LLVM = lib.versions.major (lib.getVersion stdenv'.cc.cc);
-} // lib.optionalAttrs (stdenv'.cc.isGNU or false) {
-  HAVE_GCC = lib.versions.major (lib.getVersion stdenv'.cc.cc);
-} // lib.optionalAttrs (stdenv'.isx86_32) {
-  USE_SSP = "no";
-} // lib.optionalAttrs (attrs.headersOnly or false) {
-  installPhase = "includesPhase";
-  dontBuild = true;
-} // attrs // {
-  # Files that use NetBSD-specific macros need to have nbtool_config.h
-  # included ahead of them on non-NetBSD platforms.
-  postPatch = lib.optionalString (!stdenv'.hostPlatform.isNetBSD) ''
-    set +e
-    grep -Zlr "^__RCSID
-    ^__BEGIN_DECLS" $COMPONENT_PATH | xargs -0r grep -FLZ nbtool_config.h |
-        xargs -0tr sed -i '0,/^#/s//#include <nbtool_config.h>\n\0/'
-    set -e
-  '' + attrs.postPatch or "";
-}))
+      meta = with lib; {
+        maintainers = with maintainers; [
+          matthewbauer
+          qyliss
+        ];
+        platforms = platforms.unix;
+        license = licenses.bsd2;
+      };
+    }
+    // lib.optionalAttrs stdenv'.hasCC {
+      # TODO should CC wrapper set this?
+      CPP = "${stdenv'.cc.targetPrefix}cpp";
+    }
+    // lib.optionalAttrs stdenv'.isDarwin { MKRELRO = "no"; }
+    // lib.optionalAttrs (stdenv'.cc.isClang or false) {
+      HAVE_LLVM = lib.versions.major (lib.getVersion stdenv'.cc.cc);
+    }
+    // lib.optionalAttrs (stdenv'.cc.isGNU or false) {
+      HAVE_GCC = lib.versions.major (lib.getVersion stdenv'.cc.cc);
+    }
+    // lib.optionalAttrs (stdenv'.isx86_32) { USE_SSP = "no"; }
+    // lib.optionalAttrs (attrs.headersOnly or false) {
+      installPhase = "includesPhase";
+      dontBuild = true;
+    }
+    // attrs
+    // {
+      # Files that use NetBSD-specific macros need to have nbtool_config.h
+      # included ahead of them on non-NetBSD platforms.
+      postPatch =
+        lib.optionalString (!stdenv'.hostPlatform.isNetBSD) ''
+          set +e
+          grep -Zlr "^__RCSID
+          ^__BEGIN_DECLS" $COMPONENT_PATH | xargs -0r grep -FLZ nbtool_config.h |
+              xargs -0tr sed -i '0,/^#/s//#include <nbtool_config.h>\n\0/'
+          set -e
+        ''
+        + attrs.postPatch or "";
+    }
+  )
+)
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/mknod.nix b/pkgs/os-specific/bsd/netbsd/pkgs/mknod.nix
index 5c4c172e40a7d..bf7d8b7db689b 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/mknod.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/mknod.nix
@@ -1,7 +1,3 @@
 { mkDerivation }:
 
-mkDerivation {
-  path = "sbin/mknod";
-  version = "9.2";
-  sha256 = "1d9369shzwgixz3nph991i8q5vk7hr04py3n9avbfbhzy4gndqs2";
-}
+mkDerivation { path = "sbin/mknod"; }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/mtree.nix b/pkgs/os-specific/bsd/netbsd/pkgs/mtree.nix
index 723da3ad891bc..8310941c7cb22 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/mtree.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/mtree.nix
@@ -2,7 +2,5 @@
 
 mkDerivation {
   path = "usr.sbin/mtree";
-  version = "9.2";
-  sha256 = "04p7w540vz9npvyb8g8hcf2xa05phn1y88hsyrcz3vwanvpc0yv9";
-  extraPaths = [ mknod.src ];
+  extraPaths = [ mknod.path ];
 }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/nbperf.nix b/pkgs/os-specific/bsd/netbsd/pkgs/nbperf.nix
index 02ce7cf6ce5eb..3b52eae96c3b2 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/nbperf.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/nbperf.nix
@@ -1,8 +1,3 @@
 { mkDerivation }:
 
-mkDerivation {
-  path = "usr.bin/nbperf";
-  version = "9.2";
-  sha256 = "1nxc302vgmjhm3yqdivqyfzslrg0vjpbss44s74rcryrl19mma9r";
-}
-
+mkDerivation { path = "usr.bin/nbperf"; }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/netbsdSetupHook/package.nix b/pkgs/os-specific/bsd/netbsd/pkgs/netbsdSetupHook/package.nix
index e80571f2bf9a2..31368523ee6fc 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/netbsdSetupHook/package.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/netbsdSetupHook/package.nix
@@ -1,5 +1,3 @@
 { makeSetupHook }:
 
-makeSetupHook {
-    name = "netbsd-setup-hook";
-  } ./setup-hook.sh
+makeSetupHook { name = "netbsd-setup-hook"; } ./setup-hook.sh
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/netbsdSetupHook/setup-hook.sh b/pkgs/os-specific/bsd/netbsd/pkgs/netbsdSetupHook/setup-hook.sh
index fa8b19e7d8ce4..8c3742a76534b 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/netbsdSetupHook/setup-hook.sh
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/netbsdSetupHook/setup-hook.sh
@@ -1,15 +1,6 @@
-mergeNetBSDSourceDir() {
-  # merge together all extra paths
-  # there should be a better way to do this
-  chmod -R u+w $BSDSRCDIR
-  for path in $extraPaths; do
-    rsync -Er --chmod u+w $path/ $BSDSRCDIR/
-  done
-}
-
 addNetBSDMakeFlags() {
   makeFlags="INCSDIR=${!outputDev}/include $makeFlags"
+  makeFlags="MANDIR=${!outputMan}/share/man $makeFlags"
 }
 
-postUnpackHooks+=(mergeNetBSDSourceDir)
 preConfigureHooks+=(addNetBSDMakeFlags)
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/rpcgen.nix b/pkgs/os-specific/bsd/netbsd/pkgs/rpcgen.nix
index b1482d4dff112..dc6ceb7bd634d 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/rpcgen.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/rpcgen.nix
@@ -1,7 +1,3 @@
 { mkDerivation }:
 
-mkDerivation {
-  path = "usr.bin/rpcgen";
-  version = "9.2";
-  sha256 = "1kfgfx54jg98wbg0d95p0rvf4w0302v8fz724b0bdackdsrd4988";
-}
+mkDerivation { path = "usr.bin/rpcgen"; }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/source.nix b/pkgs/os-specific/bsd/netbsd/pkgs/source.nix
new file mode 100644
index 0000000000000..432de84ffb3e1
--- /dev/null
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/source.nix
@@ -0,0 +1,12 @@
+{
+  lib,
+  fetchcvs,
+  version,
+}:
+
+fetchcvs {
+  cvsRoot = ":pserver:anoncvs@anoncvs.NetBSD.org:/cvsroot";
+  module = "src";
+  tag = "netbsd-${lib.replaceStrings [ "." ] [ "-" ] version}-RELEASE";
+  sha256 = "sha256-+onT/ajWayaKALucaZBqoiEkvBBI400Fs2OCtMf/bYU=";
+}
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/stat/hook.nix b/pkgs/os-specific/bsd/netbsd/pkgs/stat/hook.nix
index 78186f55cd455..e38c53a3aac57 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/stat/hook.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/stat/hook.nix
@@ -1,4 +1,8 @@
-{ makeSetupHook, writeText, stat }:
+{
+  makeSetupHook,
+  writeText,
+  stat,
+}:
 
 # stat isn't in POSIX, and NetBSD stat supports a completely
 # different range of flags than GNU stat, so including it in PATH
@@ -6,9 +10,8 @@
 # NetBSD's build system and NetBSD stat without including it in
 # PATH.
 
-makeSetupHook {
-  name = "netbsd-stat-hook";
-} (writeText "netbsd-stat-hook-impl" ''
-  makeFlagsArray+=(TOOL_STAT=${stat}/bin/stat)
-'')
-
+makeSetupHook { name = "netbsd-stat-hook"; } (
+  writeText "netbsd-stat-hook-impl" ''
+    makeFlagsArray+=(TOOL_STAT=${stat}/bin/stat)
+  ''
+)
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/stat/package.nix b/pkgs/os-specific/bsd/netbsd/pkgs/stat/package.nix
index fa4d5cb3e21f6..f55bab160bc2c 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/stat/package.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/stat/package.nix
@@ -1,7 +1,11 @@
-{ mkDerivation
-, bsdSetupHook, netbsdSetupHook
-, makeMinimal
-, install, mandoc, groff, rsync
+{
+  mkDerivation,
+  bsdSetupHook,
+  netbsdSetupHook,
+  makeMinimal,
+  install,
+  mandoc,
+  groff,
 }:
 
 # Don't add this to nativeBuildInputs directly.
@@ -9,11 +13,12 @@
 
 mkDerivation {
   path = "usr.bin/stat";
-  version = "9.2";
-  sha256 = "18nqwlndfc34qbbgqx5nffil37jfq9aw663ippasfxd2hlyc106x";
   nativeBuildInputs = [
-    bsdSetupHook netbsdSetupHook
+    bsdSetupHook
+    netbsdSetupHook
     makeMinimal
-    install mandoc groff rsync
+    install
+    mandoc
+    groff
   ];
 }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/sys/base.nix b/pkgs/os-specific/bsd/netbsd/pkgs/sys/base.nix
index ba458ef5c712f..116aa46e73503 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/sys/base.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/sys/base.nix
@@ -1,14 +1,21 @@
-{ lib, mkDerivation
-, include
-, bsdSetupHook, netbsdSetupHook
-, makeMinimal, install, tsort, lorder, statHook, rsync, uudecode, config, genassym
-, defaultMakeFlags
-, common
+{
+  lib,
+  mkDerivation,
+  include,
+  bsdSetupHook,
+  netbsdSetupHook,
+  makeMinimal,
+  install,
+  tsort,
+  lorder,
+  statHook,
+  uudecode,
+  config,
+  genassym,
+  defaultMakeFlags,
 }:
 {
   path = "sys";
-  version = "9.2";
-  sha256 = "03s18q8d9giipf05bx199fajc2qwikji0djz7hw63d2lya6bfnpj";
 
   # Make the build ignore linker warnings
   prePatch = ''
@@ -29,23 +36,33 @@
     ''
       substituteInPlace sys/arch/i386/stand/efiboot/Makefile.efiboot \
         --replace "-nocombreloc" "-z nocombreloc"
-    '' +
-    # multiple header dirs, see above
-    include.postPatch;
+    ''
+    +
+      # multiple header dirs, see above
+      include.postPatch;
 
   CONFIG = "GENERIC";
 
   propagatedBuildInputs = [ include ];
   nativeBuildInputs = [
-    bsdSetupHook netbsdSetupHook
-    makeMinimal install tsort lorder statHook rsync uudecode config genassym
+    bsdSetupHook
+    netbsdSetupHook
+    makeMinimal
+    install
+    tsort
+    lorder
+    statHook
+    uudecode
+    config
+    genassym
   ];
 
-  postConfigure = ''
-    pushd arch/$MACHINE/conf
-    config $CONFIG
-    popd
-  ''
+  postConfigure =
+    ''
+      pushd arch/$MACHINE/conf
+      config $CONFIG
+      popd
+    ''
     # multiple header dirs, see above
     + include.postConfigure;
 
@@ -67,6 +84,5 @@
   '';
 
   meta.platforms = lib.platforms.netbsd;
-  extraPaths = [ common ];
-
+  extraPaths = [ "common" ];
 }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/sys/headers.nix b/pkgs/os-specific/bsd/netbsd/pkgs/sys/headers.nix
index a3f20d772a3e7..d19d9d86cb382 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/sys/headers.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/sys/headers.nix
@@ -1,21 +1,45 @@
-{ lib, mkDerivation
-, include
-, bsdSetupHook, netbsdSetupHook
-, makeMinimal, install, tsort, lorder, statHook, rsync, uudecode, config, genassym
-, defaultMakeFlags
-, common
+{
+  lib,
+  mkDerivation,
+  include,
+  bsdSetupHook,
+  netbsdSetupHook,
+  makeMinimal,
+  install,
+  tsort,
+  lorder,
+  statHook,
+  uudecode,
+  config,
+  genassym,
+  defaultMakeFlags,
 }:
 let
   base = import ./base.nix {
     inherit
-      lib mkDerivation include bsdSetupHook netbsdSetupHook makeMinimal install
-      tsort lorder statHook rsync uudecode config genassym defaultMakeFlags
-      common;
+      lib
+      mkDerivation
+      include
+      bsdSetupHook
+      netbsdSetupHook
+      makeMinimal
+      install
+      tsort
+      lorder
+      statHook
+      uudecode
+      config
+      genassym
+      defaultMakeFlags
+      ;
   };
 in
-  mkDerivation (base // {
+mkDerivation (
+  base
+  // {
     pname = "sys-headers";
     installPhase = "includesPhase";
     dontBuild = true;
     noCC = true;
-  })
+  }
+)
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/sys/package.nix b/pkgs/os-specific/bsd/netbsd/pkgs/sys/package.nix
index 87031a50346ea..0bdd83126bcf7 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/sys/package.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/sys/package.nix
@@ -1,21 +1,45 @@
-{ lib, mkDerivation
-, include
-, bsdSetupHook, netbsdSetupHook
-, makeMinimal, install, tsort, lorder, statHook, rsync, uudecode, config, genassym
-, defaultMakeFlags
-, common
+{
+  lib,
+  mkDerivation,
+  include,
+  bsdSetupHook,
+  netbsdSetupHook,
+  makeMinimal,
+  install,
+  tsort,
+  lorder,
+  statHook,
+  uudecode,
+  config,
+  genassym,
+  defaultMakeFlags,
 }:
 let
   base = import ./base.nix {
     inherit
-      lib mkDerivation include bsdSetupHook netbsdSetupHook makeMinimal install
-      tsort lorder statHook rsync uudecode config genassym defaultMakeFlags
-      common;
+      lib
+      mkDerivation
+      include
+      bsdSetupHook
+      netbsdSetupHook
+      makeMinimal
+      install
+      tsort
+      lorder
+      statHook
+      uudecode
+      config
+      genassym
+      defaultMakeFlags
+      ;
   };
 in
-  mkDerivation (base // {
+mkDerivation (
+  base
+  // {
     pname = "sys";
     installPhase = null;
     noCC = false;
     dontBuild = false;
-  })
+  }
+)
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/tic.nix b/pkgs/os-specific/bsd/netbsd/pkgs/tic.nix
index 2a7e3644b6be9..42772d1d71285 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/tic.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/tic.nix
@@ -1,28 +1,34 @@
-{ mkDerivation
-, bsdSetupHook, netbsdSetupHook
-, makeMinimal
-, install, mandoc, groff, nbperf, rsync
-, compatIfNeeded
-, defaultMakeFlags
-, libterminfo
-, fetchNetBSD
+{
+  mkDerivation,
+  bsdSetupHook,
+  netbsdSetupHook,
+  makeMinimal,
+  install,
+  mandoc,
+  groff,
+  nbperf,
+  compatIfNeeded,
+  defaultMakeFlags,
+  libterminfo,
 }:
 
 mkDerivation {
   path = "tools/tic";
-  version = "9.2";
-  sha256 = "092y7db7k4kh2jq8qc55126r5qqvlb8lq8mhmy5ipbi36hwb4zrz";
   HOSTPROG = "tic";
   buildInputs = compatIfNeeded;
   nativeBuildInputs = [
-    bsdSetupHook netbsdSetupHook
+    bsdSetupHook
+    netbsdSetupHook
     makeMinimal
-    install mandoc groff nbperf rsync
+    install
+    mandoc
+    groff
+    nbperf
   ];
   makeFlags = defaultMakeFlags ++ [ "TOOLDIR=$(out)" ];
   extraPaths = [
-    libterminfo.src
-    (fetchNetBSD "usr.bin/tic" "9.2" "1mwdfg7yx1g43ss378qsgl5rqhsxskqvsd2mqvrn38qw54i8v5i1")
-    (fetchNetBSD "tools/Makefile.host" "9.2" "15b4ab0n36lqj00j5lz2xs83g7l8isk3wx1wcapbrn66qmzz2sxy")
+    libterminfo.path
+    "usr.bin/tic"
+    "tools/Makefile.host"
   ];
 }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/tsort.nix b/pkgs/os-specific/bsd/netbsd/pkgs/tsort.nix
index 334eccc64c551..c844e48b4412a 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/tsort.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/tsort.nix
@@ -1,16 +1,21 @@
-{ mkDerivation
-, bsdSetupHook, netbsdSetupHook
-, makeMinimal
-, install, mandoc, groff, rsync
+{
+  mkDerivation,
+  bsdSetupHook,
+  netbsdSetupHook,
+  makeMinimal,
+  install,
+  mandoc,
+  groff,
 }:
 
 mkDerivation {
   path = "usr.bin/tsort";
-  version = "9.2";
-  sha256 = "1dqvf9gin29nnq3c4byxc7lfd062pg7m84843zdy6n0z63hnnwiq";
   nativeBuildInputs = [
-    bsdSetupHook netbsdSetupHook
+    bsdSetupHook
+    netbsdSetupHook
     makeMinimal
-    install mandoc groff rsync
+    install
+    mandoc
+    groff
   ];
 }
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/uudecode.nix b/pkgs/os-specific/bsd/netbsd/pkgs/uudecode.nix
index dac9674f9d6f3..1e58df16ceeec 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/uudecode.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/uudecode.nix
@@ -1,10 +1,11 @@
-{ lib, mkDerivation, stdenv }:
+{
+  lib,
+  mkDerivation,
+  stdenv,
+}:
 
 mkDerivation {
   path = "usr.bin/uudecode";
-  version = "9.2";
-  sha256 = "00a3zmh15pg4vx6hz0kaa5mi8d2b1sj4h512d7p6wbvxq6mznwcn";
   env.NIX_CFLAGS_COMPILE = lib.optionalString stdenv.isLinux "-DNO_BASE64";
   NIX_LDFLAGS = lib.optional stdenv.isDarwin "-lresolv";
 }
-
diff --git a/pkgs/os-specific/bsd/openbsd/default.nix b/pkgs/os-specific/bsd/openbsd/default.nix
new file mode 100644
index 0000000000000..bfc88f097865f
--- /dev/null
+++ b/pkgs/os-specific/bsd/openbsd/default.nix
@@ -0,0 +1,50 @@
+{
+  lib,
+  makeScopeWithSplicing',
+  generateSplicesForMkScope,
+  buildPackages,
+}:
+
+let
+  otherSplices = generateSplicesForMkScope "openbsd";
+  buildOpenbsd = otherSplices.selfBuildHost;
+in
+
+makeScopeWithSplicing' {
+  inherit otherSplices;
+  f = (
+    self:
+    lib.packagesFromDirectoryRecursive {
+      callPackage = self.callPackage;
+      directory = ./pkgs;
+    }
+    // {
+      libc = self.callPackage ./pkgs/libc/package.nix {
+        inherit (self) csu include;
+        inherit (buildOpenbsd) makeMinimal;
+        inherit (buildPackages.netbsd)
+          install
+          gencat
+          rpcgen
+          tsort
+          ;
+      };
+      makeMinimal = buildPackages.netbsd.makeMinimal.override { inherit (self) make-rules; };
+      mkDerivation = self.callPackage ./pkgs/mkDerivation.nix {
+        inherit (buildPackages.netbsd) install;
+        inherit (buildPackages.buildPackages) rsync;
+      };
+      include = self.callPackage ./pkgs/include/package.nix {
+        inherit (buildOpenbsd) makeMinimal;
+        inherit (buildPackages.netbsd) install rpcgen mtree;
+      };
+      csu = self.callPackage ./pkgs/csu.nix {
+        inherit (self) include;
+        inherit (buildOpenbsd) makeMinimal;
+        inherit (buildPackages.netbsd) install;
+      };
+      make-rules = self.callPackage ./pkgs/make-rules/package.nix { };
+      lorder = self.callPackage ./pkgs/lorder.nix { inherit (buildPackages.netbsd) install; };
+    }
+  );
+}
diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/csu.nix b/pkgs/os-specific/bsd/openbsd/pkgs/csu.nix
new file mode 100644
index 0000000000000..03a7180425684
--- /dev/null
+++ b/pkgs/os-specific/bsd/openbsd/pkgs/csu.nix
@@ -0,0 +1,23 @@
+{
+  lib,
+  mkDerivation,
+  bsdSetupHook,
+  openbsdSetupHook,
+  makeMinimal,
+  install,
+  include,
+}:
+
+mkDerivation {
+  noLibc = true;
+  path = "lib/csu";
+  nativeBuildInputs = [
+    bsdSetupHook
+    openbsdSetupHook
+    makeMinimal
+    install
+  ];
+  buildInputs = [ include ];
+  meta.platforms = lib.platforms.openbsd;
+  extraPaths = [ "libexec/ld.so" ];
+}
diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/include/package.nix b/pkgs/os-specific/bsd/openbsd/pkgs/include/package.nix
new file mode 100644
index 0000000000000..481e302065fc4
--- /dev/null
+++ b/pkgs/os-specific/bsd/openbsd/pkgs/include/package.nix
@@ -0,0 +1,57 @@
+{
+  lib,
+  mkDerivation,
+  makeMinimal,
+  bsdSetupHook,
+  openbsdSetupHook,
+  install,
+  rpcgen,
+  mtree,
+  pax,
+  buildPackages,
+}:
+mkDerivation {
+  path = "include";
+  noCC = true;
+
+  extraPaths = [
+    "lib"
+    #"sys"
+    "sys/arch"
+    # LDIRS from the mmakefile
+    "sys/crypto"
+    "sys/ddb"
+    "sys/dev"
+    "sys/isofs"
+    "sys/miscfs"
+    "sys/msdosfs"
+    "sys/net"
+    "sys/netinet"
+    "sys/netinet6"
+    "sys/netmpls"
+    "sys/net80211"
+    "sys/nfs"
+    "sys/ntfs"
+    "sys/scsi"
+    "sys/sys"
+    "sys/ufs"
+    "sys/uvm"
+  ];
+
+  nativeBuildInputs = [
+    bsdSetupHook
+    install
+    makeMinimal
+    mtree
+    openbsdSetupHook
+    pax
+    rpcgen
+  ];
+
+  makeFlags = [
+    "RPCGEN_CPP=${buildPackages.stdenv.cc.cc}/bin/cpp"
+    "-B"
+  ];
+
+  headersOnly = true;
+}
diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/libc/disable-librebuild.patch b/pkgs/os-specific/bsd/openbsd/pkgs/libc/disable-librebuild.patch
new file mode 100644
index 0000000000000..58633861a826f
--- /dev/null
+++ b/pkgs/os-specific/bsd/openbsd/pkgs/libc/disable-librebuild.patch
@@ -0,0 +1,12 @@
+diff --git a/lib/libc/Makefile b/lib/libc/Makefile
+index 4bb4b67fcbb..1c8a8e08e60 100644
+--- a/lib/libc/Makefile
++++ b/lib/libc/Makefile
+@@ -6,7 +6,6 @@
+ .include <bsd.own.mk>
+
+ LIB=c
+-LIBREBUILD=y
+ CLEANFILES+=tags Symbols.map
+ CFLAGS+=-Wimplicit
+ #CFLAGS+=-Werror
diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/libc/netbsd-make-to-lower.patch b/pkgs/os-specific/bsd/openbsd/pkgs/libc/netbsd-make-to-lower.patch
new file mode 100644
index 0000000000000..e8d016ceead5a
--- /dev/null
+++ b/pkgs/os-specific/bsd/openbsd/pkgs/libc/netbsd-make-to-lower.patch
@@ -0,0 +1,16 @@
+NetBSD's make uses `${variable:tl}` not `${variable:L}`.
+
+diff --git a/lib/libc/Makefile b/lib/libc/Makefile
+index 4bb4b67fcbb..ffb35c196ea 100644
+--- a/lib/libc/Makefile
++++ b/lib/libc/Makefile
+@@ -11,8 +11,8 @@ CLEANFILES+=tags Symbols.map
+ CFLAGS+=-Wimplicit
+ #CFLAGS+=-Werror
+ LDADD=-nostdlib
+-.if ${COMPILER_VERSION:L} == "clang"
++.if ${COMPILER_VERSION:tl} == "clang"
+ LDADD+=-lcompiler_rt
+ .else
+ LDADD+=-lgcc
+ .endif
diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/libc/package.nix b/pkgs/os-specific/bsd/openbsd/pkgs/libc/package.nix
new file mode 100644
index 0000000000000..1a6b6d06a1936
--- /dev/null
+++ b/pkgs/os-specific/bsd/openbsd/pkgs/libc/package.nix
@@ -0,0 +1,121 @@
+{
+  lib,
+  crossLibcStdenv,
+  mkDerivation,
+  bsdSetupHook,
+  openbsdSetupHook,
+  makeMinimal,
+  install,
+  flex,
+  byacc,
+  gencat,
+  rpcgen,
+  csu,
+  include,
+  ctags,
+  tsort,
+  llvmPackages,
+  fetchpatch,
+}:
+
+mkDerivation {
+  noLibc = true;
+  pname = "libc";
+  path = "lib/libc";
+  extraPaths = [
+    "lib/csu/os-note-elf.h"
+    "sys/arch"
+
+    "lib/libm"
+    "lib/libpthread"
+    "lib/librpcsvc"
+    "lib/librpcsvc"
+    "lib/librthread"
+    "lib/libutil"
+  ];
+
+  patches = [
+    ./netbsd-make-to-lower.patch
+    ./disable-librebuild.patch
+    (fetchpatch {
+      url = "https://marc.info/?l=openbsd-tech&m=171575286706032&q=raw";
+      sha256 = "sha256-2fqabJZLUvXUIWe5WZ4NrTOwgQCXqH49Wo0hAPu5lu0=";
+    })
+  ];
+
+  nativeBuildInputs = [
+    bsdSetupHook
+    openbsdSetupHook
+    makeMinimal
+    install
+    flex
+    byacc
+    gencat
+    rpcgen
+    ctags
+    tsort
+  ];
+
+  buildInputs = [
+    include
+    csu
+  ];
+
+  env.NIX_CFLAGS_COMPILE = builtins.toString [
+    "-B${csu}/lib"
+    "-Wno-error"
+  ];
+
+  # Suppress lld >= 16 undefined version errors
+  # https://github.com/freebsd/freebsd-src/commit/2ba84b4bcdd6012e8cfbf8a0d060a4438623a638
+  env.NIX_LDFLAGS = lib.optionalString (
+    crossLibcStdenv.hostPlatform.linker == "lld"
+  ) "--undefined-version";
+
+  makeFlags = [
+    "STRIP=-s" # flag to install, not command
+    "COMPILER_VERSION=clang"
+    "LIBC_TAGS=no"
+  ];
+
+  postInstall = ''
+    symlink_so () {
+      pushd $out/lib
+      ln -s "lib$1".so.* "lib$1.so"
+      popd
+    }
+
+    symlink_so c
+
+    pushd ${include}
+    find . -type d -exec mkdir -p $out/\{} \;
+    find . \( -type f -o -type l \) -exec cp -pr \{} $out/\{} \;
+    popd
+    substituteInPlace $out/include/sys/time.h --replace "defined (_LIBC)" "true"
+
+    pushd ${csu}
+    find . -type d -exec mkdir -p $out/\{} \;
+    find . \( -type f -o -type l \) -exec cp -pr \{} $out/\{} \;
+    popd
+
+    NIX_CFLAGS_COMPILE+=" -B$out/lib"
+    NIX_CFLAGS_COMPILE+=" -I$out/include"
+    NIX_LDFLAGS+=" -L$out/lib"
+
+    make -C $BSDSRCDIR/lib/libm $makeFlags
+    make -C $BSDSRCDIR/lib/libm $makeFlags install
+    symlink_so m
+
+    make -C $BSDSRCDIR/lib/librthread $makeFlags
+    make -C $BSDSRCDIR/lib/librthread $makeFlags install
+    symlink_so pthread
+
+    make -C $BSDSRCDIR/lib/librpcsvc $makeFlags
+    make -C $BSDSRCDIR/lib/librpcsvc $makeFlags install
+    symlink_so rpcsv
+
+    make -C $BSDSRCDIR/lib/libutil $makeFlags
+    make -C $BSDSRCDIR/lib/libutil $makeFlags install
+    symlink_so util
+  '';
+}
diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/lorder.nix b/pkgs/os-specific/bsd/openbsd/pkgs/lorder.nix
new file mode 100644
index 0000000000000..c923a84317682
--- /dev/null
+++ b/pkgs/os-specific/bsd/openbsd/pkgs/lorder.nix
@@ -0,0 +1,21 @@
+{
+  lib,
+  mkDerivation,
+  bsdSetupHook,
+  openbsdSetupHook,
+  makeMinimal,
+  install,
+}:
+
+mkDerivation {
+  noCC = true;
+  path = "usr.bin/lorder";
+  nativeBuildInputs = [
+    bsdSetupHook
+    openbsdSetupHook
+    makeMinimal
+    install
+  ];
+
+  meta.platforms = lib.platforms.unix;
+}
diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/make-rules/netbsd-make-sinclude.patch b/pkgs/os-specific/bsd/openbsd/pkgs/make-rules/netbsd-make-sinclude.patch
new file mode 100644
index 0000000000000..daaaff56e9b7d
--- /dev/null
+++ b/pkgs/os-specific/bsd/openbsd/pkgs/make-rules/netbsd-make-sinclude.patch
@@ -0,0 +1,15 @@
+NetBSD make prefers `.-include` to `sinclude` (without a dot)
+
+diff --git a/share/mk/bsd.dep.mk b/share/mk/bsd.dep.mk
+index 7019adb57f7..277064eb5c2 100644
+--- a/share/mk/bsd.dep.mk
++++ b/share/mk/bsd.dep.mk
+@@ -11,7 +11,7 @@ depend:
+ # catch22: don't include potentially bogus files we are going to clean
+ .  if !(make(clean) || make(cleandir) || make(obj))
+ .    for o in ${DEPS}
+-       sinclude $o
++.      -include "$o"
+ .    endfor
+ .  endif
+ .endif
diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/make-rules/package.nix b/pkgs/os-specific/bsd/openbsd/pkgs/make-rules/package.nix
new file mode 100644
index 0000000000000..fefa1136eb76d
--- /dev/null
+++ b/pkgs/os-specific/bsd/openbsd/pkgs/make-rules/package.nix
@@ -0,0 +1,35 @@
+{
+  fetchpatch,
+  lib,
+  mkDerivation,
+}:
+
+mkDerivation {
+  path = "share/mk";
+  noCC = true;
+
+  buildInputs = [ ];
+  nativeBuildInputs = [ ];
+
+  dontBuild = true;
+
+  patches = [
+    (fetchpatch {
+      url = "https://marc.info/?l=openbsd-tech&m=171575284906018&q=raw";
+      sha256 = "sha256-bigxJGbaf9mCmFXxLVzQpnUUaEMMDfF3eZkTXVzd6B8=";
+    })
+    ./netbsd-make-sinclude.patch
+  ];
+
+  postPatch = ''
+    sed -i -E \
+      -e 's|/usr/lib|\$\{LIBDIR\}|' \
+      share/mk/bsd.prog.mk
+  '';
+
+  installPhase = ''
+    cp -r share/mk $out
+  '';
+
+  meta.platforms = lib.platforms.unix;
+}
diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/mkDerivation.nix b/pkgs/os-specific/bsd/openbsd/pkgs/mkDerivation.nix
new file mode 100644
index 0000000000000..371c6c58b91fb
--- /dev/null
+++ b/pkgs/os-specific/bsd/openbsd/pkgs/mkDerivation.nix
@@ -0,0 +1,98 @@
+{
+  lib,
+  stdenv,
+  stdenvNoCC,
+  crossLibcStdenv,
+  runCommand,
+  rsync,
+  source,
+  bsdSetupHook,
+  openbsdSetupHook,
+  makeMinimal,
+  install,
+}:
+
+lib.makeOverridable (
+  attrs:
+  let
+    stdenv' =
+      if attrs.noCC or false then
+        stdenvNoCC
+      else if attrs.noLibc or false then
+        crossLibcStdenv
+      else
+        stdenv;
+  in
+  stdenv'.mkDerivation (
+    rec {
+      pname = "${attrs.pname or (baseNameOf attrs.path)}-openbsd";
+      version = "0";
+      src = runCommand "${pname}-filtered-src" { nativeBuildInputs = [ rsync ]; } ''
+        for p in ${lib.concatStringsSep " " ([ attrs.path ] ++ attrs.extraPaths or [ ])}; do
+          set -x
+          path="$out/$p"
+          mkdir -p "$(dirname "$path")"
+          src_path="${source}/$p"
+          if [[ -d "$src_path" ]]; then src_path+=/; fi
+          rsync --chmod="+w" -r "$src_path" "$path"
+          set +x
+        done
+      '';
+
+      extraPaths = [ ];
+
+      nativeBuildInputs = [
+        bsdSetupHook
+        openbsdSetupHook
+        makeMinimal
+        install
+      ];
+
+      HOST_SH = stdenv'.shell;
+
+      makeFlags = [
+        "STRIP=-s" # flag to install, not command
+        "-B"
+      ];
+
+      MACHINE_ARCH =
+        {
+          # amd64 not x86_64 for this on unlike NetBSD
+          x86_64 = "amd64";
+          aarch64 = "arm64";
+          i486 = "i386";
+          i586 = "i386";
+          i686 = "i386";
+        }
+        .${stdenv'.hostPlatform.parsed.cpu.name} or stdenv'.hostPlatform.parsed.cpu.name;
+
+      MACHINE = MACHINE_ARCH;
+
+      MACHINE_CPU = MACHINE_ARCH;
+
+      MACHINE_CPUARCH = MACHINE_ARCH;
+
+      COMPONENT_PATH = attrs.path or null;
+
+      strictDeps = true;
+
+      meta = with lib; {
+        maintainers = with maintainers; [ ericson2314 ];
+        platforms = platforms.openbsd;
+        license = licenses.bsd2;
+      };
+    }
+    // lib.optionalAttrs stdenv'.hasCC {
+      # TODO should CC wrapper set this?
+      CPP = "${stdenv'.cc.targetPrefix}cpp";
+
+      # Since STRIP in `makeFlags` has to be a flag, not the binary itself
+      STRIPBIN = "${stdenv'.cc.bintools.targetPrefix}strip";
+    }
+    // lib.optionalAttrs (attrs.headersOnly or false) {
+      installPhase = "includesPhase";
+      dontBuild = true;
+    }
+    // attrs
+  )
+)
diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/openbsdSetupHook/package.nix b/pkgs/os-specific/bsd/openbsd/pkgs/openbsdSetupHook/package.nix
new file mode 100644
index 0000000000000..61147954812fd
--- /dev/null
+++ b/pkgs/os-specific/bsd/openbsd/pkgs/openbsdSetupHook/package.nix
@@ -0,0 +1,3 @@
+{ makeSetupHook }:
+
+makeSetupHook { name = "openbsd-setup-hook"; } ./setup-hook.sh
diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/openbsdSetupHook/setup-hook.sh b/pkgs/os-specific/bsd/openbsd/pkgs/openbsdSetupHook/setup-hook.sh
new file mode 100644
index 0000000000000..50e79bc2928a9
--- /dev/null
+++ b/pkgs/os-specific/bsd/openbsd/pkgs/openbsdSetupHook/setup-hook.sh
@@ -0,0 +1,21 @@
+addOpenBSDMakeFlags() {
+  makeFlags="INCSDIR=${!outputDev}/include $makeFlags"
+  makeFlags="MANDIR=${!outputMan}/share/man $makeFlags"
+}
+
+fixOpenBSDInstallDirs() {
+  find "$BSDSRCDIR" -name Makefile -exec \
+    sed -i -E \
+      -e 's|/usr/include|${INCSDIR}|' \
+      -e 's|/usr/bin|${BINDIR}|' \
+      -e 's|/usr/lib|${LIBDIR}|' \
+      {} \;
+}
+
+setBinownBingrp() {
+  export BINOWN=$(id -u)
+  export BINGRP=$(id -g)
+}
+
+preConfigureHooks+=(addOpenBSDMakeFlags)
+postPatchHooks+=(fixOpenBSDInstallDirs setBinownBingrp)
diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/source.nix b/pkgs/os-specific/bsd/openbsd/pkgs/source.nix
new file mode 100644
index 0000000000000..359c3f2fbc919
--- /dev/null
+++ b/pkgs/os-specific/bsd/openbsd/pkgs/source.nix
@@ -0,0 +1,8 @@
+{ fetchcvs }:
+
+fetchcvs {
+  cvsRoot = "anoncvs@anoncvs.fr.openbsd.org/cvs";
+  module = "src";
+  tag = "OPENBSD_7_5";
+  sha256 = "sha256-hzdATew6h/FQV72SWtg3YvUXdPoGjm2SoUS7m3c3fSU=";
+}
diff --git a/pkgs/os-specific/bsd/setup-hook.sh b/pkgs/os-specific/bsd/setup-hook.sh
index e0afefcd73f71..f9453708ab5db 100644
--- a/pkgs/os-specific/bsd/setup-hook.sh
+++ b/pkgs/os-specific/bsd/setup-hook.sh
@@ -49,7 +49,6 @@ addMakeFlags() {
   makeFlags="LIBDIR=${!outputLib}/lib $makeFlags"
   makeFlags="SHLIBDIR=${!outputLib}/lib $makeFlags"
   makeFlags="SHAREDIR=${!outputLib}/share $makeFlags"
-  makeFlags="MANDIR=${!outputMan}/share/man $makeFlags"
   makeFlags="INFODIR=${!outputInfo}/share/info $makeFlags"
   makeFlags="DOCDIR=${!outputDoc}/share/doc $makeFlags"
   makeFlags="LOCALEDIR=${!outputLib}/share/locale $makeFlags"