The manual suggests allowing packages by name:

  allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [

Is it possible to instead do this with packages directly? I tried the following:

  allowUnfreePredicate = pkg:
    builtins.elem pkg [

That doesn't seem to work:

error: Package ‘hplip-3.22.6’ in /nix/store/lh7i6yw570nkw49byrgjbrna9az3ryy7-nixos-22.05/nixos/pkgs/misc/drivers/hplip/default.nix:268 has an unfree license (‘unfree’), refusing to evaluate.

Is the pkg object not the same as the entries in pkgs, does builtins.elem not treat complex objects as equal, or is something else going on?

2 Answers 2


The reason why your code fails is not because of builtins.elem (this works fine to compare strictly equivalent packages) but because of the fact that allowInfreePredicate is called on the attributes of the package (the inside stdenv.mkDerivation {…}), and of course stdenv.mkDerivation {…} != ….

How can you check/find it yourself:

As a side remark, you can actually see if by yourself (I was not even aware of this before your question) using this simple trick to browse the great nixpkgs repository (this is for educational purpose, it took me time to discover how one could go through the nix code efficiently… and it's really awesome to be able to read easily the code of a whole OS using a few commands! That's part of what I love with Nix!):

To that end first get nixpkgs's code. Advice: download it with

git clone https://github.com/NixOS/nixpkgs

in your home folder, it's quite practical to have it next to you!

Then, using rg allowUnfreePredicate -C 5 (rg is a better grep that you should really install, -C 5 displays 5 lines above/below, it's quite practical to see the surrounding text). You will directly see that config.allowUnfreePredicate is called in nixpkgs/pkgs/stdenv/generic/check-meta.nix (here) to define a new function allowUnfreePredicate, itself called here in hasDeniedUnfreeLicense, itself called into checkValidity (same file) itself called into assertValidity (same file)… (yeah, it's a simple game where you just rg the name of the current function to find the next one ^^). This function is finally outputted from the file check-meta.nix. Using our friend rg check-meta.nix again it is not too hard to see that this file is imported from pkgs/stdenv/generic/make-derivation.nix as checkMeta. Finally (admitingly this is quite a long journey, it's often quicker ^^') this function is called here with a parameter args that turns out to be the input of the mkDerivation function (here). Done ;-)

  • Upvoted for explaining why my attempt failed. The community found a solution to do the equivalent, so I'll be marking that as accepted.
    – l0b0
    Oct 18, 2022 at 20:16

This admittedly indirect solution from TLATER works:

  allowUnfreePredicate = pkg:
    builtins.elem (
      lib.getName pkg
    ) (
      map lib.getName [
  • Doesn’t work in a flake’s inputs, because lib isn’t in scope.
    – Diti
    Nov 23, 2023 at 23:02
  • At worst that means you have to replace lib. with pkgs.lib. in the above.
    – l0b0
    Nov 23, 2023 at 23:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .