From 51a07b20cd6bd22a6d45471642f2a8ca68a1bfbc Mon Sep 17 00:00:00 2001 From: Artemis Tosini Date: Tue, 5 Nov 2024 22:09:24 +0000 Subject: [PATCH 1/6] openbsd.mkDerivation: add `extraNativeBuildInputs` input Some packages require extra hooks or tools. Rather than overriding all of the nativeBuildInputs and needing to specify all of them, add a field so that we just need to specify inputs that are not always required. Co-Authored-By: Audrey Dutcher --- pkgs/os-specific/bsd/openbsd/pkgs/mkDerivation.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/mkDerivation.nix b/pkgs/os-specific/bsd/openbsd/pkgs/mkDerivation.nix index 44709f680884..0e0778d394a6 100644 --- a/pkgs/os-specific/bsd/openbsd/pkgs/mkDerivation.nix +++ b/pkgs/os-specific/bsd/openbsd/pkgs/mkDerivation.nix @@ -53,7 +53,7 @@ lib.makeOverridable ( install tsort lorder - ]; + ] ++ (attrs.extraNativeBuildInputs or [ ]); HOST_SH = stdenv'.shell; @@ -93,6 +93,6 @@ lib.makeOverridable ( dontBuild = true; } // lib.optionalAttrs stdenv'.hostPlatform.isStatic { NOLIBSHARED = true; } - // attrs + // (builtins.removeAttrs attrs [ "extraNativeBuildInputs" ]) ) ) From d48f526db36ce5db3c3ef87079cd2feed3630e8c Mon Sep 17 00:00:00 2001 From: Artemis Tosini Date: Sun, 3 Nov 2024 22:39:16 +0000 Subject: [PATCH 2/6] openbsd.{compat,compatHook}: init OpenBSD does not provide a compatibility library for running build tools on other OSes, but we still need one. `openbsd.compat` inspired by the `freebsd.compat` package and provides a header-only compatibility layer that can be used across multiple openbsd build packages. The source is included in the nixpkgs tree because it functions similarly to per-package patches. `openbsd.compatHook` provides a build hook that can be added into `extraNativeBuildInputs` to include `compat` in the library search path as a system library. --- .../bsd/openbsd/pkgs/compat/include/err.h | 30 +++++++++++++++++++ .../bsd/openbsd/pkgs/compat/include/fcntl.h | 6 ++++ .../openbsd/pkgs/compat/include/sys/cdefs.h | 4 +++ .../openbsd/pkgs/compat/include/sys/dirent.h | 1 + .../openbsd/pkgs/compat/include/sys/endian.h | 2 ++ .../openbsd/pkgs/compat/include/sys/types.h | 10 +++++++ .../bsd/openbsd/pkgs/compat/include/unistd.h | 8 +++++ .../bsd/openbsd/pkgs/compat/include/util.h | 1 + .../bsd/openbsd/pkgs/compat/package.nix | 16 ++++++++++ .../bsd/openbsd/pkgs/compatHook/package.nix | 13 ++++++++ .../bsd/openbsd/pkgs/compatHook/setup-hook.sh | 5 ++++ 11 files changed, 96 insertions(+) create mode 100644 pkgs/os-specific/bsd/openbsd/pkgs/compat/include/err.h create mode 100644 pkgs/os-specific/bsd/openbsd/pkgs/compat/include/fcntl.h create mode 100644 pkgs/os-specific/bsd/openbsd/pkgs/compat/include/sys/cdefs.h create mode 100644 pkgs/os-specific/bsd/openbsd/pkgs/compat/include/sys/dirent.h create mode 100644 pkgs/os-specific/bsd/openbsd/pkgs/compat/include/sys/endian.h create mode 100644 pkgs/os-specific/bsd/openbsd/pkgs/compat/include/sys/types.h create mode 100644 pkgs/os-specific/bsd/openbsd/pkgs/compat/include/unistd.h create mode 100644 pkgs/os-specific/bsd/openbsd/pkgs/compat/include/util.h create mode 100644 pkgs/os-specific/bsd/openbsd/pkgs/compat/package.nix create mode 100644 pkgs/os-specific/bsd/openbsd/pkgs/compatHook/package.nix create mode 100644 pkgs/os-specific/bsd/openbsd/pkgs/compatHook/setup-hook.sh diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/compat/include/err.h b/pkgs/os-specific/bsd/openbsd/pkgs/compat/include/err.h new file mode 100644 index 000000000000..8ff38a621743 --- /dev/null +++ b/pkgs/os-specific/bsd/openbsd/pkgs/compat/include/err.h @@ -0,0 +1,30 @@ +#pragma once +#include_next + +#include +#include +static inline void __attribute__((__format__(printf, 3, 4))) +errc(int eval, int code, const char *fmt, ...) { + // verr uses the error code from errno + // No need to keep the old value since this is noreturn anyway + errno = code; + + va_list args; + va_start(args, fmt); + verr(eval, fmt, args); + va_end(args); +} + +static inline void __attribute__((__format__(printf, 2, 3))) +warnc(int code, const char *fmt, ...) { + // verr uses the error code from errno + int old_errno = errno; + errno = code; + + va_list args; + va_start(args, fmt); + vwarn(fmt, args); + va_end(args); + + errno = old_errno; +} diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/compat/include/fcntl.h b/pkgs/os-specific/bsd/openbsd/pkgs/compat/include/fcntl.h new file mode 100644 index 000000000000..d8bb61a2291d --- /dev/null +++ b/pkgs/os-specific/bsd/openbsd/pkgs/compat/include/fcntl.h @@ -0,0 +1,6 @@ +#pragma once +#include_next + +// Linux doesn't let you lock during open, make these do nothing +#define O_EXLOCK 0 +#define O_SHLOCK 0 diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/compat/include/sys/cdefs.h b/pkgs/os-specific/bsd/openbsd/pkgs/compat/include/sys/cdefs.h new file mode 100644 index 000000000000..ea433160ad7c --- /dev/null +++ b/pkgs/os-specific/bsd/openbsd/pkgs/compat/include/sys/cdefs.h @@ -0,0 +1,4 @@ +#include_next + +#define __packed __attribute__((__packed__)) +#define __aligned(x) __attribute__((__aligned__(x))) diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/compat/include/sys/dirent.h b/pkgs/os-specific/bsd/openbsd/pkgs/compat/include/sys/dirent.h new file mode 100644 index 000000000000..d66bbdf7b613 --- /dev/null +++ b/pkgs/os-specific/bsd/openbsd/pkgs/compat/include/sys/dirent.h @@ -0,0 +1 @@ +#include diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/compat/include/sys/endian.h b/pkgs/os-specific/bsd/openbsd/pkgs/compat/include/sys/endian.h new file mode 100644 index 000000000000..9abc15484c51 --- /dev/null +++ b/pkgs/os-specific/bsd/openbsd/pkgs/compat/include/sys/endian.h @@ -0,0 +1,2 @@ +// Seems to be the only header for htonl +#include diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/compat/include/sys/types.h b/pkgs/os-specific/bsd/openbsd/pkgs/compat/include/sys/types.h new file mode 100644 index 000000000000..dc2578a8a1bb --- /dev/null +++ b/pkgs/os-specific/bsd/openbsd/pkgs/compat/include/sys/types.h @@ -0,0 +1,10 @@ +#include_next + +// for makedev, major, minor +#include + +// For htonl, htons, etc. +#include + +// for uint32_t etc. +#include diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/compat/include/unistd.h b/pkgs/os-specific/bsd/openbsd/pkgs/compat/include/unistd.h new file mode 100644 index 000000000000..a52d202801f9 --- /dev/null +++ b/pkgs/os-specific/bsd/openbsd/pkgs/compat/include/unistd.h @@ -0,0 +1,8 @@ +#pragma once + +#include_next + +// Reimplementing pledge and unvail with seccomp would be a pain, +// so do nothing but claim they succeeded +static int pledge(const char *, const char *) { return 0; } +static int unveil(const char *, const char *) { return 0; } diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/compat/include/util.h b/pkgs/os-specific/bsd/openbsd/pkgs/compat/include/util.h new file mode 100644 index 000000000000..a12c43b15a9e --- /dev/null +++ b/pkgs/os-specific/bsd/openbsd/pkgs/compat/include/util.h @@ -0,0 +1 @@ +#include diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/compat/package.nix b/pkgs/os-specific/bsd/openbsd/pkgs/compat/package.nix new file mode 100644 index 000000000000..69d457a180d8 --- /dev/null +++ b/pkgs/os-specific/bsd/openbsd/pkgs/compat/package.nix @@ -0,0 +1,16 @@ +{ runCommand, lib }: + +runCommand "openbsd-compat" + { + include = ./include; + + meta = with lib; { + description = "A header-only library for running OpenBSD software on Linux"; + platforms = lib.platforms.linux; + maintainers = with lib.maintainers; [ artemist ]; + }; + } + '' + mkdir -p $out + cp -R $include $out/include + '' diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/compatHook/package.nix b/pkgs/os-specific/bsd/openbsd/pkgs/compatHook/package.nix new file mode 100644 index 000000000000..3678b60f658b --- /dev/null +++ b/pkgs/os-specific/bsd/openbsd/pkgs/compatHook/package.nix @@ -0,0 +1,13 @@ +{ + stdenv, + makeSetupHook, + compat, +}: + +makeSetupHook { + name = "openbsd-compat-hook"; + substitutions = { + inherit compat; + inherit (stdenv.cc) suffixSalt; + }; +} ./setup-hook.sh diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/compatHook/setup-hook.sh b/pkgs/os-specific/bsd/openbsd/pkgs/compatHook/setup-hook.sh new file mode 100644 index 000000000000..af70cb4bcc8a --- /dev/null +++ b/pkgs/os-specific/bsd/openbsd/pkgs/compatHook/setup-hook.sh @@ -0,0 +1,5 @@ +useOpenBSDCompat () { + export NIX_CFLAGS_COMPILE_@suffixSalt@+="-I@compat@/include" +} + +postHooks+=(useOpenBSDCompat) From 5cd3d0ef0fb09a2023e36aa1970e5a5707e625df Mon Sep 17 00:00:00 2001 From: Artemis Tosini Date: Tue, 5 Nov 2024 22:03:36 +0000 Subject: [PATCH 3/6] openbsd.boot-config: init Co-Authored-By: Audrey Dutcher --- .../bsd/openbsd/pkgs/boot-config.nix | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 pkgs/os-specific/bsd/openbsd/pkgs/boot-config.nix diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/boot-config.nix b/pkgs/os-specific/bsd/openbsd/pkgs/boot-config.nix new file mode 100644 index 000000000000..23bd63e6d1ac --- /dev/null +++ b/pkgs/os-specific/bsd/openbsd/pkgs/boot-config.nix @@ -0,0 +1,32 @@ +{ + mkDerivation, + lib, + flex, + byacc, + compatHook, +}: +mkDerivation { + path = "usr.sbin/config"; + + extraNativeBuildInputs = [ + flex + byacc + compatHook + ]; + + postPatch = '' + rm $BSDSRCDIR/usr.sbin/config/ukc.c + rm $BSDSRCDIR/usr.sbin/config/ukcutil.c + rm $BSDSRCDIR/usr.sbin/config/cmd.c + rm $BSDSRCDIR/usr.sbin/config/exec_elf.c + ''; + + buildPhase = '' + for f in *.l; do flex $f; done + for f in *.y; do yacc -H ''${f%.y}.h $f; done + for f in *.c; do $CC -I$TMP/include -DMAKE_BOOTSTRAP -c $f; done + $CC *.o -o config + ''; + + meta.platforms = lib.platforms.linux; +} From 19a0c224840d09ba8ecbf6cdbb48b920b79c3e65 Mon Sep 17 00:00:00 2001 From: Artemis Tosini Date: Tue, 5 Nov 2024 22:04:30 +0000 Subject: [PATCH 4/6] openbsd.boot-ctags: init Co-Authored-By: Audrey Dutcher --- .../bsd/openbsd/pkgs/boot-ctags.nix | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 pkgs/os-specific/bsd/openbsd/pkgs/boot-ctags.nix diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/boot-ctags.nix b/pkgs/os-specific/bsd/openbsd/pkgs/boot-ctags.nix new file mode 100644 index 000000000000..e4bf4607da47 --- /dev/null +++ b/pkgs/os-specific/bsd/openbsd/pkgs/boot-ctags.nix @@ -0,0 +1,25 @@ +{ + mkDerivation, + lib, + flex, + byacc, + compatHook, +}: +mkDerivation { + path = "usr.bin/ctags"; + + extraNativeBuildInputs = [ + flex + byacc + compatHook + ]; + + buildPhase = '' + for f in *.l; do flex $f; done + for f in *.y; do yacc -H ''${f%.y}.h $f; done + for f in *.c; do $CC -I$TMP/include -DMAKE_BOOTSTRAP -c $f; done + $CC *.o -o ctags + ''; + + meta.platforms = lib.platforms.linux; +} From 97c0dbaf3ed565dd0e0bb2de3ce2a04a65de0011 Mon Sep 17 00:00:00 2001 From: Artemis Tosini Date: Tue, 5 Nov 2024 22:13:40 +0000 Subject: [PATCH 5/6] openbsd.make-rules: Fix hardcoded /bin/pwd OpenBSD loves hardcoding paths to programs. The only example of this in `bsd.obj.mk` is `/bin/pwd`, so `substituteInPlace` is fine. Co-Authored-By: Audrey Dutcher --- pkgs/os-specific/bsd/openbsd/pkgs/make-rules/package.nix | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/make-rules/package.nix b/pkgs/os-specific/bsd/openbsd/pkgs/make-rules/package.nix index a16448752566..7e8a36ada14b 100644 --- a/pkgs/os-specific/bsd/openbsd/pkgs/make-rules/package.nix +++ b/pkgs/os-specific/bsd/openbsd/pkgs/make-rules/package.nix @@ -34,6 +34,8 @@ mkDerivation { sed -i -E \ -e 's|/usr/lib|\$\{LIBDIR\}|' \ share/mk/bsd.prog.mk + + substituteInPlace share/mk/bsd.obj.mk --replace-fail /bin/pwd pwd ''; installPhase = '' From 34065832055ab410078720631d29edb159883cd0 Mon Sep 17 00:00:00 2001 From: Artemis Tosini Date: Tue, 5 Nov 2024 23:37:25 +0000 Subject: [PATCH 6/6] openbsd.sys: init `sys` includes the OpenBSD kernel. Co-Authored-By: Audrey Dutcher --- pkgs/os-specific/bsd/openbsd/pkgs/sys.nix | 66 +++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 pkgs/os-specific/bsd/openbsd/pkgs/sys.nix diff --git a/pkgs/os-specific/bsd/openbsd/pkgs/sys.nix b/pkgs/os-specific/bsd/openbsd/pkgs/sys.nix new file mode 100644 index 000000000000..58f1a0be8430 --- /dev/null +++ b/pkgs/os-specific/bsd/openbsd/pkgs/sys.nix @@ -0,0 +1,66 @@ +{ + mkDerivation, + boot-config, + pkgsBuildTarget, + baseConfig ? "GENERIC", +}: +mkDerivation { + path = "sys/arch/amd64"; + pname = "sys"; + extraPaths = [ "sys" ]; + noLibc = true; + + extraNativeBuildInputs = [ + boot-config + ]; + + postPatch = + # The in-kernel debugger (DDB) requires compiler flags not supported by clang, disable it + '' + sed -E -i -e '/DDB/d' $BSDSRCDIR/sys/conf/GENERIC + sed -E -i -e '/pseudo-device\tdt/d' $BSDSRCDIR/sys/arch/amd64/conf/GENERIC + '' + + + # Clang flags compatibility + '' + find $BSDSRCDIR -name 'Makefile*' -exec sed -E -i -e 's/-fno-ret-protector/-fno-stack-protector/g' -e 's/-nopie/-no-pie/g' {} + + sed -E -i -e 's_^\tinstall.*$_\tinstall bsd ''${out}/bsd_' -e s/update-link// $BSDSRCDIR/sys/arch/*/conf/Makefile.* + '' + + + # Remove randomness in build + '' + sed -E -i -e 's/^PAGE_SIZE=.*$/PAGE_SIZE=4096/g' -e '/^random_uniform/a echo 0; return 0;' $BSDSRCDIR/sys/conf/makegap.sh + sed -E -i -e 's/^v=.*$/v=0 u=nixpkgs h=nixpkgs t=`date -d @1`/g' $BSDSRCDIR/sys/conf/newvers.sh + ''; + + postConfigure = '' + export BSDOBJDIR=$TMP/obj + mkdir $BSDOBJDIR + make obj + + cd conf + config ${baseConfig} + cd - + ''; + + preBuild = + # A lot of files insist on calling unprefixed GNU `ld` and `objdump`. + # It's easier to add them to PATH than patch and substitute. + '' + mkdir $TMP/bin + export PATH=$TMP/bin:$PATH + ln -s ${pkgsBuildTarget.binutils}/bin/${pkgsBuildTarget.binutils.targetPrefix}objdump $TMP/bin/objdump + ln -s ${pkgsBuildTarget.binutils}/bin/${pkgsBuildTarget.binutils.targetPrefix}ld $TMP/bin/ld + '' + + + # The Makefile claims it needs includes, but it really doesn't. + # Tell it includes aren't real and can't hurt it. + '' + cd compile/${baseConfig}/obj + echo 'includes:' >>Makefile + ''; + + # stand is in a separate package + env.SKIPDIR = "stand"; + env.NIX_CFLAGS_COMPILE = "-Wno-unused-command-line-argument -Wno-visibility"; +}