# Why does one validity check pass, and the other fail?

# nix-instantiate --eval --strict weird-meta.nix -A isValid
# { handled = true; valid = true; }

# nix-instantiate --eval --strict weird-meta.nix -A out
# error: Package ‘nvidia-settings-440.59’ in /nix/store/lcr3p8f33ksljpzmdk2x06m0ak95bah4-source/pkgs/os-specific/linux/nvidia-x11/settings.nix:102 has an unfree license (‘unfreeRedistributable’), refusing to evaluate.
#
# a) For `nixos-rebuild` you can set
#   { nixpkgs.config.allowUnfree = true; }
# in configuration.nix to override this.
# 
# b) For `nix-env`, `nix-build`, `nix-shell` or any other Nix command you can add
#   { allowUnfree = true; }
# to ~/.config/nixpkgs/config.nix.
# 
# (use '--show-trace' to show detailed location information)


let
  nixpkgs = builtins.fetchTarball "channel:nixos-unstable";
  pkgs = import nixpkgs {
    overlays = [];
    config.allowUnfreePredicate = p:
      let info = builtins.parseDrvName (p.name or "unnamed");
          allowed = builtins.elem info.name [
            "nvidia-settings"
            "nvidia-x11"
          ];
      # in  builtins.trace info true;
      in  builtins.trace info allowed;
  };

  checkMeta = import (pkgs.path + /pkgs/stdenv/generic/check-meta.nix) {
    inherit (pkgs) lib config;
    inherit (pkgs.stdenv) hostPlatform;
  };

  p = pkgs.linuxPackages.nvidia_x11.settings;
in {
  inherit p;
  info = builtins.parseDrvName p.name;

  passesPredicate = pkgs.config.allowUnfreePredicate p;
  isValid = checkMeta {
    attrs = p;
    meta = p.meta;
  };
  out = p.outPath;
}