1#! @bash@/bin/sh -e
2
3shopt -s nullglob
4
5export PATH=/empty
6for i in @path@; do PATH=$PATH:$i/bin; done
7
8usage() {
9 echo "usage: $0 -t <timeout> -c <path-to-default-configuration> [-d <boot-dir>] [-g <num-generations>] [-n <dtbName>]" >&2
10 exit 1
11}
12
13timeout= # Timeout in centiseconds
14default= # Default configuration
15target=/boot # Target directory
16numGenerations=0 # Number of other generations to include in the menu
17
18while getopts "t:c:d:g:n:" opt; do
19 case "$opt" in
20 t) # U-Boot interprets '0' as infinite and negative as instant boot
21 if [ "$OPTARG" -lt 0 ]; then
22 timeout=0
23 elif [ "$OPTARG" = 0 ]; then
24 timeout=-10
25 else
26 timeout=$((OPTARG * 10))
27 fi
28 ;;
29 c) default="$OPTARG" ;;
30 d) target="$OPTARG" ;;
31 g) numGenerations="$OPTARG" ;;
32 n) dtbName="$OPTARG" ;;
33 \?) usage ;;
34 esac
35done
36
37[ "$timeout" = "" -o "$default" = "" ] && usage
38
39mkdir -p $target/nixos
40mkdir -p $target/extlinux
41
42# Convert a path to a file in the Nix store such as
43# /nix/store/<hash>-<name>/file to <hash>-<name>-<file>.
44cleanName() {
45 local path="$1"
46 echo "$path" | sed 's|^/nix/store/||' | sed 's|/|-|g'
47}
48
49# Copy a file from the Nix store to $target/nixos.
50declare -A filesCopied
51
52copyToKernelsDir() {
53 local src=$(readlink -f "$1")
54 local dst="$target/nixos/$(cleanName $src)"
55 # Don't copy the file if $dst already exists. This means that we
56 # have to create $dst atomically to prevent partially copied
57 # kernels or initrd if this script is ever interrupted.
58 if ! test -e $dst; then
59 local dstTmp=$dst.tmp.$$
60 cp -r $src $dstTmp
61 mv $dstTmp $dst
62 fi
63 filesCopied[$dst]=1
64 result=$dst
65}
66
67# Copy its kernel, initrd and dtbs to $target/nixos, and echo out an
68# extlinux menu entry
69addEntry() {
70 local path=$(readlink -f "$1")
71 local tag="$2" # Generation number or 'default'
72
73 if ! test -e $path/kernel -a -e $path/initrd; then
74 return
75 fi
76
77 copyToKernelsDir "$path/kernel"; kernel=$result
78 copyToKernelsDir "$path/initrd"; initrd=$result
79 dtbDir=$(readlink -m "$path/dtbs")
80 if [ -e "$dtbDir" ]; then
81 copyToKernelsDir "$dtbDir"; dtbs=$result
82 fi
83
84 timestampEpoch=$(stat -L -c '%Z' $path)
85
86 timestamp=$(date "+%Y-%m-%d %H:%M" -d @$timestampEpoch)
87 nixosLabel="$(cat $path/nixos-version)"
88 extraParams="$(cat $path/kernel-params)"
89
90 echo
91 echo "LABEL nixos-$tag"
92 if [ "$tag" = "default" ]; then
93 echo " MENU LABEL NixOS - Default"
94 else
95 echo " MENU LABEL NixOS - Configuration $tag ($timestamp - $nixosLabel)"
96 fi
97 echo " LINUX ../nixos/$(basename $kernel)"
98 echo " INITRD ../nixos/$(basename $initrd)"
99 if [ -d "$dtbDir" ]; then
100 # if a dtbName was specified explicitly, use that, else use FDTDIR
101 if [ -n "$dtbName" ]; then
102 echo " FDT ../nixos/$(basename $dtbs)/${dtbName}"
103 else
104 echo " FDTDIR ../nixos/$(basename $dtbs)"
105 fi
106 else
107 if [ -n "$dtbName" ]; then
108 echo "Explicitly requested dtbName $dtbName, but there's no FDTDIR - bailing out." >&2
109 exit 1
110 fi
111 fi
112 echo " APPEND init=$path/init $extraParams"
113}
114
115tmpFile="$target/extlinux/extlinux.conf.tmp.$$"
116
117cat > $tmpFile <<EOF
118# Generated file, all changes will be lost on nixos-rebuild!
119
120# Change this to e.g. nixos-42 to temporarily boot to an older configuration.
121DEFAULT nixos-default
122
123MENU TITLE ------------------------------------------------------------
124TIMEOUT $timeout
125EOF
126
127addEntry $default default >> $tmpFile
128
129if [ "$numGenerations" -gt 0 ]; then
130 # Add up to $numGenerations generations of the system profile to the menu,
131 # in reverse (most recent to least recent) order.
132 for generation in $(
133 (cd /nix/var/nix/profiles && ls -d system-*-link) \
134 | sed 's/system-\([0-9]\+\)-link/\1/' \
135 | sort -n -r \
136 | head -n $numGenerations); do
137 link=/nix/var/nix/profiles/system-$generation-link
138 addEntry $link $generation
139 done >> $tmpFile
140fi
141
142mv -f $tmpFile $target/extlinux/extlinux.conf
143
144# Remove obsolete files from $target/nixos.
145for fn in $target/nixos/*; do
146 if ! test "${filesCopied[$fn]}" = 1; then
147 echo "Removing no longer needed boot file: $fn"
148 chmod +w -- "$fn"
149 rm -rf -- "$fn"
150 fi
151done