Cannot build unfree program even if NIXPKGS_ALLOW_UNFREE=1

❯ NIXPKGS_ALLOW_UNFREE=1 sudo nixos-rebuild switch
building Nix...
building the system configuration...
error: Package ‘netease-cloud-music-1.2.1’ in /nix/store/03g9bk2dv77ds6xd7vlpacmvyaj6fvxs-source/pkgs/netease-cloud-music/default.nix:440 has an unfree license (‘unfree’), refusing to evaluate.

       a) To temporarily allow unfree packages, you can use an environment variable
          for a single invocation of the nix tools.

            $ export NIXPKGS_ALLOW_UNFREE=1

        Note: For `nix shell`, `nix build`, `nix develop` or any other Nix 2.4+
        (Flake) command, `--impure` must be passed in order to read this
        environment variable.

       b) For `nixos-rebuild` you can set
         { nixpkgs.config.allowUnfree = true; }
       in configuration.nix to override this.

       Alternatively you can configure a predicate to allow specific packages:
         { nixpkgs.config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [
             "netease-cloud-music"
           ];
         }

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

The package I want to build is here

What something wrong I do?

When you are using flakes, the evaluation is pure by default, and nix wouldn’t be able to access the NIXPKGS_ALLOW_UNFREE variable from your environment. Since this looks like your NixOS config, you can set nixpkgs.config.allowUnfree = true; in your config, or alternatively add the --impure flag.

Beside figsodas comment, please be aware that the variable NIXPKGS_ALLOW_UNFREE is not passed into the sudo environment, if you want to define that the way you describe sudo has to come first.

❯ NIXPKGS_ALLOW_UNFREE="1"  sudo printenv NIXPKGS_ALLOW_UNFREE

vs

❯ sudo NIXPKGS_ALLOW_UNFREE="1" printenv NIXPKGS_ALLOW_UNFREE
1