diff --git a/dev/tests/eval-tests.nix b/dev/tests/eval-tests.nix index 437ee1a7..2da3d990 100644 --- a/dev/tests/eval-tests.nix +++ b/dev/tests/eval-tests.nix @@ -26,6 +26,22 @@ rec { systems = [ ]; }; + emptyExposeArgs = mkFlake + { inputs.self = { outPath = "the self outpath"; }; } + ({ config, moduleLocation, ... }: { + flake = { + inherit moduleLocation; + }; + }); + + emptyExposeArgsNoSelf = mkFlake + { inputs.self = throw "self won't be available in case of some errors"; } + ({ config, moduleLocation, ... }: { + flake = { + inherit moduleLocation; + }; + }); + example1 = mkFlake { inputs.self = { }; } { @@ -162,6 +178,8 @@ rec { assert packagesNonStrictInDevShells.packages.a.default == pkg "a" "hello"; + assert emptyExposeArgs.moduleLocation == "the self outpath/flake.nix"; + ok; result = runTests "ok"; diff --git a/extras/flakeModules.nix b/extras/flakeModules.nix index 4da738e5..b66ef9c0 100644 --- a/extras/flakeModules.nix +++ b/extras/flakeModules.nix @@ -1,4 +1,4 @@ -{ self, lib, flake-parts-lib, ... }: +{ self, lib, flake-parts-lib, moduleLocation, ... }: let inherit (lib) mapAttrs @@ -13,8 +13,8 @@ let type = types.lazyAttrsOf types.deferredModule; default = { }; apply = mapAttrs (k: v: { - _file = "${toString self.outPath}/flake.nix#flakeModules.${k}"; - key = "${toString self.outPath}/flake.nix#flakeModules.${k}"; + _file = "${toString moduleLocation}#flakeModules.${k}"; + key = "${toString moduleLocation}#flakeModules.${k}"; imports = [ v ]; }); description = '' diff --git a/lib.nix b/lib.nix index deebf702..eaa56799 100644 --- a/lib.nix +++ b/lib.nix @@ -81,7 +81,24 @@ let ${errorExample} '') + , moduleLocation ? "${self.outPath}/flake.nix" }: + let + module = lib.setDefaultModuleLocation errorLocation module; + inputsPos = builtins.unsafeGetAttrPos "inputs" args; + errorLocation = + # Best case: user makes it explicit + args.moduleLocation or ( + # Slightly worse: Nix does not technically commit to unsafeGetAttrPos semantics + if inputsPos != null + then inputsPos.file + # Slightly worse: self may not be valid when an error occurs + else if args?inputs.self.outPath + then args.inputs.self.outPath + "/flake.nix" + # Fallback + else "" + ); + in throwIf (!args?self && !args?inputs) '' When invoking flake-parts, you must pass in the flake output arguments. @@ -100,7 +117,7 @@ let (module: lib.evalModules { specialArgs = { - inherit self flake-parts-lib; + inherit self flake-parts-lib moduleLocation; inputs = args.inputs or /* legacy, warned above */ self.inputs; } // specialArgs; modules = [ ./all-modules.nix module ]; @@ -120,12 +137,7 @@ let mkFlake = args: module: let - loc = - if args?inputs.self.outPath - then args.inputs.self.outPath + "/flake.nix" - else ""; - mod = lib.setDefaultModuleLocation loc module; - eval = flake-parts-lib.evalFlakeModule args mod; + eval = flake-parts-lib.evalFlakeModule args module; in eval.config.flake; diff --git a/modules/nixosModules.nix b/modules/nixosModules.nix index 34e205ad..6803b781 100644 --- a/modules/nixosModules.nix +++ b/modules/nixosModules.nix @@ -1,4 +1,4 @@ -{ self, lib, flake-parts-lib, ... }: +{ self, lib, flake-parts-lib, moduleLocation, ... }: let inherit (lib) mapAttrs @@ -15,7 +15,7 @@ in nixosModules = mkOption { type = types.lazyAttrsOf types.unspecified; default = { }; - apply = mapAttrs (k: v: { _file = "${toString self.outPath}/flake.nix#nixosModules.${k}"; imports = [ v ]; }); + apply = mapAttrs (k: v: { _file = "${toString moduleLocation}#nixosModules.${k}"; imports = [ v ]; }); description = '' NixOS modules.