packageOverrides selective unstable without with

I’ve been trying to get packageOverrides to work for selectively pinning packages to unstable, but it only seems to work when using the with pkgs syntax there and when installing packages. It is my understanding that the use of with is discouraged, but I’m not sure how to avoid it here.

{config, pkgs, lib, ...}

{
...
# broken
/*
nixpkgs.config.packageOverrides = pkgs: builtins.attrvalues { inherit (pkgs)
  unstable = import <nixos-unstable> {
    config = config.nixpkgs.config;
  };
};
*/
# works
nixpkgs.config.packageOverrides = pkgs: with pkgs; {
  unstable = import <nixos-unstable> {
    config = config.nixpkgs.config;
  };
};

...

fonts.packages = builtins.attrValues {
  inherit (pkgs.unstable)
    junicode
  ;
  inherit (pkgs)
    noto-fonts
  ;
};

For attempting packageOverrides without with, I’m completely at a loss. I’ve dug into the language documentation trying to figure out what exactly is going on there, and how it can work without with fruitlessly. All I’ve been able to learn is that the attrValues inherit trick is nonsensical to attempt here.

On a side note, is there a cleaner way to do the package setup, or will it necessarily need two inherit statements since inherit (pkgs) unstable.junicode; doesn’t work?

Something else is wrong, your code should work. That said, I don’t recommend using an overlay for this, it just doesn’t make sense; you’re abusing pkgs as a global variable, we don’t need to do that.

We can use the module args - which are actually intended to serve as global module args - to do so instead:

# configuration.nix
{ pkgs, config, ... }:
let
  pkgs-unstable = import <nixos-unstable> {
    inherit (config.nixpkgs) config;
  };
in {
  _module.args = { inherit pkgs-unstable; };

  fonts.packages = builtins.attrValues {
    inherit (pkgs-unstable)
      junicode
    ;
    inherit (pkgs)
      noto-fonts
    ;
}
# other modules
{ pkgs-unstable, ... }: {
  environmenr.systemPackages = builtins.attrValues {
    inherit (pkgs-unstable) hello;
  };
}

If things go wrong, share the errors please.

3 Likes

Please do not use packageOverrides, they are the bad version of overlays.

If you want to use the unstable packages in more places than a single file, you might want to consider using _module.args for this.

_module.args.uPkgs = import <nixos-unstable> {
  config = config.nixpkgs.config;
};

Then you can use it in the module args like you can pkgs.


To talk about your actual issue, it would have been nice to know the actual error, though the immediate problem I see in the commented out version, is that its override function returns a list, rather than a set.

3 Likes

Yes, I do ultimately wand to be able to override what packages are being used on imported files as well. Unfortunately, the suggested methods don’t seem to work.

TLATER error
...

let
  pkgs-unstable = import <nixos-unstable> {
    inherit (config.nixpkgs) config;
  };
in {
  _module.args = { inherit pkgs-unstable };

...

I got the following error

building the system configuration...
error:
       … while evaluating the attribute 'config'
         at /nix/store/mzf1mpn0h1jq6mxypihkb2s5s1i8psxm-source/lib/modules.nix:361:9:
          360|         options = checked options;
          361|         config = checked (removeAttrs config [ "_module" ]);
             |         ^
          362|         _module = checked (config._module);

       … while calling the 'seq' builtin
         at /nix/store/mzf1mpn0h1jq6mxypihkb2s5s1i8psxm-source/lib/modules.nix:361:18:
          360|         options = checked options;
          361|         config = checked (removeAttrs config [ "_module" ]);
             |                  ^
          362|         _module = checked (config._module);

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

       error: syntax error, unexpected '}'
       at /etc/nixos/configuration.nix:14:41:
           13| in {
           14|       _module.args = { inherit pkgs-unstable };
             |                                         ^
           15|   # switch NIX_PATH to use npins
Command 'nix-build '<nixpkgs/nixos>' --attr config.system.build.toplevel --dry-run' returned non-zero exit status 1.
NobbZ error

with

...
  _module.args.uPkgs = import <nixos-unstable> {
    config = config.nixpkgs.config;
  };
...
fonts.packages = builtins.attrValues {
  inherit (uPkgs)
    junicode
  ;
  inherit (pkgs)
    noto-fonts
  ;

I got the following error

building the system configuration...
error:
       … while evaluating the attribute 'config'
         at /nix/store/mzf1mpn0h1jq6mxypihkb2s5s1i8psxm-source/lib/modules.nix:361:9:
          360|         options = checked options;
          361|         config = checked (removeAttrs config [ "_module" ]);
             |         ^
          362|         _module = checked (config._module);

       … while calling the 'seq' builtin
         at /nix/store/mzf1mpn0h1jq6mxypihkb2s5s1i8psxm-source/lib/modules.nix:361:18:
          360|         options = checked options;
          361|         config = checked (removeAttrs config [ "_module" ]);
             |                  ^
          362|         _module = checked (config._module);

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

       error: undefined variable 'uPkgs'
       at /etc/nixos/configuration.nix:203:12:
          202|   fonts.packages = builtins.attrValues {
          203|            inherit (uPkgs)
             |            ^
          204|                        junicode
Command 'nix-build '<nixpkgs/nixos>' --attr config.system.build.toplevel --dry-run' returned non-zero exit status 1.

Also, sorry for not sharing my original error. I’ll put it here for posterity.

Original error
building the system configuration...
error:
       … while evaluating the attribute 'config'
         at /nix/store/mzf1mpn0h1jq6mxypihkb2s5s1i8psxm-source/lib/modules.nix:361:9:
          360|         options = checked options;
          361|         config = checked (removeAttrs config [ "_module" ]);
             |         ^
          362|         _module = checked (config._module);

       … while calling the 'seq' builtin
         at /nix/store/mzf1mpn0h1jq6mxypihkb2s5s1i8psxm-source/lib/modules.nix:361:18:
          360|         options = checked options;
          361|         config = checked (removeAttrs config [ "_module" ]);
             |                  ^
          362|         _module = checked (config._module);

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

       error: syntax error, unexpected '='
       at /etc/nixos/configuration.nix:44:12:
           43|  nixpkgs.config.packageOverrides = pkgs: builtins.attrvalues { inherit (pkgs)
           44|              unstable = import <nixos-unstable> {
             |            ^
           45|                   config = config.nixpkgs.config;
Command 'nix-build '<nixpkgs/nixos>' --attr config.system.build.toplevel --dry-run' returned non-zero exit status 1.

When I get more time, I’ll try to comment out lines of my config to rule out the possibility of these errors originating elsewhere.

I forgot a semicolon, just add it:

-_module.args = { inherit pkgs-unstable };
+_module.args = { inherit pkgs-unstable; };

For @NobbZ variant you’ve not added the module to the args, though I deliberately didn’t because I forget if this edge case leads to infinite recursion.

1 Like

It doesn’t, unless I’ve misunderstood the question you’re asking. Setting _module.args directly and then binding from the arg is a little cleaner than using a let, imo.

1 Like

In that case, the code could be changed to:

# configuration.nix
-{ pkgs, config, ... }:
-let
-  pkgs-unstable = import <nixos-unstable> {
-    inherit (config.nixpkgs) config;
-  };
-in {
-  _module.args = { inherit pkgs-unstable; };
+{
+  pkgs,
+  pkgs-unstable,
+  config,
+  ...
+}: {
+  _module.args.pkgs-unstable = import <nixos-unstable> {
+    inherit (config.nixpkgs) config;
+  };

  fonts.packages = builtins.attrValues {
    inherit (pkgs-unstable)
      junicode
    ;
    inherit (pkgs)
      noto-fonts
    ;
}

… which I agree is clearer, I just didn’t want to accidentally share untested code that’s going to give an obtuse error. Looks like a syntax mistake was bad enough :wink:

1 Like

You have to get it from the module args, like you do with pkgs.

Looking at the snippet again, your inherit in that set is horribly messed up, I think you actually want to remove it.

But then still the list issue remains.

2 Likes

Thank you both very much. Both approaches with those corrections. looking back and forth on the error messages and how to fix them, they make a bit more sense.

Seeing as the overlays method is the wrong tool for the job here anyways, I don’t mind leaving that question unsolved.

1 Like