Skip to content

Commit

Permalink
WIP add flakeModules.modules: flake.modules
Browse files Browse the repository at this point in the history
  • Loading branch information
roberth committed Feb 16, 2024
1 parent b253292 commit 415e560
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
32 changes: 32 additions & 0 deletions dev/tests/eval-tests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,24 @@ rec {
};
};

modulesFlake = mkFlake
{
inputs.self = { };
moduleLocation = "modulesFlake";
}
{
imports = [ flake-parts.flakeModules.modules ];
systems = [ ];
flake = {
modules.generic.example = { lib, ... }: {
options.generic.example = lib.mkOption { default = "works in any module system application"; };
};
modules.foo.example = { lib, ... }: {
options.foo.example = lib.mkOption { default = "works in foo application"; };
};
};
};

flakeModulesDeclare = mkFlake
{ inputs.self = { outPath = ./.; }; }
({ config, ... }: {
Expand Down Expand Up @@ -201,6 +219,20 @@ rec {

assert emptyExposeArgs.moduleLocation == "the self outpath/flake.nix";

assert (lib.evalModules {
class = "barrr";
modules = [
modulesFlake.modules.generic.example
];
}).config.generic.example == "works in any module system application";

assert (lib.evalModules {
class = "foo";
modules = [
modulesFlake.modules.foo.example
];
}).config.foo.example == "works in foo application";

ok;

result = runTests "ok";
Expand Down
48 changes: 48 additions & 0 deletions extras/modules.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{ lib, moduleLocation, ... }:
let
inherit (lib)
mapAttrs
mkOption
types
;
inherit (lib.strings)
escapeNixIdentifier
;

addInfo = class: moduleName:
if class == "generic"
then module: module
else
module:
# TODO: set key?
{
_class = class;
_file = "${toString moduleLocation}#modules.${escapeNixIdentifier class}.${escapeNixIdentifier moduleName}";
imports = [ module ];
};
in
{
options = {
flake.modules = mkOption {
type = types.lazyAttrsOf (types.lazyAttrsOf types.deferredModule);
description = ''
Groups of modules published by the flake.
The outer attributes declare the [`class`](https://nixos.org/manual/nixpkgs/stable/#module-system-lib-evalModules-param-class) of the modules within it.
The special attribute `generic` does not declare a class, allowing its modules to be used in any module class.
'';
# TODO: Add one or two real-world examples.
example = lib.literalExpression ''
{
flake-parts = {
foo = … some module …;
};
generic = {
my-pkgs = { _module.args.my-pkgs = …; };
};
}
'';
apply = mapAttrs (k: mapAttrs (addInfo k));
};
};
}
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
};
flakeModules = {
easyOverlay = ./extras/easyOverlay.nix;
modules = ./extras/modules.nix;
flakeModules = ./extras/flakeModules.nix;
};
};
Expand Down

0 comments on commit 415e560

Please sign in to comment.