3
0
Fork 0
forked from mirrors/nixpkgs
nixpkgs/lib/tests
Maximilian Bosch e878fc4aac
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.
2021-03-11 14:55:56 +01:00
..
modules lib/tests: More functionTo tests 2021-01-27 00:17:56 +01:00
check-eval.nix
maintainers.nix lib/tests: Allow overriding pkgs independent of lib being tested 2020-04-25 23:40:20 +02:00
misc.nix lib/attrsets: add cartesianProductOfSets function 2021-01-28 23:08:59 +01:00
modules.sh lib/modules: better error message if an attr-set of options is expected 2021-03-11 14:55:56 +01:00
release.nix Don't set $NIX_DB_DIR 2020-08-24 18:50:20 +02:00
systems.nix lib.systems: add powerpc64-linux 2021-01-30 12:34:24 -08:00