1111
1212 Any unmatched variable names in the file at the provided path will cause a build failure.
1313
14- 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.
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.
1616
1717 # Inputs
1818
1919 `path` ([Store Path](https://nixos.org/manual/nix/latest/store/store-path.html#store-path) String)
2020 : The file in which to replace variables.
2121
2222 `attrs` (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).
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.
2425
2526 # Example
2627
@@ -36,13 +37,19 @@ path: attrs:
3637
3738let
3839 # We use `--replace-fail` instead of `--subst-var-by` so that if the thing isn't there, we fail.
39- subst-var-by = name : value : [
40- "--replace-fail"
41- ( lib . escapeShellArg "@${ name } @" )
42- ( lib . escapeShellArg value )
43- ] ;
40+ subst-var-by =
41+ name : value :
42+ lib . optionals ( value != null ) [
43+ "--replace-fail"
44+ ( lib . escapeShellArg "@${ name } @" )
45+ ( lib . escapeShellArg value )
46+ ] ;
4447
4548 replacements = lib . concatLists ( lib . mapAttrsToList subst-var-by attrs ) ;
49+
50+ left-overs = map ( { name , ... } : name ) (
51+ builtins . filter ( { value , ... } : value == null ) ( lib . attrsToList attrs )
52+ ) ;
4653in
4754
4855stdenvNoCC . mkDerivation {
@@ -62,13 +69,15 @@ stdenvNoCC.mkDerivation {
6269 # Look for Nix identifiers surrounded by `@` that aren't substituted.
6370 checkPhase =
6471 let
65- regex = lib . escapeShellArg "@[a-zA-Z_][0-9A-Za-z_'-]*@" ;
72+ lookahead =
73+ if builtins . length left-overs == 0 then "" else "(?!${ builtins . concatStringsSep "|" left-overs } @)" ;
74+ regex = lib . escapeShellArg "@${ lookahead } [a-zA-Z_][0-9A-Za-z_'-]*@" ;
6675 in
6776 ''
6877 runHook preCheck
69- if grep -qe ${ regex } "$out"; then
78+ if grep -Pqe ${ regex } "$out"; then
7079 echo The following look like unsubstituted Nix identifiers that remain in "$out":
71- grep -oe ${ regex } "$out"
80+ grep -Poe ${ regex } "$out"
7281 echo Use the more precise '`substitute`' function if this check is in error.
7382 exit 1
7483 fi
0 commit comments