How to pass specific nixpkgs instance to outputs.nixosConfigurations

outputs = inputs@{self, nixpkgs, ...}: let
        mySystems = [ "x86_64-linux" ];
        pkgsForSystem = nixpkgs.lib.genAttrs mySystems (system:
            import nixpkgs {
                inherit system;
                config = { allowUnfree = true; };
            }
        );
in {
        nixosConfigurations = {
            mymachine = # how to make this use pkgsForSystem.x86_64-linux as pkgs

Whatever nixpkgs you call lib.nixosSystem from, will be instantiated by the module system based on the nixpkgs.* options and passed via _module.args as pkgs.

in other words, i cannot use the nixpkgs instance from my flake in my nixos configuration, because it always creates its own instance.

You can, but you really shouldn’t. It breaks a lot of assumptions.

You can explicitly set nixpkgs.pkgs to your instantiated pkgs value. But please don’t. It’s almost never a good idea.

Take a step back and ask yourself why you want to do this. What actual benefit do you gain? Is it worth making a bunch of little things harder and more brittle? The answer is almost certainly “no.”

Just configure it properly from nixos config. That’s a more flexible and useful mechanism in the first place.

1 Like

If you want to share a nixpkgs instance between multiple hosts because you want to DRY out nixpkgs config or something along those lines, there are better ways to accomplish that, for the record.

Or in other words, this feels like a typical XY problem.

2 Likes