1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 blCfg = config.boot.loader;
7 dtCfg = config.hardware.deviceTree;
8 cfg = blCfg.generic-extlinux-compatible;
9
10 timeoutStr = if blCfg.timeout == null then "-1" else toString blCfg.timeout;
11
12 # The builder used to write during system activation
13 builder = import ./extlinux-conf-builder.nix { inherit pkgs; };
14 # The builder exposed in populateCmd, which runs on the build architecture
15 populateBuilder = import ./extlinux-conf-builder.nix { pkgs = pkgs.buildPackages; };
16in
17{
18 options = {
19 boot.loader.generic-extlinux-compatible = {
20 enable = mkOption {
21 default = false;
22 type = types.bool;
23 description = ''
24 Whether to generate an extlinux-compatible configuration file
25 under `/boot/extlinux.conf`. For instance,
26 U-Boot's generic distro boot support uses this file format.
27
28 See [U-boot's documentation](https://u-boot.readthedocs.io/en/latest/develop/distro.html)
29 for more information.
30 '';
31 };
32
33 useGenerationDeviceTree = mkOption {
34 default = true;
35 type = types.bool;
36 description = ''
37 Whether to generate Device Tree-related directives in the
38 extlinux configuration.
39
40 When enabled, the bootloader will attempt to load the device
41 tree binaries from the generation's kernel.
42
43 Note that this affects all generations, regardless of the
44 setting value used in their configurations.
45 '';
46 };
47
48 configurationLimit = mkOption {
49 default = 20;
50 example = 10;
51 type = types.int;
52 description = ''
53 Maximum number of configurations in the boot menu.
54 '';
55 };
56
57 populateCmd = mkOption {
58 type = types.str;
59 readOnly = true;
60 description = ''
61 Contains the builder command used to populate an image,
62 honoring all options except the `-c <path-to-default-configuration>`
63 argument.
64 Useful to have for sdImage.populateRootCommands
65 '';
66 };
67
68 };
69 };
70
71 config = let
72 builderArgs = "-g ${toString cfg.configurationLimit} -t ${timeoutStr}"
73 + lib.optionalString (dtCfg.name != null) " -n ${dtCfg.name}"
74 + lib.optionalString (!cfg.useGenerationDeviceTree) " -r";
75 in
76 mkIf cfg.enable {
77 system.build.installBootLoader = "${builder} ${builderArgs} -c";
78 system.boot.loader.id = "generic-extlinux-compatible";
79
80 boot.loader.generic-extlinux-compatible.populateCmd = "${populateBuilder} ${builderArgs}";
81 };
82}