Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/libexpr/primops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2376,6 +2376,26 @@ static RegisterPrimOp primop_readFileType({
.fun = prim_readFileType,
});

/* Read the target of a symlink. */
static void prim_readSymlink(EvalState & state, const PosIdx pos, Value ** args, Value & v)
{
const auto path = state.realisePath(pos, *args[0], SymlinkResolution::Ancestors);
const auto target = path.readLink();
const auto parent = path.parent();
v.mkPath(SourcePath(path.accessor, CanonPath(target, parent.path)), state.mem);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit questionable whether this should return a path value or just the raw symlink as a string (without canonicalisation). If the former, maybe this function should be called builtins.canonPath or builtins.resolvePath or whatever (and accept non-symlink paths as well).

}

static RegisterPrimOp primop_readSymlink({
.name = "__readSymlink",
.args = {"path"},
.doc = R"(
Return the target of the symlink at *path*.

If *path* does not refer to a symlink, an error is thrown.
)",
.fun = prim_readSymlink,
});

/* Read a directory (without . or ..) */
static void prim_readDir(EvalState & state, const PosIdx pos, Value ** args, Value & v)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
error: filesystem error: read_symlink: not a symlink [/pwd/lang/readDir/bar]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
builtins.readSymlink ./readDir/bar
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# shellcheck shell=bash
set -euo pipefail
testcaseBasename=$1

# Normalize platform-specific filesystem error messages
sed -i "$testcaseBasename.err" \
-e 's/filesystem error: in read_symlink:/filesystem error: read_symlink:/' \
-e 's/Invalid argument/not a symlink/' \
-e 's/Not a symbolic link/not a symlink/' \
-e 's/\["\([^"]*\)"\]/[\1]/' \
;
1 change: 1 addition & 0 deletions tests/functional/lang/eval-okay-readSymlink.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ ldir = /pwd/lang/readDir/foo; linked = /pwd/lang/readDir/foo/git-hates-directories; }
4 changes: 4 additions & 0 deletions tests/functional/lang/eval-okay-readSymlink.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
ldir = builtins.readSymlink ./readDir/ldir;
linked = builtins.readSymlink ./readDir/linked;
}
Loading