|
| 1 | +{ lib, stdenvNoCC }: |
| 2 | + |
| 3 | +/** |
| 4 | + `replaceVarsWith` is a wrapper around the [bash function `substitute`](https://nixos.org/manual/nixpkgs/stable/#fun-substitute) |
| 5 | + in the stdenv. It allows for terse replacement of names in the specified path, while checking |
| 6 | + for common mistakes such as naming a replacement that does nothing or forgetting a variable which |
| 7 | + needs to be replaced. |
| 8 | +
|
| 9 | + As with the [`--subst-var-by`](https://nixos.org/manual/nixpkgs/stable/#fun-substitute-subst-var-by) |
| 10 | + flag, names are encoded as `@name@` in the provided file at the provided path. |
| 11 | +
|
| 12 | + Any unmatched variable names in the file at the provided path will cause a build failure. |
| 13 | +
|
| 14 | + By default, any remaining text that matches `@[A-Za-z_][0-9A-Za-z_'-]@` in the output after replacement |
| 15 | + has occurred will cause a build failure. Variables can be excluded from this check by passing "null" for them. |
| 16 | +
|
| 17 | + # Inputs |
| 18 | +
|
| 19 | + `src` ([Store Path](https://nixos.org/manual/nix/latest/store/store-path.html#store-path) String) |
| 20 | + : The file in which to replace variables. |
| 21 | +
|
| 22 | + `replacements` (AttrsOf String) |
| 23 | + : Each entry in this set corresponds to a `--subst-var-by` entry in [`substitute`](https://nixos.org/manual/nixpkgs/stable/#fun-substitute) or |
| 24 | + null to keep it unchanged. |
| 25 | +
|
| 26 | + `dir` (String) |
| 27 | + : Sub directory in $out to store the result in. Commonly set to "bin". |
| 28 | +
|
| 29 | + `isExecutable` (Boolean) |
| 30 | + : Whether to mark the output file as executable. |
| 31 | +
|
| 32 | + Most arguments supported by mkDerivation are also supported, with some exceptions for which |
| 33 | + an error will be thrown. |
| 34 | +
|
| 35 | + # Example |
| 36 | +
|
| 37 | + ```nix |
| 38 | + { replaceVarsWith }: |
| 39 | +
|
| 40 | + replaceVarsWith { |
| 41 | + src = ./my-setup-hook.sh; |
| 42 | + replacements = { world = "hello"; }; |
| 43 | + dir = "bin"; |
| 44 | + isExecutable = true; |
| 45 | + } |
| 46 | + ``` |
| 47 | +
|
| 48 | + See `../../test/replace-vars/default.nix` for tests of this function. Also see `replaceVars` for a short |
| 49 | + version with src and replacements only. |
| 50 | +*/ |
| 51 | +{ |
| 52 | + src, |
| 53 | + replacements, |
| 54 | + dir ? null, |
| 55 | + isExecutable ? false, |
| 56 | + ... |
| 57 | +}@attrs: |
| 58 | + |
| 59 | +let |
| 60 | + # We use `--replace-fail` instead of `--subst-var-by` so that if the thing isn't there, we fail. |
| 61 | + subst-var-by = |
| 62 | + name: value: |
| 63 | + lib.optionals (value != null) [ |
| 64 | + "--replace-fail" |
| 65 | + (lib.escapeShellArg "@${name}@") |
| 66 | + (lib.escapeShellArg value) |
| 67 | + ]; |
| 68 | + |
| 69 | + substitutions = lib.concatLists (lib.mapAttrsToList subst-var-by replacements); |
| 70 | + |
| 71 | + left-overs = map ({ name, ... }: name) ( |
| 72 | + builtins.filter ({ value, ... }: value == null) (lib.attrsToList replacements) |
| 73 | + ); |
| 74 | + |
| 75 | + optionalAttrs = |
| 76 | + if (builtins.intersectAttrs attrs forcedAttrs == { }) then |
| 77 | + builtins.removeAttrs attrs [ "replacements" ] |
| 78 | + else |
| 79 | + throw "Passing any of ${builtins.concatStringsSep ", " (builtins.attrNames forcedAttrs)} to replaceVarsWith is not supported."; |
| 80 | + |
| 81 | + forcedAttrs = { |
| 82 | + doCheck = true; |
| 83 | + dontUnpack = true; |
| 84 | + preferLocalBuild = true; |
| 85 | + allowSubstitutes = false; |
| 86 | + |
| 87 | + buildPhase = '' |
| 88 | + runHook preBuild |
| 89 | +
|
| 90 | + target=$out |
| 91 | + if test -n "$dir"; then |
| 92 | + target=$out/$dir/$name |
| 93 | + mkdir -p $out/$dir |
| 94 | + fi |
| 95 | +
|
| 96 | + substitute "$src" "$target" ${lib.concatStringsSep " " substitutions} |
| 97 | +
|
| 98 | + if test -n "$isExecutable"; then |
| 99 | + chmod +x $target |
| 100 | + fi |
| 101 | +
|
| 102 | + runHook postBuild |
| 103 | + ''; |
| 104 | + |
| 105 | + # Look for Nix identifiers surrounded by `@` that aren't substituted. |
| 106 | + checkPhase = |
| 107 | + let |
| 108 | + lookahead = |
| 109 | + if builtins.length left-overs == 0 then "" else "(?!${builtins.concatStringsSep "|" left-overs}@)"; |
| 110 | + regex = lib.escapeShellArg "@${lookahead}[a-zA-Z_][0-9A-Za-z_'-]*@"; |
| 111 | + in |
| 112 | + '' |
| 113 | + runHook preCheck |
| 114 | + if grep -Pqe ${regex} "$out"; then |
| 115 | + echo The following look like unsubstituted Nix identifiers that remain in "$out": |
| 116 | + grep -Poe ${regex} "$out" |
| 117 | + echo Use the more precise '`substitute`' function if this check is in error. |
| 118 | + exit 1 |
| 119 | + fi |
| 120 | + runHook postCheck |
| 121 | + ''; |
| 122 | + }; |
| 123 | +in |
| 124 | + |
| 125 | +stdenvNoCC.mkDerivation ( |
| 126 | + { |
| 127 | + name = baseNameOf (toString src); |
| 128 | + } |
| 129 | + // optionalAttrs |
| 130 | + // forcedAttrs |
| 131 | +) |
0 commit comments