Merge pull request #304 from nix-community/feat/modules-builders

feat: use modules for builders
This commit is contained in:
DavHau 2022-09-27 14:57:32 +02:00 committed by GitHub
commit 12ae9b891b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 149 additions and 12 deletions

View File

@ -0,0 +1,11 @@
{
builders.dummy = {...}: {
name = "dummy";
subsystem = "hello";
type = "pure";
build = {hello, ...}: {...}: {
packages.${hello.pname}.${hello.version} =
hello;
};
};
}

View File

@ -11,12 +11,5 @@
})
];
};
builders.dummy = {...}: {
type = "pure";
build = {hello, ...}: {...}: {
packages.${hello.pname}.${hello.version} =
hello;
};
};
};
}

View File

@ -13,6 +13,7 @@
config.extra = ./extra.nix;
config.modules = [
./translators.nix
./builders.nix
];
source = ./.;
settings = [

View File

@ -23,10 +23,18 @@
};
}
'')
(builtins.toFile "brp-new.nix" ''
{
builders.brp-new = {
imports = ["${inp.dream2nix}/src/subsystems/rust/builders/build-rust-package"];
name = "brp-new";
subsystem = "rust";
};
}
'')
];
config.extra = {
subsystems.rust = {
builders.brp-new = "${inp.dream2nix}/src/subsystems/rust/builders/build-rust-package";
discoverers.default = "${inp.dream2nix}/src/subsystems/rust/discoverers/default";
};
fetchers.crates-io = "${inp.dream2nix}/src/fetchers/crates-io";

View File

@ -242,9 +242,9 @@ in let
findBuilder = dreamLock: let
subsystem = dreamLock._generic.subsystem;
in
if ! subsystems."${subsystem}" ? builders
if ! framework.buildersBySubsystem ? ${subsystem}
then throw "Could not find any builder for subsystem '${subsystem}'"
else subsystems."${subsystem}".builders.default;
else framework.buildersBySubsystem.${subsystem}.default;
# detect if granular or combined fetching must be used
findFetcher = dreamLock:
@ -394,7 +394,7 @@ in let
if builder == null
then findBuilder dreamLock
else if l.isString builder
then subsystems.${dreamLock._generic.subsystem}.builders.${builder}
then framework.buildersBySubsystem.${dreamLock._generic.subsystem}.${builder}
else builder;
fetcher' =

View File

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

View File

@ -0,0 +1,33 @@
{
config,
lib,
...
}: let
t = lib.types;
in {
options = {
disabled = lib.mkOption {
type = t.bool;
default = false;
description = "Whether to disable the builder, if disabled it can't be used.";
};
name = lib.mkOption {
type = t.str;
description = "Name of the builder.";
};
subsystem = lib.mkOption {
type = t.str;
description = "Subsystem of the builder.";
};
build = lib.mkOption {
type = t.functionTo (t.functionTo t.attrs);
default = _: _: {};
};
type = lib.mkOption {
type = t.enum [
"pure"
"ifd"
];
};
};
}

View File

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

View File

@ -0,0 +1,40 @@
{
config,
callPackageDream,
lib,
...
}: let
defaults = {
rust = "build-rust-package";
nodejs = "granular-nodejs";
python = "simple-python";
php = "granular-php";
haskell = "simple-haskell";
debian = "simple-debian";
};
loader = b: b // {build = callPackageDream b.build {};};
funcs = config.functions.subsystem-loading;
collectedModules = funcs.collect "builders";
in {
config = {
# The user can add more translators by extending this attribute
builders = funcs.import_ collectedModules;
/*
translators wrapped with extra logic to add extra attributes,
like .translateBin for pure translators
*/
builderInstances = funcs.instantiate config.builders loader;
buildersBySubsystem =
lib.mapAttrs
(
subsystem: builders:
builders
// lib.optionalAttrs (lib.hasAttr subsystem defaults) {
default = builders.${defaults.${subsystem}};
}
)
(funcs.structureBySubsystem config.builderInstances);
};
}

View File

@ -0,0 +1,25 @@
{
lib,
specialArgs,
...
}: let
t = lib.types;
in {
options = {
builders = lib.mkOption {
type = t.attrsOf (t.submoduleWith {
modules = [./builder/default.nix];
inherit specialArgs;
});
description = ''
builder module definitions
'';
};
builderInstances = lib.mkOption {
type = t.attrsOf t.anything;
};
buildersBySubsystem = lib.mkOption {
type = t.attrsOf (t.attrsOf t.anything);
};
};
}

View File

@ -44,7 +44,21 @@ modules:
(name: description:
(import description.path {inherit dlib lib;})
// {inherit (description) name subsystem;})
(lib.listToAttrs collectedModules);
(
lib.foldl'
(
all: el:
if lib.hasAttr el.name all
then
throw ''
module named ${el.name} in subsystem ${el.value.subsystem} conflicts
with a module with the same name from subsystem ${all.${el.name}.subsystem}
''
else all // {${el.name} = el.value;}
)
{}
collectedModules
);
instantiate = importedModules: loader:
lib.mapAttrs

View File

@ -9,6 +9,7 @@ in {
./functions.subsystem-loading
./functions.translators
./translators
./builders
];
options = {
lib = lib.mkOption {