-
Notifications
You must be signed in to change notification settings - Fork 0
/
relocate.nix
74 lines (62 loc) · 2.21 KB
/
relocate.nix
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
73
74
{ runCommand, nix, lib , nixrewrite }:
# Replace the prefix of the requisites tree of drv, propagating
# the change all the way up the tree, without a full rebuild.
# Example: safeFirefox = replaceDependency {
# drv = firefox;
# };
{ drv, verbose ? true, storePrefix ? "/tmp/store" }:
with lib;
let
origDrv = drv;
warn = if verbose then builtins.trace else (x: y: y);
references = import (runCommand "references.nix" { exportReferencesGraph = [ "graph" drv ]; } ''
(echo {
while read path
do
echo " \"$path\" = ["
read count
read count
while [ "0" != "$count" ]
do
read ref_path
if [ "$ref_path" != "$path" ]
then
echo " (\"$ref_path\")"
fi
count=$(($count - 1))
done
echo " ];"
done < graph
echo }) > $out
'').outPath;
discard = builtins.unsafeDiscardStringContext;
referencesOf = drv: references.${discard (toString drv)};
drvName = drv:
discard (substring 33 (stringLength (builtins.baseNameOf drv)) (builtins.baseNameOf drv));
rewriteHashes = drv: hashes: runCommand (drvName drv) {
nixStore = "${nix.out}/bin/nix-store";
buildInputs = [ nixrewrite origDrv ];
} ''
echo "${(builtins.concatStringsSep "\n" (builtins.attrValues hashes))}" > deps
echo ${drv} >> deps
for i in $outputs ; do
echo ''${!i} >> deps
done
cat deps | sort | uniq | xargs -L1 basename > hash.list
cat hash.list
$nixStore --dump ${drv} | sed 's|${baseNameOf drv}|'$(basename $out)'|g' ${
lib.optionalString (builtins.length (builtins.attrNames hashes) != 0)
"| sed -e ${concatStringsSep " -e " (mapAttrsToList (name: value:
"'s|${baseNameOf name}|${baseNameOf value}|g'"
) hashes)
}"} | nixrewrite /nix/store ${storePrefix} hash.list | $nixStore --restore $out
'';
rewriteMemo = listToAttrs (map
(drv: { name = discard (toString drv);
value = rewriteHashes (drv)
(filterAttrs (n: v: builtins.elem ((discard (toString n))) (referencesOf drv)) rewriteMemo);
})
(builtins.attrNames references)) ;
drvHash = discard (toString drv);
in
rewriteMemo.${drvHash}