flake/lib.nixosSystem: add _file-keys for error-location if needed#129924
flake/lib.nixosSystem: add _file-keys for error-location if needed#129924Ma27 merged 1 commit intoNixOS:masterfrom
_file-keys for error-location if needed#129924Conversation
|
Did you try |
|
Good point! Gonna try it out, this should simplify this by a lot! |
|
@roberth thanks a lot, very good point! Updated the PR accordingly! :) |
|
Haven't tested it myself, but I think this should be fine. |
|
I've added a tiny refactor to fix the formatting and add an inline doc. |
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>
|
Squashed this together to have a nicer commit-message and re-checked this. Would merge in a minute as several folks are fine with this in the current state :) |
|
Is there a reason for having this in |
|
I don't recall the details anymore, but I think this was a flake-specific issue. Can you reproduce the overall problem with a non-flake NixOS configuration? If that's the case, please share your code with me and if needed, we should move the change to |
|
The reason I'm asking is that I'm working on a PR to move much of the logic of I didn't run into the overall problem, but it's easy to imagine a situation where it would arise: basically any time Now I'm actually wondering if this could be moved directly to |
Motivation for this change
When inlining a module with a problematic declaration, you usually get
get a not-so helpful error like this:
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 eithertoString pathifpathis e.g. a valuefrom
imports = [ ./foo.nix ]or the file used asNIXOS_CONFIGin<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:
modules = [ ./path.nix ]inlib.nixosSystemdoesn't require anyadditional steps,
./path.nixis used as position for alldeclarations inside
./path.nix.For non-path declarations, the position of
modulesinside theflake.nixwhich declares these modules is determined by doingunsafeGetAttrPoson themodules-argument oflib.nixosSystem.So the
flake.nixfrom above would now raise the followingerror-message:
Things done
sandboxinnix.confon non-NixOS linux)nix-shell -p nixpkgs-review --run "nixpkgs-review wip"./result/bin/)