Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot convert string to path #12407

Open
2 tasks done
yajo opened this issue Feb 3, 2025 · 1 comment
Open
2 tasks done

Cannot convert string to path #12407

yajo opened this issue Feb 3, 2025 · 1 comment
Labels

Comments

@yajo
Copy link
Contributor

yajo commented Feb 3, 2025

Describe the bug

When you have a string that contains nix store info, it's impossible to convert it to a Path.

Steps To Reproduce

# flake.nix
{
  outputs = {
    self,
    nixpkgs,
  }: {
    packages.x86_64-linux.default = let
      absoluteNixpkgs = /. + nixpkgs;
    in
      assert builtins.isPath absoluteNixpkgs;
        nixpkgs.legacyPackages.x86_64-linux.runCommandLocal "test" ''
          true
        '';
  };
}

Now build it:

nix build --show-trace
error:
       … in the condition of the assert statement
         at /nix/store/v99q15bqy6lrm4wiqjwsp0ig9vl1g949-source/mrchef/nix/flake.nix:9:7:
            8|     in
            9|       assert builtins.isPath absoluteNixpkgs;
             |       ^
           10|         nixpkgs.legacyPackages.x86_64-linux.runCommandLocal "test" ''while calling the 'isPath' builtin
         at /nix/store/v99q15bqy6lrm4wiqjwsp0ig9vl1g949-source/mrchef/nix/flake.nix:9:14:
            8|     in
            9|       assert builtins.isPath absoluteNixpkgs;
             |              ^
           10|         nixpkgs.legacyPackages.x86_64-linux.runCommandLocal "test" ''

       error: a string that refers to a store path cannot be appended to a path
       at /nix/store/v99q15bqy6lrm4wiqjwsp0ig9vl1g949-source/mrchef/nix/flake.nix:7:28:
            6|     packages.x86_64-linux.default = let
            7|       absoluteNixpkgs = /. + nixpkgs;
             |                            ^
            8|     in

Expected behavior

I should be able to convert it to a path, and work with it from there onwards.

However, builtins.toPath returns a String, as shown in #1074. And the docs say to use this new method I'm using, but it also doesn't work as shown here.

Metadata

nix-env (Nix) 2.24.12

Additional context

I'm trying to use paths because they react appropriately directory traversal when concatenating with ../../ or similar stuff. These are however prohibited in other library functions in nixpkgs.lib.path.subpath, so I cannot just use strings.

Checklist


Add 👍 to issues you find important.

@yajo yajo added the bug label Feb 3, 2025
@yajo
Copy link
Contributor Author

yajo commented Feb 3, 2025

I'm trying to use paths because they react appropriately directory traversal when concatenating with ../../ or similar stuff. These are however prohibited in other library functions in nixpkgs.lib.path.subpath, so I cannot just use strings.

FWIW, the deprecated builtins.toPath function gets a String and returns it normalized:

nix-repl> builtins.toPath "/a/b/c/..///d"                       
"/a/b/d"

So, it works for me as a workaround of this issue. Hopefully it will never be removed: NixOS/nixpkgs#40937 (comment)

Indeed, the name is confusing and the operations it does are not easy to understand, but I found no substitute for this use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant