Skip to content

Commit cd02253

Browse files
authored
replaceVars: allow exemptions (#357395)
2 parents c106fbf + 2d64877 commit cd02253

3 files changed

Lines changed: 65 additions & 13 deletions

File tree

pkgs/build-support/replace-vars/default.nix

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@
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

3738
let
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+
);
4653
in
4754

4855
stdenvNoCC.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

pkgs/test/replace-vars/default.nix

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,49 @@ in
6969
# Shouldn't see the "cannot detect" version.
7070
! grep -q -F "cannot detect due to space" $failed/testBuildFailure.log
7171
72+
touch $out
73+
'';
74+
75+
replaceVars-succeeds-with-exemption = testEqualContents {
76+
assertion = "replaceVars-succeeds-with-exemption";
77+
actual = replaceVars ./source.txt {
78+
free = "free";
79+
"equal in" = "are the same in";
80+
brotherhood = null;
81+
};
82+
83+
expected = builtins.toFile "expected" ''
84+
All human beings are born free and are the same in dignity and rights.
85+
They are endowed with reason and conscience and should act towards
86+
one another in a spirit of @brotherhood@.
87+
88+
-- eroosevelt@humanrights.un.org
89+
'';
90+
};
91+
92+
replaceVars-fails-in-check-phase-with-exemption =
93+
runCommand "replaceVars-fails-with-exemption"
94+
{
95+
failed =
96+
let
97+
src = builtins.toFile "source.txt" ''
98+
@a@
99+
@b@
100+
@c@
101+
'';
102+
in
103+
testBuildFailure (
104+
replaceVars src {
105+
a = "a";
106+
b = null;
107+
}
108+
);
109+
}
110+
''
111+
grep -e "unsubstituted Nix identifiers.*source.txt" $failed/testBuildFailure.log
112+
grep -F "@c@" $failed/testBuildFailure.log
113+
! grep -F "@b@" $failed/testBuildFailure.log
114+
72115
touch $out
73116
'';
74117
}

pkgs/tools/virtualization/mkosi/default.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ buildPythonApplication rec {
6868
(replaceVars ./0001-Use-wrapped-binaries-instead-of-Python-interpreter.patch {
6969
UKIFY = "${systemdForMkosi}/lib/systemd/ukify";
7070
PYTHON_PEFILE = "${python3pefile}/bin/python3.12";
71-
MKOSI_SANDBOX = "~MKOSI_SANDBOX~"; # to satisfy replaceVars, will be replaced in postPatch
71+
MKOSI_SANDBOX = null; # will be replaced in postPatch
7272
})
7373
(replaceVars ./0002-Fix-library-resolving.patch {
7474
LIBC = "${stdenv.cc.libc}/lib/libc.so.6";
@@ -84,7 +84,7 @@ buildPythonApplication rec {
8484
postPatch = ''
8585
# As we need the $out reference, we can't use `replaceVars` here.
8686
substituteInPlace mkosi/run.py \
87-
--replace-fail '~MKOSI_SANDBOX~' "\"$out/bin/mkosi-sandbox\""
87+
--replace-fail '@MKOSI_SANDBOX@' "\"$out/bin/mkosi-sandbox\""
8888
'';
8989

9090
nativeBuildInputs = [

0 commit comments

Comments
 (0)