[Nix#9253] Symlinks to flakes have poor UX #106

Open
opened 2024-03-16 06:44:56 +00:00 by lix-bot · 0 comments
Member

Upstream-Issue: NixOS/nix#9253

Describe the bug

Flakes do not place nicely with symlinks. This leads to a plethora of UX issues.

First, you might try to symlink the flake.nix, which leads to this confusing error message:

$ mkdir flake-symlink
$ mkdir my-actual-flake-dir
$ cd flake-symlink/
$ touch ../my-actual-flake-dir/flake.nix
$ ln -s ../my-actual-flake-dir/flake.nix .
$ ls -la
lrwxr-xr-x flake.nix -> ../my-actual-flake-dir/flake.nix
$ nix build
error: getting status of '/nix/store/my-actual-flake-dir': No such file or directory

In this case, Nix copies the flake-symlink directory to the Nix store (probably) and then tries to evaluate flake.nix, which is a symlink to the parent directory, so it looks for /nix/store/my-actual-flake-dir, which does not exist.

If we use a symlink to an absolute path, we get another confusing error message:

$ ln -fs $(pwd)/../my-actual-flake-dir/flake.nix .
$ nix build
error: 'flake.nix' file of flake 'path:/Users/wiggles/tmp/flake-symlink?lastModified=1698541348&narHash=sha256-KOZb8VsWWEVgYjhS4BSMP8vLeoYSiP6%2BsFCqONtrV%2B0%3D' escapes from '/nix/store/3kagg1nr7d97szk16k392rhv6dj3xbl8-source'

Then we might try to symlink the directory itself. This leads to another two confusing errors:

$ cd ..
$ ln -fs my-actual-flake-dir flake-symlink
$ nix build flake-symlink
error: cannot find flake 'flake:flake-symlink' in the flake registries
$ nix build ./flake-symlink
error: path '/Users/wiggles/tmp/flake-symlink' is not a flake (because it's not a directory)

But if we actually cd into the "not a directory", we can nix build fine (if we've filled in our flake.nix):

$ cd flake-symlink/
$ nix build
$ echo $?
0

This is inconsistent with the "not a flake" diagnosis that nix build with a path to the symlink gives.

Steps To Reproduce

See above. Note that no actual content in the flake.nix is required.

Expected behavior

I expect the build to "just work", as if the symlink was a regular file. I would expect Nix to copy the destination file in place of the symlink when it copies the file to the Nix store, at least if the symlink's destination is neither in the Nix store or in the repo/directory being copied into the Nix store.

If we instruct Nix to nix build a symlink, we should probably nix build the symlink destination instead, transparently — in the same way that if I have a Git repository at ~/.dotfiles with a flake in ~/.dotfiles/config/home-manager, Nix will seamlessly translate the current directory to git+file://{path-to-repo-root}?dir={path-to-current-dir-from-root}:

$ cd ~/.dotfiles/config/home-manager
$ nix build
error: flake 'git+file:///Users/wiggles/.dotfiles?dir=config/home-mangler' does not provide attribute 'packages.aarch64-darwin.default' or 'defaultPackage.aarch64-darwin'

nix-env --version output

nix-env (Nix) 2.17.0

Upstream-Issue: https://git.lix.systems/NixOS/nix/issues/9253 ## Describe the bug Flakes do not place nicely with symlinks. This leads to a plethora of UX issues. ### Symlink `flake.nix` to relative path First, you might try to symlink the `flake.nix`, which leads to this confusing error message: ``` $ mkdir flake-symlink $ mkdir my-actual-flake-dir $ cd flake-symlink/ $ touch ../my-actual-flake-dir/flake.nix $ ln -s ../my-actual-flake-dir/flake.nix . $ ls -la lrwxr-xr-x flake.nix -> ../my-actual-flake-dir/flake.nix $ nix build error: getting status of '/nix/store/my-actual-flake-dir': No such file or directory ``` In this case, Nix copies the `flake-symlink` directory to the Nix store (probably) and then tries to evaluate `flake.nix`, which is a symlink to the parent directory, so it looks for `/nix/store/my-actual-flake-dir`, which does not exist. ### Symlink `flake.nix` to absolute path If we use a symlink to an absolute path, we get another confusing error message: ``` $ ln -fs $(pwd)/../my-actual-flake-dir/flake.nix . $ nix build error: 'flake.nix' file of flake 'path:/Users/wiggles/tmp/flake-symlink?lastModified=1698541348&narHash=sha256-KOZb8VsWWEVgYjhS4BSMP8vLeoYSiP6%2BsFCqONtrV%2B0%3D' escapes from '/nix/store/3kagg1nr7d97szk16k392rhv6dj3xbl8-source' ``` ### Symlink to a flake directory Then we might try to symlink the directory itself. This leads to another two confusing errors: ``` $ cd .. $ ln -fs my-actual-flake-dir flake-symlink $ nix build flake-symlink error: cannot find flake 'flake:flake-symlink' in the flake registries $ nix build ./flake-symlink error: path '/Users/wiggles/tmp/flake-symlink' is not a flake (because it's not a directory) ``` But if we actually `cd` into the "not a directory", we can `nix build` fine (if we've filled in our `flake.nix`): ``` $ cd flake-symlink/ $ nix build $ echo $? 0 ``` This is inconsistent with the "not a flake" diagnosis that `nix build` with a path to the symlink gives. ## Steps To Reproduce See above. Note that no actual content in the `flake.nix` is required. ## Expected behavior I expect the build to "just work", as if the symlink was a regular file. I would expect Nix to copy the destination file in place of the symlink when it copies the file to the Nix store, at least if the symlink's destination is neither in the Nix store or in the repo/directory being copied into the Nix store. If we instruct Nix to `nix build` a symlink, we should probably `nix build` the symlink destination instead, transparently — in the same way that if I have a Git repository at `~/.dotfiles` with a flake in `~/.dotfiles/config/home-manager`, Nix will seamlessly translate the current directory to `git+file://{path-to-repo-root}?dir={path-to-current-dir-from-root}`: ``` $ cd ~/.dotfiles/config/home-manager $ nix build error: flake 'git+file:///Users/wiggles/.dotfiles?dir=config/home-mangler' does not provide attribute 'packages.aarch64-darwin.default' or 'defaultPackage.aarch64-darwin' ``` ## `nix-env --version` output `nix-env (Nix) 2.17.0`
lix-bot added the
bug
imported
labels 2024-03-16 06:44:56 +00:00
jade added the
Area/flakes
label 2024-03-30 00:06:39 +00:00
qyriad added the
ux
label 2024-05-19 02:50:13 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: lix-project/lix#106
No description provided.