Rename kboot-conf to petitboot and simplify a few things

This commit is contained in:
Josh Buker 2026-01-28 22:53:01 -08:00
parent cb8f383995
commit 1e1205f6f7
No known key found for this signature in database
GPG key ID: E077B96C5EFF10F2
4 changed files with 96 additions and 89 deletions

View file

@ -0,0 +1,75 @@
{
config,
lib,
pkgs,
...
}:
let
cfg = config.boot.loader.petitboot;
args = "-g ${toString cfg.configurationLimit} -n ${config.hardware.deviceTree.name}";
# The installer used when running nixos-rebuild switch or similar
installer = pkgs.replaceVarsWith {
src = ./install-petitboot.sh;
replacements = {
bash = pkgs.bash;
path = lib.makeBinPath [
pkgs.coreutils
pkgs.gnused
pkgs.gnugrep
];
};
name = "install-petitboot";
isExecutable = true;
};
# The installer used to write to SD card images
sdImageInstaller = pkgs.buildPackages.replaceVarsWith {
src = ./install-petitboot.sh;
replacements = {
bash = pkgs.buildPackages.bash;
path = lib.makeBinPath [
pkgs.buildPackages.coreutils
pkgs.buildPackages.gnused
pkgs.buildPackages.gnugrep
];
};
name = "install-petitboot-sd-image";
isExecutable = true;
};
in
{
options = {
boot.loader.petitboot = {
enable = lib.mkOption {
default = false;
type = lib.types.bool;
description = ''
Whether to enable the Petitboot bootloader.
'';
};
configurationLimit = lib.mkOption {
default = 10;
example = 5;
type = lib.types.int;
description = ''
Maximum number of configurations to display when booting.
'';
};
};
};
config = lib.mkMerge[
(lib.mkIf config.boot.loader.petitboot.enable {
system.build.installBootLoader = lib.mkForce "${installer} ${args} -c";
system.boot.loader.id = "petitboot";
})
(lib.mkIf config.boot.loader.petitboot.enable && options ? sdImage {
sdImage.populateRootCommands = lib.mkForce ''
${sdImageInstaller} ${args} -c ${config.system.build.toplevel} -d ./files/kboot.conf
''
})
];
}

View file

@ -0,0 +1,76 @@
#! @bash@/bin/bash -e
shopt -s nullglob
export PATH="/empty:@path@"
usage() {
echo "usage: $0 -c <path-to-default-configuration> -n <dtbName> [-g <num-generations>] [-d <target>]" >&2
exit 1
}
target=/kboot.conf
default= # Default configuration
numGenerations=0 # Number of other generations to include in the menu
while getopts "t:c:d:g:n:" opt; do
case "$opt" in
c) default="$OPTARG" ;;
g) numGenerations="$OPTARG" ;;
d) target="$OPTARG" ;;
n) dtbName="$OPTARG" ;;
\?) usage ;;
esac
done
[ "$default" = "" -o "$dtbName" = "" ] && usage
tmp=$target.tmp
# Echo out an kboot.conf menu entry
addEntry() {
local path=$(readlink -f "$1")
local tag="$2" # Generation number or 'default'
if ! test -e $path/kernel -a -e $path/initrd; then
return
fi
timestampEpoch=$(stat -L -c '%Z' $path)
timestamp=$(date "+%Y-%m-%d %H:%M" -d @$timestampEpoch)
nixosLabel="$(cat $path/nixos-version)"
extraParams="$(cat $path/kernel-params)"
local kernel=$(readlink -f "$path/kernel")
local initrd=$(readlink -f "$path/initrd")
local dtbs=$(readlink -f "$path/dtbs")
local id="nixos-$tag--$nixosLabel"
if [ "$tag" = "default" ]; then
echo "default=$id"
fi
echo -n "$id='"
echo -n "$kernel initrd=$initrd dtb=$dtbs/$dtbName "
echo -n "systemConfig=$path init=$path/init $extraParams"
echo "'"
}
echo "# Auto-generated by install-petitboot.sh, do not edit or remove" > $tmp
addEntry $default default >> $tmp
if [ "$numGenerations" -gt 0 ]; then
# Add up to $numGenerations generations of the system profile to the menu,
# in reverse (most recent to least recent) order.
for generation in $(
(cd /nix/var/nix/profiles && ls -d system-*-link) \
| sed 's/system-\([0-9]\+\)-link/\1/' \
| sort -n -r \
| head -n $numGenerations); do
link=/nix/var/nix/profiles/system-$generation-link
addEntry $link $generation
done >> $tmp
fi
mv -f $tmp $target