aboutsummaryrefslogtreecommitdiff
path: root/modules/corenix/default.nix
blob: ff7acb1e950b480b87356e37ae0d3b55fd44a9a6 (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
{ config, pkgs, lib, ... }:

with lib;

let
  cfg = config.corenix;

  fileOptions.options = {
    src = mkOption {
      type = types.nullOr types.path;
    };

    type = mkOption {
      type = types.str;
      default = "raw";
    };
  };
in {
  options.corenix = {
    installCommands = mkOption {
      type = types.lines;
    };

    extraFiles = mkOption {
      type = types.attrsOf (types.submodule fileOptions);
    };

    corebootConfig = mkOption {
      type = types.attrsOf (types.nullOr types.str);
      default = {};
    };

    rom = mkOption {
      readOnly = true;
      type = types.path;
    };
  };

  config = {
    corenix.installCommands = let
      filteredFiles = filterAttrs (k: v: v.src != null) cfg.extraFiles;
      filesList = mapAttrsToList (k: v: v // { name = k; }) filteredFiles;
    in concatMapStringsSep "\n" (file: 
      if file.type == "payload" then ''
        cbfstool $out/coreboot.rom add-payload \
          -f "${file.src}" \
          -n "${file.name}" \
      '' else ''
        cbfstool $out/coreboot.rom add \
          -f "${file.src}" \
          -n "${file.name}" \
          -t "${file.type}"
      ''
    ) filesList;

    corenix.rom = let
      base = pkgs.coreboot.override {
        inherit (cfg) corebootConfig;
      };
    in pkgs.runCommand "coreboot-rom" {
      buildInputs = with pkgs; [ cbfstool ];
    } ''
      install -D ${base}/coreboot.rom -t $out
      ${cfg.installCommands}
    '';
  };
}