Skip to content

Commit e309107

Browse files
committed
nixos/limine: add secureBoot.configFile option
This adds a new `boot.loader.limine.secureBoot.configFile` option that allows users to specify a custom sbctl configuration file. This is useful for managing secure boot keys in non-standard locations, such as when using declarative secret management systems like sops-nix or clan-core vars. The configuration file is passed to sbctl via the --config flag for all sbctl operations (create-keys, enroll-keys, sign), including fwupd EFI binary signing. When configFile is not specified, sbctl uses its default location (/var/lib/sbctl).
1 parent d391d01 commit e309107

2 files changed

Lines changed: 36 additions & 9 deletions

File tree

nixos/modules/system/boot/loader/limine/limine-install.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,11 @@ def install_bootloader() -> None:
430430
partition formatted as FAT.
431431
'''))
432432

433-
if config('secureBoot', 'enable') and not config('secureBoot', 'autoGenerateKeys') and not os.path.exists("/var/lib/sbctl"):
434-
print("There are no sbctl secure boot keys present. Please generate some.")
435-
sys.exit(1)
433+
# Check if sbctl keys exist (only if not using custom config)
434+
if config('secureBoot', 'enable') and not config('secureBoot', 'autoGenerateKeys'):
435+
if config('secureBoot', 'configFile') is None and not os.path.exists("/var/lib/sbctl"):
436+
print("There are no sbctl secure boot keys present at /var/lib/sbctl. Please generate some or specify a configFile.")
437+
sys.exit(1)
436438

437439
if not os.path.exists(limine_install_dir):
438440
os.makedirs(limine_install_dir)
@@ -557,16 +559,22 @@ def install_bootloader() -> None:
557559

558560
if config('secureBoot', 'enable'):
559561
sbctl = os.path.join(str(config('secureBoot', 'sbctl')), 'bin', 'sbctl')
562+
sbctl_args = []
563+
564+
# If using a custom config file, pass it to sbctl
565+
if config('secureBoot', 'configFile') is not None:
566+
sbctl_args = ['--config', config('secureBoot', 'configFile')]
567+
560568
if not os.path.exists("/var/lib/sbctl") and config('secureBoot', 'autoGenerateKeys'):
561569
print('auto generating keys')
562570
try:
563-
subprocess.run([sbctl, 'create-keys'])
571+
subprocess.run([sbctl] + sbctl_args + ['create-keys'])
564572
except:
565573
print('error: failed to create keys', file=sys.stderr)
566574
sys.exit(1)
567575
if config('secureBoot', 'autoEnrollKeys', 'enable'):
568576
try:
569-
command = [sbctl, 'enroll-keys']
577+
command = [sbctl] + sbctl_args + ['enroll-keys']
570578
command.extend(config('secureBoot', 'autoEnrollKeys', 'extraArgs'))
571579
subprocess.run(command)
572580
except:
@@ -575,7 +583,7 @@ def install_bootloader() -> None:
575583

576584
print('signing limine...')
577585
try:
578-
subprocess.run([sbctl, 'sign', dest_path])
586+
subprocess.run([sbctl] + sbctl_args + ['sign', dest_path])
579587
except:
580588
print('error: failed to sign limine', file=sys.stderr)
581589
sys.exit(1)

nixos/modules/system/boot/loader/limine/limine.nix

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ let
1919
canTouchEfiVariables = efi.canTouchEfiVariables;
2020
efiSupport = cfg.efiSupport;
2121
efiRemovable = cfg.efiInstallAsRemovable;
22-
secureBoot = cfg.secureBoot;
22+
secureBoot = cfg.secureBoot // {
23+
configFile = if cfg.secureBoot.configFile != null then toString cfg.secureBoot.configFile else null;
24+
};
2325
biosSupport = cfg.biosSupport;
2426
biosDevice = cfg.biosDevice;
2527
partitionIndex = cfg.partitionIndex;
@@ -225,6 +227,23 @@ in
225227
'';
226228
};
227229

230+
configFile = lib.mkOption {
231+
type = lib.types.nullOr lib.types.path;
232+
default = null;
233+
example = lib.literalExpression ''
234+
pkgs.writeText "sbctl.conf" '''
235+
keydir: ''${config.clan.core.vars.generators.secureboot.directory}/keys
236+
'''
237+
'';
238+
description = ''
239+
Path to sbctl configuration file.
240+
If specified, this will be passed to sbctl via the --config flag.
241+
242+
The configuration file should specify at minimum the keydir location.
243+
See sbctl.conf(5) for all available options.
244+
'';
245+
};
246+
228247
autoGenerateKeys = lib.mkEnableOption null // {
229248
description = "Generate keys automatically when none exists during bootloader installation";
230249
};
@@ -474,7 +493,7 @@ in
474493
partOf = [ "fwupd.service" ];
475494
before = [ "fwupd.service" ];
476495

477-
unitConfig.ConditionPathIsDirectory = "/var/lib/sbctl";
496+
unitConfig.ConditionPathIsDirectory = lib.mkIf (cfg.secureBoot.configFile == null) "/var/lib/sbctl";
478497
serviceConfig = {
479498
Type = "oneshot";
480499
RemainAfterExit = true;
@@ -483,7 +502,7 @@ in
483502

484503
script = ''
485504
fwupd_efi=(${config.services.fwupd.package.fwupd-efi}/libexec/fwupd/efi/fwupd*.efi)
486-
${lib.getExe cfg.secureBoot.sbctl} sign -o /run/fwupd-efi/$(basename "$fwupd_efi").signed "$fwupd_efi"
505+
${lib.getExe cfg.secureBoot.sbctl} ${lib.optionalString (cfg.secureBoot.configFile != null) "--config ${cfg.secureBoot.configFile}"} sign -o /run/fwupd-efi/$(basename "$fwupd_efi").signed "$fwupd_efi"
487506
'';
488507
};
489508

0 commit comments

Comments
 (0)