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

prim_toPath: Actually make the string a path #47

Closed
wants to merge 1 commit into from

Conversation

shlevy
Copy link
Member

@shlevy shlevy commented Aug 29, 2012

builtins.toPath currently doesn't actually turn something into a path. In fact, while it does check if the argument is indeed a path and canonicalize it, it actually returns a string even the argument was a path! This fixes that.

$ cat test.nix

let
  a = builtins.readFile (builtins.unsafeDiscardStringContext (derivation {
    name = "a";
    builder = "/bin/false";
    system = "sys";
    path = /etc/nixos/configuration.nix;
  }).drvPath);

  b = builtins.readFile (builtins.unsafeDiscardStringContext (derivation {
    name = "b";
    builder = "/bin/false";
    system = "sys";
    path = builtins.toPath "/etc/nixos/configuration.nix";
  }).drvPath);

  c = builtins.readFile (builtins.unsafeDiscardStringContext (derivation {
    name = "c";
    builder = "/bin/false";
    system = "sys";
    path = builtins.toPath /etc/nixos/configuration.nix;
  }).drvPath);
in

import "${(import <nixpkgs> {}).runCommand "paths.nix" {} ''
  echo "'''" > $out
  echo "${a}" | sed 's/.*\(path,[^)]*\)).*/\1/' >> $out
  echo "${b}" | sed 's/.*\(path,[^)]*\)).*/\1/' >> $out
  echo "${c}" | sed 's/.*\(path,[^)]*\)).*/\1/' >> $out
  echo "'''" >> $out
''}"

Before:
$ echo -e $(nix-instantiate test.nix >/dev/null 2>&1 || nix-instantiate test.nix --eval-only | sed 's"\g')

path,/nix/store/ms6xqxb4pvxga4m9wlmj5rp8my418dwz-configuration.nix
path,/etc/nixos/configuration.nix
path,/etc/nixos/configuration.nix

After:
$ echo -e $(nix-instantiate test.nix >/dev/null 2>&1 || nix-instantiate test.nix --eval-only | sed 's"\g')

path,/nix/store/ms6xqxb4pvxga4m9wlmj5rp8my418dwz-configuration.nix
path,/nix/store/ms6xqxb4pvxga4m9wlmj5rp8my418dwz-configuration.nix
path,/nix/store/ms6xqxb4pvxga4m9wlmj5rp8my418dwz-configuration.nix

@shlevy
Copy link
Member Author

shlevy commented Aug 29, 2012

Wow, that was a really complicated test-case. I can post a much simpler one if the issue isn't clear.

@edolstra
Copy link
Member

Did you see the comment just above toPath that suggests it's obsolete?

@shlevy
Copy link
Member Author

shlevy commented Aug 29, 2012

There's a question mark :) Eh, probably anything that can be done with toPath can be done without it, but it seems misleading to have it be called that and still there and not working. It's not documented in the nix manual as being obsolete and there's no warning message printed or anything, so most users probably wouldn't know that it is and are almost certainly expecting a different behavior than what actually happens. IMO even if it is obsolete it should be correct as long as it's still there and usable.

@edolstra
Copy link
Member

Merged, thanks.

@edolstra edolstra closed this Oct 26, 2012
edolstra pushed a commit that referenced this pull request May 16, 2019
Content hashes
meditans pushed a commit to Ericson2314/nix that referenced this pull request Aug 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants