Skip to content

Commit

Permalink
fix: always isolate modules
Browse files Browse the repository at this point in the history
Previous to this fix, if `dir` was passed as a string instead of a path
literal, then the isolation mechanisms provided by the implicit copy
behavior of a path in string interpolation was broken, and modules could
see the files in their directory.

Now both cases are treated identically and isolation is preserved.
`strToPath` takes a path like string and converts it back to a path
literal to accomplish this.
  • Loading branch information
nrdxp committed Aug 3, 2024
1 parent d2318c3 commit fdd29ff
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
8 changes: 7 additions & 1 deletion default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ let
fix = import ./std/fix.nix;
filterMap = scopedImport { std = builtins; } ./std/set/filterMap.nix;
parse = scopedImport { std = builtins; } ./std/file/parse.nix;
strToPath = scopedImport { std = builtins; } ./std/path/strToPath.nix;
cond = import ./std/set/cond.nix;
compose = import ./.;

Expand Down Expand Up @@ -42,8 +43,13 @@ let
);

atom = fix (
f: pre: dir:
f: pre: dir':
let
# It is crucial that the directory is a path literal, not a string
# since the implicit copy to the /nix/store, which provides isolation,
# only happens for path literals.
dir = strToPath dir';

contents = builtins.readDir dir;

hasMod = contents."mod.nix" or null == "regular";
Expand Down
1 change: 1 addition & 0 deletions std/mod.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
pub_fix = mod.fix;
pub_file = mod.file;
pub_set = mod.set;
pub_path = mod.path;
}
1 change: 1 addition & 0 deletions std/path/mod.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ pub_strToPath = mod.strToPath; }
13 changes: 13 additions & 0 deletions std/path/strToPath.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
str:
let
# strip off the first `/` of an absolute path
frag = std.substring 1 (-1) str;
# will fail if the string does not represent an absolute path
# which is what we want since this function makes little sense otherwise
validate = std.toPath str;
# recombine the fragment string with an absolute path starting at the root
# the result with be a path literal instead of a string
path = /. + frag;
in
# avoid the extra work if it is already a path literal
if std.isPath str then str else std.seq validate path
5 changes: 4 additions & 1 deletion test/bld.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
let
mod = import ../. { } ./bld;
mod =
import ../. { }
# added to test implicit path conversion when path is a string
(builtins.toPath ./bld);
in
builtins.deepSeq mod mod

0 comments on commit fdd29ff

Please sign in to comment.