warbo-utilities: 43aa444ca87945f7b944bdb6a512313e075020a4

     1: {
     2:   nix-helpers ? warbo-packages.nix-helpers,
     3:   nixpkgs ? nix-helpers.nixpkgs,
     4:   nixpkgs-lib ? nix-helpers.nixpkgs-lib,
     5:   warbo-packages ? import ./warbo-packages.nix,
     6: }:
     7: 
     8: with rec {
     9:   inherit (builtins)
    10:     attrValues
    11:     elem
    12:     isAttrs
    13:     mapAttrs
    14:     readDir
    15:     substring
    16:     ;
    17: 
    18:   inherit (nixpkgs-lib) escapeShellArg foldl;
    19: 
    20:   inherit (nix-helpers)
    21:     attrsToDirs
    22:     dirsToAttrs
    23:     fail
    24:     foldAttrs'
    25:     nixFilesIn
    26:     nixpkgs1709
    27:     patchShebang
    28:     withDeps
    29:     ;
    30: 
    31:   inherit (nixpkgs)
    32:     bash
    33:     makeWrapper
    34:     newScope
    35:     runCommand
    36:     shellcheck
    37:     ;
    38: 
    39:   # Let scripts depend on each other by adding 'bin' to the argument set
    40:   extraArgs =
    41:     nix-helpers
    42:     // warbo-packages
    43:     // {
    44:       raw = mapAttrs (
    45:         name: entry:
    46:         if isAttrs entry then
    47:           ./raw + "/${name}"
    48:         else if
    49:           elem name [
    50:             "alert.wav"
    51:             "bbcExamplePage.html.gz"
    52:           ]
    53:         then
    54:           entry
    55:         else
    56:           patchShebang {
    57:             inherit name;
    58:             file = entry;
    59:           }
    60:       ) (dirsToAttrs ./raw);
    61: 
    62:       scripts = warbo-utilities-scripts;
    63:     };
    64: 
    65:   scripts = mapAttrs (_: f: newScope extraArgs f { }) (nixFilesIn ./scripts);
    66: 
    67:   cmds =
    68:     foldl (rest: dir: rest // mapAttrs (f: _: dir + "/${f}") (readDir dir)) { }
    69:       [
    70:         ./system
    71:         ./web
    72:         ./git
    73:         ./docs
    74:       ];
    75: 
    76:   check = mapAttrs (
    77:     name: script:
    78:     runCommand "check-${name}"
    79:       {
    80:         inherit script;
    81:         buildInputs = [
    82:           fail
    83:           shellcheck
    84:         ];
    85:         LANG = "en_US.UTF-8";
    86:       }
    87:       ''
    88:         set -e
    89: 
    90:         # Unwrap until we get to the real implementation
    91:         while grep -q "extraFlagsArray" < "$script"
    92:         do
    93:           script=$(grep '^exec' < "$script" | cut -d ' ' -f2 |
    94:                                               tr -d '"')
    95:         done
    96:         echo "Checking '$script'" 1>&2
    97: 
    98:         SHEBANG=$(head -n1 < "$script")
    99:         echo "$SHEBANG" | grep -q '^#!' || {
   100:           # Binaries, etc.
   101:           mkdir "$out"
   102:           exit 0
   103:         }
   104: 
   105:         echo "$SHEBANG" | grep -q 'usr/bin/env' ||
   106:         echo "$SHEBANG" | grep -q '/nix/store' ||
   107:           fail "Didn't use /usr/bin/env or /nix/store:\n$SHEBANG"
   108: 
   109:         if echo "$SHEBANG" | grep -q 'bash'
   110:         then
   111:           shellcheck "$script"
   112:         fi
   113:         mkdir "$out"
   114:       ''
   115:   ) warbo-utilities-scripts;
   116: 
   117:   warbo-utilities-scripts = cmds // scripts;
   118: };
   119: withDeps (attrValues check) (
   120:   runCommand "warbo-utilities"
   121:     {
   122:       bin = attrsToDirs warbo-utilities-scripts;
   123:       buildInputs = [
   124:         fail
   125:         makeWrapper
   126:       ];
   127:       forContext = foldAttrs' (
   128:         _: val: str:
   129:         substring 0 0 ''
   130:           ${val} ${str}
   131:         ''
   132:       ) "" warbo-utilities-scripts;
   133:     }
   134:     ''
   135:       echo "Tying the knot between scripts" 1>&2
   136:       mkdir -p "$out/bin" || fail "Couldn't make '$out/bin'"
   137:       for F in ${fail}/bin/fail "$bin"/*
   138:       do
   139:         N=$(basename "$F") || fail "No basename for '$F'"
   140:         # Create a trampoline, to avoid problems with symlinks, etc.
   141:         {
   142:           echo '#!${bash}/bin/bash'
   143:           printf 'exec %s "$@"' "$F"
   144:         } > "$out/bin/$N"
   145:         chmod +x "$out/bin/$N"
   146:         wrapProgram "$out/bin/$N" --prefix PATH : "$out/bin" ||
   147:           fail "Couldn't wrap '$F'"
   148:       done
   149:       echo "Finished wrapping scripts" 1>&2
   150:     ''
   151: )
   152: // {
   153:   inherit
   154:     cmds
   155:     nix-helpers
   156:     nixpkgs
   157:     nixpkgs-lib
   158:     scripts
   159:     warbo-packages
   160:     ;
   161:   warbo-utilities-src = ./.;
   162: }

Generated by git2html.