Skip to content

Instantly share code, notes, and snippets.

@Profpatsch
Created October 25, 2022 20:20
Embed
What would you like to do?
nixos options json schema
let
pkgs = import ./. {};
lib = pkgs.lib;
allOptions = (import ./nixos {}).options;
depot = import /home/philip/depot {};
optionSchema = opt: {
type = simpletype opt;
description = opt.description.text or "";
};
simpletype = option: lib.pipe option [
({type,...}: let n = type.name; in
if n == "bool" then "boolean"
else if n == "int" then "number"
else if n == "str" then "string"
else "undefined")
];
valMap = f: xs: map ({name, value}: {inherit name; value = f name value; }) xs;
optionDiscr = depot.nix.tag.discr [
{ namespace = v: !(v ? _type); }
{ invisibleOption = v:
# Apparently "shallow" is also an option for `visible`.
(v.visible or true)
== false; }
{ option = _: true; }
];
optionNamespaceSchema = nsName: ns: lib.pipe ns [
(depot.nix.mapAttrsMaybe (name: val: lib.pipe val [
optionDiscr
(depot.nix.tag.matchLam {
namespace = optionNamespaceSchema name;
invisibleOption = v: lib.traceSeqN 2 (v.type) null;
option = optionSchema;
})
]))
(vals: {
type = "object";
description = "Option namespace ${nsName}";
properties = vals;
})
];
in optionNamespaceSchema "toplevel" allOptions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment