summaryrefslogtreecommitdiff
path: root/nixos/bootloader.nix
blob: e2921bf3b43a7dd276723e9028eaaf1c03ed416b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
{ lib, config, ... }: let
	cfg = config.boot._loader;
in {
	options.boot._loader = {
		enable = lib.mkEnableOption "enable unified bootloader config";
		loader = lib.mkOption {
			type = lib.types.enum [ "grub" "systemd-boot" ];
			default = "systemd-boot";
			description = "whether to install grub or systemd-boot as the bootloader";
		};
		mode = lib.mkOption {
			type = lib.types.enum [ "efi" "bios" ];
			default = "efi";
			description = "whether to install the bootloader in efi or bios mode";
		};
		grub = {
			biosDevice = lib.mkOption {
				type = lib.types.str;
				description = "device to install grub on";
			};
			zfsSupport = lib.mkEnableOption "zfs boot device support";
		};
		memtest86.enable = lib.mkEnableOption "make Memtest86+ available from the bootloader";
	};

	config = let
		usingEfi  = cfg.mode == "efi";
		usingBios = cfg.mode == "bios";
	in lib.mkIf cfg.enable {
		assertions = [
			{
				assertion = cfg.loader != "grub" || !cfg.grub.zfsSupport || usingEfi;
				message = "zfsSupport option requires using efi boot mode";
			}
		];

		boot.loader = {
			grub = lib.mkIf (cfg.loader == "grub") {
				enable = true;
				# bios
				device = lib.mkIf usingBios cfg.grub.biosDevice;
				# efi
				efiSupport = usingEfi;
				efiInstallAsRemovable = usingEfi;
				# zfs
				zfsSupport = cfg.grub.zfsSupport;
				mirroredBoots = lib.mkIf cfg.grub.zfsSupport [
					{ devices = [ "nodev" ]; path = "/boot"; }
				];
				# misc
				enableCryptodisk = true;
				memtest86.enable = cfg.memtest86.enable;
			};
			systemd-boot = lib.mkIf (cfg.loader == "systemd-boot") {
				enable = true;
				editor = false;
				memtest86.enable = cfg.memtest86.enable;
			};
			efi = lib.mkIf usingEfi {
				efiSysMountPoint = lib.mkIf (cfg.loader == "grub") "/boot/efi";
				canTouchEfiVariables = true;
			};
		};
	};
}