forked from clan/clan-core
Merge pull request 'Inventory: add concrete use-case examples' (#1650) from hsjobeki/clan-core:hsjobeki-main into main
This commit is contained in:
commit
37311f8145
@ -5,6 +5,7 @@
|
|||||||
...
|
...
|
||||||
}:
|
}:
|
||||||
{
|
{
|
||||||
|
evalClanModules = import ./eval-clan-modules { inherit clan-core nixpkgs lib; };
|
||||||
jsonschema = import ./jsonschema { inherit lib; };
|
jsonschema = import ./jsonschema { inherit lib; };
|
||||||
modules = import ./description.nix { inherit clan-core lib; };
|
modules = import ./description.nix { inherit clan-core lib; };
|
||||||
buildClan = import ./build-clan { inherit clan-core lib nixpkgs; };
|
buildClan = import ./build-clan { inherit clan-core lib nixpkgs; };
|
||||||
|
34
lib/eval-clan-modules/default.nix
Normal file
34
lib/eval-clan-modules/default.nix
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
{
|
||||||
|
nixpkgs,
|
||||||
|
clan-core,
|
||||||
|
lib,
|
||||||
|
}:
|
||||||
|
let
|
||||||
|
inherit (import nixpkgs { system = "x86_64-linux"; }) pkgs;
|
||||||
|
|
||||||
|
inherit (clan-core) clanModules;
|
||||||
|
|
||||||
|
baseModule = {
|
||||||
|
imports = (import (pkgs.path + "/nixos/modules/module-list.nix")) ++ [
|
||||||
|
{
|
||||||
|
nixpkgs.hostPlatform = "x86_64-linux";
|
||||||
|
clan.core.clanName = "dummy";
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
|
|
||||||
|
# This function takes a list of module names and evaluates them
|
||||||
|
# evalClanModules :: [ String ] -> { config, options, ... }
|
||||||
|
evalClanModules =
|
||||||
|
modulenames:
|
||||||
|
let
|
||||||
|
evaled = lib.evalModules {
|
||||||
|
modules = [
|
||||||
|
baseModule
|
||||||
|
clan-core.nixosModules.clanCore
|
||||||
|
] ++ (map (name: clanModules.${name}) modulenames);
|
||||||
|
};
|
||||||
|
in
|
||||||
|
evaled;
|
||||||
|
in
|
||||||
|
evalClanModules
|
@ -7,7 +7,7 @@
|
|||||||
{
|
{
|
||||||
imports = [ ./jsonschema/flake-module.nix ];
|
imports = [ ./jsonschema/flake-module.nix ];
|
||||||
flake.lib = import ./default.nix {
|
flake.lib = import ./default.nix {
|
||||||
inherit lib;
|
inherit lib inputs;
|
||||||
inherit (inputs) nixpkgs;
|
inherit (inputs) nixpkgs;
|
||||||
clan-core = self;
|
clan-core = self;
|
||||||
};
|
};
|
||||||
|
@ -47,7 +47,7 @@ rec {
|
|||||||
let
|
let
|
||||||
evaled = lib.evalModules { modules = [ module ]; };
|
evaled = lib.evalModules { modules = [ module ]; };
|
||||||
in
|
in
|
||||||
parseOptions evaled.options;
|
{ "$schema" = "http://json-schema.org/draft-07/schema#"; } // parseOptions evaled.options;
|
||||||
|
|
||||||
# parses a set of evaluated nixos options to a jsonschema
|
# parses a set of evaluated nixos options to a jsonschema
|
||||||
parseOptions =
|
parseOptions =
|
||||||
@ -66,6 +66,7 @@ rec {
|
|||||||
// {
|
// {
|
||||||
type = "object";
|
type = "object";
|
||||||
inherit properties;
|
inherit properties;
|
||||||
|
additionalProperties = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
# parses and evaluated nixos option to a jsonschema property definition
|
# parses and evaluated nixos option to a jsonschema property definition
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
{
|
{
|
||||||
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||||
"type": "object",
|
"type": "object",
|
||||||
|
"additionalProperties": false,
|
||||||
"properties": {
|
"properties": {
|
||||||
"name": {
|
"name": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
@ -38,6 +40,7 @@
|
|||||||
},
|
},
|
||||||
"services": {
|
"services": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
|
"additionalProperties": false,
|
||||||
"properties": {
|
"properties": {
|
||||||
"opt": {
|
"opt": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
@ -59,9 +62,8 @@
|
|||||||
"type": "string"
|
"type": "string"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"required": [
|
"required": ["repo"],
|
||||||
"repo"
|
"additionalProperties": false,
|
||||||
],
|
|
||||||
"type": "object"
|
"type": "object"
|
||||||
},
|
},
|
||||||
"default": {},
|
"default": {},
|
||||||
|
@ -278,6 +278,7 @@ in
|
|||||||
expr = slib.parseOption (evalType (lib.types.submodule subModule) { });
|
expr = slib.parseOption (evalType (lib.types.submodule subModule) { });
|
||||||
expected = {
|
expected = {
|
||||||
type = "object";
|
type = "object";
|
||||||
|
additionalProperties = false;
|
||||||
properties = {
|
properties = {
|
||||||
opt = {
|
opt = {
|
||||||
type = "boolean";
|
type = "boolean";
|
||||||
@ -301,6 +302,7 @@ in
|
|||||||
expr = slib.parseOption (evalType (lib.types.submodule subModule) { });
|
expr = slib.parseOption (evalType (lib.types.submodule subModule) { });
|
||||||
expected = {
|
expected = {
|
||||||
type = "object";
|
type = "object";
|
||||||
|
additionalProperties = false;
|
||||||
properties = {
|
properties = {
|
||||||
opt = {
|
opt = {
|
||||||
type = "boolean";
|
type = "boolean";
|
||||||
@ -331,6 +333,7 @@ in
|
|||||||
type = "object";
|
type = "object";
|
||||||
additionalProperties = {
|
additionalProperties = {
|
||||||
type = "object";
|
type = "object";
|
||||||
|
additionalProperties = false;
|
||||||
properties = {
|
properties = {
|
||||||
opt = {
|
opt = {
|
||||||
type = "boolean";
|
type = "boolean";
|
||||||
@ -363,6 +366,7 @@ in
|
|||||||
type = "array";
|
type = "array";
|
||||||
items = {
|
items = {
|
||||||
type = "object";
|
type = "object";
|
||||||
|
additionalProperties = false;
|
||||||
properties = {
|
properties = {
|
||||||
opt = {
|
opt = {
|
||||||
type = "boolean";
|
type = "boolean";
|
||||||
|
@ -4,16 +4,9 @@
|
|||||||
lib ? (import <nixpkgs> { }).lib,
|
lib ? (import <nixpkgs> { }).lib,
|
||||||
slib ? import ./. { inherit lib; },
|
slib ? import ./. { inherit lib; },
|
||||||
}:
|
}:
|
||||||
let
|
|
||||||
evaledOptions =
|
|
||||||
let
|
|
||||||
evaledConfig = lib.evalModules { modules = [ ./example-interface.nix ]; };
|
|
||||||
in
|
|
||||||
evaledConfig.options;
|
|
||||||
in
|
|
||||||
{
|
{
|
||||||
testParseOptions = {
|
testParseOptions = {
|
||||||
expr = slib.parseOptions evaledOptions;
|
expr = slib.parseModule ./example-interface.nix;
|
||||||
expected = builtins.fromJSON (builtins.readFile ./example-schema.json);
|
expected = builtins.fromJSON (builtins.readFile ./example-schema.json);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -26,8 +19,10 @@ in
|
|||||||
{
|
{
|
||||||
expr = slib.parseOptions evaled.options;
|
expr = slib.parseOptions evaled.options;
|
||||||
expected = {
|
expected = {
|
||||||
|
additionalProperties = false;
|
||||||
properties = {
|
properties = {
|
||||||
foo = {
|
foo = {
|
||||||
|
additionalProperties = false;
|
||||||
properties = {
|
properties = {
|
||||||
bar = {
|
bar = {
|
||||||
type = "boolean";
|
type = "boolean";
|
||||||
|
@ -10,37 +10,21 @@
|
|||||||
# borgbackup = self.clanModules.borgbackup;
|
# borgbackup = self.clanModules.borgbackup;
|
||||||
# };
|
# };
|
||||||
|
|
||||||
baseModule = {
|
|
||||||
imports = (import (pkgs.path + "/nixos/modules/module-list.nix")) ++ [
|
|
||||||
{
|
|
||||||
nixpkgs.hostPlatform = "x86_64-linux";
|
|
||||||
clan.core.clanName = "dummy";
|
|
||||||
}
|
|
||||||
];
|
|
||||||
};
|
|
||||||
|
|
||||||
optionsFromModule =
|
optionsFromModule =
|
||||||
modulename: module:
|
mName:
|
||||||
let
|
let
|
||||||
evaled = lib.evalModules {
|
eval = self.lib.evalClanModules [ mName ];
|
||||||
modules = [
|
|
||||||
module
|
|
||||||
baseModule
|
|
||||||
self.nixosModules.clanCore
|
|
||||||
];
|
|
||||||
};
|
|
||||||
in
|
in
|
||||||
# Filter out "injected" options that are not part of the module
|
if (eval.options.clan ? "${mName}") then eval.options.clan.${mName} else { };
|
||||||
if (evaled.options.clan ? "${modulename}") then evaled.options.clan.${modulename} else { };
|
|
||||||
|
|
||||||
clanModuleSchemas = lib.mapAttrs (
|
clanModuleSchemas = lib.mapAttrs (
|
||||||
modulename: module: self.lib.jsonschema.parseOptions (optionsFromModule modulename module)
|
modulename: _: self.lib.jsonschema.parseOptions (optionsFromModule modulename)
|
||||||
) clanModules;
|
) clanModules;
|
||||||
|
|
||||||
clanModuleFunctionSchemas = lib.mapAttrsFlatten (modulename: module: {
|
clanModuleFunctionSchemas = lib.mapAttrsFlatten (modulename: _: {
|
||||||
name = modulename;
|
name = modulename;
|
||||||
description = self.lib.modules.getShortDescription modulename;
|
description = self.lib.modules.getShortDescription modulename;
|
||||||
parameters = self.lib.jsonschema.parseOptions (optionsFromModule modulename module);
|
parameters = self.lib.jsonschema.parseOptions (optionsFromModule modulename);
|
||||||
}) clanModules;
|
}) clanModules;
|
||||||
in
|
in
|
||||||
rec {
|
rec {
|
||||||
|
Loading…
Reference in New Issue
Block a user