blob: 57036ebd74d14f15c70757305451782eb230898e (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
{
lib,
runCommandLocal,
nix,
}:
# Replace some direct dependencies of drv, not recursing into the dependency tree.
# You likely want to use replaceDependencies instead, unless you plan to implement your own recursion mechanism.
{
drv,
replacements ? [ ],
}:
let
inherit (lib)
isStorePath
substring
stringLength
optionalString
escapeShellArgs
concatMap
;
in
if replacements == [ ] then
drv
else
let
drvName =
if isStorePath drv then
# Reconstruct the name from the actual store path if available.
substring 33 (stringLength (baseNameOf drv)) (baseNameOf drv)
else if drv ? drvAttrs.name then
# Try to get the name from the derivation arguments otherwise (for floating or deferred derivations).
drv.drvAttrs.name
+ (
let
outputName = drv.outputName or "out";
in
optionalString (outputName != "out") "-${outputName}"
)
else
throw "cannot reconstruct the derivation name from ${drv}";
in
runCommandLocal drvName { nativeBuildInputs = [ nix.out ]; } ''
createRewriteScript() {
while [ $# -ne 0 ]; do
oldBasename="$(basename "$1")"
newBasename="$(basename "$2")"
shift 2
if [ ''${#oldBasename} -ne ''${#newBasename} ]; then
echo "cannot rewrite $oldBasename to $newBasename: length does not match" >&2
exit 1
fi
echo "s|$oldBasename|$newBasename|g" >> rewrite.sed
done
}
createRewriteScript ${
escapeShellArgs (
[
drv
(placeholder "out")
]
++ concatMap (
{ oldDependency, newDependency }:
[
oldDependency
newDependency
]
) replacements
)
}
nix-store --dump ${drv} | sed -f rewrite.sed | nix-store --restore $out
''
|