query
On this page

splitRoot

lib.path.splitRoot

Docs pulled from | This Revision | 10 minutes ago


Split the filesystem root from a path. The result is an attribute set with these attributes:

  • root: The filesystem root of the path, meaning that this directory has no parent directory.
  • subpath: The normalised subpath string that when appended to root returns the original path.

Laws:

  • Appending the root and subpath gives the original path:

    p ==
      append
        (splitRoot p).root
        (splitRoot p).subpath
    
  • Trying to get the parent directory of root using readDir returns root itself:

    dirOf (splitRoot p).root == (splitRoot p).root
    

Inputs

path

The path to split the root off of

Type

splitRoot :: Path -> { root :: Path, subpath :: String }

Examples

splitRoot usage example

splitRoot /foo/bar
=> { root = /.; subpath = "./foo/bar"; }

splitRoot /.
=> { root = /.; subpath = "./."; }

# Nix neutralises `..` path components for all path values automatically
splitRoot /foo/../bar
=> { root = /.; subpath = "./bar"; }

splitRoot "/foo/bar"
=> <error>

Noogle detected

Implementation

The following is the current implementation of this function.

splitRoot =
    # The path to split the root off of
    path:
    assert assertMsg (isPath path)
      "lib.path.splitRoot: Argument is of type ${typeOf path}, but a path was expected";
    let
      deconstructed = deconstructPath path;
    in
    {
      root = deconstructed.root;
      subpath = joinRelPath deconstructed.components;
    };