From b92568fbdec7878c59c405e4165a10f170adc1f5 Mon Sep 17 00:00:00 2001 From: Gerg-L Date: Thu, 13 Mar 2025 19:38:04 -0400 Subject: [PATCH] lib: add overlay --- lib/_default.nix | 135 +--------------------------------------------- lib/overlay.nix | 137 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 134 deletions(-) create mode 100644 lib/overlay.nix diff --git a/lib/_default.nix b/lib/_default.nix index 2b2a99f..47ce68b 100644 --- a/lib/_default.nix +++ b/lib/_default.nix @@ -1,141 +1,8 @@ { unstable, - self, ... }@inputs: let inherit (unstable) lib; in -lib.fix (myLib: { - wrench = lib.flip lib.pipe; - - needsSystem = lib.flip builtins.elem [ - "apps" - "checks" - "defaultPackage" - "devShell" - "devShells" - "formatter" - "legacyPackages" - "packages" - ]; - - constructInputs' = - system: - myLib.wrench [ - (lib.filterAttrs (_: lib.isType "flake")) - (lib.mapAttrs ( - _: lib.mapAttrs (name: value: if myLib.needsSystem name then value.${system} else value) - )) - ]; - - listNixFilesRecursive = myLib.wrench [ - builtins.unsafeDiscardStringContext - lib.filesystem.listFilesRecursive - (builtins.filter (x: !lib.hasPrefix "_" (builtins.baseNameOf x) && lib.hasSuffix ".nix" x)) - ]; - - addSchizophreniaToModule = - x: - let - # the imported module - imported = import x; - in - /* - If the module isn't a function then - it doesn't need arguments and error - message locations will function correctly - */ - if !lib.isFunction imported then - x - else - let - # all arguments defined in the module - funcArgs = lib.functionArgs imported; - /* - The names of all arguments which will be - available to be inserted into the module arguments - */ - argNames = builtins.attrNames inputs ++ [ - "inputs" - "inputs'" - "self'" - "_dir" - ]; - - /* - arguments to be passed minus - per system attributes - for example flake-parts-esque inputs' - */ - argsPre = { - inherit inputs self; - /* - _dir is the "self" derived - path to the directory containing the module - */ - _dir = builtins.dirOf x; - }; - - /* - arguments which will be inserted - set to the before per-system values - */ - providedArgs = lib.pipe funcArgs [ - (lib.filterAttrs (n: _: builtins.elem n argNames)) - (lib.mapAttrs (n: _: argsPre.${n} or { })) - ]; - - /* - arguments which the module system - not provided here. either to be - provided by the module system or invalid - */ - neededArgs = lib.filterAttrs (n: _: !builtins.elem n argNames) funcArgs; - in - { - __functionArgs = neededArgs // { - /* - always require pkgs to be passed - to derive system from pkgs.stdenv.system - */ - pkgs = false; - }; - - __functor = - /* - args is specialArgs + _module.args which are needed - and always pkgs - */ - _: args: - imported ( - /* - take module system provided arguments - filter them so only the required ones are passed - */ - (lib.filterAttrs (n: _: neededArgs ? ${n}) args) - # add needed arguments - // ( - providedArgs - # add system dependent arguments - // ( - let - inputs' = myLib.constructInputs' args.pkgs.stdenv.system inputs; - - actuallyAllArgs = inputs' // { - inherit inputs'; - self' = inputs'.self; - inherit (inputs) self; - }; - in - lib.filterAttrs (n: _: providedArgs ? ${n}) actuallyAllArgs - ) - ) - ) - # add _file to the final module attribute set - // { - _file = x; - }; - }; - -}) +lib.fix (self: (import ./overlay.nix inputs self lib)) diff --git a/lib/overlay.nix b/lib/overlay.nix new file mode 100644 index 0000000..1552d73 --- /dev/null +++ b/lib/overlay.nix @@ -0,0 +1,137 @@ +{ self, ... }@inputs: + +myLib: lib: { + overlay = import ./overlay.nix inputs; + + wrench = lib.flip lib.pipe; + + needsSystem = lib.flip builtins.elem [ + "apps" + "checks" + "defaultPackage" + "devShell" + "devShells" + "formatter" + "legacyPackages" + "packages" + ]; + + constructInputs' = + system: + myLib.wrench [ + (lib.filterAttrs (_: lib.isType "flake")) + (lib.mapAttrs ( + _: lib.mapAttrs (name: value: if myLib.needsSystem name then value.${system} else value) + )) + ]; + + listNixFilesRecursive = myLib.wrench [ + builtins.unsafeDiscardStringContext + lib.filesystem.listFilesRecursive + (builtins.filter (x: !lib.hasPrefix "_" (builtins.baseNameOf x) && lib.hasSuffix ".nix" x)) + ]; + + addSchizophreniaToModule = + x: + let + # the imported module + imported = import x; + in + /* + If the module isn't a function then + it doesn't need arguments and error + message locations will function correctly + */ + if !lib.isFunction imported then + x + else + let + # all arguments defined in the module + funcArgs = lib.functionArgs imported; + /* + The names of all arguments which will be + available to be inserted into the module arguments + */ + argNames = builtins.attrNames inputs ++ [ + "inputs" + "inputs'" + "self'" + "_dir" + ]; + + /* + arguments to be passed minus + per system attributes + for example flake-parts-esque inputs' + */ + argsPre = { + inherit inputs self; + /* + _dir is the "self" derived + path to the directory containing the module + */ + _dir = builtins.dirOf x; + }; + + /* + arguments which will be inserted + set to the before per-system values + */ + providedArgs = lib.pipe funcArgs [ + (lib.filterAttrs (n: _: builtins.elem n argNames)) + (lib.mapAttrs (n: _: argsPre.${n} or { })) + ]; + + /* + arguments which the module system + not provided here. either to be + provided by the module system or invalid + */ + neededArgs = lib.filterAttrs (n: _: !builtins.elem n argNames) funcArgs; + in + { + __functionArgs = neededArgs // { + /* + always require pkgs to be passed + to derive system from pkgs.stdenv.system + */ + pkgs = false; + }; + + __functor = + /* + args is specialArgs + _module.args which are needed + and always pkgs + */ + _: args: + imported ( + /* + take module system provided arguments + filter them so only the required ones are passed + */ + (lib.filterAttrs (n: _: neededArgs ? ${n}) args) + # add needed arguments + // ( + providedArgs + # add system dependent arguments + // ( + let + inputs' = myLib.constructInputs' args.pkgs.stdenv.system inputs; + + actuallyAllArgs = inputs' // { + inherit inputs'; + self' = inputs'.self; + inherit (inputs) self; + }; + in + lib.filterAttrs (n: _: providedArgs ? ${n}) actuallyAllArgs + ) + ) + ) + # add _file to the final module attribute set + // { + _file = x; + }; + }; + +}