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; +} 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; +} 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) 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 = '' 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" ]) ) ) 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"; +}