]> git.immae.eu Git - perso/Immae/Config/Nixies.git/blame - stdenv_prefix/prehook.sh
Add some fixes to stdenv prefix hook
[perso/Immae/Config/Nixies.git] / stdenv_prefix / prehook.sh
CommitLineData
b4e94e5f
IB
1count_chunks() {
2 elements="${1//[^\/]}"
3 echo $(( "${#elements}" ))
4}
5
6oldout=$out
7prefix_chunks=$(count_chunks $NIX_STORE)
8
9: ${storepath_length=${#NIX_STORE}}
10: ${storepath_chunks=$prefix_chunks}
11
12if [ "$storepath_chunks" -lt $prefix_chunks ]; then
13 echo "Need to have at least as much path elements as the initial prefix"
14 exit 1
15fi
16
17min_length=$(( ${#NIX_STORE} + 2 * $storepath_chunks - 2 * $prefix_chunks ))
18if [ "$storepath_length" -lt "$min_length" ]; then
19 echo "The storepath length needs to be at least the length of the prefix"
20 exit 1
21fi
22
23if [ "$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
26fi
27
28appendOut() {
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=}
5275f756
IB
38 : ${installFlags=}
39 installFlags="${installFlags//$oldout/$out}"
b4e94e5f
IB
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}"
5275f756 45 installFlags="${installFlags//$libc_oldpath/$libc_newpath}"
b4e94e5f
IB
46 fi
47}
48
49addNixSupport() {
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
57overridePlaceholder() {
58function _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
74preHooks+=(appendOut overridePlaceholder)
75
76restoreSingleFile() {
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
84addSymlinks() {
85 if [ "$out" != "$oldout" -a -d $out ]; then
86 ln -s $out/* $oldout/
87 fi
88}
89
90rename_generic_build() {
91eval "$(echo "orig_genericBuild()"; declare -f genericBuild | tail -n +2)"
92function genericBuild {
93 orig_genericBuild "$@"
94 restoreSingleFile
95 addSymlinks
96 addNixSupport
97}
98}
99
100exitHooks+=(restoreSingleFile addSymlinks addNixSupport)
101postHooks+=(rename_generic_build)