`nix run` refuses to evaluate unfree packages even though `allowUnfree = true;` in my config

So I just tried

> nix run nixpkgs#vscode
error: Package ‘vscode-1.57.0’ in /nix/store/z9jsqkhnq7cwq7szvkx0yyp5fy8z8czv-source/pkgs/applications/editors/vscode/vscode.nix:43 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

       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) [
             "vscode"
           ];
         }

       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)

but I already have ~/.config/nixpkgs/config.nix:

{ allowUnfree = true; }

Ok, well then I tried:

> NIXPKGS_ALLOW_UNFREE=1 nix run nixpkgs#vscode
error: Package ‘vscode-1.57.0’ in /nix/store/z9jsqkhnq7cwq7szvkx0yyp5fy8z8czv-source/pkgs/applications/editors/vscode/vscode.nix:43 has an unfree license (‘unfree’), refusing to evaluate.
...

How do I evaluate unfree derivations with nix run?

I’m running the nixUnstable package:

> nix --version         
nix (Nix) 2.4pre20210601_5985b8b
1 Like

Nix evaluates purely in flakes mode, so changing the local config or setting an environment variable is not going to affect things (thankfully).

Instead, you must explicit take a nixpkgs, configure it to be unfree, expose that as an output attribute, and then use it.

My config is rather complex, but when I do nix run github:colemickens/nixcfg#pkgs.x86_64-linux.vscode, it does more or less what I hoped.

This is because pkgs is actually an attribute set mapped over an import of nixpkgs with config.allowUnfree = true set.

We should probably have a minimal example of this, for this exact use-case; it comes up a lot.

1 Like

Hmm, I’m not sure I fully understand but it at least sounds like the nix run error message is outdated then?

I don’t think so, no. The output you reported is what I would expect.

In the command I gave, I am using my personal nix config repo and referring to a specially-configured output attribute. My nixcfg repo specifically configures pkgs to be a re-export of nixpkgs with allowUnfree set to true. If you trace through my config (and my very personal helper functions) you see that it boils down to roughly pkgs.x86_64-linux = import inputs.nixpkgs { system = "x86_64-linux"; allowUnfree = true; }. One cannot escape the explicit-ness!

EDIT: Oh! Re-reading your post, one could indeed argue that the error message is out-of-date. Those tips aren’t very useful for you at all!!

1 Like

For one-offs you can work a bit around flakes purity mode by doing this:

$ NIXPKGS_ALLOW_UNFREE=1 nix run --impure nixpkgs#vscode
2 Likes

Related–a PR I had opened when I ran into this: Add a fourth hint for `nix run` by waxlamp · Pull Request #117842 · NixOS/nixpkgs · GitHub.

1 Like

Another option is to add --impure, which will then read your user’s config (but also open the door on local overlays and other impurities as well).

2 Likes