Misleading error from `specialArgs = { pkgs = myPkgs }`

I get this error when I pkgs = import nixpkgs { overlays = [ /* .. */ ]; };' and then specialArgs = { inherit pkgs; /* … */ }`

evaluation warning: You have set specialArgs.pkgs, which means that options like nixpkgs.config
                    and nixpkgs.overlays will be ignored. If you wish to reuse an already created
                    pkgs, which you know is configured correctly for this NixOS configuration,
                    please import the `nixosModules.readOnlyPkgs` module from the nixpkgs flake or
                    `(modulesPath + "/misc/nixpkgs/read-only.nix"), and set `{ nixpkgs.pkgs = <your pkgs>; }`.
                    This properly disables the ignored options to prevent future surprises.

I tried every iteration of things suggested in the error message, and noticed that the error remains in all cases where I continue to provide the pkgs argument. However, once I remove the pkgs argument to specialArgs, I no longer error, even if I am passing readOnlyPkgs module to nixpkgs.lib.nixosSystem.modules

Whats the right way to pass a pre-instantiated nixpkgs to nixos? Because I am skill issuing hard.

Even if your using the readOnlyPkgs module you should not be adding pkgs to specialArgs.

You should just add a module to your module list or your lib.nixosSystem. Rather then providing it via specialArgs.

e.g.

lib.nixosSystem = {
  modules = [
    { nixpkgs.pkgs = pkgs; }
  ];
};
1 Like

As isabelroses already said, using specialArgs or _module.args to inject pkgs is plain wrong.

Still, it is even more idiomatic to also not use nixpkgs.pkgs, but instead just use nixpkgs.config, nixpkgs.overlays, etc to configure the pkgs as passed to the module system, that makes it much more flexible, and you are working with the module system than, rather than against it.

1 Like