From a448ae9fb194fa267e1822403fc6f35a6412af47 Mon Sep 17 00:00:00 2001 From: Felix Stupp Date: Sun, 25 Aug 2024 14:16:38 +0000 Subject: [PATCH] flake: add module self-reflection on import - hereby replacing old self argument from flake --- flake.nix | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/flake.nix b/flake.nix index 483d48c..d077d1d 100644 --- a/flake.nix +++ b/flake.nix @@ -52,12 +52,12 @@ # tools / shortcuts lib # nixpkgs & my lib combined # flake refs - self # reflection inputs # evaluated inputs outputs # evaluated outputs ; + # self: the module’s result, via self-reflection }; - importFlakeMod = path: import path flakeArg; + importFlakeMod = path: outputs.libAnchors.reflect (import path) flakeArg; importFlakeModWithSystem = path: lib.forAllSystems (importFlakeMod path); in { @@ -72,7 +72,33 @@ default.imports = [ ./nix/hmModules ]; }; - lib = importFlakeMod ./nix/lib; + lib = outputs.libAnchors // importFlakeMod ./nix/lib; + + # anchors required for importing modules + libAnchors = + let + lib = inputs.nixpkgs.lib; + inherit (lib.asserts) assertMsg; + in + { + # ({?} -> ?) -> {?} -> ? + # gives a function access to its own return value + # by adding it to its first argument (assuming that’s an attrset) + reflect = + fun: attrs: + # TODO is there a more official way? + assert assertMsg (builtins.isAttrs attrs) '' + expected a set, got an ${builtins.typeOf attrs} + ''; + assert assertMsg (!attrs ? "self") '' + reflect argument already contains a self attribute + ''; + let + outputs = fun (attrs // { self = result; }); + result = outputs; + in + result; + }; nixosConfigurations = let