From 1d564082d5f732e96516672e30e4890585708ea4 Mon Sep 17 00:00:00 2001 From: Luke Granger-Brown Date: Mon, 4 Jan 2021 15:06:15 +0000 Subject: [PATCH] 3p/nixpkgs: systemd-boot changes --- .../systemd-boot/systemd-boot-builder.py | 59 ++++++++++++------- .../nixpkgs/nixos/tests/systemd-boot.nix | 23 ++++++++ 2 files changed, 61 insertions(+), 21 deletions(-) diff --git a/third_party/nixpkgs/nixos/modules/system/boot/loader/systemd-boot/systemd-boot-builder.py b/third_party/nixpkgs/nixos/modules/system/boot/loader/systemd-boot/systemd-boot-builder.py index 97e824fe62..8c5e8bcfe6 100644 --- a/third_party/nixpkgs/nixos/modules/system/boot/loader/systemd-boot/systemd-boot-builder.py +++ b/third_party/nixpkgs/nixos/modules/system/boot/loader/systemd-boot/systemd-boot-builder.py @@ -20,13 +20,20 @@ def copy_if_not_exists(source, dest): if not os.path.exists(dest): shutil.copyfile(source, dest) -def system_dir(profile, generation): +def generation_dir(profile, generation): if profile: return "/nix/var/nix/profiles/system-profiles/%s-%d-link" % (profile, generation) else: return "/nix/var/nix/profiles/system-%d-link" % (generation) -BOOT_ENTRY = """title NixOS{profile} +def system_dir(profile, generation, specialisation): + d = generation_dir(profile, generation) + if specialisation: + return os.path.join(d, "specialisation", specialisation) + else: + return d + +BOOT_ENTRY = """title NixOS{profile}{specialisation} version Generation {generation} {description} linux {kernel} initrd {initrd} @@ -42,24 +49,26 @@ MEMTEST_BOOT_ENTRY = """title MemTest86 efi /efi/memtest86/BOOTX64.efi """ -def write_loader_conf(profile, generation): +def generation_conf_filename(profile, generation, specialisation): + profile_part = f"-{profile}" if profile else "" + specialisation_part = f"-specialisation-{specialisation}" if specialisation else "" + return f"nixos{profile_part}{specialisation_part}-generation-{generation}.conf" + +def write_loader_conf(profile, generation, specialisation): with open("@efiSysMountPoint@/loader/loader.conf.tmp", 'w') as f: if "@timeout@" != "": f.write("timeout @timeout@\n") - if profile: - f.write("default nixos-%s-generation-%d.conf\n" % (profile, generation)) - else: - f.write("default nixos-generation-%d.conf\n" % (generation)) + f.write("default %s\n" % generation_conf_filename(profile, generation, specialisation)) if not @editor@: f.write("editor 0\n"); f.write("console-mode @consoleMode@\n"); os.rename("@efiSysMountPoint@/loader/loader.conf.tmp", "@efiSysMountPoint@/loader/loader.conf") -def profile_path(profile, generation, name): - return os.readlink("%s/%s" % (system_dir(profile, generation), name)) +def profile_path(profile, generation, specialisation, name): + return os.readlink("%s/%s" % (system_dir(profile, generation, specialisation), name)) -def copy_from_profile(profile, generation, name, dry_run=False): - store_file_path = profile_path(profile, generation, name) +def copy_from_profile(profile, generation, specialisation, name, dry_run=False): + store_file_path = profile_path(profile, generation, specialisation, name) suffix = os.path.basename(store_file_path) store_dir = os.path.basename(os.path.dirname(store_file_path)) efi_file_path = "/efi/nixos/%s-%s.efi" % (store_dir, suffix) @@ -87,19 +96,17 @@ def describe_generation(generation_dir): return description -def write_entry(profile, generation, machine_id): - kernel = copy_from_profile(profile, generation, "kernel") - initrd = copy_from_profile(profile, generation, "initrd") +def write_entry(profile, generation, specialisation, machine_id): + kernel = copy_from_profile(profile, generation, specialisation, "kernel") + initrd = copy_from_profile(profile, generation, specialisation, "initrd") try: - append_initrd_secrets = profile_path(profile, generation, "append-initrd-secrets") + append_initrd_secrets = profile_path(profile, generation, specialisation, "append-initrd-secrets") subprocess.check_call([append_initrd_secrets, "@efiSysMountPoint@%s" % (initrd)]) except FileNotFoundError: pass - if profile: - entry_file = "@efiSysMountPoint@/loader/entries/nixos-%s-generation-%d.conf" % (profile, generation) - else: - entry_file = "@efiSysMountPoint@/loader/entries/nixos-generation-%d.conf" % (generation) - generation_dir = os.readlink(system_dir(profile, generation)) + entry_file = "@efiSysMountPoint@/loader/entries/%s" % ( + generation_conf_filename(profile, generation, specialisation)) + generation_dir = os.readlink(system_dir(profile, generation, specialisation)) tmp_path = "%s.tmp" % (entry_file) kernel_params = "systemConfig=%s init=%s/init " % (generation_dir, generation_dir) @@ -107,6 +114,7 @@ def write_entry(profile, generation, machine_id): kernel_params = kernel_params + params_file.read() with open(tmp_path, 'w') as f: f.write(BOOT_ENTRY.format(profile=" [" + profile + "]" if profile else "", + specialisation=" (%s)" % specialisation if specialisation else "", generation=generation, kernel=kernel, initrd=initrd, @@ -135,7 +143,14 @@ def get_generations(profile=None): gen_lines.pop() configurationLimit = @configurationLimit@ - return [ (profile, int(line.split()[0])) for line in gen_lines ][-configurationLimit:] + return [ (profile, int(line.split()[0]), None) for line in gen_lines ][-configurationLimit:] + +def get_specialisations(profile, generation, _): + specialisations_dir = os.path.join( + system_dir(profile, generation, None), "specialisation") + if not os.path.exists(specialisations_dir): + return [] + return [(profile, generation, spec) for spec in os.listdir(specialisations_dir)] def remove_old_entries(gens): rex_profile = re.compile("^@efiSysMountPoint@/loader/entries/nixos-(.*)-generation-.*\.conf$") @@ -223,6 +238,8 @@ def main(): remove_old_entries(gens) for gen in gens: write_entry(*gen, machine_id) + for specialisation in get_specialisations(*gen): + write_entry(*specialisation, machine_id) if os.readlink(system_dir(*gen)) == args.default_config: write_loader_conf(*gen) diff --git a/third_party/nixpkgs/nixos/tests/systemd-boot.nix b/third_party/nixpkgs/nixos/tests/systemd-boot.nix index 7a663dd9b4..e8acef7855 100644 --- a/third_party/nixpkgs/nixos/tests/systemd-boot.nix +++ b/third_party/nixpkgs/nixos/tests/systemd-boot.nix @@ -39,6 +39,29 @@ in ''; }; + # Check that specialisations create corresponding boot entries. + specialisation = makeTest { + name = "systemd-boot-specialisation"; + meta.maintainers = with pkgs.stdenv.lib.maintainers; [ lukegb ]; + + machine = { pkgs, lib, ... }: { + imports = [ common ]; + specialisation.something.configuration = {}; + }; + + testScript = '' + machine.start() + machine.wait_for_unit("multi-user.target") + + machine.succeed( + "test -e /boot/loader/entries/nixos-specialisation-something-generation-1.conf" + ) + machine.succeed( + "grep -q 'title NixOS (something)' /boot/loader/entries/nixos-specialisation-something-generation-1.conf" + ) + ''; + }; + # Boot without having created an EFI entry--instead using default "/EFI/BOOT/BOOTX64.EFI" fallback = makeTest { name = "systemd-boot-fallback";