NixOS/nix — pull request #47 by shlevy (2012-08-29 02:19:04+00:00)
Title: prim_toPath: Actually make the string a path

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 {}).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