From e14c24593420bb9057e7f38b40d17137eaeff9dd Mon Sep 17 00:00:00 2001 From: Maximilian Bosch Date: Sun, 11 Jul 2021 01:33:17 +0200 Subject: 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 `': 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 ``. 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 Co-authored-by: Silvan Mosberger Co-authored-by: Robert Hensing --- flake.nix | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'flake.nix') 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"}"; -- cgit 1.4.1