···
14
-
from typing import NamedTuple
15
+
from typing import NamedTuple, Dict, List
16
+
from dataclasses import dataclass
25
+
kernelParams: List[str]
29
+
specialisations: Dict[str, "BootSpec"]
libc = ctypes.CDLL("libc.so.6")
···
os.rename("@efiSysMountPoint@/loader/loader.conf.tmp", "@efiSysMountPoint@/loader/loader.conf")
74
-
def profile_path(profile: str | None, generation: int, specialisation: str | None, name: str) -> str:
75
-
return os.path.realpath("%s/%s" % (system_dir(profile, generation, specialisation), name))
90
+
def get_bootspec(profile: str | None, generation: int) -> BootSpec:
91
+
boot_json_path = os.path.realpath("%s/%s" % (system_dir(profile, generation, None), "boot.json"))
92
+
boot_json_f = open(boot_json_path, 'r')
93
+
bootspec_json = json.load(boot_json_f)
94
+
return bootspec_from_json(bootspec_json)
96
+
def bootspec_from_json(bootspec_json: Dict) -> BootSpec:
97
+
specialisations = bootspec_json['org.nixos.specialisation.v1']
98
+
specialisations = {k: bootspec_from_json(v) for k, v in specialisations.items()}
99
+
return BootSpec(**bootspec_json['org.nixos.bootspec.v1'], specialisations=specialisations)
78
-
def copy_from_profile(profile: str | None, generation: int, specialisation: str | None, name: str, dry_run: bool = False) -> str:
79
-
store_file_path = profile_path(profile, generation, specialisation, name)
102
+
def copy_from_file(file: str, dry_run: bool = False) -> str:
103
+
store_file_path = os.path.realpath(file)
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)
···
copy_if_not_exists(store_file_path, "@efiSysMountPoint@%s" % (efi_file_path))
88
-
def describe_generation(profile: str | None, generation: int, specialisation: str | None) -> str:
90
-
with open(profile_path(profile, generation, specialisation, "nixos-version")) as f:
91
-
nixos_version = f.read()
93
-
nixos_version = "Unknown"
95
-
kernel_dir = os.path.dirname(profile_path(profile, generation, specialisation, "kernel"))
96
-
module_dir = glob.glob("%s/lib/modules/*" % kernel_dir)[0]
97
-
kernel_version = os.path.basename(module_dir)
99
-
build_time = int(os.path.getctime(system_dir(profile, generation, specialisation)))
100
-
build_date = datetime.datetime.fromtimestamp(build_time).strftime('%F')
102
-
description = "@distroName@ {}, Linux Kernel {}, Built on {}".format(
103
-
nixos_version, kernel_version, build_date
def write_entry(profile: str | None, generation: int, specialisation: str | None,
110
-
machine_id: str, current: bool) -> None:
111
-
kernel = copy_from_profile(profile, generation, specialisation, "kernel")
112
-
initrd = copy_from_profile(profile, generation, specialisation, "initrd")
112
+
machine_id: str, bootspec: BootSpec, current: bool) -> None:
114
+
bootspec = bootspec.specialisations[specialisation]
115
+
kernel = copy_from_file(bootspec.kernel)
116
+
initrd = copy_from_file(bootspec.initrd)
title = "@distroName@{profile}{specialisation}".format(
profile=" [" + profile + "]" if profile else "",
specialisation=" (%s)" % specialisation if specialisation else "")
119
-
append_initrd_secrets = profile_path(profile, generation, specialisation, "append-initrd-secrets")
120
-
subprocess.check_call([append_initrd_secrets, "@efiSysMountPoint@%s" % (initrd)])
123
+
subprocess.check_call([bootspec.initrdSecrets, "@efiSysMountPoint@%s" % (initrd)])
except FileNotFoundError:
except subprocess.CalledProcessError:
···
entry_file = "@efiSysMountPoint@/loader/entries/%s" % (
generation_conf_filename(profile, generation, specialisation))
tmp_path = "%s.tmp" % (entry_file)
135
-
kernel_params = "init=%s " % profile_path(profile, generation, specialisation, "init")
138
+
kernel_params = "init=%s " % bootspec.init
137
-
with open(profile_path(profile, generation, specialisation, "kernel-params")) as params_file:
138
-
kernel_params = kernel_params + params_file.read()
140
+
kernel_params = kernel_params + " ".join(bootspec.kernelParams)
141
+
build_time = int(os.path.getctime(system_dir(profile, generation, specialisation)))
142
+
build_date = datetime.datetime.fromtimestamp(build_time).strftime('%F')
with open(tmp_path, 'w') as f:
f.write(BOOT_ENTRY.format(title=title,
kernel_params=kernel_params,
145
-
description=describe_generation(profile, generation, specialisation)))
150
+
description=f"{bootspec.label}, built on {build_date}"))
if machine_id is not None:
f.write("machine-id %s\n" % machine_id)
···
return configurations[-configurationLimit:]
176
-
def get_specialisations(profile: str | None, generation: int, _: str | None) -> list[SystemIdentifier]:
177
-
specialisations_dir = os.path.join(
178
-
system_dir(profile, generation, None), "specialisation")
179
-
if not os.path.exists(specialisations_dir):
181
-
return [SystemIdentifier(profile, generation, spec) for spec in os.listdir(specialisations_dir)]
def remove_old_entries(gens: list[SystemIdentifier]) -> None:
rex_profile = re.compile(r"^@efiSysMountPoint@/loader/entries/nixos-(.*)-generation-.*\.conf$")
rex_generation = re.compile(r"^@efiSysMountPoint@/loader/entries/nixos.*-generation-([0-9]+)(-specialisation-.*)?\.conf$")
189
-
known_paths.append(copy_from_profile(*gen, "kernel", True))
190
-
known_paths.append(copy_from_profile(*gen, "initrd", True))
186
+
bootspec = get_bootspec(gen.profile, gen.generation)
187
+
known_paths.append(copy_from_file(bootspec.kernel, True))
188
+
known_paths.append(copy_from_file(bootspec.initrd, True))
for path in glob.iglob("@efiSysMountPoint@/loader/entries/nixos*-generation-[1-9]*.conf"):
if rex_profile.match(path):
prof = rex_profile.sub(r"\1", path)
···
282
-
is_default = os.path.dirname(profile_path(*gen, "init")) == args.default_config
283
-
write_entry(*gen, machine_id, current=is_default)
284
-
for specialisation in get_specialisations(*gen):
285
-
write_entry(*specialisation, machine_id, current=is_default)
280
+
bootspec = get_bootspec(gen.profile, gen.generation)
281
+
is_default = os.path.dirname(bootspec.init) == args.default_config
282
+
write_entry(*gen, machine_id, bootspec, current=is_default)
283
+
for specialisation in bootspec.specialisations.keys():
284
+
write_entry(gen.profile, gen.generation, specialisation, machine_id, bootspec, current=is_default)