feat: validate config using modules

This commit is contained in:
Yusuf Bera Ertan 2022-10-06 15:43:53 +03:00
parent cfd0f9f368
commit 484ca10f32
No known key found for this signature in database
GPG Key ID: 1D8F8FAF2294D6EA
6 changed files with 112 additions and 7 deletions

View File

@ -10,9 +10,17 @@
config ?
# if called via CLI, load config via env
if builtins ? getEnv && builtins.getEnv "dream2nixConfig" != ""
then (import ./utils/config.nix).loadConfig (builtins.toPath (builtins.getEnv "dream2nixConfig"))
then
import ./modules/config.nix {
configRaw = builtins.toPath (builtins.getEnv "dream2nixConfig");
inherit lib;
}
# load from default directory
else (import ./utils/config.nix).loadConfig {},
else
import ./modules/config.nix {
configRaw = {};
inherit lib;
},
/*
Inputs that are not required for building, and therefore not need to be
copied alongside a dream2nix installation.
@ -50,13 +58,15 @@ in let
l = lib // builtins;
config = (import ./utils/config.nix).loadConfig argsConfig;
config = import ./modules/config.nix {
configRaw = argsConfig;
inherit lib;
};
configFile = pkgs.writeText "dream2nix-config.json" (b.toJSON config);
dlib = import ./lib {
inherit lib;
config = (import ./utils/config.nix).loadConfig config;
inherit lib config;
inherit framework;
};

View File

@ -16,7 +16,10 @@
{inherit config inputs pkgs externalPaths externalSources;};
loadConfig = config'': let
config' = (import ./utils/config.nix).loadConfig config'';
config' = import ./modules/config.nix {
configRaw = config'';
inherit lib;
};
config =
config'
@ -197,7 +200,7 @@ in {
inherit init makeFlakeOutputs makeFlakeOutputsForIndexes;
dlib = import ./lib {
inherit lib;
config = (import ./utils/config.nix).loadConfig {};
config = loadConfig {};
};
riseAndShine = throw "Use makeFlakeOutputs instead of riseAndShine.";
}

32
src/modules/config.nix Normal file
View File

@ -0,0 +1,32 @@
{
configRaw,
lib,
}: let
b = builtins;
# loads attrs either from s:
# - json file
# - json string
# - attrset (no changes)
loadAttrs = input:
if b.isPath input
# discarding context here should be fine since we read the text from
# a path, which will be realized and nothing else will need to be realized
then b.fromJSON (b.unsafeDiscardStringContext (b.readFile input))
else if b.isString input
then b.fromJSON input
else if b.isAttrs input
then input
else throw "input for loadAttrs must be json file or string or attrs";
config = loadAttrs configRaw;
evaled = lib.evalModules {
modules = [./config {inherit config;}];
specialArgs = {inherit lib;};
};
in
# If the config was already loaded, then we skip validating it again
if configRaw._loaded or null == true
then configRaw
else evaled.config

View File

@ -0,0 +1,6 @@
{
imports = [
./interface.nix
./implementation.nix
];
}

View File

@ -0,0 +1,5 @@
{
config = {
_loaded = true;
};
}

View File

@ -0,0 +1,49 @@
{lib, ...}: let
l = lib // builtins;
t = l.types;
in {
options = {
_loaded = l.mkOption {
type = t.bool;
default = false;
readOnly = true;
internal = true;
};
overridesDirs = l.mkOption {
type = t.listOf t.path;
default = [];
description = ''
Override directories to pull overrides from.
'';
};
packagesDir = l.mkOption {
type = t.str;
default = "./dream2nix-packages";
description = ''
Relative path to the project root to put generated dream-lock files in.
'';
};
projectRoot = l.mkOption {
type = t.nullOr t.path;
default = null;
description = ''
Absolute path to the root of this project.
'';
};
repoName = l.mkOption {
type = t.nullOr t.str;
default = null;
description = ''
Name of the repository this project is in.
'';
};
modules = l.mkOption {
type = t.listOf (t.oneOf [t.attrs t.path (t.functionTo t.attrs)]);
default = [];
description = ''
Extra modules to import in while evaluating the dream2nix framework.
This allows you to add new discoverers, translators, builders etc. and lets you override existing ones.
'';
};
};
}