librandombytes: init at 20240318

Co-authored-by: Abdullah Imad <me@imad.nyc>
Co-authored-by: Alberto Merino <amerinor01@gmail.com>
Co-authored-by: Enric Morales <me@enric.me>
Co-authored-by: Jack Leightcap <jack@leightcap.com>
Co-authored-by: Roland Coeurjoly <rolandcoeurjoly@gmail.com>
Signed-off-by: Jack Leightcap <jack@leightcap.com>

updated the patch for cross compilin

fixed patch

Apply suggestions from code review

Co-authored-by: Weijia Wang <9713184+wegank@users.noreply.github.com>

+115
+34
pkgs/by-name/li/librandombytes/environment-variable-tools.patch
···
··· 1 + diff --git a/configure b/configure 2 + index 36fcf67..39612f3 100755 3 + --- a/configure 4 + +++ b/configure 5 + @@ -143,6 +143,16 @@ firstcompiler = None 6 + with open('compilers/default') as f: 7 + for c in f.readlines(): 8 + c = c.strip() 9 + + if env_cc := os.getenv('CC'): 10 + + c_as_list= c.split() 11 + + # check if the compiler we're testing has the name inside the last 12 + + # part of the CC env var 13 + + # i.e. gcc == x86_64-linux-unknown-gnu-gcc 14 + + # or gcc == gcc 15 + + if c_as_list[0] == env_cc.split("-")[-1]: 16 + + c_as_list[0] = env_cc 17 + + c = ' '.join(c_as_list) 18 + + log('patched command as %s' % c) 19 + cv = compilerversion(c) 20 + if cv == None: 21 + log('skipping default compiler %s' % c) 22 + diff --git a/scripts-build/staticlib b/scripts-build/staticlib 23 + index 7b2fc92..a6bbe41 100755 24 + --- a/scripts-build/staticlib 25 + +++ b/scripts-build/staticlib 26 + @@ -4,6 +4,6 @@ lib="$1" 27 + shift 28 + 29 + rm -f package/lib/"$lib".a 30 + -ar cr package/lib/"$lib".a "$@" 31 + -ranlib package/lib/"$lib".a || : 32 + +${AR:-ar} cr package/lib/"$lib".a "$@" 33 + +${RANLIB:-ranlib} package/lib/"$lib".a || : 34 + chmod 644 package/lib/"$lib".a
+81
pkgs/by-name/li/librandombytes/package.nix
···
··· 1 + { 2 + stdenv, 3 + lib, 4 + python3, 5 + openssl, 6 + fetchzip, 7 + }: 8 + stdenv.mkDerivation (finalAttrs: { 9 + pname = "librandombytes"; 10 + version = "20240318"; 11 + 12 + src = fetchzip { 13 + url = "https://randombytes.cr.yp.to/librandombytes-${finalAttrs.version}.tar.gz"; 14 + hash = "sha256-LE8iWw7FxckPREyqefgKtslD6CPDsL7VsfHScQ6JmLs="; 15 + }; 16 + 17 + patches = [ ./environment-variable-tools.patch ]; 18 + 19 + postPatch = '' 20 + patchShebangs configure 21 + patchShebangs scripts-build 22 + ''; 23 + 24 + # NOTE: librandombytes uses a custom Python `./configure`: it does not expect standard 25 + # autoconfig --build --host etc. arguments: disable 26 + configurePlatforms = [ ]; 27 + 28 + # NOTE: the librandombytes library has required specific CFLAGS defined: 29 + # https://randombytes.cr.yp.to/librandombytes-20240318/compilers/default.html 30 + # - `-O` (alias `-O1`) safe optimization 31 + # - `-Qunused-arguments` suppress clang warning 32 + # the default "fortify" hardening sets -O2, -D_FORTIFY_SOURCE=2: 33 + # since librandombytes uses -O1, we disable the fortify hardening, and then manually re-enable -D_FORTIFY_SOURCE. 34 + hardeningDisable = [ "fortify" ]; 35 + env.NIX_CFLAGS_COMPILE = toString ( 36 + lib.optionals stdenv.cc.isClang [ "-Qunused-arguments" ] 37 + ++ [ 38 + "-D_FORTIFY_SOURCE=2" 39 + "-O1" 40 + ] 41 + ); 42 + 43 + nativeBuildInputs = [ python3 ]; 44 + 45 + buildInputs = [ openssl ]; 46 + 47 + meta = { 48 + homepage = "https://randombytes.cr.yp.to/"; 49 + description = "A simple API for applications generating fresh randomness"; 50 + changelog = "https://randombytes.cr.yp.to/download.html"; 51 + license = with lib.licenses; [ 52 + # Upstream specifies the public domain licenses with the terms here https://cr.yp.to/spdx.html 53 + publicDomain 54 + cc0 55 + bsd0 56 + mit 57 + mit0 58 + ]; 59 + maintainers = with lib.maintainers; [ 60 + kiike 61 + imadnyc 62 + jleightcap 63 + ]; 64 + platforms = [ 65 + "i686-linux" 66 + "x86_64-linux" 67 + "armv7a-linux" 68 + "aarch64-linux" 69 + # Cannot support 32 bit MIPS because options in libcpucycles only supports mips64: https://cpucycles.cr.yp.to/libcpucycles-20240318/cpucycles/options.html 70 + "mips64-linux" 71 + "mips64el-linux" 72 + # powerpc-linux (32 bits) is supported by upstream project but not by nix 73 + "powerpc64-linux" 74 + "powerpc64le-linux" 75 + "riscv32-linux" 76 + "riscv64-linux" 77 + "s390x-linux" 78 + # Upstream package supports sparc, but nix does not 79 + ] ++ lib.platforms.darwin; # Work on MacOS X mentioned: https://randombytes.cr.yp.to/download.html 80 + }; 81 + })