Mildly insane argument passing to modules

This commit is contained in:
Gerg-L 2024-06-12 23:05:45 -04:00
parent 5254992a3a
commit 03fe49b85a
Signed by: gerg-l
SSH key fingerprint: SHA256:FPYDHIkvMocr4wdmZXpgpJjsb2Tw6rASs2ISPbOb0KI
32 changed files with 134 additions and 164 deletions

View file

@ -43,11 +43,74 @@ rec {
(lib.removeSuffix ".nix")
(lib.removePrefix "${path}/")
];
value = name;
value = addSchizophreniaToModule name;
}))
builtins.listToAttrs
];
addSchizophreniaToModule =
x:
let
imported = import x;
in
if !lib.isFunction imported then
x
else
let
allArgs = {
inherit inputs self;
_dir =
let
dir = builtins.dirOf x;
in
if (dir == builtins.storeDir) then null else dir;
};
funcArgs = lib.functionArgs imported;
argNames = builtins.attrNames inputs ++ [
"inputs"
"inputs'"
"self'"
"_dir"
];
providedArgs = lib.pipe funcArgs [
(lib.filterAttrs (n: _: builtins.elem n argNames))
(lib.mapAttrs (n: _: allArgs.${n} or { }))
];
neededArgs = (lib.filterAttrs (n: _: !builtins.elem n argNames) funcArgs);
in
{
__functor =
_: args:
imported (
(lib.filterAttrs (n: _: neededArgs ? ${n}) args)
# (formatter helping comment)
// (
providedArgs
// (
let
inputs' = constructInputs' (args.pkgs.stdenv.system) inputs;
actuallyAllArgs = inputs' // {
inherit inputs';
self' = inputs'.self;
};
in
lib.filterAttrs (n: _: providedArgs ? ${n}) actuallyAllArgs
)
)
)
// {
_file = x;
};
__functionArgs = neededArgs // {
pkgs = false;
};
};
gerg-utils =
pkgsf: outputs:
lib.pipe
@ -73,56 +136,21 @@ rec {
lib.evalModules {
specialArgs.modulesPath = "${unstable}/nixos/modules";
modules =
let
importWithArgs = map (
x:
let
allArgs = (constructInputs' system inputs) // {
inherit inputs;
_dir =
let
dir = builtins.dirOf x;
in
if (dir != builtins.storeDir) then dir else null;
_file = x;
};
imported = import x;
in
lib.pipe imported [
lib.functionArgs
builtins.attrNames
(map (
x:
if allArgs ? ${x} then
{
name = x;
value = allArgs.${x};
}
else
null
))
(builtins.filter builtins.isAttrs)
builtins.listToAttrs
imported
]
);
in
builtins.concatLists [
(importWithArgs (builtins.attrValues self.nixosModules))
(importWithArgs (listNixFilesRecursive "${self}/hosts/${hostName}"))
(import "${unstable}/nixos/modules/module-list.nix")
(lib.singleton {
networking = {
inherit hostName;
};
nixpkgs.hostPlatform = system;
})
(lib.optionals (self.diskoConfigurations ? "disko-${hostName}") [
self.diskoConfigurations."disko-${hostName}"
disko.nixosModules.default
])
];
modules = builtins.concatLists [
(builtins.attrValues self.nixosModules)
(map addSchizophreniaToModule (listNixFilesRecursive "${self}/hosts/${hostName}"))
(import "${unstable}/nixos/modules/module-list.nix")
(lib.singleton {
networking = {
inherit hostName;
};
nixpkgs.hostPlatform = system;
})
(lib.optionals (self.diskoConfigurations ? "disko-${hostName}") [
self.diskoConfigurations."disko-${hostName}"
disko.nixosModules.default
])
];
}
);
mkDisko = wrench [