1#! @bash@/bin/sh -e
2
3shopt -s nullglob
4
5export PATH=/empty
6for i in @path@; do PATH=$PATH:$i/bin; done
7
8if test $# -ne 1; then
9 echo "Usage: init-script-builder.sh DEFAULT-CONFIG"
10 exit 1
11fi
12
13defaultConfig="$1"
14
15
16[ "$(stat -f -c '%i' /)" = "$(stat -f -c '%i' /boot)" ] || {
17 # see grub-menu-builder.sh
18 echo "WARNING: /boot being on a different filesystem not supported by init-script-builder.sh"
19}
20
21
22
23target="/sbin/init"
24targetOther="/boot/init-other-configurations-contents.txt"
25
26tmp="$target.tmp"
27tmpOther="$targetOther.tmp"
28
29
30configurationCounter=0
31numAlienEntries=`cat <<EOF | egrep '^[[:space:]]*title' | wc -l
32@extraEntries@
33EOF`
34
35
36
37
38# Add an entry to $targetOther
39addEntry() {
40 local name="$1"
41 local path="$2"
42 local shortSuffix="$3"
43
44 configurationCounter=$((configurationCounter + 1))
45
46 local stage2=$path/init
47
48 content="$(
49 echo "#!/bin/sh"
50 echo "# $name"
51 echo "# created by init-script-builder.sh"
52 echo "export systemConfig=$(readlink -f $path)"
53 echo "exec $stage2"
54 )"
55
56 [ "$path" != "$defaultConfig" ] || {
57 echo "$content" > $tmp
58 echo "# older configurations: $targetOther" >> $tmp
59 chmod +x $tmp
60 }
61
62 echo -e "$content\n\n" >> $tmpOther
63}
64
65
66mkdir -p /boot /sbin
67
68addEntry "NixOS - Default" $defaultConfig ""
69
70# Add all generations of the system profile to the menu, in reverse
71# (most recent to least recent) order.
72for link in $((ls -d $defaultConfig/fine-tune/* ) | sort -n); do
73 date=$(stat --printf="%y\n" $link | sed 's/\..*//')
74 addEntry "NixOS - variation" $link ""
75done
76
77for generation in $(
78 (cd /nix/var/nix/profiles && ls -d system-*-link) \
79 | sed 's/system-\([0-9]\+\)-link/\1/' \
80 | sort -n -r); do
81 link=/nix/var/nix/profiles/system-$generation-link
82 date=$(stat --printf="%y\n" $link | sed 's/\..*//')
83 if [ -d $link/kernel ]; then
84 kernelVersion=$(cd $(dirname $(readlink -f $link/kernel))/lib/modules && echo *)
85 suffix="($date - $kernelVersion)"
86 else
87 suffix="($date)"
88 fi
89 addEntry "NixOS - Configuration $generation $suffix" $link "$generation ($date)"
90done
91
92mv $tmpOther $targetOther
93mv $tmp $target