Skip to content

Instantly share code, notes, and snippets.

@risicle
Last active June 3, 2025 19:22
Show Gist options
  • Select an option

  • Save risicle/b51e58f77e9ef2a78e4eda5dc778813e to your computer and use it in GitHub Desktop.

Select an option

Save risicle/b51e58f77e9ef2a78e4eda5dc778813e to your computer and use it in GitHub Desktop.
src-inspector.nix - related to https://github.com/NixOS/nixpkgs/pull/179845 but more specialized to native binary sniffing
# nix-build -E 'let pkgs = import ./. {}; lib = pkgs.lib; attrList = builtins.fromJSON (builtins.readFile ./attrs.json); in pkgs.callPackage ./src-inspector.nix { toInspect = (lib.genAttrs attrList (x: lib.attrByPath (lib.splitString "." x) {} pkgs)); }'
# given a json array (attrs.json) of dotted attr paths
{ lib, stdenv, dpkg, unzip, toInspect }:
builtins.mapAttrs (
name: value: let
dp = builtins.tryEval value.drvPath or null;
res = if dp.success && dp.value != null then builtins.tryEval value.src or null else dp;
in if !(value.dontUnpack or false)
&& (builtins.match "" name) != null
&& res.success
&& res.value != null
&& (builtins.elem lib.sourceTypes.binaryNativeCode ((value.meta or {}).sourceProvenance or []) == false)
then (stdenv.mkDerivation {
inherit name;
src = res.value;
sourceRoot = ".";
preUnpack = ''
if [[ "$src" =~ .*[.](whl|jar|war) ]] ; then
oldSrc="$src"
src="$(mktemp -d)/src.zip"
ln -s "$oldSrc" "$src"
elif [[ "$src" =~ .*[.]gem ]] ; then
oldSrc="$src"
src="$(mktemp -d)/src.tar.gz"
ln -s "$oldSrc" "$src"
fi
'';
nativeBuildInputs = [ dpkg unzip ];
dontPatch = true;
dontConfigure = true;
dontBuild = true;
dontFixup = true;
installPhase = ''
find . \
-type f \
-size +0c \
-exec sed -e '1b p ; Q1 ; :p ; /^\x7fELF/Q0 ; /^[\xce\xcf]\xfa\xed\xfe/Q0 ; /^MZ/Q0 ; Q1' '{}' ';' \
-print > $out || true
'';
}) else null
) toInspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment