<commit_before>params: with params;
# combine =
args@{
  pkgFilter ? (pkg: pkg.tlType == "run" || pkg.tlType == "bin" || pkg.pname == "core")
, extraName ? "combined", ...
}:
let
  pkgSet = removeAttrs args [ "pkgFilter" "extraName" ] // {
    # include a fake "core" package
    core.pkgs = [
      (bin.core.out // { pname = "core"; tlType = "bin"; })
      (bin.core.doc // { pname = "core"; tlType = "doc"; })
    ];
  };
  pkgList = rec {
    all = lib.filter pkgFilter (combinePkgs pkgSet);
    splitBin = lib.partition (p: p.tlType == "bin") all;
    bin = mkUniquePkgs splitBin.right;
    nonbin = mkUniquePkgs splitBin.wrong;

    # extra interpreters needed for shebangs, based on 2015 schemes "medium" and "tetex"
    # (omitted tk needed in pname == "epspdf", bin/epspdftk)
    pkgNeedsPython = pkg: pkg.tlType == "run" && lib.elem pkg.pname
      [ "de-macro" "pythontex" "dviasm" "texliveonfly" ];
    pkgNeedsRuby = pkg: pkg.tlType == "run" && pkg.pname == "match-parens";
    extraInputs =
      lib.optional (lib.any pkgNeedsPython splitBin.wrong) python
      ++ lib.optional (lib.any pkgNeedsPython splitBin.wrong) ruby;
  };

  mkUniquePkgs = pkgs: fastUnique (a: b: a < b)
    # here we deal with those dummy packages needed for hyphenation filtering
    (map (p: if lib.isDerivation p then builtins.toPath p else "") pkgs);

in buildEnv {
  name = "texlive-${extraName}-${bin.texliveYear}";

  extraPrefix = "/share/texmf";

  ignoreCollisions = false;
  paths = pkgList.nonbin;

  buildInputs = [ makeWrapper ] ++ pkgList.extraInputs;

  postBuild = ''
    cd "$out"
    mkdir -p ./bin
  '' +
    lib.concatMapStrings
      (path: ''
        for f in '${path}'/bin/*; do
          if [[ -L "$f" ]]; then
            cp -d "$f" ./bin/
          else
            ln -s "$f" ./bin/
          fi
        done
      '')
      pkgList.bin
    +
  ''
    export PATH="$out/bin:$out/share/texmf/scripts/texlive:${perl}/bin:$PATH"
    export TEXMFCNF="$out/share/texmf/web2c"
    export TEXMFDIST="$out/share/texmf"
    export TEXMFSYSCONFIG="$out/share/texmf-config"
    export TEXMFSYSVAR="$out/share/texmf-var"
    export PERL5LIB="$out/share/texmf/scripts/texlive"
  '' +
    # patch texmf-dist -> texmf to be sure
    # TODO: cleanup the search paths incl. SELFAUTOLOC, and perhaps do lua actions?
    # tried inspiration from install-tl, sub do_texmf_cnf
  ''
    (
      cd ./share/texmf/web2c/
      local cnfOrig="$(realpath ./texmf.cnf)"
      rm ./texmf.cnf
      cat "$cnfOrig" | sed 's/texmf-dist/texmf/g' > ./texmf.cnf

      rm updmap.cfg
    )
  '' +
    # updmap.cfg seems like not needing changes

    # now filter hyphenation patterns, in a hacky way ATM
  (let script =
    writeText "hyphens.sed" (
      lib.concatMapStrings (pkg: "/^\% from ${pkg.pname}/,/^\%/p;\n") pkgList.splitBin.wrong
      + "1,/^\% from/p;" );
  in ''
    (
      cd ./share/texmf/tex/generic/config/
      for fname in language.dat language.def; do
        [ -e $fname ] || continue;
        cnfOrig="$(realpath ./$fname)"
        rm ./$fname
        cat "$cnfOrig" | sed -n -f '${script}' > ./$fname
      done
    )
  '') +

  # function to wrap created executables with required env vars
  ''
    wrapBin() {
    for link in ./bin/*; do
      [ -L "$link" -a -x "$link" ] || continue # if not link, assume OK
      local target=$(readlink "$link")

      # skip simple local symlinks; mktexfmt in particular
      echo "$target" | grep / > /dev/null || continue;

      echo -n "Wrapping '$link'"
      rm "$link"
      makeWrapper "$target" "$link" \
        --prefix PATH : "$out/bin:${perl}/bin" \
        --set TEXMFCNF "$out/share/texmf/web2c" \
        --set TEXMFDIST "$out/share/texmf" \
        --set TEXMFSYSCONFIG "$out/share/texmf-config" \
        --set TEXMFSYSVAR "$out/share/texmf-var" \
        --prefix PERL5LIB : "$out/share/texmf/scripts/texlive"

      # avoid using non-nix shebang in $target by calling interpreter
      if [[ "$(head -c 2 $target)" = "#!" ]]; then
        local cmdline="$(head -n 1 $target | sed 's/^\#\! *//;s/ *$//')"
        local relative=`basename "$cmdline" | sed 's/^env //' `
        local newInterp=`echo "$relative" | cut -d\  -f1`
        local params=`echo "$relative" | cut -d\  -f2- -s`
        local newPath="$(type -P $newInterp)"
        if [[ -z "$newPath" ]]; then
          echo " Warning: unknown shebang '$cmdline' in '$target'"
          continue
        fi
        echo " and patching shebang '$cmdline'"
        sed "s|^exec |exec $newPath $params |" -i "$link"
      else
        sed 's|^exec |exec -a "$0" |' -i "$link"
        echo
      fi
    done
    }
  '' +
  # texlive post-install actions<commit_msg>texlive.combine: patch paths into texmf.cnf<commit_after>params: with params;
# combine =
args@{
  pkgFilter ? (pkg: pkg.tlType == "run" || pkg.tlType == "bin" || pkg.pname == "core")
, extraName ? "combined", ...
}:
let
  pkgSet = removeAttrs args [ "pkgFilter" "extraName" ] // {
    # include a fake "core" package
    core.pkgs = [
      (bin.core.out // { pname = "core"; tlType = "bin"; })
      (bin.core.doc // { pname = "core"; tlType = "doc"; })
    ];
  };
  pkgList = rec {
    all = lib.filter pkgFilter (combinePkgs pkgSet);
    splitBin = lib.partition (p: p.tlType == "bin") all;
    bin = mkUniquePkgs splitBin.right;
    nonbin = mkUniquePkgs splitBin.wrong;

    # extra interpreters needed for shebangs, based on 2015 schemes "medium" and "tetex"
    # (omitted tk needed in pname == "epspdf", bin/epspdftk)
    pkgNeedsPython = pkg: pkg.tlType == "run" && lib.elem pkg.pname
      [ "de-macro" "pythontex" "dviasm" "texliveonfly" ];
    pkgNeedsRuby = pkg: pkg.tlType == "run" && pkg.pname == "match-parens";
    extraInputs =
      lib.optional (lib.any pkgNeedsPython splitBin.wrong) python
      ++ lib.optional (lib.any pkgNeedsPython splitBin.wrong) ruby;
  };

  mkUniquePkgs = pkgs: fastUnique (a: b: a < b)
    # here we deal with those dummy packages needed for hyphenation filtering
    (map (p: if lib.isDerivation p then builtins.toPath p else "") pkgs);

in buildEnv {
  name = "texlive-${extraName}-${bin.texliveYear}";

  extraPrefix = "/share/texmf";

  ignoreCollisions = false;
  paths = pkgList.nonbin;

  buildInputs = [ makeWrapper ] ++ pkgList.extraInputs;

  postBuild = ''
    cd "$out"
    mkdir -p ./bin
  '' +
    lib.concatMapStrings
      (path: ''
        for f in '${path}'/bin/*; do
          if [[ -L "$f" ]]; then
            cp -d "$f" ./bin/
          else
            ln -s "$f" ./bin/
          fi
        done
      '')
      pkgList.bin
    +
  ''
    export PATH="$out/bin:$out/share/texmf/scripts/texlive:${perl}/bin:$PATH"
    export PERL5LIB="$out/share/texmf/scripts/texlive"
  '' +
    # patch texmf-{dist,local} -> texmf to be sure
    # TODO: perhaps do lua actions?
    # tried inspiration from install-tl, sub do_texmf_cnf
  ''
    (
      cd ./share/texmf/web2c/
      local cnfOrig="$(realpath ./texmf.cnf)"
      rm ./texmf.cnf
      sed \
        -e 's,texmf-dist,texmf,g' \
        -e 's,texmf-local,texmf,g' \
        -e "s,\$SELFAUTOLOC,$out,g" \
        -e "s,\$SELFAUTODIR,$out/share,g" \
        -e "s,\$SELFAUTOPARENT,$out/share,g" \
        -e "s,\$SELFAUTOGRANDPARENT,$out/share,g" \
        "$cnfOrig" > ./texmf.cnf

      rm updmap.cfg
    )
  '' +
    # updmap.cfg seems like not needing changes

    # now filter hyphenation patterns, in a hacky way ATM
  (let script =
    writeText "hyphens.sed" (
      lib.concatMapStrings (pkg: "/^\% from ${pkg.pname}/,/^\%/p;\n") pkgList.splitBin.wrong
      + "1,/^\% from/p;" );
  in ''
    (
      cd ./share/texmf/tex/generic/config/
      for fname in language.dat language.def; do
        [ -e $fname ] || continue;
        cnfOrig="$(realpath ./$fname)"
        rm ./$fname
        cat "$cnfOrig" | sed -n -f '${script}' > ./$fname
      done
    )
  '') +

  # function to wrap created executables with required env vars
  ''
    wrapBin() {
    for link in ./bin/*; do
      [ -L "$link" -a -x "$link" ] || continue # if not link, assume OK
      local target=$(readlink "$link")

      # skip simple local symlinks; mktexfmt in particular
      echo "$target" | grep / > /dev/null || continue;

      echo -n "Wrapping '$link'"
      rm "$link"
      makeWrapper "$target" "$link" \
        --prefix PATH : "$out/bin:${perl}/bin" \
        --prefix PERL5LIB : "$out/share/texmf/scripts/texlive"

      # avoid using non-nix shebang in $target by calling interpreter
      if [[ "$(head -c 2 $target)" = "#!" ]]; then
        local cmdline="$(head -n 1 $target | sed 's/^\#\! *//;s/ *$//')"
        local relative=`basename "$cmdline" | sed 's/^env //' `
        local newInterp=`echo "$relative" | cut -d\  -f1`
        local params=`echo "$relative" | cut -d\  -f2- -s`
        local newPath="$(type -P $newInterp)"
        if [[ -z "$newPath" ]]; then
          echo " Warning: unknown shebang '$cmdline' in '$target'"
          continue
        fi
        echo " and patching shebang '$cmdline'"
        sed "s|^exec |exec $newPath $params |" -i "$link"
      else
        sed 's|^exec |exec -a "$0" |' -i "$link"
        echo
      fi
    done
    }
  '' +
  # texlive post-install actions
{'fraction_non_alphanumeric': 0.16513065163179008, 'fraction_numerical': 0.003035888539520763, 'mean_word_length': 2.2421792618629173, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '670191', 'n_tokens_mistral': 3390, 'n_tokens_neox': 3241, 'n_words': 834}