What is the default nixpkgs source for the nix-shell?

To clarify my question, I immediately switched to flakes after installing nixos.

Here is the output of the nix-channel command:

$ nix-channel --list
$ sudo nix-channel --list
nixos https://nixos.org/channels/nixos-24.05

In my flake.nix file i use nixos-24.05 and nixos-unstable. I realized that the nixos-24.05 is still getting changes when i ran nix flake update

So the 24.05 version in the channels might not be same commit with the 24.05 in my flake.

My question is that which of these the nix-shell command use by default when i run this in a random directory:

$ nix-shell -p fortune

It uses channels, or more accurately, the nixpkgs entry in NIX_PATH, which should point at your user’s nixpkgs channel or root’s nixos channel (by default). You can correct your NIX_PATH to avoid this issue:

  1. Delete all channels.
  2. Configure your system’s NIX_PATH using the nix.nixPath setting to point nixpkgs at the desired nixpkgs instance.
1 Like

I didn’t know that i could safely remove that one root channel.

by the way this is the contents of NIX_PATH variable:

$ echo $NIX_PATH
/home/osbm/.nix-defexpr/channels nixpkgs=flake nixpkgs /nix/var/nix/profiles/per-user/root/channels

I dont know what they all mean but it says “nixpkgs=flake”. So it must be good :smiley:

  1. Configure your system’s NIX_PATH using the nix.nixPath setting to point nixpkgs at the desired nixpkgs instance.

How does one do this with flakes?

Flakes do not change the set of available NixOS options for you. Something like nix.nixPath = [ "nixpkgs=${pkgs.path}" ]; should do. It is exactly what I use (albeit in a flakeless setup).

1 Like

It seems like it’s no longer necessary to set it explicitly with recent nixpkgs revisions when using flakes. So you can skip step 2 as it’s already done for you.

2 Likes

nixpkgs=flake:nixpkgs means it’ll use the flake registry to get nixpkgs. And when building a NixOS system with flakes, by default your /etc/nix/registry.json file will point the nixpkgs flake to the copy of nixpkgs that your system was built with.

2 Likes

I found this mentioned in the release notes. Can we mention this in the documentation?

I am thinking of opening a PR in the nix.dev website.

1 Like

Interesting, I had missed this change too. Cool.

I have been using the following to do this:

  nix = {
    # ..
    registry = lib.mapAttrs (_: value: { flake = value; }) inputs;
    nixPath = lib.mapAttrsToList (key: value: "${key}=${value.to.path}") config.nix.registry;
  };

The difference being that this creates an entry for every input, rather than just nixpkgs.

Realistically, though, the only one I ever use is nixpkgs, via nix shell or nix run, and even those most often via comma. So I can probably just remove it.

It does also create an entry for self, which I haven’t used but I can imagine using maybe in some particular circumstances, including some kind of disaster recovery. I have nixos-version with the git revisions of the flake, but this copy includes any dirty changes that the particular revision may have been built with.

1 Like