flake.nix: Only add `_file`-key if position of `args.modules` is actually known to the evaluator

This happens if the evaluator "loses" the position of an
attr-declaration[1] because of e.g. too many nested function-calls to
build the final attr-set.

While the actual issue should be fixed in Nix itself, this is IMHO a
fair workaround to unblock affected users[2].

[1] e14c245934 (commitcomment-53645936)
[2] It seems as everyone using `divnix/digga` or `flake-utils-plus`
    are affected:
    * https://github.com/divnix/digga/issues/87
Maximilian Bosch 2 years ago
parent b3d80503cf
commit 6797b92f8d
No known key found for this signature in database
GPG Key ID: 091DBF4D1FC46B8E

@ -49,12 +49,18 @@
moduleDeclarationFile =
(builtins.unsafeGetAttrPos "modules" args).file;
# Even though `modules` is a mandatory argument for `nixosSystem`, it doesn't
# mean that the evaluator always keeps track of its position. If there
# are too many levels of indirection, the position gets lost at some point.
intermediatePos = builtins.unsafeGetAttrPos "modules" args;
if intermediatePos == null then null else intermediatePos.file;
# Add the invoking file as error message location for modules
# that don't have their own locations; presumably inline modules.
addModuleDeclarationFile =
m: {
m: if moduleDeclarationFile == null then m else {
_file = moduleDeclarationFile;
imports = [ m ];