1#! @python@/bin/python
2import argparse
3import shutil
4import os
5import errno
6import subprocess
7import glob
8import tempfile
9import errno
10
11def copy_if_not_exists(source, dest):
12 if not os.path.exists(dest):
13 shutil.copyfile(source, dest)
14
15system_dir = lambda generation: "/nix/var/nix/profiles/system-%d-link" % (generation)
16
17def write_entry(generation, kernel, initrd):
18 entry_file = "@efiSysMountPoint@/loader/entries/nixos-generation-%d.conf" % (generation)
19 generation_dir = os.readlink(system_dir(generation))
20 tmp_path = "%s.tmp" % (entry_file)
21 kernel_params = "systemConfig=%s init=%s/init " % (generation_dir, generation_dir)
22 with open("%s/kernel-params" % (generation_dir)) as params_file:
23 kernel_params = kernel_params + params_file.read()
24 with open(tmp_path, 'w') as f:
25 print >> f, "title NixOS"
26 print >> f, "version Generation %d" % (generation)
27 if machine_id is not None: print >> f, "machine-id %s" % (machine_id)
28 print >> f, "linux %s" % (kernel)
29 print >> f, "initrd %s" % (initrd)
30 print >> f, "options %s" % (kernel_params)
31 os.rename(tmp_path, entry_file)
32
33def write_loader_conf(generation):
34 with open("@efiSysMountPoint@/loader/loader.conf.tmp", 'w') as f:
35 if "@timeout@" != "":
36 print >> f, "timeout @timeout@"
37 print >> f, "default nixos-generation-%d" % (generation)
38 os.rename("@efiSysMountPoint@/loader/loader.conf.tmp", "@efiSysMountPoint@/loader/loader.conf")
39
40def copy_from_profile(generation, name, dry_run=False):
41 store_file_path = os.readlink("%s/%s" % (system_dir(generation), name))
42 suffix = os.path.basename(store_file_path)
43 store_dir = os.path.basename(os.path.dirname(store_file_path))
44 efi_file_path = "/efi/nixos/%s-%s.efi" % (store_dir, suffix)
45 if not dry_run:
46 copy_if_not_exists(store_file_path, "@efiSysMountPoint@%s" % (efi_file_path))
47 return efi_file_path
48
49def add_entry(generation):
50 efi_kernel_path = copy_from_profile(generation, "kernel")
51 efi_initrd_path = copy_from_profile(generation, "initrd")
52 write_entry(generation, efi_kernel_path, efi_initrd_path)
53
54def mkdir_p(path):
55 try:
56 os.makedirs(path)
57 except OSError as e:
58 if e.errno != errno.EEXIST or not os.path.isdir(path):
59 raise
60
61def get_generations(profile):
62 gen_list = subprocess.check_output([
63 "@nix@/bin/nix-env",
64 "--list-generations",
65 "-p",
66 "/nix/var/nix/profiles/%s" % (profile),
67 "--option", "build-users-group", ""
68 ])
69 gen_lines = gen_list.split('\n')
70 gen_lines.pop()
71 return [ int(line.split()[0]) for line in gen_lines ]
72
73def remove_old_entries(gens):
74 slice_start = len("@efiSysMountPoint@/loader/entries/nixos-generation-")
75 slice_end = -1 * len(".conf")
76 known_paths = []
77 for gen in gens:
78 known_paths.append(copy_from_profile(gen, "kernel", True))
79 known_paths.append(copy_from_profile(gen, "initrd", True))
80 for path in glob.iglob("@efiSysMountPoint@/loader/entries/nixos-generation-[1-9]*.conf"):
81 try:
82 gen = int(path[slice_start:slice_end])
83 if not gen in gens:
84 os.unlink(path)
85 except ValueError:
86 pass
87 for path in glob.iglob("@efiSysMountPoint@/efi/nixos/*"):
88 if not path in known_paths:
89 os.unlink(path)
90
91parser = argparse.ArgumentParser(description='Update NixOS-related gummiboot files')
92parser.add_argument('default_config', metavar='DEFAULT-CONFIG', help='The default NixOS config to boot')
93args = parser.parse_args()
94
95# We deserve our own env var!
96if os.getenv("NIXOS_INSTALL_GRUB") == "1":
97 if "@canTouchEfiVariables@" == "1":
98 subprocess.check_call(["@gummiboot@/bin/gummiboot", "--path=@efiSysMountPoint@", "install"])
99 else:
100 subprocess.check_call(["@gummiboot@/bin/gummiboot", "--path=@efiSysMountPoint@", "--no-variables", "install"])
101
102mkdir_p("@efiSysMountPoint@/efi/nixos")
103mkdir_p("@efiSysMountPoint@/loader/entries")
104try:
105 with open("/etc/machine-id") as machine_file:
106 machine_id = machine_file.readlines()[0]
107except IOError as e:
108 if e.errno != errno.ENOENT:
109 raise
110 machine_id = None
111
112gens = get_generations("system")
113remove_old_entries(gens)
114for gen in gens:
115 add_entry(gen)
116 if os.readlink(system_dir(gen)) == args.default_config:
117 write_loader_conf(gen)