Skip to content

Commit 300f48d

Browse files
committed
stylix/mk-target: polish implementation and improve error reporting
Polish the mkTarget implementation to improve error reporting and simplify future enhancements. Configuration elements can now recursively resolve to paths.
1 parent af6b815 commit 300f48d

File tree

1 file changed

+58
-52
lines changed

1 file changed

+58
-52
lines changed

stylix/mk-target.nix

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@
182182
configElements ? [ ],
183183
enableExample ? null,
184184
extraOptions ? { },
185-
generalConfig ? null,
185+
generalConfig ? { },
186186
imports ? [ ],
187187
}@args:
188188
let
@@ -191,54 +191,66 @@ let
191191
let
192192
cfg = config.stylix.targets.${name};
193193

194-
# Get the list of function de-structured argument names.
195-
functionArgNames =
196-
fn:
197-
lib.pipe fn [
198-
lib.functionArgs
199-
builtins.attrNames
200-
];
201-
202-
getStylixAttrs =
203-
fn:
204-
lib.genAttrs (functionArgNames fn) (
205-
arg:
206-
if arg == "cfg" then
207-
cfg
208-
else if arg == "colors" then
209-
config.lib.stylix.colors
194+
mkConfig =
195+
let
196+
areArgumentsEnabled = lib.flip lib.pipe [
197+
builtins.attrValues
198+
(builtins.all (argument: argument.enable or (argument != null)))
199+
];
200+
201+
getArguments =
202+
function:
203+
lib.genAttrs
204+
(lib.pipe function [
205+
lib.functionArgs
206+
builtins.attrNames
207+
])
208+
(
209+
argument:
210+
if argument == "cfg" then
211+
cfg
212+
213+
else if argument == "colors" then
214+
config.lib.stylix.colors
215+
216+
else
217+
config.stylix.${argument} or (throw "stylix: mkTarget expected one of ${
218+
lib.concatMapStringsSep ", " (expected: "`${expected}`") (
219+
lib.naturalSort (
220+
[
221+
"cfg"
222+
"colors"
223+
]
224+
++ builtins.attrNames config.stylix
225+
)
226+
)
227+
}, but got: ${argument}")
228+
);
229+
in
230+
safeguard: config':
231+
let
232+
arguments = getArguments config';
233+
in
234+
if builtins.isFunction config' then
235+
if safeguard then
236+
lib.mkIf (areArgumentsEnabled arguments) (config' arguments)
210237
else
211-
config.stylix.${arg}
212-
or (throw "stylix: mkTarget expected one of `cfg`, `colors`, ${
213-
lib.concatMapStringsSep ", " (name: "`${name}`") (
214-
builtins.attrNames config.stylix
215-
)
216-
}, but got: ${arg}")
217-
);
218-
219-
# Call the configuration function with its required Stylix arguments.
220-
mkConfig = fn: fn (getStylixAttrs fn);
221-
222-
# Safeguard configuration functions when any of their arguments is
223-
# disabled.
224-
mkConditionalConfig =
225-
c:
226-
if builtins.isFunction c then
227-
let
228-
allAttrsEnabled = lib.pipe c [
229-
getStylixAttrs
230-
builtins.attrValues
231-
# If the attr has no enable option, it is instead disabled when null
232-
(builtins.all (attr: attr.enable or (attr != null)))
233-
];
234-
in
235-
lib.mkIf allAttrsEnabled (mkConfig c)
238+
config' arguments
239+
240+
else if builtins.isAttrs config' then
241+
config'
242+
243+
else if builtins.isPath config' then
244+
mkConfig safeguard (import config')
245+
236246
else
237-
c;
247+
throw "stylix: mkTarget expected a configuration to be a function, an attribute set, or a path, but got ${builtins.typeOf config'}: ${
248+
lib.generators.toPretty { } config'
249+
}";
238250
in
239251
{
240252
imports = imports ++ [
241-
{ options.stylix.targets.${name} = mkConfig (lib.toFunction extraOptions); }
253+
{ options.stylix.targets.${name} = mkConfig false extraOptions; }
242254
];
243255

244256
options.stylix.targets.${name}.enable =
@@ -258,14 +270,8 @@ let
258270

259271
config = lib.mkIf (config.stylix.enable && cfg.enable) (
260272
lib.mkMerge (
261-
lib.optional (generalConfig != null) (
262-
mkConfig (
263-
if builtins.isPath generalConfig then import generalConfig else generalConfig
264-
)
265-
)
266-
++ map (c: mkConditionalConfig (if builtins.isPath c then import c else c)) (
267-
lib.toList configElements
268-
)
273+
lib.singleton (mkConfig false generalConfig)
274+
++ map (mkConfig true) (lib.toList configElements)
269275
)
270276
);
271277
};

0 commit comments

Comments
 (0)