Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/modules: better error message if an attr-set of options is expected #115919

Merged
merged 1 commit into from
Mar 25, 2021

Commits on Mar 11, 2021

  1. lib/modules: better error message if an attr-set of options is expected

    I recently wrote some Nix code where I wrongly set a value to an option
    which wasn't an actual option, but an attr-set of options. The mistake I
    made can be demonstrated with an expression like this:
    
        {
          foo = { lib, pkgs, config, ... }: with lib; {
            options.foo.bar.baz = mkOption {
              type = types.str;
            };
            config.foo.bar = 23;
          };
        }
    
    While it wasn't too hard to find the cause of the mistake for me, it was
    necessary to have some practice in reading stack traces from the module
    system since the eval-error I got was not very helpful:
    
        error: --- TypeError --------------------------------------------------------- nix-build
        at: (323:25) in file: /nix/store/3nm31brdz95pj8gch5gms6xwqh0xx55c-source/lib/modules.nix
    
           322|         foldl' (acc: module:
           323|                 acc // (mapAttrs (n: v:
              |                         ^
           324|                                    (acc.${n} or []) ++ f module v
    
        value is an integer while a set was expected
        (use '--show-trace' to show detailed location information)
    
    I figured that such an error can be fairly confusing for someone who's
    new to NixOS, so I decided to catch this case in th `byName` function in
    `lib/modules.nix` by checking if the value to map through is an actual
    attr-set. If not, a different error will be thrown.
    Ma27 committed Mar 11, 2021
    Configuration menu
    Copy the full SHA
    e878fc4 View commit details
    Browse the repository at this point in the history