Skip to content

Commit

Permalink
chore: reorg
Browse files Browse the repository at this point in the history
  • Loading branch information
GTrunSec committed Dec 7, 2023
1 parent 848f21b commit 1cc46c3
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 129 deletions.
15 changes: 3 additions & 12 deletions examples/callFlake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,17 @@ let
in
{
a =
((((flake.pops.default.setInitInputs ./__nixpkgsFlake).setSystem "x86_64-linux")
(((flake.pops.default.setInitInputs ./__nixpkgsFlake).setSystem "x86_64-linux")
.addInputsExtender
(
POP.lib.extendPop flake.pops.inputsExtender (
self: super: {
inputs.nixlib = inputs.nixlib;
inputs.b = throw "should not be evaluated";
# inputs.b = throw "should not be evaluated";
inputs.custom = self.inputs.nixlib;
}
)
)
).addInputsOverrideExtender
(
POP.lib.extendPop flake.pops.inputsExtender (
self: super: { overrideInputs.std = "s"; }
)
)
);

b = self.a.addInputsExtender (
POP.lib.extendPop flake.pops.inputsExtender (self: super: { inputs.b = "2"; })
);
b = self.a.addInputsExtender { inputs.b = "2"; };
}
18 changes: 9 additions & 9 deletions src/flake/pops.nix
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ in
# defun
# (
# with types; [
# (list inputsExtenderPop)
# (either (list inputsExtenderPop) (list inputsExtender))
# flakePop
# ]
# )
Expand All @@ -164,14 +164,14 @@ in
);

addInputsExtender =
# defun
# (
# with types; [
# (either inputsExtenderPop (attrs any))
# flakePop
# ]
# )
(inputsExtender: self.addInputsExtenders [ inputsExtender ]);
defun
(
with types; [
(either inputsExtender inputsExtenderPop)
flakePop
]
)
(inputsExtender: self.addInputsExtenders [ inputsExtender ]);

addExporters =
defun
Expand Down
8 changes: 7 additions & 1 deletion src/flake/structAttrs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,22 @@ let

inputsExtenderPop = structAttrs.pop // {
initInputs = attrs any;
initFlake = attrs any;
sysInputs = attrs any;
inputs = attrs any;
setInitInputs = function;
};

inputsExtender = {
inputs = attrs any;
};

flakePop =
structAttrs.exporterPop
// structAttrs.inputsExtenderPop
// {
exporters = list types.exporterPop;
inputsExtenders = list types.inputsExtenderPop;

system = string;

exports = attrs any;
Expand Down
15 changes: 10 additions & 5 deletions src/flake/types.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{ yants, root }:
{ yants, super }:
with (yants "flops"); {
pop = openStruct root.flake.structAttrs.pop;
exporterPop = openStruct root.flake.structAttrs.exporterPop;
inputsExtenderPop = openStruct root.flake.structAttrs.inputsExtenderPop;
flakePop = openStruct root.flake.structAttrs.flakePop;
pop = openStruct super.structAttrs.pop;
exporterPop = openStruct super.structAttrs.exporterPop;

inputsExtenderPop =
openStruct "inputsExtenderPop"
super.structAttrs.inputsExtenderPop;
inputsExtender = struct "inputsExtender" super.structAttrs.inputsExtender;

flakePop = openStruct super.structAttrs.flakePop;
}
211 changes: 109 additions & 102 deletions src/haumea/nixosModules.nix
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ let
(mapAttrs (name: _: inputs.${name}))
f
];

callModuleLazily =
inputs: path:
let
importer = l.scopedImport inputs;
f = toFunction (importer path);
in
lazyArgsPerParameter f inputs;

removeFileSuffix = l.removeSuffix ".nix";
removeDefault = l.removeSuffix "/default";
relModulePathWithoutDefault = relModulePathWithoutDefault' removeDefault;
Expand All @@ -43,6 +45,17 @@ let
)
));

isTopLevel =
path:
if
l.length (l.splitString "/" (l.last (l.splitString cfg.src (toString path))))
== 2
then
true
else
false
;

base =
{
extender ? [ ],
Expand All @@ -57,119 +70,115 @@ let
(
let
module =
{
config ? { },
options ? { },
...
}@args:
let
mkModulePath =
attrs': l.setAttrByPath (relModulePathWithoutDefault path) attrs';
test = config._module.args.pkgs or { };
baseModuleArgs =
(
inputs
// args
// (
{
if (isTopLevel path) && cfg.type == "nixosProfilesOmnibus" then
callModuleLazily inputs path
else
{
config ? { },
options ? { },
...
}@args:
let
mkModulePath =
attrs': l.setAttrByPath (relModulePathWithoutDefault path) attrs';
test = config._module.args.pkgs or { };
baseModuleArgs =
(
inputs
// args
// ({
cfg = l.attrByPath (relModulePathWithoutDefault path) { } config;
opt = l.attrByPath (relModulePathWithoutDefault path) { } options;
inherit mkModulePath;
moduleArgs = config._module.args // config._module.specialArgs;
# override the self for the module
self = inputs.self { };
}
//
l.optionalAttrs
(l.elem cfg.type [
"nixosModules"
"nixosProfiles"
])
{ pkgs = config._module.args.pkgs; }
)
);
pkgs = config._module.args.pkgs;
})
);

moduleArgs = baseModuleArgs // {
loadSubmodule = path: (mkExtenders (callModuleLazily baseModuleArgs path) path);
};
moduleArgs = baseModuleArgs // {
loadSubmodule = path: (mkExtender (callModuleLazily baseModuleArgs path) path);
};

callArgsLazily =
attrs: extraArgs:
if (l.isFunction attrs) then
lazyArgsPerParameter attrs (moduleArgs // extraArgs)
else
attrs
;
callArgsLazily =
attrs: extraArgs:
if (l.isFunction attrs) then
lazyArgsPerParameter attrs (moduleArgs // extraArgs)
else
attrs
;

s3 = callModuleLazily moduleArgs path;
# => { config = { }; imports = [... ]; _file }
s3final =
let
s3Module = mkExtenders (winnow path s3 mkModulePath) path;
s3Profile = mkExtenders s3 path;
in
if isModule then s3Module else s3Profile;

mkExtenders =
module: path:
let
removedOptionModule = removeAttrs module [ "options" ];
filteredList =
l.filter
(
item:
item.path == (
if isModule then relModulePathWithoutDefault path else relModulePath path
mkExtender =
module: path:
let
removedOptionModule = removeAttrs module [ "options" ];
filteredList =
l.filter
(
item:
item.path == (
if isModule then relModulePathWithoutDefault path else relModulePath path
)
)
)
extender;
extender;

foundItem =
if (builtins.length filteredList) > 0 then
(builtins.head filteredList)
else
[ ]
;

foundItem =
if (builtins.length filteredList) > 0 then
(builtins.head filteredList)
else
[ ]
;
loadExtendModuleFromValue =
if foundItem != [ ] then
(callArgsLazily foundItem.value {
selfModule = module;
# add the options back in
# dmerge self' {}
selfModule' = x: module // (x removedOptionModule);
# add dmerge support
inherit dmerge;
})
else
module
;
in
loadExtendModuleFromValue;

loadExtendModuleFromValue =
if foundItem != [ ] then
(callArgsLazily foundItem.value {
selfModule = module;
# add the options back in
# dmerge self' {}
selfModule' = x: module // (x removedOptionModule);
# add dmerge support
inherit dmerge;
})
else
module
;
in
loadExtendModuleFromValue;
winnow =
path: module: fun:
({
config =
if module ? config then
module.config
else
fun (
removeAttrs module [
"options"
"imports"
]
)
;
imports = module.imports or [ ];
})
// {
_file = path;
options = fun module.options or { };
}
;

winnow =
path: module: fun:
({
config =
if module ? config then
module.config
else
fun (
removeAttrs module [
"options"
"imports"
]
)
;
imports = module.imports or [ ];
})
// {
_file = path;
options = fun module.options or { };
}
;
in
s3final;
callDefaultModule = callModuleLazily moduleArgs path;
# => { config = { }; imports = [... ]; _file }
finalModule =
let
extendedModule = mkExtender (winnow path callDefaultModule mkModulePath) path;
extendedProfile = mkExtender callDefaultModule path;
in
if isModule then extendedModule else extendedProfile;
in
finalModule
;
in
module
)
Expand All @@ -189,5 +198,3 @@ in
default = base { };
__extender = extender: base { inherit extender; };
}
// l.optionalAttrs (cfg.type == "nixosModules") { nixosModules = base { }; }
// l.optionalAttrs (cfg.type == "nixosProfiles") { nixosProfiles = base { }; }
1 change: 1 addition & 0 deletions src/haumea/pops.nix
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ let
"nixosModules"
"nixosProfiles"
"evalModules"
"nixosProfilesOmnibus"
])
then
nixosModules { inherit cfg; }
Expand Down
1 change: 1 addition & 0 deletions src/haumea/structAttrs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ let
"default"
"nixosProfiles"
"evalModules"
"nixosProfilesOmnibus"
]
"type";
};
Expand Down

0 comments on commit 1cc46c3

Please sign in to comment.