6 for i
in @path@
; do PATH
=$PATH:$i/bin
; done
9 echo "usage: $0 -t <timeout> -c <path-to-default-configuration> [-d <boot-dir>] [-g <num-generations>] [-n <dtbName>] [-r]" >&2
13 timeout
= # Timeout in centiseconds
14 default
= # Default configuration
15 target
=/boot
# Target directory
16 numGenerations
=0 # Number of other generations to include in the menu
18 while getopts "t:c:d:g:n:r" opt
; do
20 t
) # U-Boot interprets '0' as infinite and negative as instant boot
21 if [ "$OPTARG" -lt 0 ]; then
23 elif [ "$OPTARG" = 0 ]; then
26 timeout
=$
((OPTARG
* 10))
29 c
) default
="$OPTARG" ;;
30 d
) target
="$OPTARG" ;;
31 g
) numGenerations
="$OPTARG" ;;
32 n
) dtbName
="$OPTARG" ;;
38 [ "$timeout" = "" -o "$default" = "" ] && usage
40 mkdir
-p $target/nixos
41 mkdir
-p $target/extlinux
43 # Convert a path to a file in the Nix store such as
44 # /nix/store/<hash>-<name>/file to <hash>-<name>-<file>.
47 echo "$path" |
sed 's|^/nix/store/||' |
sed 's|/|-|g'
50 # Copy a file from the Nix store to $target/nixos.
51 declare -A filesCopied
54 local src
=$
(readlink
-f "$1")
55 local dst
="$target/nixos/$(cleanName $src)"
56 # Don't copy the file if $dst already exists. This means that we
57 # have to create $dst atomically to prevent partially copied
58 # kernels or initrd if this script is ever interrupted.
59 if ! test -e $dst; then
60 local dstTmp
=$dst.tmp.$$
68 # Copy its kernel, initrd and dtbs to $target/nixos, and echo out an
71 local path
=$
(readlink
-f "$1")
72 local tag
="$2" # Generation number or 'default'
74 if ! test -e $path/kernel
-a -e $path/initrd
; then
78 copyToKernelsDir
"$path/kernel"; kernel
=$result
79 copyToKernelsDir
"$path/initrd"; initrd
=$result
80 dtbDir
=$
(readlink
-m "$path/dtbs")
81 if [ -e "$dtbDir" ]; then
82 copyToKernelsDir
"$dtbDir"; dtbs
=$result
85 timestampEpoch
=$
(stat
-L -c '%Z' $path)
87 timestamp
=$
(date "+%Y-%m-%d %H:%M" -d @
$timestampEpoch)
88 nixosLabel
="$(cat $path/nixos-version)"
89 extraParams
="$(cat $path/kernel-params)"
92 echo "LABEL nixos-$tag"
93 if [ "$tag" = "default" ]; then
94 echo " MENU LABEL NixOS - Default"
96 echo " MENU LABEL NixOS - Configuration $tag ($timestamp - $nixosLabel)"
98 echo " LINUX ../nixos/$(basename $kernel)"
99 echo " INITRD ../nixos/$(basename $initrd)"
100 echo " APPEND init=$path/init $extraParams"
102 if [ -n "$noDeviceTree" ]; then
106 if [ -d "$dtbDir" ]; then
107 # if a dtbName was specified explicitly, use that, else use FDTDIR
108 if [ -n "$dtbName" ]; then
109 echo " FDT ../nixos/$(basename $dtbs)/${dtbName}"
111 echo " FDTDIR ../nixos/$(basename $dtbs)"
114 if [ -n "$dtbName" ]; then
115 echo "Explicitly requested dtbName $dtbName, but there's no FDTDIR - bailing out." >&2
121 tmpFile
="$target/extlinux/extlinux.conf.tmp.$$"
124 # Generated file, all changes will be lost on nixos-rebuild!
126 # Change this to e.g. nixos-42 to temporarily boot to an older configuration.
127 DEFAULT nixos-default
129 MENU TITLE ------------------------------------------------------------
133 addEntry
$default default
>> $tmpFile
135 if [ "$numGenerations" -gt 0 ]; then
136 # Add up to $numGenerations generations of the system profile to the menu,
137 # in reverse (most recent to least recent) order.
139 (cd /nix
/var
/nix
/profiles
&& ls -d system-
*-link) \
140 |
sed 's/system-\([0-9]\+\)-link/\1/' \
142 |
head -n $numGenerations); do
143 link
=/nix
/var
/nix
/profiles
/system-
$generation-link
144 addEntry
$link "${generation}-default"
145 for specialisation
in $
(
146 ls /nix
/var
/nix
/profiles
/system-
$generation-link/specialisation \
148 link
=/nix
/var
/nix
/profiles
/system-
$generation-link/specialisation
/$specialisation
149 addEntry
$link "${generation}-${specialisation}"
154 mv -f $tmpFile $target/extlinux
/extlinux.conf
156 # Remove obsolete files from $target/nixos.
157 for fn
in $target/nixos
/*; do
158 if ! test "${filesCopied[$fn]}" = 1; then
159 echo "Removing no longer needed boot file: $fn"