forked from nix-community/raspberry-pi-nix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.nix
144 lines (142 loc) · 4.45 KB
/
config.nix
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
{ lib, config, pkgs, ... }:
let
cfg = config.hardware.raspberry-pi;
render-raspberrypi-config =
let
render-options = opts:
lib.strings.concatStringsSep "\n" (render-dt-kvs opts);
render-dt-param = x: "dtparam=" + x;
render-dt-kv = k: v:
if isNull v.value then
k
else
let vstr = toString v.value; in "${k}=${vstr}";
render-dt-kvs = x:
lib.attrsets.mapAttrsToList render-dt-kv
(lib.filterAttrs (k: v: v.enable) x);
render-dt-overlay = { overlay, args }:
"dtoverlay=" + overlay + "\n"
+ lib.strings.concatMapStringsSep "\n" render-dt-param args + "\n"
+ "dtoverlay=";
render-base-dt-params = params:
lib.strings.concatMapStringsSep "\n" render-dt-param
(render-dt-kvs params);
render-dt-overlays = overlays:
lib.strings.concatMapStringsSep "\n" render-dt-overlay
(lib.attrsets.mapAttrsToList
(k: v: {
overlay = k;
args = render-dt-kvs v.params;
})
(lib.filterAttrs (k: v: v.enable) overlays));
render-config-section = k:
{ options, base-dt-params, dt-overlays }:
let
all-config = lib.concatStringsSep "\n" (lib.filter (x: x != "") [
(render-options options)
(render-base-dt-params base-dt-params)
(render-dt-overlays dt-overlays)
]);
in
''
[${k}]
${all-config}
'';
in
conf:
lib.strings.concatStringsSep "\n"
(lib.attrsets.mapAttrsToList render-config-section conf);
in
{
options = {
hardware.raspberry-pi = {
config =
let
rpi-config-param = {
options = {
enable = lib.mkEnableOption "attr";
value =
lib.mkOption { type = with lib.types; oneOf [ int str bool ]; };
};
};
dt-param = {
options = {
enable = lib.mkEnableOption "attr";
value = lib.mkOption {
type = with lib.types; nullOr (oneOf [ int str bool ]);
default = null;
};
};
};
dt-overlay = {
options = {
enable = lib.mkEnableOption "overlay";
params = lib.mkOption {
type = with lib.types; attrsOf (submodule dt-param);
};
};
};
raspberry-pi-config-options = {
options = {
options = lib.mkOption {
type = with lib.types; attrsOf (submodule rpi-config-param);
default = { };
example = {
enable_gic = {
enable = true;
value = true;
};
arm_boost = {
enable = true;
value = true;
};
};
};
base-dt-params = lib.mkOption {
type = with lib.types; attrsOf (submodule dt-param);
default = { };
example = {
i2c = {
enable = true;
value = "on";
};
audio = {
enable = true;
value = "on";
};
};
description = "parameters to pass to the base dtb";
};
dt-overlays = lib.mkOption {
type = with lib.types; attrsOf (submodule dt-overlay);
default = { };
example = { vc4-kms-v3d = { cma-256 = { enable = true; }; }; };
description = "dtb overlays to apply";
};
};
};
in
lib.mkOption {
type = with lib.types; attrsOf (submodule raspberry-pi-config-options);
};
config-generated = lib.mkOption {
type = lib.types.str;
description = "the config text generated by raspberrypi.hardware.config";
readOnly = true;
};
config-output = lib.mkOption {
type = lib.types.package;
default = pkgs.writeTextFile {
name = "config.txt";
text = ''
# This is a generated file. Do not edit!
${cfg.config-generated}
'';
};
};
};
};
config = {
hardware.raspberry-pi.config-generated = render-raspberrypi-config cfg.config;
};
}