aboutsummaryrefslogtreecommitdiff
path: root/modules/grub2/default.nix
blob: 8066d3ac5a173b1b7a36d70be1d55753e95e9db2 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.grub2;
  payloadName =
    if cfg.asSecondaryPayload then "img/grub2" else "fallback/payload";

  configText = (readFile ./files/grub.cfg) + cfg.extraConfig
    + (optionalString (cfg.scanDevices) (readFile ./files/grub-scan.cfg))
    + (optionalString (cfg.users != { }) ((concatStringsSep "\n" (mapAttrsToList
      (n: u: ''
        ${
          if u.passwordIsHashed then "password_pbkdf2" else "password"
        } ${n} ${u.password}
      '') cfg.users)) + ''
        set superusers="${
          concatStringsSep " "
          (attrNames (filterAttrs (n: u: u.superuser) cfg.users))
        }"
        export superusers
      '')) + (optionalString cfg.generateSecondaryPayloadEntries
        (concatMapStrings (n: ''
          menuentry '${removePrefix "img/" n}' {
            chainloader (cbfsdisk)/${n}
          }
        '') (filter (hasPrefix "img/") (attrNames config.corenix.extraFiles))));

  userOpts = { ... }: {
    options = {
      superuser = mkOption {
        type = types.bool;
        default = true;
      };
      password = mkOption { type = types.str; };
      passwordIsHashed = mkOption {
        type = types.bool;
        default = true;
      };
    };
  };
in {
  options.grub2 = {
    enable = mkEnableOption "grub2 coreboot primary payload";

    asSecondaryPayload = mkOption {
      type = types.bool;
      default = false;
    };

    generateSecondaryPayloadEntries = mkOption {
      type = types.bool;
      default = true;
    };

    scanDevices = mkOption {
      type = types.bool;
      default = true;
      description = ''
        Scan internal and external storage devices for GRUB2/syslinux/isolinux/NetBSD
        configs and at runtime and create boot entries for each of them.
      '';
    };

    extraConfig = mkOption {
      type = types.lines;
      default = "";
    };

    configFile = mkOption { type = types.path; };

    users = mkOption {
      type = types.attrsOf (types.submodule userOpts);
      default = { };
    };

    font = mkOption {
      type = types.path;
      default = "${pkgs.unifont}/share/fonts/truetype/unifont.ttf";
      example = "${pkgs.dejavu_fonts}/share/fonts/truetype/DejaVuSansMono.ttf";
    };
  };

  config = mkIf cfg.enable {
    grub2.configFile = pkgs.writeText "grub.cfg" configText;

    corenix.extraFiles = {
      ${payloadName} = {
        type = "payload";
        src = "${pkgs.coreboot-payload-grub2}/default_payload.elf";
      };
      "font.pf2".src =
        (pkgs.runCommand "font.pf2" { buildInputs = with pkgs; [ grub2 ]; }
          "grub-mkfont --range=0x20-0x7E,0x2501-0x251F,0x2191-0x2193 --size=14 -o $out ${cfg.font}");
      "etc/grub.cfg".src = cfg.configFile;
      "background.png".src = ./files/background.png;
    };
  };
}