Pinning the Flake Registry for Nix Search

Nix flakes is new packaging system for Nix that provides better separation of concerns and improved reproducibility.
Flakes have lockfile that pins all inputs to a specific version, much like the lockfiles used by other package managers like npm, yarn or cargo.

To learn more about flakes, I recommend this introductory blog post from Tweag.

An annoying aspect of the current implementation is that searching the package registry has some unexpected behaviour. The package registry is not pinned to the version specified in the lockfile by default.

Instead, nix search nixpkgs some-package will try to download the current nixpkgs registry snapshot on each invocation, which leads to very frequent downloads if you are using nixpkgs-unstable, and really degrades the user experience with delays.

Thanks to a friendly tip by balsoft on Matrix I learned that the Nix registry can be pinned to the specific snapshot with a simple configuration change.

With nix.registry.self.flake = inputs.self, we specify self as a pinned reference to a specific version of nixpkgs, which will remain stable and won’t be updated each time you run nix search.

After re-building with nixos-rebuild switch, we can now query the pinned registry with:

nix search self PKGNAME.

(note the self instead of nixpkgs)

Here is a full NixOS config example:

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";

  outputs = { self, nixpkgs, home-manager, nixpkgs-wayland }@inputs:
    let system = "x86_64-linux";
      nixosConfigurations = {
        my-host = nixpkgs.lib.nixosSystem {
          system = system;
          modules = [
            # ... your nix config modules go here
              # Pin registry so `nix search` doesn't download all the time.
              nix.registry.self.flake = inputs.self;