Nix flakes /nix/store/***-source no such file or directory

A problem I keep running into with flakes is that by builds fail after running nix-collected-garbage -d. Here’s an example:

nix build .#whatever
warning: Git tree '/home/emmanuel/projects/erosanix' is dirty
error (ignored): error: end of string reached
error: getting status of '/nix/store/cwihclkh4g6wghh67nq9lyswfjh650im-source': No such file or directory
(use '--show-trace' to show detailed location information)

The path matches the nixpkgs pinned in my flakes.lock:

{
  "nodes": {
    "nixpkgs": {
      "locked": {
        "lastModified": 1642770210,
        "narHash": "sha256-2kMgAMy3ThQP5UvHkV/z2zMkoml9bQhatEzCpLq5Iwc=",
        "path": "/nix/store/cwihclkh4g6wghh67nq9lyswfjh650im-source",
        "rev": "0bef0d60524fcdb2b609625e34d5c224e150c17a",
        "type": "path"
      },
      "original": {
        "id": "nixpkgs",
        "type": "indirect"
      }
    },
    "root": {
      "inputs": {
        "nixpkgs": "nixpkgs"
      }
    }
  },
  "root": "root",
  "version": 7
}

I suspect this happens because I use “follows” in order to avoid using a bunch of different versions of Nixpkgs:

inputs.nixpkgs.url = "github:NixOS/nixpkgs/master";
inputs.agenix.url = "github:ryantm/agenix";
inputs.agenix.inputs.nixpkgs.follows = "nixpkgs";
inputs.erosanix.url = "github:emmanuelrosa/erosanix";
inputs.erosanix.inputs.nixpkgs.follows = "nixpkgs";

I can’t figure out how to get the Nixpkgs version I need (ex. /nix/store/cwihclkh4g6wghh67nq9lyswfjh650im-source) back in the Nix store. Using nixos-rebuild built --flake works only after updating my inputs with nix flake update. In other words, it won’t bring back the one that I need.

I don’t want to update Nixpkgs right now because as you can see, I use the master branch which is having issues at the moment.

For some reason the flake was locked to a local path. You can not reconstruct it. Though there is a rev in the metadata, if you are lucky you can use nix build github:nixos/nixpkgs/0bef0d60524fcdb2b609625e34d5c224e150c17a#hello to recosntruct the nixpkgs.

At least I had exactly that path after I ran the command.

I do not think, that this is from follows, usually this happens when manually overriding with commit.

1 Like

OK. I’m not sure how I’m doing that, but I found a fix. I got the version of Nixpkgs from nixos-version and then ran this:
nix build --override-input nixpkgs github:NixOS/nixpkgs/add2ba16087cfb104a2ccfbb60630492627c996a .#whatever

This will get me by until my next update.

Are you sure that is the correct commit? And not one you got by updating in-between?

In the lock file you pasted there’s clearly a git commit visible, that belongs to master from end of January.

I don’t know. I’m utterly confused. I tried the commit in the lock file, but Nix began to download a ton of files. I bet that was correct, but I really just wanted to use whatever Nixpkgs I already had.

It did allow me to build my flake, which is what I needed.

The locking to a local path can happen if you use the nix.registry.<name>.flake option or otherwise pin nixpkgs in the flake registry.

If you then do a nix flake update on a flake, wich uses the plain nixpkgs input, this input will get locked to the path specified by the pinned registry entry.

So if you use registry pinning, I would suggest to replace the nixpkgs input of your flake with github:NixOS/nixpkgs/nixos-unstable or similar to avoid the pinned registry entry.

1 Like

Had the same issue after running nix shell github:name/repo. Then I got the no such file or directory when creating a flake.nix and running flakes command on it. A nix flake init solved the issue.

3 Likes

I was experiencing the same issue. A nix flake init did not work for me. But making arbitrary changes and committing them in git seemed to allow me to run it again it worked.

3 Likes

Hello,

I face the same bug…
I manage to rebuild my system when pinning NixOS to a specific commit but not when using nixos-unstable.
And I cannot get home-manager to work at all.
I wonder if this is fixable without reinstalling.

I do not use nix.registry.<name>.flake

EDIT: sudo nix-store --repair --verify --check-contents solved the issue !

2 Likes

Hey,
I was having the same issue, I moved my flake.nix, ran flake init (it generated a simple flake.nix, which I deleted), then moved my flake.nix back and everything worked.

4 Likes

I wound up here while looking for help with this error:

error: getting status of '/nix/store/d59fka53ir1das1085ly6hngj6ba1xm3-source/flake.nix': No such file or directory

Which I experienced after adding a flake.nix to a repo. Direnv was trying to build the environment, but then nix refused to see the file because it wasn’t added yet. I just had to git add flake.nix

13 Likes

I just ran into this issue on a newly initialized git repo where I had not yet committed anything. nix develop worked before I ran git init (one of the last things I did a while back). I came back a few days later and it was suddenly broken. Turns out merely running git add flake.nix fixed the problem.

I must say, this is an absolutely terrible error that provides absolutely no useful feedback. If a flake.nix file exists and is being ignored because it isn’t tracked by git, then the error should say so.

5 Likes

There is active work on a feature that would enable us such a report, though currently it is technically not possiboe to do such an error reporting.

I understand. I’m currently working with a company that is rolling out Nix flakes-based builds and development environment management across all their teams. There’s only a relative handful of Nix experts, while there are hundreds of developers that have never used Nix at all.

My concern is that people who know how to use Nix and see the benefit of flakes want to use it in production despite its “experimental” status (clearly it isn’t experimental anymore). Yet these rough-around-the-edges experiences might turn away the aforementioned newbie Nix users.

4 Likes

Thanks. That saved me some pain. I hear you. I’m that nix newbie and new to flakes. Loving nix, but have had to remain headstrong in not going back to typical linux work arounds I’m used to. Although, I’m sure this would have been the case pre-flakes. Getting into the habit of ensuring the current state I’m working with has been at least git add’ed. Turns out I didn’t have to commit the flake.nix so much just add it. A better error in such situations such as ‘have you checked that you’ve added a flake.nix’ would have been appreciated.

Much pain was caused because a file was not added to git. Please fix the error message or add a helpful hint if it is not possible to know for sure.

4 Likes

This solved it for me, thanks.

Just ran into this error. Nothing fixed it, only to realize I had previously changed the folder name where I keep my nixos configs and not changed it in the flake that imports it.
So heads up for anyone, this can be a cause too.

i got this error when i moved hardware-configuration.nix into a sub folder system/. In turns out i had hardware-configuration.nix in my .gitgnore file which seemed to be blocking the nix build from seeing it. i fixed with git add -f system/hardware-configuration.nix and then rebuilding.

1 Like

I had this error when attempting to initialize a flake template

$ nix flake init -t github:purs-nix/purs-nix
error: opening directory '/nix/store/2hbz2waa9pscqv8yrm6f581b5vlzh52z-default': No such file or directory

Tried many things unsuccessfully including what other people reported as success in here.
In the end I solved the problem by deleting ~/.cache/nix.