core/lib/fileset/mock-splitRoot.nix

30 lines
927 B
Nix
Raw Normal View History

2024-05-01 22:14:04 +00:00
# This overlay implements mocking of the lib.path.splitRoot function
# It pretends that the last component named "mock-root" is the root:
#
# splitRoot /foo/mock-root/bar/mock-root/baz
# => {
# root = /foo/mock-root/bar/mock-root;
# subpath = "./baz";
# }
self: super: {
path = super.path // {
2024-06-30 08:16:52 +00:00
splitRoot =
path:
2024-05-01 22:14:04 +00:00
let
parts = super.path.splitRoot path;
components = self.path.subpath.components parts.subpath;
count = self.length components;
2024-06-30 08:16:52 +00:00
rootIndex =
count
- self.lists.findFirstIndex (component: component == "mock-root") (self.length components) (
self.reverseList components
);
2024-05-01 22:14:04 +00:00
root = self.path.append parts.root (self.path.subpath.join (self.take rootIndex components));
subpath = self.path.subpath.join (self.drop rootIndex components);
2024-06-30 08:16:52 +00:00
in
{
2024-05-01 22:14:04 +00:00
inherit root subpath;
};
};
}