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 ;-)