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

with lib;

let
  cfg = config.corenix;

  fileOptions.options = {
    src = mkOption {
      type = types.path;
    };
    type = mkOption {
      type = types.str;
      default = "raw";
    };
  };
in {
  options.corenix = {
    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.rom = let
      base = pkgs.coreboot.override {
        inherit (cfg) corebootConfig;
      };

      filteredFiles = filterAttrs (k: v: v != null) cfg.extraFiles;
      filesList = mapAttrsToList (k: v: v // { name = k; }) filteredFiles;

      installCommands = 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;

    in pkgs.runCommand "coreboot-rom" {
      buildInputs = with pkgs; [ cbfstool ];
    } ''
      install -D ${base}/coreboot.rom -t $out
      ${installCommands}
    '';
  };
}