Merge pull request #332 from nix-community/feat/modules-config

feat: validate config using modules
This commit is contained in:
DavHau 2022-10-20 16:16:48 +02:00 committed by GitHub
commit 74143c8c62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 105 additions and 15 deletions

View File

@ -163,9 +163,7 @@
import ./src rec {
externalDir = externalDirFor."${system}";
inherit externalPaths externalSources inputs lib pkgs;
config = {
inherit overridesDirs;
};
config = {inherit overridesDirs;};
});
docsCli = forAllSystems (

View File

@ -6,13 +6,16 @@
pkgs ? import <nixpkgs> {},
lib ? pkgs.lib,
nix ? pkgs.nix,
# default to empty dream2nix config
# already validated config.
# this is mainly used by src/lib.nix since it loads the config beforehand.
loadedConfig ? null,
# default to empty dream2nix config. This is assumed to be not loaded.
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"))
# load from default directory
else (import ./utils/config.nix).loadConfig {},
if builtins ? getEnv && builtins.getEnv "dream2nixConfig" != ""
# if called via CLI, load config via env
then builtins.toPath (builtins.getEnv "dream2nixConfig")
# load from default directory
else {},
/*
Inputs that are not required for building, and therefore not need to be
copied alongside a dream2nix installation.
@ -50,13 +53,19 @@ in let
l = lib // builtins;
config = (import ./utils/config.nix).loadConfig argsConfig;
config =
if loadedConfig != null
then loadedConfig
else
import ./modules/config.nix {
inherit lib;
rawConfig = argsConfig;
};
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

@ -13,10 +13,16 @@
initDream2nix = config: pkgs:
import ./default.nix
{inherit config inputs pkgs externalPaths externalSources;};
{
loadedConfig = config;
inherit inputs pkgs externalPaths externalSources;
};
loadConfig = config'': let
config' = (import ./utils/config.nix).loadConfig config'';
config' = import ./modules/config.nix {
rawConfig = config'';
inherit lib;
};
config =
config'
@ -197,7 +203,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.";
}

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

@ -0,0 +1,29 @@
{
rawConfig,
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 rawConfig;
evaled = lib.evalModules {
modules = [./config {inherit config;}];
specialArgs = {inherit lib;};
};
in
evaled.config

View File

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

View File

@ -0,0 +1,43 @@
{lib, ...}: let
l = lib // builtins;
t = l.types;
in {
options = {
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.
'';
};
};
}