<commit_before>{ stdenv, runCommand, nodejs, neededNatives}:

args @ { name, src, deps ? [], flags ? [], ... }:

with stdenv.lib;

let
  npmFlags = concatStringsSep " " (map (v: "--${v}") flags);

  sources = runCommand "node-sources" {} ''
    tar xf ${nodejs.src}
    mv *node* $out
  '';

  requireName = (builtins.parseDrvName name).name;
in
stdenv.mkDerivation ({
  unpackPhase = "true";

  configurePhase = ''
    runHook preConfigure
    mkdir node_modules
    ${stdenv.lib.concatStrings (map (dep: ''
      ln -sv ${dep}/lib/node_modules/${(builtins.parseDrvName dep.name).name} node_modules/
    '') deps)}
    export HOME=$(pwd)
    runHook postConfigure
  '';

  buildPhase = ''
    runHook preBuild
    npm --registry http://www.example.com --nodedir=${sources} install ${src} ${npmFlags}
    runHook postBuild
  '';

  installPhase = ''
    runHook preInstall
    mkdir -p $out/lib/node_modules
    mv node_modules/${requireName} $out/lib/node_modules
    mv node_modules/.bin $out/lib/node_modules 2>/dev/null || true
    rm -fR $out/lib/node_modules/${requireName}/node_modules
    mv node_modules $out/lib/node_modules/${requireName}
    if [ -d "$out/lib/node_modules/.bin" ]; then
      ln -sv node_modules/.bin $out/bin
      node=`type -p node`
      coffee=`type -p coffee || true`
      find -L $out/lib/node_modules/.bin/* -type f -print0 | \
        xargs -0 sed --follow-symlinks -i \
          -e 's@#!/usr/bin/env node@#!'"$node"'@' \
          -e 's@#!/usr/bin/env coffee@#!'"$coffee"'@' \
          -e 's@#!/.*/node@#!'"$node"'@' \
          -e 's@#!/.*/coffee@#!'"$coffee"'@'
    fi
    if [ -e "$out/lib/node_modules/${requireName}/man" ]; then
      mkdir $out/share
      ln -sv $out/lib/node_modules/${requireName}/man $out/share/man
    fi
    runHook postInstall
  '';

  preFixup = ''
    find $out -type f -print0 | xargs -0 sed -i 's|${src}|${src.name}|g'
  '';
} // args // {
  # Run the node setup hook when this package is a build input
  propagatedNativeBuildInputs = (args.propagatedNativeBuildInputs or []) ++ [ nodejs ];

  # Make buildNodePackage useful with --run-env
  nativeBuildInputs = (args.nativeBuildInputs or []) ++ deps ++ neededNatives;
} )
<commit_msg>buildNodePackage: Fix $out/bin symlink<commit_after>{ stdenv, runCommand, nodejs, neededNatives}:

args @ { name, src, deps ? [], flags ? [], ... }:

with stdenv.lib;

let
  npmFlags = concatStringsSep " " (map (v: "--${v}") flags);

  sources = runCommand "node-sources" {} ''
    tar xf ${nodejs.src}
    mv *node* $out
  '';

  requireName = (builtins.parseDrvName name).name;
in
stdenv.mkDerivation ({
  unpackPhase = "true";

  configurePhase = ''
    runHook preConfigure
    mkdir node_modules
    ${stdenv.lib.concatStrings (map (dep: ''
      ln -sv ${dep}/lib/node_modules/${(builtins.parseDrvName dep.name).name} node_modules/
    '') deps)}
    export HOME=$(pwd)
    runHook postConfigure
  '';

  buildPhase = ''
    runHook preBuild
    npm --registry http://www.example.com --nodedir=${sources} install ${src} ${npmFlags}
    runHook postBuild
  '';

  installPhase = ''
    runHook preInstall
    mkdir -p $out/lib/node_modules
    mv node_modules/${requireName} $out/lib/node_modules
    mv node_modules/.bin $out/lib/node_modules 2>/dev/null || true
    rm -fR $out/lib/node_modules/${requireName}/node_modules
    mv node_modules $out/lib/node_modules/${requireName}
    if [ -d "$out/lib/node_modules/.bin" ]; then
      ln -sv $out/lib/node_modules/.bin $out/bin
      node=`type -p node`
      coffee=`type -p coffee || true`
      find -L $out/lib/node_modules/.bin/* -type f -print0 | \
        xargs -0 sed --follow-symlinks -i \
          -e 's@#!/usr/bin/env node@#!'"$node"'@' \
          -e 's@#!/usr/bin/env coffee@#!'"$coffee"'@' \
          -e 's@#!/.*/node@#!'"$node"'@' \
          -e 's@#!/.*/coffee@#!'"$coffee"'@'
    fi
    if [ -e "$out/lib/node_modules/${requireName}/man" ]; then
      mkdir $out/share
      ln -sv $out/lib/node_modules/${requireName}/man $out/share/man
    fi
    runHook postInstall
  '';

  preFixup = ''
    find $out -type f -print0 | xargs -0 sed -i 's|${src}|${src.name}|g'
  '';
} // args // {
  # Run the node setup hook when this package is a build input
  propagatedNativeBuildInputs = (args.propagatedNativeBuildInputs or []) ++ [ nodejs ];

  # Make buildNodePackage useful with --run-env
  nativeBuildInputs = (args.nativeBuildInputs or []) ++ deps ++ neededNatives;
} )

{'fraction_non_alphanumeric': 0.17684021543985637, 'fraction_numerical': 0.002244165170556553, 'mean_word_length': 2.806148590947908, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4886268', 'n_tokens_mistral': 1636, 'n_tokens_neox': 1600, 'n_words': 349}