nix-helpers: eb3dbe1991e72ef8579d118bb64ffc440afd4c49

     1: # Replaces /usr/bin/env in shebangs, since it doesn't exist in sandboxes
     2: { attrsToDirs', coreutils, die, dummyBuild, fail, lib, runCommand, sanitiseName
     3: , writeScript }:
     4: 
     5: with builtins;
     6: with lib;
     7: with rec {
     8:   # Splits off the first line of the given string, to give { first; rest; }
     9:   splitLine = first: s:
    10:     with {
    11:       char = substring 0 1 s;
    12:       rest = substring 1 (stringLength s) s;
    13:     };
    14:     if s == "" || char == "\n" then {
    15:       inherit first rest;
    16:     } else
    17:       splitLine (first + char) rest;
    18: 
    19:   patchString = s:
    20:     if hasPrefix "#!" s then
    21:       with splitLine "" s;
    22:       concatStringsSep "\n" [
    23:         (replaceStrings [ "/usr/bin/env" ] [ "${coreutils}/bin/env" ] first)
    24:         rest
    25:       ]
    26:     else
    27:       s;
    28: 
    29:   patchFiles = name: dir: given:
    30:     runCommand (if name == null then
    31:       "${unsafeDiscardStringContext (baseNameOf "${given}")}"
    32:     else
    33:       name) { inherit given; } ''
    34:         cp -L ${if dir then "-r" else ""} "$given" "$out"
    35:         chmod +w -R "$out"
    36:         while read -r F
    37:         do
    38:           sed -i "$F" -e '1 s@^#! */usr/bin/env@#!${coreutils}/bin/env@'
    39:         done < <(find "$out" -type f)
    40:       '';
    41: };
    42: { dir ? null, file ? null, name ? null, string ? null }:
    43: with {
    44:   count = fold (x: count: if x == null then count + 1 else count) 0 [
    45:     string
    46:     file
    47:     dir
    48:   ];
    49: };
    50: assert count == 2 || die {
    51:   inherit name;
    52:   error = "Exactly 1 patchShebang arg must be non-null";
    53:   dir = typeOf dir;
    54:   file = typeOf file;
    55:   string = typeOf string;
    56: };
    57: if string != null then
    58:   patchString string
    59: else if dir == null then
    60:   patchFiles name false file
    61: else
    62:   patchFiles name true dir

Generated by git2html.