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 menu
=1 # Enable menu by default
15 default
= # Default configuration
16 target
=/boot
# Target directory
17 numGenerations
=0 # Number of other generations to include in the menu
19 while getopts "t:c:d:g:n:r" opt
; do
21 t
) # U-Boot interprets '0' as infinite
22 if [ "$OPTARG" -lt 0 ]; then
23 # When negative (or null coerced to -1), disable timeout which means that we wait forever for input
25 elif [ "$OPTARG" = 0 ]; then
26 # When zero, which means disabled in Nix module, disable menu which results in instant boot of the default item
27 # .. timeout is actually ignored by u-Boot but set here for the rest of the script
31 # Positive results in centi-seconds of timeout, which when passed with no input results in boot of the default item
32 timeout
=$
((OPTARG
* 10))
35 c
) default
="$OPTARG" ;;
36 d
) target
="$OPTARG" ;;
37 g
) numGenerations
="$OPTARG" ;;
38 n
) dtbName
="$OPTARG" ;;
44 [ "$timeout" = "" -o "$default" = "" ] && usage
46 mkdir
-p $target/nixos
47 mkdir
-p $target/extlinux
49 # Convert a path to a file in the Nix store such as
50 # /nix/store/<hash>-<name>/file to <hash>-<name>-<file>.
53 echo "$path" |
sed 's|^/nix/store/||' |
sed 's|/|-|g'
56 # Copy a file from the Nix store to $target/nixos.
57 declare -A filesCopied
60 local src
=$
(readlink
-f "$1")
61 local dst
="$target/nixos/$(cleanName $src)"
62 # Don't copy the file if $dst already exists. This means that we
63 # have to create $dst atomically to prevent partially copied
64 # kernels or initrd if this script is ever interrupted.
65 if ! test -e $dst; then
66 local dstTmp
=$dst.tmp.$$
74 # Copy its kernel, initrd and dtbs to $target/nixos, and echo out an
77 local path
=$
(readlink
-f "$1")
78 local tag
="$2" # Generation number or 'default'
80 if ! test -e $path/kernel
-a -e $path/initrd
; then
84 copyToKernelsDir
"$path/kernel"; kernel
=$result
85 copyToKernelsDir
"$path/initrd"; initrd
=$result
86 dtbDir
=$
(readlink
-m "$path/dtbs")
87 if [ -e "$dtbDir" ]; then
88 copyToKernelsDir
"$dtbDir"; dtbs
=$result
91 timestampEpoch
=$
(stat
-L -c '%Z' $path)
93 timestamp
=$
(date "+%Y-%m-%d %H:%M" -d @
$timestampEpoch)
94 nixosLabel
="$(cat $path/nixos-version)"
95 extraParams
="$(cat $path/kernel-params)"
98 echo "LABEL nixos-$tag"
99 if [ "$tag" = "default" ]; then
100 echo " MENU LABEL NixOS - Default"
102 echo " MENU LABEL NixOS - Configuration $tag ($timestamp - $nixosLabel)"
104 echo " LINUX ../nixos/$(basename $kernel)"
105 echo " INITRD ../nixos/$(basename $initrd)"
106 echo " APPEND init=$path/init $extraParams"
108 if [ -n "$noDeviceTree" ]; then
112 if [ -d "$dtbDir" ]; then
113 # if a dtbName was specified explicitly, use that, else use FDTDIR
114 if [ -n "$dtbName" ]; then
115 echo " FDT ../nixos/$(basename $dtbs)/${dtbName}"
117 echo " FDTDIR ../nixos/$(basename $dtbs)"
120 if [ -n "$dtbName" ]; then
121 echo "Explicitly requested dtbName $dtbName, but there's no FDTDIR - bailing out." >&2
127 tmpFile
="$target/extlinux/extlinux.conf.tmp.$$"
130 # Generated file, all changes will be lost on nixos-rebuild!
132 # Change this to e.g. nixos-42 to temporarily boot to an older configuration.
133 DEFAULT nixos-default
139 && echo "MENU TITLE ------------------------------------------------------------" >> $tmpFile
141 addEntry
$default default
>> $tmpFile
143 if [ "$numGenerations" -gt 0 ]; then
144 # Add up to $numGenerations generations of the system profile to the menu,
145 # in reverse (most recent to least recent) order.
147 (cd /nix
/var
/nix
/profiles
&& ls -d system-
*-link) \
148 |
sed 's/system-\([0-9]\+\)-link/\1/' \
150 |
head -n $numGenerations); do
151 link
=/nix
/var
/nix
/profiles
/system-
$generation-link
152 addEntry
$link "${generation}-default"
153 for specialisation
in $
(
154 ls /nix
/var
/nix
/profiles
/system-
$generation-link/specialisation \
156 link
=/nix
/var
/nix
/profiles
/system-
$generation-link/specialisation
/$specialisation
157 addEntry
$link "${generation}-${specialisation}"
162 mv -f $tmpFile $target/extlinux
/extlinux.conf
164 # Remove obsolete files from $target/nixos.
165 for fn
in $target/nixos
/*; do
166 if ! test "${filesCopied[$fn]}" = 1; then
167 echo "Removing no longer needed boot file: $fn"