NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
17.63k stars 13.78k forks source link

References to `pkgs` from `~/.config/nixpkgs/config.nix` produce infinite recursion #246676

Open lilyball opened 1 year ago

lilyball commented 1 year ago

Describe the bug

When using ~/.config/nixpkgs/config.nix to configure nixpkgs, this file may optionally be a function that takes a single { pkgs }: parameter. This is necessary when adding configuration for blocklistedLicenses in order to reference the licenses in question. Example usage:

{ pkgs }:

{
  blocklistedLicenses = with pkgs.lib.licenses; [ agpl3Only agpl3Plus ];
}

As of https://github.com/NixOS/nixpkgs/pull/240433 (darwin.stdenv: Darwin stdenv rework), the reference to pkgs within this file produces an infinite recursion error:

error:
       … <borked>

         at «none»:0: (source not available)

       … while evaluating a branch condition

         at /Users/lily/Dev/Nix/nixpkgs/pkgs/stdenv/booter.nix:99:7:

           98|     thisStage =
           99|       if args.__raw or false
             |       ^
          100|       then args'

       (stack trace truncated; use '--show-trace' to show the full trace)

       error: infinite recursion encountered

       at /Users/lily/.config/nixpkgs/config.nix:4:30:

            3| {
            4|   blocklistedLicenses = with pkgs.lib.licenses; [ agpl3Only agpl3Plus ];
             |                              ^
            5| }

Steps To Reproduce

Steps to reproduce the behavior:

  1. Ensure your nixpkgs contains commit a845397040d1b85cec4ee41edb8598d8086c3d95
  2. Ensure the file ~/.config/nixpkgs/config.nix contains the contents listed above and that there is no NIXPKGS_CONFIG env var (or set NIXPKGS_CONFIG to the path of a file containing the above)
  3. Run something like nix-instantiate --eval --expr 'with import ./. {}; hello.version' (if from a nixpkgs checkout, or import <nixpkgs> if appropriate).

Expected behavior

This should run successfully (in this case, printing the version of the hello package).

Additional context

Merely doing something like blocklistedLicenses = assert builtins.isAttrs pkgs; []; is sufficient to cause this issue. Sticking this in some of the other config values (like allowUnfree) doesn't cause a problem, though I didn't test all of them. Looking at the full stack trace I think it's because it's trying to test the validity of ../../build-support/cc-wrapper during bootstrap, so it's likely just the allowlisted/blocklisted licenses that will end up evaluated here.

I really wish the config here was given { lib } instead, but that's a backwards-incompatible change.

Notify maintainers

Folks involved in the relevant PR: @reckenrode @viraptor @veprbl @emilazy @Atemu @tjni @willcohen @siraben @uri-canva

Metadata

Please run nix-shell -p nix-info --run "nix-info -m" and paste the result.

[user@system:~]$ nix run nixpkgs#nix-info -- -m
 - system: `"x86_64-darwin"`
 - host os: `Darwin 22.6.0, macOS 10.16`
 - multi-user?: `no`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.15.1`
 - channels(lily): `"darwin, home-manager"`
 - channels(root): `""`
 - nixpkgs: `/etc/nix/inputs/nixpkgs`
reckenrode commented 1 year ago

This also appears broken on Linux.

$ nix run nixpkgs#nix-info -- -m
 - system: `"x86_64-linux"`
 - host os: `Linux 6.1.41, NixOS, 22.11 (Raccoon), 22.11.20230703.ea4c80b`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.11.1`
 - nixpkgs: `/etc/nix/inputs/nixpkgs`
$  git rev-list a845397 -n 1
a845397040d1b85cec4ee41edb8598d8086c3d95
$ NIXPKGS_CONFIG=$(pwd)/../bug/config.nix nix-instantiate --eval --expr 'with import ./. {}; hello.version' --show-trace
error: infinite recursion encountered

       at /home/reckenrode/nixpkgs/../bug/config.nix:4:30:

            3| {
            4|   blocklistedLicenses = with pkgs.lib.licenses; [ agpl3Only agpl3Plus ];
             |                              ^
            5| }

       … while evaluating anonymous lambda

       at /home/reckenrode/nixpkgs/pkgs/stdenv/generic/check-meta.nix:41:40:

           40|   hasBlocklistedLicense = assert areLicenseListsValid; attrs:
           41|     hasLicense attrs && lib.lists.any (l: builtins.elem l blocklist) (lib.lists.toList attrs.meta.license);
             |                                        ^
           42|

       … from call site
# etc
reckenrode commented 1 year ago

The nixos-22.11 channel works, so I’m using that as a known good commit to do a git bisect on Linux.

reckenrode commented 1 year ago

git bisect points to d7aad245314ef6b026707bf41c36461d450ef3ab, which is the logic the Darwin stdenv rework copied. 😫

lilyball commented 1 year ago

I'm curious if anyone actually has any use for referencing anything other than lib from pkgs in the config file. In theory we could just hand the config file { pkgs = { inherit lib; }; } and call it a day. Though I'm guessing there's at least one person out there who's actually accessing other things from pkgs for whatever reason (I suppose there's a potential use of e.g. permittedInsecurePackages = [ pkgs.someInsecurePackage.name ]).