about summary refs log tree commit diff
path: root/pkgs/os-specific/bsd/freebsd
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/os-specific/bsd/freebsd')
-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/lib/install-wrapper.sh30
-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
83 files changed, 2104 insertions, 639 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/lib/install-wrapper.sh b/pkgs/os-specific/bsd/freebsd/lib/install-wrapper.sh
deleted file mode 100644
index 91a7a2679f209..0000000000000
--- a/pkgs/os-specific/bsd/freebsd/lib/install-wrapper.sh
+++ /dev/null
@@ -1,30 +0,0 @@
-set -eu
-
-args=()
-declare -i path_args=0
-
-while (( $# )); do
-  if (( $# == 1 )); then
-    if (( path_args > 1)) || [[ "$1" = */ ]]; then
-      mkdir -p "$1"
-    else
-      mkdir -p "$(dirname "$1")"
-    fi
-  fi
-  case $1 in
-    -C) ;;
-    -o | -g) shift ;;
-    -s) ;;
-    -m | -l)
-      # handle next arg so not counted as path arg
-      args+=("$1" "$2")
-      shift
-      ;;
-    -*) args+=("$1") ;;
-    *)
-      path_args+=1
-      args+=("$1")
-      ;;
-  esac
-  shift
-done
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"
     }
   }
 }