about summary refs log tree commit diff
path: root/pkgs/os-specific/bsd/freebsd/pkgs
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/os-specific/bsd/freebsd/pkgs')
-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/compat-fix-typedefs-locations.patch32
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/compat/compat-install-dirs.patch42
-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/no-perms-BSD.include.dist.patch11
-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/libc-msun-arch-subdir.patch11
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/libc/libc-no-force--lcompiler-rt.patch10
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/libc/librpcsvc-include-subdir.patch11
-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/libnetbsd-do-install.patch32
-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/sys/sys-gnu-date.patch13
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/sys/sys-no-explicit-intrinsics-dep.patch45
-rw-r--r--pkgs/os-specific/bsd/freebsd/pkgs/tsort.nix28
59 files changed, 1436 insertions, 716 deletions
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/compat-fix-typedefs-locations.patch b/pkgs/os-specific/bsd/freebsd/pkgs/compat/compat-fix-typedefs-locations.patch
deleted file mode 100644
index 3336a2504e584..0000000000000
--- a/pkgs/os-specific/bsd/freebsd/pkgs/compat/compat-fix-typedefs-locations.patch
+++ /dev/null
@@ -1,32 +0,0 @@
---- 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/pkgs/compat/compat-install-dirs.patch b/pkgs/os-specific/bsd/freebsd/pkgs/compat/compat-install-dirs.patch
deleted file mode 100644
index 9bb2bea32ee98..0000000000000
--- a/pkgs/os-specific/bsd/freebsd/pkgs/compat/compat-install-dirs.patch
+++ /dev/null
@@ -1,42 +0,0 @@
-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:
- # 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 \
--	usr/include/private/zstd \
--	usr/lib \
--	usr/libdata/pkgconfig \
--	usr/libexec
-+	${BINDIR} \
-+	${LIBDIR}/casper \
-+	${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/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/no-perms-BSD.include.dist.patch b/pkgs/os-specific/bsd/freebsd/pkgs/include/no-perms-BSD.include.dist.patch
deleted file mode 100644
index 985617ee0f457..0000000000000
--- a/pkgs/os-specific/bsd/freebsd/pkgs/include/no-perms-BSD.include.dist.patch
+++ /dev/null
@@ -1,11 +0,0 @@
---- 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/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/libc-msun-arch-subdir.patch b/pkgs/os-specific/bsd/freebsd/pkgs/libc/libc-msun-arch-subdir.patch
deleted file mode 100644
index 4a69e85a986a0..0000000000000
--- a/pkgs/os-specific/bsd/freebsd/pkgs/libc/libc-msun-arch-subdir.patch
+++ /dev/null
@@ -1,11 +0,0 @@
---- 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/pkgs/libc/libc-no-force--lcompiler-rt.patch b/pkgs/os-specific/bsd/freebsd/pkgs/libc/libc-no-force--lcompiler-rt.patch
deleted file mode 100644
index 60176fb73cf76..0000000000000
--- a/pkgs/os-specific/bsd/freebsd/pkgs/libc/libc-no-force--lcompiler-rt.patch
+++ /dev/null
@@ -1,10 +0,0 @@
---- 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/pkgs/libc/librpcsvc-include-subdir.patch b/pkgs/os-specific/bsd/freebsd/pkgs/libc/librpcsvc-include-subdir.patch
deleted file mode 100644
index 38e06682869fb..0000000000000
--- a/pkgs/os-specific/bsd/freebsd/pkgs/libc/librpcsvc-include-subdir.patch
+++ /dev/null
@@ -1,11 +0,0 @@
---- 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/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/libnetbsd-do-install.patch b/pkgs/os-specific/bsd/freebsd/pkgs/libnetbsd/libnetbsd-do-install.patch
deleted file mode 100644
index a7bd032d2be54..0000000000000
--- a/pkgs/os-specific/bsd/freebsd/pkgs/libnetbsd/libnetbsd-do-install.patch
+++ /dev/null
@@ -1,32 +0,0 @@
-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/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/sys/sys-gnu-date.patch b/pkgs/os-specific/bsd/freebsd/pkgs/sys/sys-gnu-date.patch
deleted file mode 100644
index 2356446baf853..0000000000000
--- a/pkgs/os-specific/bsd/freebsd/pkgs/sys/sys-gnu-date.patch
+++ /dev/null
@@ -1,13 +0,0 @@
-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/pkgs/sys/sys-no-explicit-intrinsics-dep.patch b/pkgs/os-specific/bsd/freebsd/pkgs/sys/sys-no-explicit-intrinsics-dep.patch
deleted file mode 100644
index edf44de5bb0d7..0000000000000
--- a/pkgs/os-specific/bsd/freebsd/pkgs/sys/sys-no-explicit-intrinsics-dep.patch
+++ /dev/null
@@ -1,45 +0,0 @@
-diff --git a/sys/modules/aesni/Makefile b/sys/modules/aesni/Makefile
-index cb8c744adde..1c327331890 100644
---- a/sys/modules/aesni/Makefile
-+++ b/sys/modules/aesni/Makefile
-@@ -1,7 +1,6 @@
- # $FreeBSD$
- 
- .PATH: ${SRCTOP}/sys/crypto/aesni
--.PATH: ${SRCTOP}/contrib/llvm-project/clang/lib/Headers
- 
- KMOD=	aesni
- SRCS=	aesni.c
-@@ -40,8 +39,8 @@ intel_sha256.o: intel_sha256.c
- 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/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
   ];
 }