From 2bc864f50e5b5182c3c88cdc3445dd14f188b85d Mon Sep 17 00:00:00 2001 From: Jack Leightcap Date: Thu, 13 Jun 2024 11:28:11 -0400 Subject: [PATCH] librandombytes: init at 20240318 Co-authored-by: Abdullah Imad Co-authored-by: Alberto Merino Co-authored-by: Enric Morales Co-authored-by: Jack Leightcap Co-authored-by: Roland Coeurjoly Signed-off-by: Jack Leightcap updated the patch for cross compilin fixed patch Apply suggestions from code review Co-authored-by: Weijia Wang <9713184+wegank@users.noreply.github.com> --- .../environment-variable-tools.patch | 34 ++++++++ pkgs/by-name/li/librandombytes/package.nix | 81 +++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 pkgs/by-name/li/librandombytes/environment-variable-tools.patch create mode 100644 pkgs/by-name/li/librandombytes/package.nix diff --git a/pkgs/by-name/li/librandombytes/environment-variable-tools.patch b/pkgs/by-name/li/librandombytes/environment-variable-tools.patch new file mode 100644 index 000000000000..e342a74fe4d0 --- /dev/null +++ b/pkgs/by-name/li/librandombytes/environment-variable-tools.patch @@ -0,0 +1,34 @@ +diff --git a/configure b/configure +index 36fcf67..39612f3 100755 +--- a/configure ++++ b/configure +@@ -143,6 +143,16 @@ firstcompiler = None + with open('compilers/default') as f: + for c in f.readlines(): + c = c.strip() ++ if env_cc := os.getenv('CC'): ++ c_as_list= c.split() ++ # check if the compiler we're testing has the name inside the last ++ # part of the CC env var ++ # i.e. gcc == x86_64-linux-unknown-gnu-gcc ++ # or gcc == gcc ++ if c_as_list[0] == env_cc.split("-")[-1]: ++ c_as_list[0] = env_cc ++ c = ' '.join(c_as_list) ++ log('patched command as %s' % c) + cv = compilerversion(c) + if cv == None: + log('skipping default compiler %s' % c) +diff --git a/scripts-build/staticlib b/scripts-build/staticlib +index 7b2fc92..a6bbe41 100755 +--- a/scripts-build/staticlib ++++ b/scripts-build/staticlib +@@ -4,6 +4,6 @@ lib="$1" + shift + + rm -f package/lib/"$lib".a +-ar cr package/lib/"$lib".a "$@" +-ranlib package/lib/"$lib".a || : ++${AR:-ar} cr package/lib/"$lib".a "$@" ++${RANLIB:-ranlib} package/lib/"$lib".a || : + chmod 644 package/lib/"$lib".a diff --git a/pkgs/by-name/li/librandombytes/package.nix b/pkgs/by-name/li/librandombytes/package.nix new file mode 100644 index 000000000000..1a4ae6885b0d --- /dev/null +++ b/pkgs/by-name/li/librandombytes/package.nix @@ -0,0 +1,81 @@ +{ + stdenv, + lib, + python3, + openssl, + fetchzip, +}: +stdenv.mkDerivation (finalAttrs: { + pname = "librandombytes"; + version = "20240318"; + + src = fetchzip { + url = "https://randombytes.cr.yp.to/librandombytes-${finalAttrs.version}.tar.gz"; + hash = "sha256-LE8iWw7FxckPREyqefgKtslD6CPDsL7VsfHScQ6JmLs="; + }; + + patches = [ ./environment-variable-tools.patch ]; + + postPatch = '' + patchShebangs configure + patchShebangs scripts-build + ''; + + # NOTE: librandombytes uses a custom Python `./configure`: it does not expect standard + # autoconfig --build --host etc. arguments: disable + configurePlatforms = [ ]; + + # NOTE: the librandombytes library has required specific CFLAGS defined: + # https://randombytes.cr.yp.to/librandombytes-20240318/compilers/default.html + # - `-O` (alias `-O1`) safe optimization + # - `-Qunused-arguments` suppress clang warning + # the default "fortify" hardening sets -O2, -D_FORTIFY_SOURCE=2: + # since librandombytes uses -O1, we disable the fortify hardening, and then manually re-enable -D_FORTIFY_SOURCE. + hardeningDisable = [ "fortify" ]; + env.NIX_CFLAGS_COMPILE = toString ( + lib.optionals stdenv.cc.isClang [ "-Qunused-arguments" ] + ++ [ + "-D_FORTIFY_SOURCE=2" + "-O1" + ] + ); + + nativeBuildInputs = [ python3 ]; + + buildInputs = [ openssl ]; + + meta = { + homepage = "https://randombytes.cr.yp.to/"; + description = "A simple API for applications generating fresh randomness"; + changelog = "https://randombytes.cr.yp.to/download.html"; + license = with lib.licenses; [ + # Upstream specifies the public domain licenses with the terms here https://cr.yp.to/spdx.html + publicDomain + cc0 + bsd0 + mit + mit0 + ]; + maintainers = with lib.maintainers; [ + kiike + imadnyc + jleightcap + ]; + platforms = [ + "i686-linux" + "x86_64-linux" + "armv7a-linux" + "aarch64-linux" + # Cannot support 32 bit MIPS because options in libcpucycles only supports mips64: https://cpucycles.cr.yp.to/libcpucycles-20240318/cpucycles/options.html + "mips64-linux" + "mips64el-linux" + # powerpc-linux (32 bits) is supported by upstream project but not by nix + "powerpc64-linux" + "powerpc64le-linux" + "riscv32-linux" + "riscv64-linux" + "s390x-linux" + # Upstream package supports sparc, but nix does not + ] ++ lib.platforms.darwin; # Work on MacOS X mentioned: https://randombytes.cr.yp.to/download.html + }; +})