How to set nixpkgs.config options in 23.11 while providing an external `pkgs` instance

I’m trying to wrap my head around a change in 23.11 that is not allowing me installing unfree packages. According to the 23.11 release page:

Setting nixpkgs.config options while providing an external pkgs instance will now raise an error instead of silently ignoring the options. NixOS modules no longer set nixpkgs.config to accommodate this. This specifically affects services.locate, services.xserver.displayManager.lightdm.greeters.tiny and programs.firefox NixOS modules. No manual intervention should be required in most cases, however, configurations relying on those modules affecting packages outside the system environment should switch to explicit overlays.

{
  description = "NixOS configurations";
  inputs = {
    nixpkgs.url = "nixpkgs/nixos-23.11";
    nixpkgs-unstable.url = "nixpkgs/master";
    home-manager = {
      url = "github:nix-community/home-manager/release-23.11";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };
  outputs = { self, nixpkgs, nixpkgs-unstable, home-manager, ... }@inputs:
  let
    mkHost = hostName: system:
      nixpkgs.lib.nixosSystem {
        pkgs = import nixpkgs {
          inherit system;
          nixpkgs.config = {
            allowUnfree = true;
          };
        };
        specialArgs = {
          pkgs-unstable = import nixpkgs-unstable {
            inherit system;
            nixpkgs-unstable.config = {
              allowUnfree = true;
            };
          };
         inherit inputs;
        };
        modules = [
          ./modules
          ./shared/configuration.nix
          ./hosts/${hostName}
        ];
      };
  in {
    nixosConfigurations = {
        host = mkHost "host" "x86_64-linux";
    };
  };
}
# shared/configuration.nix
{ pkgs, pkgs-unstable, inputs, ... }:                                                                                   
  let inherit (inputs) self;                                                                                              
  in {            
    nixpkgs.config.allowUnfree = true;
  }

Error:

       error:
       Failed assertions:
       - Your system configures nixpkgs with an externally created instance.
       `nixpkgs.config` options should be passed when creating the instance instead.

       Current value:
       {
         __toString = <function>;
         _type = "option";
         declarationPositions = [
           {
             column = 5;
             file = "/nix/store/zici691hpiy6maxz6zh1hp2if797x28d-source/nixos/modules/misc/nixpkgs.nix";
             line = 148;
           }
         ];
         declarations = [
           "/nix/store/zici691hpiy6maxz6zh1hp2if797x28d-source/nixos/modules/misc/nixpkgs.nix"
         ];
         default = { };
         definitions = [
           {
             allowUnfree = true;
           }
         ];
         definitionsWithLocations = [
           {
             file = "/nix/store/cyhszyf8ysvam29sji4wdrvg6bm806qy-source/shared/configuration.nix";
             value = {
               allowUnfree = true;
             };
           }
         ];
         description = ''
           The configuration of the Nix Packages collection.  (For
           details, see the Nixpkgs documentation.)  It allows you to set
           package configuration options.

           Ignored when `nixpkgs.pkgs` is set.
         '';
         example = {
           _type = "literalExpression";
           text = ''
             { allowBroken = true; allowUnfree = true; }
           '';
         };
         files = [
           "/nix/store/cyhszyf8ysvam29sji4wdrvg6bm806qy-source/shared/configuration.nix"
         ];
         highestPrio = 100;
         isDefined = true;
         loc = [
           "nixpkgs"
           "config"
         ];
         options = [ ];
         type = {
           _type = "option-type";
           check = <function>;
           deprecationMessage = null;
           description = "nixpkgs config";
           descriptionClass = null;
           emptyValue = { };
           functor = {
             binOp = <function>;
             name = "nixpkgs-config";
             payload = null;
             type = null;
             wrapped = null;
           };
           getSubModules = null;
           getSubOptions = <function>;
           merge = <function>;
           name = "nixpkgs-config";
           nestedTypes = { };
           substSubModules = <function>;
           typeMerge = <function>;
         };
         value = {
           allowUnfree = true;
         };
       }

So, my main concern is how do I have to define config options when nixpkgs.pkgs is defined after 23.11?.

You do not really need the NixOS option in your case, you can just pass the config when you create the Nixpkgs instances. You are attempting to do that but it does not work because you are passing the argument incorrectly:

 import nixpkgs {
   inherit system;
-  nixpkgs.config = {
+  config = {
     allowUnfree = true;
   };
 };
 import nixpkgs-unstable {
   inherit system;
-  nixpkgs-unstable.config = {
+  config = {
     allowUnfree = true;
   };
 };

When you import nixpkgs, it imports the default.nix file in the root of Nixpkgs directory, which imports impure.nix:

That file contains a function, to which you are passing the arguments but it expects config attribute, not nixpkgs or nixpkgs-unstable:

Those will just end up swallowed by the ellipsis:

@jtojnar, good catch.

I just tested it. It still toss the same error.

Change:

nixpkgs.lib.nixosSystem {
        pkgs = import nixpkgs {
          inherit system;
          config = {
            allowUnfree = true;
          };
        };
        specialArgs = {
          pkgs-unstable = import nixpkgs-unstable {
            inherit system;
            config = {
              allowUnfree = true;
            };
          };
         inherit inputs;
        };

Did you also remove nixpkgs.config.allowUnfree = true; from your NixOS configuration?

3 Likes