Different ways of "populating" pkgs variable?

should I use pkgs = nixpkgs.legacyPackages.${system}; or import nixpkgs { inherit system; }; in my flakes?

1 Like

Aiui, when you use import nixpkgs { inherit system; }; the evaluation cache won’t capture this properly.

This is because, when nix evaluates nixpkgs.legacyPackages.${system}, that’s an attribute and access to it can be cached. When you call the nixpkgs “function” though, it needs to be evaluated every time, because nix doesn’t memoize function calls.

This doesn’t matter that much if no other flakes depend on yours, but if any do it’s going to cause lots of unnecessary evaluations. You probably also want to actually use the evaluation cache.

Hence import nixpkgs { inherit system; }; is a bit of an antipattern. Use it only if you absolutely must (i.e., usually when you need to apply an overlay without using the NixOS module to do so).

2 Likes

@TLATER is correct, you should prefer calling nixpkgs directly instead of importing it.

This normally doesn’t matter if nobody but you is using the flake, but it’s a good habit to get into.

See also 1000 instances of nixpkgs - #18 by zimbatm for extended discussion on this topic.

2 Likes

It still matters a bit, especially if you use follows. Just instead of 1000 duplicate instances you’ll have 2 or 3.