Merge pull request #28903 from grahamc/systemd-boot-nixos-version
Include date and NixOS version in systemd-boot entries
This commit is contained in:
commit
8d8b5f57eb
|
@ -12,6 +12,9 @@ import warnings
|
||||||
import ctypes
|
import ctypes
|
||||||
libc = ctypes.CDLL("libc.so.6")
|
libc = ctypes.CDLL("libc.so.6")
|
||||||
import re
|
import re
|
||||||
|
import datetime
|
||||||
|
import glob
|
||||||
|
import os.path
|
||||||
|
|
||||||
def copy_if_not_exists(source, dest):
|
def copy_if_not_exists(source, dest):
|
||||||
if not os.path.exists(dest):
|
if not os.path.exists(dest):
|
||||||
|
@ -24,7 +27,7 @@ def system_dir(profile, generation):
|
||||||
return "/nix/var/nix/profiles/system-%d-link" % (generation)
|
return "/nix/var/nix/profiles/system-%d-link" % (generation)
|
||||||
|
|
||||||
BOOT_ENTRY = """title NixOS{profile}
|
BOOT_ENTRY = """title NixOS{profile}
|
||||||
version Generation {generation}
|
version Generation {generation} {description}
|
||||||
linux {kernel}
|
linux {kernel}
|
||||||
initrd {initrd}
|
initrd {initrd}
|
||||||
options {kernel_params}
|
options {kernel_params}
|
||||||
|
@ -54,6 +57,26 @@ def copy_from_profile(profile, generation, name, dry_run=False):
|
||||||
copy_if_not_exists(store_file_path, "@efiSysMountPoint@%s" % (efi_file_path))
|
copy_if_not_exists(store_file_path, "@efiSysMountPoint@%s" % (efi_file_path))
|
||||||
return efi_file_path
|
return efi_file_path
|
||||||
|
|
||||||
|
def describe_generation(generation_dir):
|
||||||
|
try:
|
||||||
|
with open("%s/nixos-version" % generation_dir) as f:
|
||||||
|
nixos_version = f.read()
|
||||||
|
except IOError:
|
||||||
|
nixos_version = "Unknown"
|
||||||
|
|
||||||
|
kernel_dir = os.path.dirname(os.path.realpath("%s/kernel" % generation_dir))
|
||||||
|
module_dir = glob.glob("%s/lib/modules/*" % kernel_dir)[0]
|
||||||
|
kernel_version = os.path.basename(module_dir)
|
||||||
|
|
||||||
|
build_time = int(os.path.getctime(generation_dir))
|
||||||
|
build_date = datetime.datetime.fromtimestamp(build_time).strftime('%F')
|
||||||
|
|
||||||
|
description = "NixOS {}, Linux Kernel {}, Built on {}".format(
|
||||||
|
nixos_version, kernel_version, build_date
|
||||||
|
)
|
||||||
|
|
||||||
|
return description
|
||||||
|
|
||||||
def write_entry(profile, generation, machine_id):
|
def write_entry(profile, generation, machine_id):
|
||||||
kernel = copy_from_profile(profile, generation, "kernel")
|
kernel = copy_from_profile(profile, generation, "kernel")
|
||||||
initrd = copy_from_profile(profile, generation, "initrd")
|
initrd = copy_from_profile(profile, generation, "initrd")
|
||||||
|
@ -69,6 +92,7 @@ def write_entry(profile, generation, machine_id):
|
||||||
generation_dir = os.readlink(system_dir(profile, generation))
|
generation_dir = os.readlink(system_dir(profile, generation))
|
||||||
tmp_path = "%s.tmp" % (entry_file)
|
tmp_path = "%s.tmp" % (entry_file)
|
||||||
kernel_params = "systemConfig=%s init=%s/init " % (generation_dir, generation_dir)
|
kernel_params = "systemConfig=%s init=%s/init " % (generation_dir, generation_dir)
|
||||||
|
|
||||||
with open("%s/kernel-params" % (generation_dir)) as params_file:
|
with open("%s/kernel-params" % (generation_dir)) as params_file:
|
||||||
kernel_params = kernel_params + params_file.read()
|
kernel_params = kernel_params + params_file.read()
|
||||||
with open(tmp_path, 'w') as f:
|
with open(tmp_path, 'w') as f:
|
||||||
|
@ -76,7 +100,8 @@ def write_entry(profile, generation, machine_id):
|
||||||
generation=generation,
|
generation=generation,
|
||||||
kernel=kernel,
|
kernel=kernel,
|
||||||
initrd=initrd,
|
initrd=initrd,
|
||||||
kernel_params=kernel_params))
|
kernel_params=kernel_params,
|
||||||
|
description=describe_generation(generation_dir)))
|
||||||
if machine_id is not None:
|
if machine_id is not None:
|
||||||
f.write("machine-id %s\n" % machine_id)
|
f.write("machine-id %s\n" % machine_id)
|
||||||
os.rename(tmp_path, entry_file)
|
os.rename(tmp_path, entry_file)
|
||||||
|
|
Loading…
Reference in New Issue