about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJohn Ericson <git@JohnEricson.me>2024-05-29 13:42:32 -0400
committerGitHub <noreply@github.com>2024-05-29 13:42:32 -0400
commit61c678f71e564386a2f093503c6aab798ca054ae (patch)
tree716502b1af649e55b7fc9151ed0b9342f47ec3ee
parenta36653dd26f1b8dc381e7652a19f9de1fbad4706 (diff)
parent888dee445d72fa292f52bd477950b2eaf3ee8c3e (diff)
Merge pull request #311836 from obsidiansystems/aa-openbsd-1
openbsd: init at 7.5
-rw-r--r--lib/systems/default.nix1
-rw-r--r--lib/systems/examples.nix5
-rw-r--r--lib/systems/parse.nix1
-rw-r--r--pkgs/development/compilers/llvm/18/default.nix14
-rw-r--r--pkgs/development/compilers/llvm/common/compiler-rt/default.nix15
-rw-r--r--pkgs/development/compilers/llvm/common/libcxx/default.nix6
-rw-r--r--pkgs/os-specific/bsd/freebsd/lib/default.nix2
-rw-r--r--pkgs/os-specific/bsd/lib/install-wrapper.sh (renamed from pkgs/os-specific/bsd/freebsd/lib/install-wrapper.sh)0
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/install/package.nix12
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/make-rules.nix75
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/make.nix52
-rw-r--r--pkgs/os-specific/bsd/netbsd/pkgs/makeMinimal.nix9
-rw-r--r--pkgs/os-specific/bsd/openbsd/default.nix49
-rw-r--r--pkgs/os-specific/bsd/openbsd/pkgs/csu.nix22
-rw-r--r--pkgs/os-specific/bsd/openbsd/pkgs/include/package.nix57
-rw-r--r--pkgs/os-specific/bsd/openbsd/pkgs/libc/disable-librebuild.patch12
-rw-r--r--pkgs/os-specific/bsd/openbsd/pkgs/libc/netbsd-make-to-lower.patch16
-rw-r--r--pkgs/os-specific/bsd/openbsd/pkgs/libc/package.nix120
-rw-r--r--pkgs/os-specific/bsd/openbsd/pkgs/lorder.nix20
-rw-r--r--pkgs/os-specific/bsd/openbsd/pkgs/make-rules/netbsd-make-sinclude.patch15
-rw-r--r--pkgs/os-specific/bsd/openbsd/pkgs/make-rules/package.nix36
-rw-r--r--pkgs/os-specific/bsd/openbsd/pkgs/mkDerivation.nix91
-rw-r--r--pkgs/os-specific/bsd/openbsd/pkgs/openbsdSetupHook/package.nix3
-rw-r--r--pkgs/os-specific/bsd/openbsd/pkgs/openbsdSetupHook/setup-hook.sh21
-rw-r--r--pkgs/os-specific/bsd/openbsd/pkgs/source.nix8
-rw-r--r--pkgs/top-level/all-packages.nix7
26 files changed, 604 insertions, 65 deletions
diff --git a/lib/systems/default.nix b/lib/systems/default.nix
index fbd6c323bf425..d37ff720edce6 100644
--- a/lib/systems/default.nix
+++ b/lib/systems/default.nix
@@ -93,6 +93,7 @@ let
         else if final.isAndroid             then "bionic"
         else if final.isLinux /* default */ then "glibc"
         else if final.isFreeBSD             then "fblibc"
+        else if final.isOpenBSD             then "oblibc"
         else if final.isNetBSD              then "nblibc"
         else if final.isAvr                 then "avrlibc"
         else if final.isGhcjs               then null
diff --git a/lib/systems/examples.nix b/lib/systems/examples.nix
index 8a3726f369682..79ec4461e4193 100644
--- a/lib/systems/examples.nix
+++ b/lib/systems/examples.nix
@@ -342,6 +342,11 @@ rec {
     useLLVM = true;
   };
 
+  x86_64-openbsd = {
+    config = "x86_64-unknown-openbsd";
+    useLLVM = true;
+  };
+
   #
   # WASM
   #
diff --git a/lib/systems/parse.nix b/lib/systems/parse.nix
index 4890912d7fed4..1d7c95943a794 100644
--- a/lib/systems/parse.nix
+++ b/lib/systems/parse.nix
@@ -469,6 +469,7 @@ rec {
               elem (elemAt l 2) [ "wasi" "redox" "mmixware" "ghcjs" "mingw32" ] ||
               hasPrefix "freebsd" (elemAt l 2) ||
               hasPrefix "netbsd" (elemAt l 2) ||
+              hasPrefix "openbsd" (elemAt l 2) ||
               hasPrefix "genode" (elemAt l 2)
       then {
         cpu    = elemAt l 0;
diff --git a/pkgs/development/compilers/llvm/18/default.nix b/pkgs/development/compilers/llvm/18/default.nix
index 5b2b09bbb6ec2..f50af308cc4d0 100644
--- a/pkgs/development/compilers/llvm/18/default.nix
+++ b/pkgs/development/compilers/llvm/18/default.nix
@@ -1,6 +1,6 @@
 { lowPrio, newScope, pkgs, lib, stdenv, cmake, ninja
 , preLibcCrossHeaders
-, libxml2, python3, fetchFromGitHub, substituteAll, overrideCC, wrapCCWith, wrapBintoolsWith
+, libxml2, python3, fetchFromGitHub, fetchpatch, substituteAll, overrideCC, wrapCCWith, wrapBintoolsWith
 , buildLlvmTools # tools, but from the previous stage, for cross
 , targetLlvmLibraries # libraries, but from the next stage, for cross
 , targetLlvm
@@ -146,6 +146,11 @@ in let
         # https://reviews.llvm.org/D51899
         ./clang/gnu-install-dirs.patch
         ../common/clang/add-nostdlibinc-flag.patch
+        (fetchpatch {
+          url = "https://github.com/llvm/llvm-project/commit/48c1364200b5649dda2f9ccbe382b0bd908b99de.patch";
+          sha256 = "sha256-beSydky0P/06YDLk0GJDtcREpMcU2FspxuYU6EnQGfA=";
+          stripLen = 1;
+        })
         (substituteAll {
           src = ../common/clang/clang-at-least-16-LLVMgold-path.patch;
           libllvmLibdir = "${tools.libllvm.lib}/lib";
@@ -391,7 +396,12 @@ in let
     # so: we use the clang from this LLVM package set instead of the regular
     # stdenv's compiler.
     libcxx = callPackage ../common/libcxx {
-      patches = lib.optionals (stdenv.isDarwin && lib.versionOlder stdenv.hostPlatform.darwinMinVersion "10.13") [
+      patches = [
+        (fetchpatch {
+          url = "https://github.com/llvm/llvm-project/commit/1118c2e05e67a36ed8ca250524525cdb66a55256.patch";
+          sha256 = "sha256-2lDO9UsglvYAxDG3/iTXqGVHn1Hq3O7wtkCUb9I/Buc=";
+        })
+      ] ++ lib.optionals (stdenv.isDarwin && lib.versionOlder stdenv.hostPlatform.darwinMinVersion "10.13") [
         # https://github.com/llvm/llvm-project/issues/64226
         ./libcxx/0001-darwin-10.12-mbstate_t-fix.patch
       ];
diff --git a/pkgs/development/compilers/llvm/common/compiler-rt/default.nix b/pkgs/development/compilers/llvm/common/compiler-rt/default.nix
index 4f44d6396d301..7d47436b714ff 100644
--- a/pkgs/development/compilers/llvm/common/compiler-rt/default.nix
+++ b/pkgs/development/compilers/llvm/common/compiler-rt/default.nix
@@ -14,7 +14,18 @@
 , libllvm
 , linuxHeaders
 , libxcrypt
+
+# Some platforms have switched to using compiler-rt, but still want a
+# libgcc.a for ABI compat purposes. The use case would be old code that
+# expects to link `-lgcc` but doesn't care exactly what its contents
+# are, so long as it provides some builtins.
 , doFakeLibgcc ? stdenv.hostPlatform.isFreeBSD
+
+# In recent releases, the compiler-rt build seems to produce
+# many `libclang_rt*` libraries, but not a single unified
+# `libcompiler_rt` library, at least under certain configurations. Some
+# platforms stil expect this, however, so we symlink one into place.
+, forceLinkCompilerRt ? stdenv.hostPlatform.isOpenBSD
 }:
 
 let
@@ -149,7 +160,9 @@ stdenv.mkDerivation ({
     ln -s $out/lib/*/clang_rt.crtbegin_shared-*.o $out/lib/crtbeginS.o
     ln -s $out/lib/*/clang_rt.crtend_shared-*.o $out/lib/crtendS.o
   '' + lib.optionalString doFakeLibgcc ''
-     ln -s $out/lib/freebsd/libclang_rt.builtins-*.a $out/lib/libgcc.a
+     ln -s $out/lib/*/libclang_rt.builtins-*.a $out/lib/libgcc.a
+  '' + lib.optionalString forceLinkCompilerRt ''
+     ln -s $out/lib/*/libclang_rt.builtins-*.a $out/lib/libcompiler_rt.a
   '';
 
   meta = llvm_meta // {
diff --git a/pkgs/development/compilers/llvm/common/libcxx/default.nix b/pkgs/development/compilers/llvm/common/libcxx/default.nix
index 5b4c2ca56ae1a..de2efed85936f 100644
--- a/pkgs/development/compilers/llvm/common/libcxx/default.nix
+++ b/pkgs/development/compilers/llvm/common/libcxx/default.nix
@@ -93,12 +93,6 @@ let
 
   cmakeFlags = [
     "-DLLVM_ENABLE_RUNTIMES=${lib.concatStringsSep ";" runtimes}"
-  ] ++ lib.optionals (useLLVM && !stdenv.hostPlatform.isWasm) [
-    # libcxxabi's CMake looks as though it treats -nostdlib++ as implying -nostdlib,
-    # but that does not appear to be the case for example when building
-    # pkgsLLVM.libcxxabi (which uses clangNoCompilerRtWithLibc).
-    "-DCMAKE_EXE_LINKER_FLAGS=-nostdlib"
-    "-DCMAKE_SHARED_LINKER_FLAGS=-nostdlib"
   ] ++ lib.optionals stdenv.hostPlatform.isWasm [
     "-DCMAKE_C_COMPILER_WORKS=ON"
     "-DCMAKE_CXX_COMPILER_WORKS=ON"
diff --git a/pkgs/os-specific/bsd/freebsd/lib/default.nix b/pkgs/os-specific/bsd/freebsd/lib/default.nix
index 26119a8ba1968..d022f7cfa14c6 100644
--- a/pkgs/os-specific/bsd/freebsd/lib/default.nix
+++ b/pkgs/os-specific/bsd/freebsd/lib/default.nix
@@ -14,5 +14,5 @@
     }
     .${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/lib/install-wrapper.sh
index 91a7a2679f209..91a7a2679f209 100644
--- a/pkgs/os-specific/bsd/freebsd/lib/install-wrapper.sh
+++ b/pkgs/os-specific/bsd/lib/install-wrapper.sh
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/install/package.nix b/pkgs/os-specific/bsd/netbsd/pkgs/install/package.nix
index 69b0c073259e2..0fc6ca452be0d 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/install/package.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/install/package.nix
@@ -17,12 +17,12 @@
 # HACK: to ensure parent directories exist. This emulates GNU
 # install’s -D option. No alternative seems to exist in BSD install.
 let
-  binstall = writeShellScript "binstall" ''
-    set -eu
-    for last in "$@"; do true; done
-    mkdir -p $(dirname $last)
-    @out@/bin/xinstall "$@"
-  '';
+  binstall = writeShellScript "binstall" (
+    builtins.readFile ../../../lib/install-wrapper.sh
+    + ''
+      @out@/bin/xinstall "''${args[@]}"
+    ''
+  );
 in
 mkDerivation {
   path = "usr.bin/xinstall";
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/make-rules.nix b/pkgs/os-specific/bsd/netbsd/pkgs/make-rules.nix
new file mode 100644
index 0000000000000..8528606165468
--- /dev/null
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/make-rules.nix
@@ -0,0 +1,75 @@
+{
+  lib,
+  mkDerivation,
+  fetchNetBSD,
+  stdenv,
+  bsdSetupHook,
+  netbsdSetupHook,
+  rsync,
+}:
+
+mkDerivation {
+  path = "share/mk";
+  sha256 = "0w9x77cfnm6zwy40slradzi0ip9gz80x6lk7pvnlxzsr2m5ra5sy";
+  version = "9.2";
+  noCC = true;
+
+  buildInputs = [ ];
+  nativeBuildInputs = [
+    bsdSetupHook
+    netbsdSetupHook
+    rsync
+  ];
+
+  dontBuild = true;
+
+  postPatch =
+    ''
+      substituteInPlace $BSDSRCDIR/share/mk/bsd.doc.mk \
+        --replace '-o ''${DOCOWN}' "" \
+        --replace '-g ''${DOCGRP}' ""
+      for mk in $BSDSRCDIR/share/mk/bsd.inc.mk $BSDSRCDIR/share/mk/bsd.kinc.mk; do
+        substituteInPlace $mk \
+          --replace '-o ''${BINOWN}' "" \
+          --replace '-g ''${BINGRP}' ""
+      done
+      substituteInPlace $BSDSRCDIR/share/mk/bsd.kmodule.mk \
+        --replace '-o ''${KMODULEOWN}' "" \
+        --replace '-g ''${KMODULEGRP}' ""
+      substituteInPlace $BSDSRCDIR/share/mk/bsd.lib.mk \
+        --replace '-o ''${LIBOWN}' "" \
+        --replace '-g ''${LIBGRP}' "" \
+        --replace '-o ''${DEBUGOWN}' "" \
+        --replace '-g ''${DEBUGGRP}' ""
+      substituteInPlace $BSDSRCDIR/share/mk/bsd.lua.mk \
+        --replace '-o ''${LIBOWN}' "" \
+        --replace '-g ''${LIBGRP}' ""
+      substituteInPlace $BSDSRCDIR/share/mk/bsd.man.mk \
+        --replace '-o ''${MANOWN}' "" \
+        --replace '-g ''${MANGRP}' ""
+      substituteInPlace $BSDSRCDIR/share/mk/bsd.nls.mk \
+        --replace '-o ''${NLSOWN}' "" \
+        --replace '-g ''${NLSGRP}' ""
+      substituteInPlace $BSDSRCDIR/share/mk/bsd.prog.mk \
+        --replace '-o ''${BINOWN}' "" \
+        --replace '-g ''${BINGRP}' "" \
+        --replace '-o ''${RUMPBINOWN}' "" \
+        --replace '-g ''${RUMPBINGRP}' "" \
+        --replace '-o ''${DEBUGOWN}' "" \
+        --replace '-g ''${DEBUGGRP}' ""
+
+       substituteInPlace $BSDSRCDIR/share/mk/bsd.lib.mk \
+         --replace '_INSTRANLIB=''${empty(PRESERVE):?-a "''${RANLIB} -t":}' '_INSTRANLIB='
+       substituteInPlace $BSDSRCDIR/share/mk/bsd.kinc.mk \
+         --replace /bin/rm rm
+    ''
+    + lib.optionalString stdenv.targetPlatform.isDarwin ''
+      substituteInPlace $BSDSRCDIR/share/mk/bsd.sys.mk \
+        --replace '-Wl,--fatal-warnings' "" \
+        --replace '-Wl,--warn-shared-textrel' ""
+    '';
+
+  installPhase = ''
+    cp -r . $out
+  '';
+}
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/make.nix b/pkgs/os-specific/bsd/netbsd/pkgs/make.nix
index 381e30916407b..4d0b9f4b4cf22 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/make.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/make.nix
@@ -3,6 +3,7 @@
   mkDerivation,
   fetchNetBSD,
   stdenv,
+  make-rules,
 }:
 
 mkDerivation {
@@ -11,53 +12,12 @@ mkDerivation {
   version = "9.2";
 
   postPatch =
-    ''
-      substituteInPlace $BSDSRCDIR/share/mk/bsd.doc.mk \
-        --replace '-o ''${DOCOWN}' "" \
-        --replace '-g ''${DOCGRP}' ""
-      for mk in $BSDSRCDIR/share/mk/bsd.inc.mk $BSDSRCDIR/share/mk/bsd.kinc.mk; do
-        substituteInPlace $mk \
-          --replace '-o ''${BINOWN}' "" \
-          --replace '-g ''${BINGRP}' ""
-      done
-      substituteInPlace $BSDSRCDIR/share/mk/bsd.kmodule.mk \
-        --replace '-o ''${KMODULEOWN}' "" \
-        --replace '-g ''${KMODULEGRP}' ""
-      substituteInPlace $BSDSRCDIR/share/mk/bsd.lib.mk \
-        --replace '-o ''${LIBOWN}' "" \
-        --replace '-g ''${LIBGRP}' "" \
-        --replace '-o ''${DEBUGOWN}' "" \
-        --replace '-g ''${DEBUGGRP}' ""
-      substituteInPlace $BSDSRCDIR/share/mk/bsd.lua.mk \
-        --replace '-o ''${LIBOWN}' "" \
-        --replace '-g ''${LIBGRP}' ""
-      substituteInPlace $BSDSRCDIR/share/mk/bsd.man.mk \
-        --replace '-o ''${MANOWN}' "" \
-        --replace '-g ''${MANGRP}' ""
-      substituteInPlace $BSDSRCDIR/share/mk/bsd.nls.mk \
-        --replace '-o ''${NLSOWN}' "" \
-        --replace '-g ''${NLSGRP}' ""
-      substituteInPlace $BSDSRCDIR/share/mk/bsd.prog.mk \
-        --replace '-o ''${BINOWN}' "" \
-        --replace '-g ''${BINGRP}' "" \
-        --replace '-o ''${RUMPBINOWN}' "" \
-        --replace '-g ''${RUMPBINGRP}' "" \
-        --replace '-o ''${DEBUGOWN}' "" \
-        --replace '-g ''${DEBUGGRP}' ""
-
-       # make needs this to pick up our sys make files
-       export NIX_CFLAGS_COMPILE+=" -D_PATH_DEFSYSPATH=\"$out/share/mk\""
-
-       substituteInPlace $BSDSRCDIR/share/mk/bsd.lib.mk \
-         --replace '_INSTRANLIB=''${empty(PRESERVE):?-a "''${RANLIB} -t":}' '_INSTRANLIB='
-       substituteInPlace $BSDSRCDIR/share/mk/bsd.kinc.mk \
-         --replace /bin/rm rm
-    ''
-    + lib.optionalString stdenv.isDarwin ''
-      substituteInPlace $BSDSRCDIR/share/mk/bsd.sys.mk \
-        --replace '-Wl,--fatal-warnings' "" \
-        --replace '-Wl,--warn-shared-textrel' ""
+    make-rules.postPatch
+    + ''
+      # make needs this to pick up our sys make files
+      appendToVar NIX_CFLAGS_COMPILE "-D_PATH_DEFSYSPATH=\"$out/share/mk\""
     '';
+
   postInstall = ''
     make -C $BSDSRCDIR/share/mk FILESDIR=$out/share/mk install
   '';
diff --git a/pkgs/os-specific/bsd/netbsd/pkgs/makeMinimal.nix b/pkgs/os-specific/bsd/netbsd/pkgs/makeMinimal.nix
index 585c72bd17987..62a4d4a6a1246 100644
--- a/pkgs/os-specific/bsd/netbsd/pkgs/makeMinimal.nix
+++ b/pkgs/os-specific/bsd/netbsd/pkgs/makeMinimal.nix
@@ -4,6 +4,7 @@
   netbsdSetupHook,
   rsync,
   make,
+  make-rules,
 }:
 
 mkDerivation {
@@ -22,7 +23,9 @@ mkDerivation {
 
   postPatch = ''
     patchShebangs $COMPONENT_PATH/configure
-    ${make.postPatch}
+
+    # make needs this to pick up our sys make files
+    appendToVar NIX_CFLAGS_COMPILE "-D_PATH_DEFSYSPATH=\"$out/share/mk\""
   '';
 
   buildPhase = ''
@@ -39,10 +42,10 @@ mkDerivation {
     install -D nbmake $out/bin/nbmake
     ln -s $out/bin/nbmake $out/bin/make
     mkdir -p $out/share
-    cp -r $BSDSRCDIR/share/mk $out/share/mk
+    cp -r ${make-rules} $out/share/mk
 
     runHook postInstall
   '';
 
-  extraPaths = [ make.src ] ++ make.extraPaths;
+  extraPaths = [ make.src ];
 }
diff --git a/pkgs/os-specific/bsd/openbsd/default.nix b/pkgs/os-specific/bsd/openbsd/default.nix
new file mode 100644
index 0000000000000..00dba195b92f5
--- /dev/null
+++ b/pkgs/os-specific/bsd/openbsd/default.nix
@@ -0,0 +1,49 @@
+{
+  stdenv,
+  lib,
+  stdenvNoCC,
+  makeScopeWithSplicing',
+  generateSplicesForMkScope,
+  pkgs,
+  buildPackages,
+  netbsd,
+}:
+
+makeScopeWithSplicing' {
+  otherSplices = generateSplicesForMkScope "openbsd";
+  f = (
+    self:
+    lib.packagesFromDirectoryRecursive {
+      callPackage = self.callPackage;
+      directory = ./pkgs;
+    }
+    // {
+      libc = self.callPackage ./pkgs/libc/package.nix {
+        inherit (self) csu include lorder;
+        inherit (buildPackages.openbsd) makeMinimal;
+        inherit (buildPackages.netbsd)
+          install
+          gencat
+          rpcgen
+          tsort
+          ;
+      };
+      makeMinimal = buildPackages.netbsd.makeMinimal.override { inherit (self) make-rules; };
+      mkDerivation = self.callPackage ./pkgs/mkDerivation.nix {
+        inherit stdenv;
+        inherit (buildPackages.netbsd) install;
+      };
+      include = self.callPackage ./pkgs/include/package.nix {
+        inherit (buildPackages.openbsd) makeMinimal;
+        inherit (buildPackages.netbsd) install rpcgen mtree;
+      };
+      csu = self.callPackage ./pkgs/csu.nix {
+        inherit (self) include;
+        inherit (buildPackages.openbsd) makeMinimal;
+        inherit (buildPackages.netbsd) install;
+      };
+      make-rules = self.callPackage ./pkgs/make-rules/package.nix { };
+      lorder = self.callPackage ./pkgs/lorder.nix { inherit (buildPackages.netbsd) install; };
+    }
+  );
+}
diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/csu.nix b/pkgs/os-specific/bsd/openbsd/pkgs/csu.nix
new file mode 100644
index 0000000000000..a2b2153a729b1
--- /dev/null
+++ b/pkgs/os-specific/bsd/openbsd/pkgs/csu.nix
@@ -0,0 +1,22 @@
+{
+  lib,
+  mkDerivation,
+  bsdSetupHook,
+  openbsdSetupHook,
+  makeMinimal,
+  install,
+  include,
+}:
+
+mkDerivation {
+  path = "lib/csu";
+  nativeBuildInputs = [
+    bsdSetupHook
+    openbsdSetupHook
+    makeMinimal
+    install
+  ];
+  buildInputs = [ include ];
+  meta.platforms = lib.platforms.openbsd;
+  extraPaths = [ "libexec/ld.so" ];
+}
diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/include/package.nix b/pkgs/os-specific/bsd/openbsd/pkgs/include/package.nix
new file mode 100644
index 0000000000000..481e302065fc4
--- /dev/null
+++ b/pkgs/os-specific/bsd/openbsd/pkgs/include/package.nix
@@ -0,0 +1,57 @@
+{
+  lib,
+  mkDerivation,
+  makeMinimal,
+  bsdSetupHook,
+  openbsdSetupHook,
+  install,
+  rpcgen,
+  mtree,
+  pax,
+  buildPackages,
+}:
+mkDerivation {
+  path = "include";
+  noCC = true;
+
+  extraPaths = [
+    "lib"
+    #"sys"
+    "sys/arch"
+    # LDIRS from the mmakefile
+    "sys/crypto"
+    "sys/ddb"
+    "sys/dev"
+    "sys/isofs"
+    "sys/miscfs"
+    "sys/msdosfs"
+    "sys/net"
+    "sys/netinet"
+    "sys/netinet6"
+    "sys/netmpls"
+    "sys/net80211"
+    "sys/nfs"
+    "sys/ntfs"
+    "sys/scsi"
+    "sys/sys"
+    "sys/ufs"
+    "sys/uvm"
+  ];
+
+  nativeBuildInputs = [
+    bsdSetupHook
+    install
+    makeMinimal
+    mtree
+    openbsdSetupHook
+    pax
+    rpcgen
+  ];
+
+  makeFlags = [
+    "RPCGEN_CPP=${buildPackages.stdenv.cc.cc}/bin/cpp"
+    "-B"
+  ];
+
+  headersOnly = true;
+}
diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/libc/disable-librebuild.patch b/pkgs/os-specific/bsd/openbsd/pkgs/libc/disable-librebuild.patch
new file mode 100644
index 0000000000000..58633861a826f
--- /dev/null
+++ b/pkgs/os-specific/bsd/openbsd/pkgs/libc/disable-librebuild.patch
@@ -0,0 +1,12 @@
+diff --git a/lib/libc/Makefile b/lib/libc/Makefile
+index 4bb4b67fcbb..1c8a8e08e60 100644
+--- a/lib/libc/Makefile
++++ b/lib/libc/Makefile
+@@ -6,7 +6,6 @@
+ .include <bsd.own.mk>
+
+ LIB=c
+-LIBREBUILD=y
+ CLEANFILES+=tags Symbols.map
+ CFLAGS+=-Wimplicit
+ #CFLAGS+=-Werror
diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/libc/netbsd-make-to-lower.patch b/pkgs/os-specific/bsd/openbsd/pkgs/libc/netbsd-make-to-lower.patch
new file mode 100644
index 0000000000000..e8d016ceead5a
--- /dev/null
+++ b/pkgs/os-specific/bsd/openbsd/pkgs/libc/netbsd-make-to-lower.patch
@@ -0,0 +1,16 @@
+NetBSD's make uses `${variable:tl}` not `${variable:L}`.
+
+diff --git a/lib/libc/Makefile b/lib/libc/Makefile
+index 4bb4b67fcbb..ffb35c196ea 100644
+--- a/lib/libc/Makefile
++++ b/lib/libc/Makefile
+@@ -11,8 +11,8 @@ CLEANFILES+=tags Symbols.map
+ CFLAGS+=-Wimplicit
+ #CFLAGS+=-Werror
+ LDADD=-nostdlib
+-.if ${COMPILER_VERSION:L} == "clang"
++.if ${COMPILER_VERSION:tl} == "clang"
+ LDADD+=-lcompiler_rt
+ .else
+ LDADD+=-lgcc
+ .endif
diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/libc/package.nix b/pkgs/os-specific/bsd/openbsd/pkgs/libc/package.nix
new file mode 100644
index 0000000000000..cf233c827840a
--- /dev/null
+++ b/pkgs/os-specific/bsd/openbsd/pkgs/libc/package.nix
@@ -0,0 +1,120 @@
+{
+  lib,
+  stdenv,
+  mkDerivation,
+  bsdSetupHook,
+  openbsdSetupHook,
+  makeMinimal,
+  install,
+  flex,
+  byacc,
+  gencat,
+  rpcgen,
+  lorder,
+  csu,
+  include,
+  ctags,
+  tsort,
+  llvmPackages,
+  fetchpatch,
+}:
+
+mkDerivation rec {
+  pname = "libc";
+  path = "lib/libc";
+  extraPaths = [
+    "lib/csu/os-note-elf.h"
+    "sys/arch"
+
+    "lib/libm"
+    "lib/libpthread"
+    "lib/librpcsvc"
+    "lib/librpcsvc"
+    "lib/librthread"
+    "lib/libutil"
+  ];
+
+  patches = [
+    ./netbsd-make-to-lower.patch
+    ./disable-librebuild.patch
+    (fetchpatch {
+      url = "https://marc.info/?l=openbsd-tech&m=171575286706032&q=raw";
+      sha256 = "sha256-2fqabJZLUvXUIWe5WZ4NrTOwgQCXqH49Wo0hAPu5lu0=";
+    })
+  ];
+
+  nativeBuildInputs = [
+    bsdSetupHook
+    openbsdSetupHook
+    makeMinimal
+    install
+    flex
+    byacc
+    gencat
+    rpcgen
+    ctags
+    lorder
+    tsort
+  ];
+
+  buildInputs = [
+    include
+    csu
+  ];
+
+  env.NIX_CFLAGS_COMPILE = builtins.toString [
+    "-B${csu}/lib"
+    "-Wno-error"
+  ];
+
+  # Suppress lld >= 16 undefined version errors
+  # https://github.com/freebsd/freebsd-src/commit/2ba84b4bcdd6012e8cfbf8a0d060a4438623a638
+  env.NIX_LDFLAGS = lib.optionalString (stdenv.hostPlatform.linker == "lld") "--undefined-version";
+
+  makeFlags = [
+    "STRIP=-s" # flag to install, not command
+    "COMPILER_VERSION=clang"
+    "LIBC_TAGS=no"
+  ];
+
+  postInstall = ''
+    symlink_so () {
+      pushd $out/lib
+      ln -s "lib$1".so.* "lib$1.so"
+      popd
+    }
+
+    symlink_so c
+
+    pushd ${include}
+    find . -type d -exec mkdir -p $out/\{} \;
+    find . \( -type f -o -type l \) -exec cp -pr \{} $out/\{} \;
+    popd
+    substituteInPlace $out/include/sys/time.h --replace "defined (_LIBC)" "true"
+
+    pushd ${csu}
+    find . -type d -exec mkdir -p $out/\{} \;
+    find . \( -type f -o -type l \) -exec cp -pr \{} $out/\{} \;
+    popd
+
+    NIX_CFLAGS_COMPILE+=" -B$out/lib"
+    NIX_CFLAGS_COMPILE+=" -I$out/include"
+    NIX_LDFLAGS+=" -L$out/lib"
+
+    make -C $BSDSRCDIR/lib/libm $makeFlags
+    make -C $BSDSRCDIR/lib/libm $makeFlags install
+    symlink_so m
+
+    make -C $BSDSRCDIR/lib/librthread $makeFlags
+    make -C $BSDSRCDIR/lib/librthread $makeFlags install
+    symlink_so pthread
+
+    make -C $BSDSRCDIR/lib/librpcsvc $makeFlags
+    make -C $BSDSRCDIR/lib/librpcsvc $makeFlags install
+    symlink_so rpcsv
+
+    make -C $BSDSRCDIR/lib/libutil $makeFlags
+    make -C $BSDSRCDIR/lib/libutil $makeFlags install
+    symlink_so util
+  '';
+}
diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/lorder.nix b/pkgs/os-specific/bsd/openbsd/pkgs/lorder.nix
new file mode 100644
index 0000000000000..25ff1fcbd14f6
--- /dev/null
+++ b/pkgs/os-specific/bsd/openbsd/pkgs/lorder.nix
@@ -0,0 +1,20 @@
+{
+  lib,
+  mkDerivation,
+  bsdSetupHook,
+  openbsdSetupHook,
+  makeMinimal,
+  install,
+}:
+
+mkDerivation {
+  path = "usr.bin/lorder";
+  nativeBuildInputs = [
+    bsdSetupHook
+    openbsdSetupHook
+    makeMinimal
+    install
+  ];
+
+  meta.platforms = lib.platforms.unix;
+}
diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/make-rules/netbsd-make-sinclude.patch b/pkgs/os-specific/bsd/openbsd/pkgs/make-rules/netbsd-make-sinclude.patch
new file mode 100644
index 0000000000000..daaaff56e9b7d
--- /dev/null
+++ b/pkgs/os-specific/bsd/openbsd/pkgs/make-rules/netbsd-make-sinclude.patch
@@ -0,0 +1,15 @@
+NetBSD make prefers `.-include` to `sinclude` (without a dot)
+
+diff --git a/share/mk/bsd.dep.mk b/share/mk/bsd.dep.mk
+index 7019adb57f7..277064eb5c2 100644
+--- a/share/mk/bsd.dep.mk
++++ b/share/mk/bsd.dep.mk
+@@ -11,7 +11,7 @@ depend:
+ # catch22: don't include potentially bogus files we are going to clean
+ .  if !(make(clean) || make(cleandir) || make(obj))
+ .    for o in ${DEPS}
+-       sinclude $o
++.      -include "$o"
+ .    endfor
+ .  endif
+ .endif
diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/make-rules/package.nix b/pkgs/os-specific/bsd/openbsd/pkgs/make-rules/package.nix
new file mode 100644
index 0000000000000..1e7c705c0dfd0
--- /dev/null
+++ b/pkgs/os-specific/bsd/openbsd/pkgs/make-rules/package.nix
@@ -0,0 +1,36 @@
+{
+  fetchpatch,
+  lib,
+  mkDerivation,
+  stdenv,
+}:
+
+mkDerivation {
+  path = "share/mk";
+  noCC = true;
+
+  buildInputs = [ ];
+  nativeBuildInputs = [ ];
+
+  dontBuild = true;
+
+  patches = [
+    (fetchpatch {
+      url = "https://marc.info/?l=openbsd-tech&m=171575284906018&q=raw";
+      sha256 = "sha256-bigxJGbaf9mCmFXxLVzQpnUUaEMMDfF3eZkTXVzd6B8=";
+    })
+    ./netbsd-make-sinclude.patch
+  ];
+
+  postPatch = ''
+    sed -i -E \
+      -e 's|/usr/lib|\$\{LIBDIR\}|' \
+      share/mk/bsd.prog.mk
+  '';
+
+  installPhase = ''
+    cp -r share/mk $out
+  '';
+
+  meta.platforms = lib.platforms.unix;
+}
diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/mkDerivation.nix b/pkgs/os-specific/bsd/openbsd/pkgs/mkDerivation.nix
new file mode 100644
index 0000000000000..6c5bc5cd17193
--- /dev/null
+++ b/pkgs/os-specific/bsd/openbsd/pkgs/mkDerivation.nix
@@ -0,0 +1,91 @@
+{
+  lib,
+  stdenv,
+  stdenvNoCC,
+  runCommand,
+  rsync,
+  source,
+  bsdSetupHook,
+  openbsdSetupHook,
+  makeMinimal,
+  install,
+}:
+
+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)}-openbsd";
+      version = "0";
+      src = runCommand "${pname}-filtered-src" { nativeBuildInputs = [ rsync ]; } ''
+        for p in ${lib.concatStringsSep " " ([ attrs.path ] ++ attrs.extraPaths or [ ])}; do
+          set -x
+          path="$out/$p"
+          mkdir -p "$(dirname "$path")"
+          src_path="${source}/$p"
+          if [[ -d "$src_path" ]]; then src_path+=/; fi
+          rsync --chmod="+w" -r "$src_path" "$path"
+          set +x
+        done
+      '';
+
+      extraPaths = [ ];
+
+      nativeBuildInputs = [
+        bsdSetupHook
+        openbsdSetupHook
+        makeMinimal
+        install
+      ];
+
+      HOST_SH = stdenv'.shell;
+
+      # Since STRIP below is the flag
+      STRIPBIN = "${stdenv.cc.bintools.targetPrefix}strip";
+
+      makeFlags = [
+        "STRIP=-s" # flag to install, not command
+        "-B"
+      ];
+
+      MACHINE_ARCH =
+        {
+          # amd64 not x86_64 for this on unlike NetBSD
+          x86_64 = "amd64";
+          aarch64 = "arm64";
+          i486 = "i386";
+          i586 = "i386";
+          i686 = "i386";
+        }
+        .${stdenv'.hostPlatform.parsed.cpu.name} or stdenv'.hostPlatform.parsed.cpu.name;
+
+      MACHINE = MACHINE_ARCH;
+
+      MACHINE_CPU = MACHINE_ARCH;
+
+      MACHINE_CPUARCH = MACHINE_ARCH;
+
+      COMPONENT_PATH = attrs.path or null;
+
+      strictDeps = true;
+
+      meta = with lib; {
+        maintainers = with maintainers; [ ericson2314 ];
+        platforms = platforms.openbsd;
+        license = licenses.bsd2;
+      };
+    }
+    // lib.optionalAttrs stdenv'.hasCC {
+      # TODO should CC wrapper set this?
+      CPP = "${stdenv'.cc.targetPrefix}cpp";
+    }
+    // lib.optionalAttrs (attrs.headersOnly or false) {
+      installPhase = "includesPhase";
+      dontBuild = true;
+    }
+    // attrs
+  )
+)
diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/openbsdSetupHook/package.nix b/pkgs/os-specific/bsd/openbsd/pkgs/openbsdSetupHook/package.nix
new file mode 100644
index 0000000000000..61147954812fd
--- /dev/null
+++ b/pkgs/os-specific/bsd/openbsd/pkgs/openbsdSetupHook/package.nix
@@ -0,0 +1,3 @@
+{ makeSetupHook }:
+
+makeSetupHook { name = "openbsd-setup-hook"; } ./setup-hook.sh
diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/openbsdSetupHook/setup-hook.sh b/pkgs/os-specific/bsd/openbsd/pkgs/openbsdSetupHook/setup-hook.sh
new file mode 100644
index 0000000000000..50e79bc2928a9
--- /dev/null
+++ b/pkgs/os-specific/bsd/openbsd/pkgs/openbsdSetupHook/setup-hook.sh
@@ -0,0 +1,21 @@
+addOpenBSDMakeFlags() {
+  makeFlags="INCSDIR=${!outputDev}/include $makeFlags"
+  makeFlags="MANDIR=${!outputMan}/share/man $makeFlags"
+}
+
+fixOpenBSDInstallDirs() {
+  find "$BSDSRCDIR" -name Makefile -exec \
+    sed -i -E \
+      -e 's|/usr/include|${INCSDIR}|' \
+      -e 's|/usr/bin|${BINDIR}|' \
+      -e 's|/usr/lib|${LIBDIR}|' \
+      {} \;
+}
+
+setBinownBingrp() {
+  export BINOWN=$(id -u)
+  export BINGRP=$(id -g)
+}
+
+preConfigureHooks+=(addOpenBSDMakeFlags)
+postPatchHooks+=(fixOpenBSDInstallDirs setBinownBingrp)
diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/source.nix b/pkgs/os-specific/bsd/openbsd/pkgs/source.nix
new file mode 100644
index 0000000000000..359c3f2fbc919
--- /dev/null
+++ b/pkgs/os-specific/bsd/openbsd/pkgs/source.nix
@@ -0,0 +1,8 @@
+{ fetchcvs }:
+
+fetchcvs {
+  cvsRoot = "anoncvs@anoncvs.fr.openbsd.org/cvs";
+  module = "src";
+  tag = "OPENBSD_7_5";
+  sha256 = "sha256-hzdATew6h/FQV72SWtg3YvUXdPoGjm2SoUS7m3c3fSU=";
+}
diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix
index 4d9e2dd6cc87e..1ca0f726e3249 100644
--- a/pkgs/top-level/all-packages.nix
+++ b/pkgs/top-level/all-packages.nix
@@ -16276,6 +16276,7 @@ with pkgs;
     choose = platform:
       /**/ if platform.isDarwin then 16
       else if platform.isFreeBSD then 16
+      else if platform.isOpenBSD then 18
       else if platform.isAndroid then 12
       else if platform.isLinux then 17
       else if platform.isWasm then 16
@@ -21014,6 +21015,7 @@ with pkgs;
       then targetPackages.darwin.iosSdkPkgs.libraries or darwin.iosSdkPkgs.libraries
       else targetPackages.darwin.LibsystemCross or (throw "don't yet have a `targetPackages.darwin.LibsystemCross for ${stdenv.targetPlatform.config}`")
     else if name == "fblibc" then targetPackages.freebsdCross.libc or freebsdCross.libc
+    else if name == "oblibc" then targetPackages.openbsdCross.libc or openbsdCross.libc
     else if name == "nblibc" then targetPackages.netbsdCross.libc or netbsdCross.libc
     else if name == "wasilibc" then targetPackages.wasilibc or wasilibc
     else if name == "relibc" then targetPackages.relibc or relibc
@@ -40742,6 +40744,11 @@ with pkgs;
     stdenv = crossLibcStdenv;
   };
 
+  openbsd = callPackage ../os-specific/bsd/openbsd { };
+  openbsdCross = callPackage ../os-specific/bsd/openbsd {
+    stdenv = crossLibcStdenv;
+  };
+
   yrd = callPackage ../tools/networking/yrd { };
 
   powershell = callPackage ../shells/powershell { };