summary refs log tree commit diff
diff options
context:
space:
mode:
authorRobert Hensing <roberth@users.noreply.github.com>2022-02-06 22:49:17 +0100
committerGitHub <noreply@github.com>2022-02-06 22:49:17 +0100
commit8403e02a5d2ed920b80f8171d048af53e4c84bcb (patch)
tree966368cb63f142a92a97dff84e3002e1abf67bad
parentab253b13327c7d7dfc2daaa7a0e4e13441cc819d (diff)
parent5ab62e17b51087d3ee38684c3911354ec70f5f0b (diff)
downloadnixpkgs-8403e02a5d2ed920b80f8171d048af53e4c84bcb.tar
nixpkgs-8403e02a5d2ed920b80f8171d048af53e4c84bcb.tar.gz
nixpkgs-8403e02a5d2ed920b80f8171d048af53e4c84bcb.tar.bz2
nixpkgs-8403e02a5d2ed920b80f8171d048af53e4c84bcb.tar.lz
nixpkgs-8403e02a5d2ed920b80f8171d048af53e4c84bcb.tar.xz
nixpkgs-8403e02a5d2ed920b80f8171d048af53e4c84bcb.tar.zst
nixpkgs-8403e02a5d2ed920b80f8171d048af53e4c84bcb.zip
Merge pull request #126769 from ncfavier/nixosSystem-lib
nixos: move default module location logic to `eval-config.nix`
-rw-r--r--flake.nix34
-rw-r--r--lib/default.nix4
-rw-r--r--lib/modules.nix12
-rw-r--r--nixos/lib/eval-config.nix14
4 files changed, 28 insertions, 36 deletions
diff --git a/flake.nix b/flake.nix
index 29dffa9fa4e..c48d6c68e58 100644
--- a/flake.nix
+++ b/flake.nix
@@ -21,35 +21,13 @@
 
         nixos = import ./nixos/lib { lib = final; };
 
-        nixosSystem = { modules, ... } @ args:
+        nixosSystem = args:
           import ./nixos/lib/eval-config.nix (args // {
-            modules =
-              let
-                moduleDeclarationFile =
-                  let
-                    # 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;
-                  in
-                    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: if moduleDeclarationFile == null then m else {
-                    _file = moduleDeclarationFile;
-                    imports = [ m ];
-                  };
-
-              in
-              map addModuleDeclarationFile modules ++ [
-                {
-                  system.nixos.versionSuffix =
-                    ".${final.substring 0 8 (self.lastModifiedDate or self.lastModified or "19700101")}.${self.shortRev or "dirty"}";
-                  system.nixos.revision = final.mkIf (self ? rev) self.rev;
-                }
-              ];
+            modules = args.modules ++ [ {
+              system.nixos.versionSuffix =
+                ".${final.substring 0 8 (self.lastModifiedDate or self.lastModified or "19700101")}.${self.shortRev or "dirty"}";
+              system.nixos.revision = final.mkIf (self ? rev) self.rev;
+            } ];
           });
       });
 
diff --git a/lib/default.nix b/lib/default.nix
index 26842253880..3e43733ad20 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -111,8 +111,8 @@ let
       cleanSource sourceByRegex sourceFilesBySuffices
       commitIdFromGitRepo cleanSourceWith pathHasContext
       canCleanSource pathIsRegularFile pathIsGitRepo;
-    inherit (self.modules) evalModules unifyModuleSyntax
-      applyIfFunction mergeModules
+    inherit (self.modules) evalModules setDefaultModuleLocation
+      unifyModuleSyntax applyIfFunction mergeModules
       mergeModules' mergeOptionDecls evalOptionValue mergeDefinitions
       pushDownProperties dischargeProperties filterOverrides
       sortProperties fixupOptionType mkIf mkAssert mkMerge mkOverride
diff --git a/lib/modules.nix b/lib/modules.nix
index c68bbfcaa3e..f1125aca0a3 100644
--- a/lib/modules.nix
+++ b/lib/modules.nix
@@ -334,6 +334,10 @@ rec {
     in modulesPath: initialModules: args:
       filterModules modulesPath (collectStructuredModules unknownModule "" initialModules args);
 
+  /* Wrap a module with a default location for reporting errors. */
+  setDefaultModuleLocation = file: m:
+    { _file = file; imports = [ m ]; };
+
   /* Massage a module into canonical form, that is, a set consisting
      of ‘options’, ‘config’ and ‘imports’ attributes. */
   unifyModuleSyntax = file: key: m:
@@ -534,11 +538,9 @@ rec {
      correspond to the definition of 'loc' in 'opt.file'. */
   mergeOptionDecls =
    let
-    packSubmodule = file: m:
-      { _file = file; imports = [ m ]; };
     coerceOption = file: opt:
-      if isFunction opt then packSubmodule file opt
-      else packSubmodule file { options = opt; };
+      if isFunction opt then setDefaultModuleLocation file opt
+      else setDefaultModuleLocation file { options = opt; };
    in loc: opts:
     foldl' (res: opt:
       let t  = res.type;
@@ -568,7 +570,7 @@ rec {
 
           getSubModules = opt.options.type.getSubModules or null;
           submodules =
-            if getSubModules != null then map (packSubmodule opt._file) getSubModules ++ res.options
+            if getSubModules != null then map (setDefaultModuleLocation opt._file) getSubModules ++ res.options
             else if opt.options ? options then map (coerceOption opt._file) options' ++ res.options
             else res.options;
         in opt.options // res //
diff --git a/nixos/lib/eval-config.nix b/nixos/lib/eval-config.nix
index e3eb88a60eb..2daaa8a1186 100644
--- a/nixos/lib/eval-config.nix
+++ b/nixos/lib/eval-config.nix
@@ -21,6 +21,7 @@ evalConfigArgs@
 , # !!! See comment about args in lib/modules.nix
   specialArgs ? {}
 , modules
+, modulesLocation ? (builtins.unsafeGetAttrPos "modules" evalConfigArgs).file or null
 , # !!! See comment about check in lib/modules.nix
   check ? true
 , prefix ? []
@@ -74,7 +75,18 @@ let
         _module.check = lib.mkDefault check;
       };
     };
-  allUserModules = modules ++ legacyModules;
+
+  allUserModules =
+    let
+      # Add the invoking file (or specified modulesLocation) as error message location
+      # for modules that don't have their own locations; presumably inline modules.
+      locatedModules =
+        if modulesLocation == null then
+          modules
+        else
+          map (lib.setDefaultModuleLocation modulesLocation) modules;
+    in
+      locatedModules ++ legacyModules;
 
   noUserModules = evalModulesMinimal ({
     inherit prefix specialArgs;