]> git.immae.eu Git - perso/Immae/Config/Nixies.git/blob - stdenv_prefix/prehook.sh
Add some fixes to stdenv prefix hook
[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 : ${installFlags=}
39 installFlags="${installFlags//$oldout/$out}"
40 configureFlags="${configureFlags//$oldout/$out}"
41 if [ -n "$libc_path" ]; then
42 libc_oldpath=$libc_path/lib
43 libc_newpath=$(cat $libc_path/nix-support/new-out)/lib
44 configureFlags="${configureFlags//$libc_oldpath/$libc_newpath}"
45 installFlags="${installFlags//$libc_oldpath/$libc_newpath}"
46 fi
47 }
48
49 addNixSupport() {
50 if [ ! -d $out ]; then
51 return
52 fi
53 mkdir -p $out/nix-support
54 echo $out > $out/nix-support/new-out
55 }
56
57 overridePlaceholder() {
58 function _callImplicitHook {
59 local def="$1"
60 local hookName="$2"
61 if declare -F "$hookName" > /dev/null; then
62 "$hookName"
63 elif type -p "$hookName" > /dev/null; then
64 source "$hookName"
65 elif [ -n "${!hookName:-}" ]; then
66 content="${!hookName}"
67 eval "${content//$oldout/$out}"
68 else
69 return "$def"
70 fi
71 }
72 }
73
74 preHooks+=(appendOut overridePlaceholder)
75
76 restoreSingleFile() {
77 if [ "$out" != "$oldout" -a -f $out ]; then
78 cp -a $out ./file
79 rm -rf $oldout
80 cp -a ./file $oldout
81 fi
82 }
83
84 addSymlinks() {
85 if [ "$out" != "$oldout" -a -d $out ]; then
86 ln -s $out/* $oldout/
87 fi
88 }
89
90 rename_generic_build() {
91 eval "$(echo "orig_genericBuild()"; declare -f genericBuild | tail -n +2)"
92 function genericBuild {
93 orig_genericBuild "$@"
94 restoreSingleFile
95 addSymlinks
96 addNixSupport
97 }
98 }
99
100 exitHooks+=(restoreSingleFile addSymlinks addNixSupport)
101 postHooks+=(rename_generic_build)