nix icon indicating copy to clipboard operation
nix copied to clipboard

building `path:` flake urls produces incorrect output links when `realpath .` is in `/nix/store`

Open tejing1 opened this issue 6 months ago • 6 comments

Describe the bug

nix building a path: flake url with the current working directory located in the nix store produces a symlink pointing to the base storepath of the (realpath of the) current working directory rather than the output store path.

The same effect does not occur when given the directory not prefixed by path:.

Steps To Reproduce

Running:

#! /usr/bin/env bash

unset tmpdir
cleanup() {
    while popd >/dev/null 2>&1; do :; done
    [ -d "${tmpdir:-}" ] && rm -rf "$tmpdir"
}
trap cleanup EXIT
tmpdir="$(mktemp -td nix-path-issue-test-XXXXX)"

pushd "$tmpdir" >/dev/null

mkdir -p flake/dir/subdir
cat >flake/flake.nix <<'EOF'
{
  outputs = {...}: {
    foo = builtins.toFile "foo" "bar";
    dir = "${./dir}";
  };
}
EOF

nix build ./flake#.dir --out-link dir-in-nix-store

pushd dir-in-nix-store/subdir >/dev/null

nix build "$tmpdir"/flake#.foo --out-link "$tmpdir"/without-path
printf "without-path points to %s\n" "$(readlink "$tmpdir"/without-path)"

nix build path:"$tmpdir"/flake#.foo --out-link "$tmpdir"/with-path
printf "with-path points to %s\n" "$(readlink "$tmpdir"/with-path)"

Produces:

without-path points to /nix/store/vxjiwkjkn7x4079qvh1jkl5pn05j2aw0-foo
with-path points to /nix/store/l9pp9z5306crjh3a4rckdp36hlkvfhdj-dir

That value for with-path is "bafflingly wrong", as colemickens put it in NixOS/nixpkgs#144811

Expected behavior

with-path should point to /nix/store/vxjiwkjkn7x4079qvh1jkl5pn05j2aw0-foo

Metadata

Tested with nix 2.29.0 as well as 2.24.10.

Additional context

  • Root cause behind NixOS/nixpkgs#144811 which I just helped someone deal with recently... it seems to be back.
  • See the less specific, but related #5510

Checklist

  • [x] checked [latest Nix manual] ([source])
  • [x] checked [open bug issues and pull requests] for possible duplicates

Add :+1: to issues you find important.

tejing1 avatar Jun 16 '25 01:06 tejing1

Same thing occurs with git+file:// vs bare path. And when building github:nixos/nixpkgs/nixos-unstable#hello inside the nix store. I'm starting to think it's just anytime the url contains :.

tejing1 avatar Jun 16 '25 02:06 tejing1

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/cant-run-the-switch-to-configuration-command/66047/2

nixos-discourse avatar Jun 25 '25 06:06 nixos-discourse

I've encountered this bug twice and discovered that it was linked to me installing either eget or configuring my local nix to accept nur repo. Either way, the first fix I git logged and rollback to the previous version and from there I updated to the latest flake update. The second time, I went the the config path and sudo ./activate and then I went back to my nixconfig directory to rebuild switch and it was successful.

Hope this helps anyone else out and here's the screen dump

building the system configuration...
exec: no command
error: your NixOS configuration path seems to be missing essential files.
To avoid corrupting your current NixOS installation, the activation will abort.

This could be caused by Nix bug: https://github.com/NixOS/nix/issues/13367.
This is the evaluated NixOS configuration path: /nix/store/pz34k5fjcpq28zdp64yz36nmr5isdsaq-nixos-system-thinsandy-25.11.20251108.f6b44b2.
Change the directory to somewhere else (e.g., `cd $HOME`) before trying again.

If you think this is a mistake, you can set the environment variable
NIXOS_REBUILD_I_UNDERSTAND_THE_CONSEQUENCES_PLEASE_BREAK_MY_SYSTEM to 1
and re-run the command to continue.
Please open an issue if this is the case.

shaoyanji avatar Nov 14 '25 16:11 shaoyanji