nix-pkgset icon indicating copy to clipboard operation
nix-pkgset copied to clipboard

Intended way to instantiate a flake's package set that uses nix-pkgset without resulting in an extra nixpkgs import/eval

Open LunNova opened this issue 8 months ago • 1 comments

I've been trying out nix-pkgset to add cross support to a flake that previously exposed a bare attrset of packages. I think I have it working but am unsure about best practice.

Example of downstream usage which I'm not sure is optimal:
https://github.com/LunNova/luvit-nix/blob/lunnova/cross-support/demo.nix

There are two ways of instantiating the set against an arbitrary existing pkgs instance in the demo file, one uses .override on the legacyPackages output to set pkgs to a new pkgs, which is probably causing an extra eval, and one relies on a non-standard flake attr to provide a function to instantiate it.

LunNova avatar Mar 27 '25 19:03 LunNova

If you wanna avoid re-initializing nixpkgs across flakes, you can also call makePackageSet with nixpkgs.legacyPackages.${system}, or for cross nixpkgs.legacyPackages.${system}.pkgsCross.${platform}(see https://github.com/NixOS/nixpkgs/blob/master/lib/systems/examples.nix for a list of all cross platforms).

As for best practices for exposing cross packages through flakes, I don't really have a strong opinion on this and I intentionally reduced the scope of what this library is providing. Personally I've been doing something equivalent to this:

# legacyPackages.<system> =
(makePackageSet "foo" pkgs.newScope f) // {
  cross.aarch64-linux = makePackageSet "foo" pkgs.pkgsCross.aarch64-multiplatform.newScope f;
  cross.x86_64-linux = makePackageSet "foo" pkgs.pkgsCross.gnu64.newScope f;
}

You can then:

nix build .#hello
nix build .#cross.aarch64-linux.hello
nix build .#cross.x86_64-linux.hello

I think either way it's also a good idea to also provide a package set building function if anyone wants to re-initialise it, or they don't use flakes.

lib.makeMyPkgset = pkgs: makePackageSet "foo" pkgs.newScope f;

I prefer just providing a regular function, over relying on callPackage's override.

szlend avatar Mar 28 '25 21:03 szlend