]> git.immae.eu Git - perso/Immae/Config/Nixies.git/blob - stdenv_prefix/prehook.sh
db9a4d258bbd99372a9b817948abec9e342c6fbb
[perso/Immae/Config/Nixies.git] / stdenv_prefix / prehook.sh
1 count_chunks() {
2 elements="${1//[^\/]}"
3 echo $(( "${#elements}" ))
4 }
5
6 oldout=$out
7 prefix_chunks=$(count_chunks $NIX_STORE)
8
9 : ${storepath_length=${#NIX_STORE}}
10 : ${storepath_chunks=$prefix_chunks}
11
12 if [ "$storepath_chunks" -lt $prefix_chunks ]; then
13 echo "Need to have at least as much path elements as the initial prefix"
14 exit 1
15 fi
16
17 min_length=$(( ${#NIX_STORE} + 2 * $storepath_chunks - 2 * $prefix_chunks ))
18 if [ "$storepath_length" -lt "$min_length" ]; then
19 echo "The storepath length needs to be at least the length of the prefix"
20 exit 1
21 fi
22
23 if [ "$storepath_chunks" -eq "$prefix_chunks" -a "$storepath_length" -gt "$min_length" ]; then
24 echo "You need at least one more chunk for a non-standard path length"
25 exit 1
26 fi
27
28 appendOut() {
29 chunk_prefix=$(for (( c=$prefix_chunks ; c < $storepath_chunks ; c++ )); do echo -n "/x"; done)
30 length_prefix=$(for (( c=$(( ${#NIX_STORE} + ${#chunk_prefix} )) ; c < $storepath_length ; c++ )); do echo -n "x"; done)
31 out="$out$chunk_prefix$length_prefix"
32 if [ -n $chunk_prefix -o -n $length_prefix ]; then
33 mkdir -p $(dirname $out)
34 fi
35
36 : ${libc_path=}
37 : ${configureFlags=}
38 configureFlags="${configureFlags//$oldout/$out}"
39 if [ -n "$libc_path" ]; then
40 libc_oldpath=$libc_path/lib
41 libc_newpath=$(cat $libc_path/nix-support/new-out)/lib
42 configureFlags="${configureFlags//$libc_oldpath/$libc_newpath}"
43 fi
44 }
45
46 addNixSupport() {
47 if [ ! -d $out ]; then
48 return
49 fi
50 mkdir -p $out/nix-support
51 echo $out > $out/nix-support/new-out
52 }
53
54 overridePlaceholder() {
55 function _callImplicitHook {
56 local def="$1"
57 local hookName="$2"
58 if declare -F "$hookName" > /dev/null; then
59 "$hookName"
60 elif type -p "$hookName" > /dev/null; then
61 source "$hookName"
62 elif [ -n "${!hookName:-}" ]; then
63 content="${!hookName}"
64 eval "${content//$oldout/$out}"
65 else
66 return "$def"
67 fi
68 }
69 }
70
71 preHooks+=(appendOut overridePlaceholder)
72
73 restoreSingleFile() {
74 if [ "$out" != "$oldout" -a -f $out ]; then
75 cp -a $out ./file
76 rm -rf $oldout
77 cp -a ./file $oldout
78 fi
79 }
80
81 addSymlinks() {
82 if [ "$out" != "$oldout" -a -d $out ]; then
83 ln -s $out/* $oldout/
84 fi
85 }
86
87 rename_generic_build() {
88 eval "$(echo "orig_genericBuild()"; declare -f genericBuild | tail -n +2)"
89 function genericBuild {
90 orig_genericBuild "$@"
91 restoreSingleFile
92 addSymlinks
93 addNixSupport
94 }
95 }
96
97 exitHooks+=(restoreSingleFile addSymlinks addNixSupport)
98 postHooks+=(rename_generic_build)