`nix-shell -p [anything]` broken

Something has broken my NixOS setup, and now I can no longer use nix-shell:

❯ nix-shell -p bc
error:
       … while calling the 'import' builtin
         at «string»:1:18:
            1| {...}@args: with import <nixpkgs> args; (pkgs.runCommandCC or pkgs.runCommand) "shell" { buildInputs = [ (bc) ]; } ""
             |                  ^

       error: opening file '/nix/store/04g5j6d9za4pgjcyb9p57j87ywmqfa9g-source/default.nix': No such file or directory

/nix/store/04g5j6d9za4pgjcyb9p57j87ywmqfa9g-source contains a copy of the files in the repository I run deploy --hostname 127.0.0.1 .#$(hostname) in, so maybe it’s an issue with deploy-rs?

What I’ve tried so far:

  1. Re-deploying the latest configuration and rebooting. No change.

Seems like your nixpkgs entry in NIX_PATH is broken.

2 Likes
❯ echo $NIX_PATH
nixpkgs=flake:nixpkgs:/nix/var/nix/profiles/per-user/root/channels

How would I fix it?

It’s indirecting through the registry… What’s nixpkgs point to in your registry?

Not sure what you mean by that question, but:

❯ readlink --canonicalize /nix/var/nix/profiles/per-user/root/channels
/nix/store/yv7yrawqphhyxffsqg9br15m1ragfp9p-user-environment
❯ ls -lA /nix/store/yv7yrawqphhyxffsqg9br15m1ragfp9p-user-environment
total 16
lrwxrwxrwx 1 root root 69 Jan  1  1970 home-manager -> /nix/store/2wz7hgwbbrbcf5mgyx5b8fdr97rs07jp-home-manager/home-manager
lrwxrwxrwx 1 root root 60 Jan  1  1970 manifest.nix -> /nix/store/s9yza4axnl73z52czn558aq46c8dmjgc-env-manifest.nix
lrwxrwxrwx 1 root root 55 Jan  1  1970 nixos -> /nix/store/my1yjdazlxpbnzazzrpmzqf8gza63zlb-nixos/nixos
lrwxrwxrwx 1 root root 73 Jan  1  1970 nixos-hardware -> /nix/store/jr10wlr5lnimzsa1lrghsziy2bnwi155-nixos-hardware/nixos-hardware
lrwxrwxrwx 1 root root 75 Jan  1  1970 nixos-stable -> /nix/store/vqijzr6z8ccswl3vsmdac9kj1nj4pw7c-nixos-stable-24.05/nixos-stable

I mean what does nix registry list show for flake:nixpkgs?

❯ nix registry list | grep flake:nixpkgs
system flake:nixpkgs path:/nix/store/04g5j6d9za4pgjcyb9p57j87ywmqfa9g-source
global flake:nixpkgs github:NixOS/nixpkgs/nixpkgs-unstable

I don’t know where this value comes from, or how to correct it, though.

And you said /nix/store/04g5j6d9za4pgjcyb9p57j87ywmqfa9g-source is your config flake, not a copy of nixpkgs, correct? You must have set this wrong somewhere. Look for nix.registry settings in your nixos config that might be wrong.

I don’t have that setting anywhere in my NixOS config.

Is /etc/nix/registry.json a symlink to the nix store /etc/static/nix/registry.json or a normal file?

❯ ls -l /etc/nix/registry.json
lrwxrwxrwx 1 root root 29 Jul 26 12:07 /etc/nix/registry.json -> /etc/static/nix/registry.json
❯ cat /etc/static/nix/registry.json
{"flakes":[{"exact":true,"from":{"id":"nixpkgs","type":"indirect"},"to":{"path":"/nix/store/04g5j6d9za4pgjcyb9p57j87ywmqfa9g-source","type":"path"}}],"version":2}

If it’s a symlink, then the registry options are being set somehow. If it’s not directly in your config, it’s indirect through some other module.

Looks like it:

❯ nix repl ".#nixosConfigurations.\"$(hostname)\""
Nix 2.28.4
Type :? for help.
Loading installable 'git+file:///home/victor/nixos#nixosConfigurations."strong"'...
Added 10 variables.
nix-repl> :p config.nix.registry
{
  nixpkgs = {
    exact = true;
    flake = null;
    from = {
      id = "nixpkgs";
      type = "indirect";
    };
    to = {
      path = "/nix/store/04g5j6d9za4pgjcyb9p57j87ywmqfa9g-source";
      type = "path";
    };
  };
}

It is the default now for a flake config to set a registry entry for nixpkgs… so if the nixpkgs.flake.source option is somehow set incorrectly, that could be the cause.

nixpkgs.flake.source is set correctly when you use nixpkgs.lib.nixosSystem, but I’m not sure how deploy-rs deals with that.

You can check options.nixpkgs.flake.source.definitionsWithLocations to get some idea what’s setting it.

Oh, it’s probably related to my attempt to apply nixpkgs PR patches! Four different packages I use are broken right now (IDEA, Renovate, Darktable, and Mcomix), and I really wanted to not get stuck until 25.11 is released. So I tried a few things (there doesn’t seem to be an idiomatic way to do this yet) and ended up with this (trimmed for legibility):

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

  outputs =
    { nixpkgs }:
    let
      system = "x86_64-linux";
      pkgs = import nixpkgs { inherit system; };
    in
    {
      nixosConfigurations =
        let
          nixpkgsWithPatches = pkgs.applyPatches {
            name = "nixpkgs-patched";
            src = nixpkgs;
            patches = [
            ];
          };
          patchedNixpkgs = (import "${nixpkgsWithPatches}/flake.nix").outputs {
            inherit self;
          };
        in
        {
          foo = patchedNixpkgs.lib.nixosSystem { … };
        };
    };
}

Yeah, you passed nixpkgs’ outpus function a self attribute pointing to your flake, not nixpkgs. No wonder it’s confused.

2 Likes

Thanks for the help! Hopefully we’ll have a way to patch nixpkgs in flake setups soon :slight_smile:.

2 Likes

I think you should be able to use builtins.getFlake "path:${nixpkgsWithPatches}" to get what you want more cleanly.

Do you mean patchedNixpkgs = builtins.getFlake "path:${nixpkgsWithPatches}";? If I do that then nix flake check complains:

error: the string 'path:/nix/store/bgl6ldj5ihbwcq8p42z3a0qzgqafgk2b-source' is not allowed to refer to a store path (such as 'bgl6ldj5ihbwcq8p42z3a0qzgqafgk2b-source')

Update: I could’ve sworn I tried this originally, but it works:

      nixosConfigurations =
        let
          patchedNixpkgs = pkgs.applyPatches {
            name = "nixpkgs-patched";
            src = nixpkgs;
            patches = [
            ];
          };
        in
        {
          foo = patchedNixpkgs.lib.nixosSystem {
            …
          };
        };

Update: Nope, this breaks as soon as I actually add anything to patches.

Update 2: I was able to use this to get to nixosSystem = import (nixpkgsWithPatches + "/nixos/lib/eval-config.nix");, which gets me the one thing I need from the patched nixpkgs. Hopefully this doesn’t have another weird side effect.

Why not use overlays and overriding? Nixpkgs Reference Manual

E.g. nixos/flake.nix at aa3988f9047ff17a7ae27d58694ec180253cb7bb · RyanGibb/nixos · GitHub