1#! @python3@/bin/python3 -B
2import argparse
3import shutil
4import os
5import sys
6import errno
7import subprocess
8import glob
9import tempfile
10import errno
11import warnings
12import ctypes
13libc = ctypes.CDLL("libc.so.6")
14import re
15import datetime
16import glob
17import os.path
18from typing import NamedTuple, List, Optional
19from packaging import version
20
21class SystemIdentifier(NamedTuple):
22 profile: Optional[str]
23 generation: int
24 specialisation: Optional[str]
25
26
27def copy_if_not_exists(source: str, dest: str) -> None:
28 if not os.path.exists(dest):
29 shutil.copyfile(source, dest)
30
31
32def generation_dir(profile: Optional[str], generation: int) -> str:
33 if profile:
34 return "/nix/var/nix/profiles/system-profiles/%s-%d-link" % (profile, generation)
35 else:
36 return "/nix/var/nix/profiles/system-%d-link" % (generation)
37
38def system_dir(profile: Optional[str], generation: int, specialisation: Optional[str]) -> str:
39 d = generation_dir(profile, generation)
40 if specialisation:
41 return os.path.join(d, "specialisation", specialisation)
42 else:
43 return d
44
45BOOT_ENTRY = """title {title}
46version Generation {generation} {description}
47linux {kernel}
48initrd {initrd}
49options {kernel_params}
50"""
51
52def generation_conf_filename(profile: Optional[str], generation: int, specialisation: Optional[str]) -> str:
53 pieces = [
54 "nixos",
55 profile or None,
56 "generation",
57 str(generation),
58 f"specialisation-{specialisation}" if specialisation else None,
59 ]
60 return "-".join(p for p in pieces if p) + ".conf"
61
62
63def write_loader_conf(profile: Optional[str], generation: int, specialisation: Optional[str]) -> None:
64 with open("@efiSysMountPoint@/loader/loader.conf.tmp", 'w') as f:
65 if "@timeout@" != "":
66 f.write("timeout @timeout@\n")
67 f.write("default %s\n" % generation_conf_filename(profile, generation, specialisation))
68 if not @editor@:
69 f.write("editor 0\n");
70 f.write("console-mode @consoleMode@\n");
71 os.rename("@efiSysMountPoint@/loader/loader.conf.tmp", "@efiSysMountPoint@/loader/loader.conf")
72
73
74def profile_path(profile: Optional[str], generation: int, specialisation: Optional[str], name: str) -> str:
75 return os.path.realpath("%s/%s" % (system_dir(profile, generation, specialisation), name))
76
77
78def copy_from_profile(profile: Optional[str], generation: int, specialisation: Optional[str], name: str, dry_run: bool = False) -> str:
79 store_file_path = profile_path(profile, generation, specialisation, name)
80 suffix = os.path.basename(store_file_path)
81 store_dir = os.path.basename(os.path.dirname(store_file_path))
82 efi_file_path = "/efi/nixos/%s-%s.efi" % (store_dir, suffix)
83 if not dry_run:
84 copy_if_not_exists(store_file_path, "@efiSysMountPoint@%s" % (efi_file_path))
85 return efi_file_path
86
87
88def describe_generation(profile: Optional[str], generation: int, specialisation: Optional[str]) -> str:
89 try:
90 with open(profile_path(profile, generation, specialisation, "nixos-version")) as f:
91 nixos_version = f.read()
92 except IOError:
93 nixos_version = "Unknown"
94
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)
98
99 build_time = int(os.path.getctime(system_dir(profile, generation, specialisation)))
100 build_date = datetime.datetime.fromtimestamp(build_time).strftime('%F')
101
102 description = "@distroName@ {}, Linux Kernel {}, Built on {}".format(
103 nixos_version, kernel_version, build_date
104 )
105
106 return description
107
108
109def write_entry(profile: Optional[str], generation: int, specialisation: Optional[str],
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")
113
114 title = "@distroName@{profile}{specialisation}".format(
115 profile=" [" + profile + "]" if profile else "",
116 specialisation=" (%s)" % specialisation if specialisation else "")
117
118 try:
119 append_initrd_secrets = profile_path(profile, generation, specialisation, "append-initrd-secrets")
120 subprocess.check_call([append_initrd_secrets, "@efiSysMountPoint@%s" % (initrd)])
121 except FileNotFoundError:
122 pass
123 except subprocess.CalledProcessError:
124 if current:
125 print("failed to create initrd secrets!", file=sys.stderr)
126 sys.exit(1)
127 else:
128 print("warning: failed to create initrd secrets "
129 f'for "{title} - Configuration {generation}", an older generation', file=sys.stderr)
130 print("note: this is normal after having removed "
131 "or renamed a file in `boot.initrd.secrets`", file=sys.stderr)
132 entry_file = "@efiSysMountPoint@/loader/entries/%s" % (
133 generation_conf_filename(profile, generation, specialisation))
134 tmp_path = "%s.tmp" % (entry_file)
135 kernel_params = "init=%s " % profile_path(profile, generation, specialisation, "init")
136
137 with open(profile_path(profile, generation, specialisation, "kernel-params")) as params_file:
138 kernel_params = kernel_params + params_file.read()
139 with open(tmp_path, 'w') as f:
140 f.write(BOOT_ENTRY.format(title=title,
141 generation=generation,
142 kernel=kernel,
143 initrd=initrd,
144 kernel_params=kernel_params,
145 description=describe_generation(profile, generation, specialisation)))
146 if machine_id is not None:
147 f.write("machine-id %s\n" % machine_id)
148 os.rename(tmp_path, entry_file)
149
150
151def mkdir_p(path: str) -> None:
152 try:
153 os.makedirs(path)
154 except OSError as e:
155 if e.errno != errno.EEXIST or not os.path.isdir(path):
156 raise
157
158
159def get_generations(profile: Optional[str] = None) -> List[SystemIdentifier]:
160 gen_list = subprocess.check_output([
161 "@nix@/bin/nix-env",
162 "--list-generations",
163 "-p",
164 "/nix/var/nix/profiles/%s" % ("system-profiles/" + profile if profile else "system"),
165 "--option", "build-users-group", ""],
166 universal_newlines=True)
167 gen_lines = gen_list.split('\n')
168 gen_lines.pop()
169
170 configurationLimit = @configurationLimit@
171 configurations = [
172 SystemIdentifier(
173 profile=profile,
174 generation=int(line.split()[0]),
175 specialisation=None
176 )
177 for line in gen_lines
178 ]
179 return configurations[-configurationLimit:]
180
181
182def get_specialisations(profile: Optional[str], generation: int, _: Optional[str]) -> List[SystemIdentifier]:
183 specialisations_dir = os.path.join(
184 system_dir(profile, generation, None), "specialisation")
185 if not os.path.exists(specialisations_dir):
186 return []
187 return [SystemIdentifier(profile, generation, spec) for spec in os.listdir(specialisations_dir)]
188
189
190def remove_old_entries(gens: List[SystemIdentifier]) -> None:
191 rex_profile = re.compile("^@efiSysMountPoint@/loader/entries/nixos-(.*)-generation-.*\.conf$")
192 rex_generation = re.compile("^@efiSysMountPoint@/loader/entries/nixos.*-generation-([0-9]+)(-specialisation-.*)?\.conf$")
193 known_paths = []
194 for gen in gens:
195 known_paths.append(copy_from_profile(*gen, "kernel", True))
196 known_paths.append(copy_from_profile(*gen, "initrd", True))
197 for path in glob.iglob("@efiSysMountPoint@/loader/entries/nixos*-generation-[1-9]*.conf"):
198 if rex_profile.match(path):
199 prof = rex_profile.sub(r"\1", path)
200 else:
201 prof = None
202 try:
203 gen_number = int(rex_generation.sub(r"\1", path))
204 except ValueError:
205 continue
206 if not (prof, gen_number, None) in gens:
207 os.unlink(path)
208 for path in glob.iglob("@efiSysMountPoint@/efi/nixos/*"):
209 if not path in known_paths and not os.path.isdir(path):
210 os.unlink(path)
211
212
213def get_profiles() -> List[str]:
214 if os.path.isdir("/nix/var/nix/profiles/system-profiles/"):
215 return [x
216 for x in os.listdir("/nix/var/nix/profiles/system-profiles/")
217 if not x.endswith("-link")]
218 else:
219 return []
220
221def main() -> None:
222 parser = argparse.ArgumentParser(description='Update @distroName@-related systemd-boot files')
223 parser.add_argument('default_config', metavar='DEFAULT-CONFIG', help='The default @distroName@ config to boot')
224 args = parser.parse_args()
225
226 try:
227 with open("/etc/machine-id") as machine_file:
228 machine_id = machine_file.readlines()[0]
229 except IOError as e:
230 if e.errno != errno.ENOENT:
231 raise
232 # Since systemd version 232 a machine ID is required and it might not
233 # be there on newly installed systems, so let's generate one so that
234 # bootctl can find it and we can also pass it to write_entry() later.
235 cmd = ["@systemd@/bin/systemd-machine-id-setup", "--print"]
236 machine_id = subprocess.run(
237 cmd, text=True, check=True, stdout=subprocess.PIPE
238 ).stdout.rstrip()
239
240 if os.getenv("NIXOS_INSTALL_GRUB") == "1":
241 warnings.warn("NIXOS_INSTALL_GRUB env var deprecated, use NIXOS_INSTALL_BOOTLOADER", DeprecationWarning)
242 os.environ["NIXOS_INSTALL_BOOTLOADER"] = "1"
243
244 # flags to pass to bootctl install/update
245 bootctl_flags = []
246
247 if "@canTouchEfiVariables@" != "1":
248 bootctl_flags.append("--no-variables")
249
250 if "@graceful@" == "1":
251 bootctl_flags.append("--graceful")
252
253 if os.getenv("NIXOS_INSTALL_BOOTLOADER") == "1":
254 # bootctl uses fopen() with modes "wxe" and fails if the file exists.
255 if os.path.exists("@efiSysMountPoint@/loader/loader.conf"):
256 os.unlink("@efiSysMountPoint@/loader/loader.conf")
257
258 subprocess.check_call(["@systemd@/bin/bootctl", "--esp-path=@efiSysMountPoint@"] + bootctl_flags + ["install"])
259 else:
260 # Update bootloader to latest if needed
261 available_out = subprocess.check_output(["@systemd@/bin/bootctl", "--version"], universal_newlines=True).split()[2]
262 installed_out = subprocess.check_output(["@systemd@/bin/bootctl", "--esp-path=@efiSysMountPoint@", "status"], universal_newlines=True)
263
264 # See status_binaries() in systemd bootctl.c for code which generates this
265 installed_match = re.search(r"^\W+File:.*/EFI/(?:BOOT|systemd)/.*\.efi \(systemd-boot ([\d.]+[^)]*)\)$",
266 installed_out, re.IGNORECASE | re.MULTILINE)
267
268 available_match = re.search(r"^\((.*)\)$", available_out)
269
270 if installed_match is None:
271 raise Exception("could not find any previously installed systemd-boot")
272
273 if available_match is None:
274 raise Exception("could not determine systemd-boot version")
275
276 installed_version = version.parse(installed_match.group(1))
277 available_version = version.parse(available_match.group(1))
278
279 # systemd 252 has a regression that leaves some machines unbootable, so we skip that update.
280 # The fix is in 252.2
281 # See https://github.com/systemd/systemd/issues/25363 and https://github.com/NixOS/nixpkgs/pull/201558#issuecomment-1348603263
282 if installed_version < available_version:
283 if version.parse('252') <= available_version < version.parse('252.2'):
284 print("skipping systemd-boot update to %s because of known regression" % available_version)
285 else:
286 print("updating systemd-boot from %s to %s" % (installed_version, available_version))
287 subprocess.check_call(["@systemd@/bin/bootctl", "--esp-path=@efiSysMountPoint@"] + bootctl_flags + ["update"])
288
289 mkdir_p("@efiSysMountPoint@/efi/nixos")
290 mkdir_p("@efiSysMountPoint@/loader/entries")
291
292 gens = get_generations()
293 for profile in get_profiles():
294 gens += get_generations(profile)
295 remove_old_entries(gens)
296 for gen in gens:
297 try:
298 is_default = os.path.dirname(profile_path(*gen, "init")) == args.default_config
299 write_entry(*gen, machine_id, current=is_default)
300 for specialisation in get_specialisations(*gen):
301 write_entry(*specialisation, machine_id, current=is_default)
302 if is_default:
303 write_loader_conf(*gen)
304 except OSError as e:
305 # See https://github.com/NixOS/nixpkgs/issues/114552
306 if e.errno == errno.EINVAL:
307 profile = f"profile '{gen.profile}'" if gen.profile else "default profile"
308 print("ignoring {} in the list of boot entries because of the following error:\n{}".format(profile, e), file=sys.stderr)
309 else:
310 raise e
311
312 for root, _, files in os.walk('@efiSysMountPoint@/efi/nixos/.extra-files', topdown=False):
313 relative_root = root.removeprefix("@efiSysMountPoint@/efi/nixos/.extra-files").removeprefix("/")
314 actual_root = os.path.join("@efiSysMountPoint@", relative_root)
315
316 for file in files:
317 actual_file = os.path.join(actual_root, file)
318
319 if os.path.exists(actual_file):
320 os.unlink(actual_file)
321 os.unlink(os.path.join(root, file))
322
323 if not len(os.listdir(actual_root)):
324 os.rmdir(actual_root)
325 os.rmdir(root)
326
327 mkdir_p("@efiSysMountPoint@/efi/nixos/.extra-files")
328
329 subprocess.check_call("@copyExtraFiles@")
330
331 # Since fat32 provides little recovery facilities after a crash,
332 # it can leave the system in an unbootable state, when a crash/outage
333 # happens shortly after an update. To decrease the likelihood of this
334 # event sync the efi filesystem after each update.
335 rc = libc.syncfs(os.open("@efiSysMountPoint@", os.O_RDONLY))
336 if rc != 0:
337 print("could not sync @efiSysMountPoint@: {}".format(os.strerror(rc)), file=sys.stderr)
338
339
340if __name__ == '__main__':
341 main()