summary refs log tree commit diff
path: root/flake.nix
diff options
context:
space:
mode:
authorMaximilian Bosch <maximilian@mbosch.me>2021-07-11 01:33:17 +0200
committerMaximilian Bosch <maximilian@mbosch.me>2021-07-14 10:12:57 +0200
commite14c24593420bb9057e7f38b40d17137eaeff9dd (patch)
tree320ce633cd97440e9162704f9b1e989d9000b938 /flake.nix
parentaa1561e7801dd25964202e980dfbb3e1d9924d74 (diff)
downloadnixpkgs-e14c24593420bb9057e7f38b40d17137eaeff9dd.tar
nixpkgs-e14c24593420bb9057e7f38b40d17137eaeff9dd.tar.gz
nixpkgs-e14c24593420bb9057e7f38b40d17137eaeff9dd.tar.bz2
nixpkgs-e14c24593420bb9057e7f38b40d17137eaeff9dd.tar.lz
nixpkgs-e14c24593420bb9057e7f38b40d17137eaeff9dd.tar.xz
nixpkgs-e14c24593420bb9057e7f38b40d17137eaeff9dd.tar.zst
nixpkgs-e14c24593420bb9057e7f38b40d17137eaeff9dd.zip
flake/lib.nixosSystem: add `_file`-keys for error-location
When inlining a module with a problematic declaration, you usually get
get a not-so helpful error like this:

    $ cat flake.nix
    {
      description = "A very basic flake";
      inputs.nixpkgs.url = path:../.;
      outputs = { self, nixpkgs }: {
        nixosConfigurations.foo = nixpkgs.lib.nixosSystem {
          system = "x86_64-linux";
          modules = [
            ({ lib, ... }: { services.wrong = 2; })
            { services.nginx.enable = true; }
          ];
        };
      };
    }
    $ nixos-rebuild build --flake .#foo -L
    error: The option `services.wrong' does not exist. Definition values:
           - In `<unknown-file>': 2

While it's certainly possible to guess where this comes from, this is
IMHO fairly confusing for beginners (and kinda reminds me of the
infamous "infinite recursion at undefined position"-error).

The module-system determines the position of a declaration using the
`_file`-key: this is either `toString path` if `path` is e.g. a value
from `imports = [ ./foo.nix ]` or the file used as `NIXOS_CONFIG` in
`<nixpkgs/nixos>`.

However such a mechanism doesn't exist (yet) for inlined flake modules,
so I tried to implement this in a fairly basic way:

* For non-path declarations, the position of `modules` inside the
  `flake.nix` which declares these modules is determined by doing
  `unsafeGetAttrPos` on the `modules`-argument of `lib.nixosSystem`.

  So the `flake.nix` from above would now raise the following
  error-message:

        $ nixos-rebuild build --flake .#foo -L
        error: The option `services.wrong' does not exist. Definition values:
               - In `/nix/store/4vi3nhqjyma73ygs4f93q38qjkhkaxw8-source/flake.nix': 2

Co-authored-by: Cole Helbling <cole.e.helbling@outlook.com>
Co-authored-by: Silvan Mosberger <github@infinisil.com>
Co-authored-by: Robert Hensing <robert@roberthensing.nl>
Diffstat (limited to 'flake.nix')
-rw-r--r--flake.nix14
1 files changed, 13 insertions, 1 deletions
diff --git a/flake.nix b/flake.nix
index 5237cae86f1..92c0d97c4a2 100644
--- a/flake.nix
+++ b/flake.nix
@@ -47,8 +47,20 @@
                       })
                     ];
                   })).config;
+
+                moduleDeclarationFile =
+                  (builtins.unsafeGetAttrPos "modules" args).file;
+
+                # Add the invoking file as error message location for modules
+                # that don't have their own locations; presumably inline modules.
+                addModuleDeclarationFile =
+                  m: {
+                    _file = moduleDeclarationFile;
+                    imports = [ m ];
+                  };
+
               in
-              modules ++ [
+              map addModuleDeclarationFile modules ++ [
                 {
                   system.nixos.versionSuffix =
                     ".${final.substring 0 8 (self.lastModifiedDate or self.lastModified or "19700101")}.${self.shortRev or "dirty"}";