3 This document explains why the `lib.path` library is designed the way it is.
5 The purpose of this library is to process [filesystem paths]. It does not read files from the filesystem.
6 It exists to support the native Nix [path value type] with extra functionality.
8 [filesystem paths]: https://en.m.wikipedia.org/wiki/Path_(computing)
9 [path value type]: https://nixos.org/manual/nix/stable/language/values.html#type-path
11 As an extension of the path value type, it inherits the same intended use cases and limitations:
12 - Only use paths to access files at evaluation time, such as the local project source.
13 - Paths cannot point to derivations, so they are unfit to represent dependencies.
14 - A path implicitly imports the referenced files into the Nix store when interpolated to a string. Therefore paths are not suitable to access files at build- or run-time, as you risk importing the path from the evaluation system instead.
16 Overall, this library works with two types of paths:
17 - Absolute paths are represented with the Nix [path value type]. Nix automatically normalises these paths.
18 - Subpaths are represented with the [string value type] since path value types don't support relative paths. This library normalises these paths as safely as possible. Absolute paths in strings are not supported.
20 A subpath refers to a specific file or directory within an absolute base directory.
21 It is a stricter form of a relative path, notably [without support for `..` components][parents] since those could escape the base directory.
23 [string value type]: https://nixos.org/manual/nix/stable/language/values.html#type-string
25 This library is designed to be as safe and intuitive as possible, throwing errors when operations are attempted that would produce surprising results, and giving the expected result otherwise.
27 This library is designed to work well as a dependency for the `lib.filesystem` and `lib.sources` library components. Contrary to these library components, `lib.path` does not read any paths from the filesystem.
29 This library makes only these assumptions about paths and no others:
30 - `dirOf path` returns the path to the parent directory of `path`, unless `path` is the filesystem root, in which case `path` is returned.
31 - There can be multiple filesystem roots: `p == dirOf p` and `q == dirOf q` does not imply `p == q`.
32 - While there's only a single filesystem root in stable Nix, the [lazy trees feature](https://github.com/NixOS/nix/pull/6530) introduces [additional filesystem roots](https://github.com/NixOS/nix/pull/6530#discussion_r1041442173).
33 - `path + ("/" + string)` returns the path to the `string` subdirectory in `path`.
34 - If `string` contains no `/` characters, then `dirOf (path + ("/" + string)) == path`.
35 - If `string` contains no `/` characters, then `baseNameOf (path + ("/" + string)) == string`.
36 - `path1 == path2` returns `true` only if `path1` points to the same filesystem path as `path2`.
38 Notably we do not make the assumption that we can turn paths into strings using `toString path`.
42 Each subsection here contains a decision along with arguments and counter-arguments for (+) and against (-) that decision.
44 ### Leading dots for relative paths
45 [leading-dots]: #leading-dots-for-relative-paths
47 Observing: Since subpaths are a form of relative paths, they can have a leading `./` to indicate it being a relative path, this is generally not necessary for tools though.
49 Considering: Paths should be as explicit, consistent and unambiguous as possible.
51 Decision: Returned subpaths should always have a leading `./`.
54 <summary>Arguments</summary>
56 - (+) In shells, just running `foo` as a command wouldn't execute the file `foo`, whereas `./foo` would execute the file. In contrast, `foo/bar` does execute that file without the need for `./`. This can lead to confusion about when a `./` needs to be prefixed. If a `./` is always included, this becomes a non-issue. This effectively then means that paths don't overlap with command names.
57 - (+) Prepending with `./` makes the subpaths always valid as relative Nix path expressions.
58 - (+) Using paths in command line arguments could give problems if not escaped properly, e.g. if a path was `--version`. This is not a problem with `./--version`. This effectively then means that paths don't overlap with GNU-style command line options.
59 - (-) `./` is not required to resolve relative paths, resolution always has an implicit `./` as prefix.
60 - (-) It's less noisy without the `./`, e.g. in error messages.
61 - (+) But similarly, it could be confusing whether something was even a path.
62 e.g. `foo` could be anything, but `./foo` is more clearly a path.
63 - (+) Makes it more uniform with absolute paths (those always start with `/`).
64 - (-) That is not relevant for practical purposes.
65 - (+) `find` also outputs results with `./`.
66 - (-) But only if you give it an argument of `.`. If you give it the argument `some-directory`, it won't prefix that.
67 - (-) `realpath --relative-to` doesn't prefix relative paths with `./`.
68 - (+) There is no need to return the same result as `realpath`.
72 ### Representation of the current directory
73 [curdir]: #representation-of-the-current-directory
75 Observing: The subpath that produces the base directory can be represented with `.` or `./` or `./.`.
77 Considering: Paths should be as consistent and unambiguous as possible.
79 Decision: It should be `./.`.
82 <summary>Arguments</summary>
84 - (+) `./` would be inconsistent with [the decision to not persist trailing slashes][trailing-slashes].
85 - (-) `.` is how `realpath` normalises paths.
86 - (+) `.` can be interpreted as a shell command (it's a builtin for sourcing files in `bash` and `zsh`).
87 - (+) `.` would be the only path without a `/`. It could not be used as a Nix path expression, since those require at least one `/` to be parsed as such.
88 - (-) `./.` is rather long.
89 - (-) We don't require users to type this though, as it's only output by the library.
90 As inputs all three variants are supported for subpaths (and we can't do anything about absolute paths)
91 - (-) `builtins.dirOf "foo" == "."`, so `.` would be consistent with that.
92 - (+) `./.` is consistent with the [decision to have leading `./`][leading-dots].
93 - (+) `./.` is a valid Nix path expression, although this property does not hold for every relative path or subpath.
97 ### Subpath representation
98 [relrepr]: #subpath-representation
100 Observing: Subpaths such as `foo/bar` can be represented in various ways:
101 - string: `"foo/bar"`
102 - list with all the components: `[ "foo" "bar" ]`
103 - attribute set: `{ type = "relative-path"; components = [ "foo" "bar" ]; }`
105 Considering: Paths should be as safe to use as possible. We should generate string outputs in the library and not encourage users to do that themselves.
107 Decision: Paths are represented as strings.
110 <summary>Arguments</summary>
112 - (+) It's simpler for the users of the library. One doesn't have to convert a path a string before it can be used.
113 - (+) Naively converting the list representation to a string with `concatStringsSep "/"` would break for `[]`, requiring library users to be more careful.
114 - (+) It doesn't encourage people to do their own path processing and instead use the library.
115 With a list representation it would seem easy to just use `lib.lists.init` to get the parent directory, but then it breaks for `.`, which would be represented as `[ ]`.
116 - (+) `+` is convenient and doesn't work on lists and attribute sets.
117 - (-) Shouldn't use `+` anyways, we export safer functions for path manipulation.
122 [parents]: #parent-directory
124 Observing: Relative paths can have `..` components, which refer to the parent directory.
126 Considering: Paths should be as safe and unambiguous as possible.
128 Decision: `..` path components in string paths are not supported, neither as inputs nor as outputs. Hence, string paths are called subpaths, rather than relative paths.
131 <summary>Arguments</summary>
133 - (+) If we wanted relative paths to behave according to the "physical" interpretation (as a directory tree with relations between nodes), it would require resolving symlinks, since e.g. `foo/..` would not be the same as `.` if `foo` is a symlink.
134 - (-) The "logical" interpretation is also valid (treating paths as a sequence of names), and is used by some software. It is simpler, and not using symlinks at all is safer.
135 - (+) Mixing both models can lead to surprises.
136 - (+) We can't resolve symlinks without filesystem access.
137 - (+) Nix also doesn't support reading symlinks at evaluation time.
138 - (-) We could just not handle such cases, e.g. `equals "foo" "foo/bar/.. == false`. The paths are different, we don't need to check whether the paths point to the same thing.
139 - (+) Assume we said `relativeTo /foo /bar == "../bar"`. If this is used like `/bar/../foo` in the end, and `bar` turns out to be a symlink to somewhere else, this won't be accurate.
140 - (-) We could decide to not support such ambiguous operations, or mark them as such, e.g. the normal `relativeTo` will error on such a case, but there could be `extendedRelativeTo` supporting that.
141 - (-) `..` are a part of paths, a path library should therefore support it.
142 - (+) If we can convincingly argue that all such use cases are better done e.g. with runtime tools, the library not supporting it can nudge people towards using those.
143 - (-) We could allow "..", but only in the prefix.
144 - (+) Then we'd have to throw an error for doing `append /some/path "../foo"`, making it non-composable.
145 - (+) The same is for returning paths with `..`: `relativeTo /foo /bar => "../bar"` would produce a non-composable path.
146 - (+) We argue that `..` is not needed at the Nix evaluation level, since we'd always start evaluation from the project root and don't go up from there.
147 - (+) `..` is supported in Nix paths, turning them into absolute paths.
148 - (-) This is ambiguous in the presence of symlinks.
149 - (+) If you need `..` for building or runtime, you can use build-/run-time tooling to create those (e.g. `realpath` with `--relative-to`), or use absolute paths instead.
150 This also gives you the ability to correctly handle symlinks.
155 [trailing-slashes]: #trailing-slashes
157 Observing: Subpaths can contain trailing slashes, like `foo/`, indicating that the path points to a directory and not a file.
159 Considering: Paths should be as consistent as possible, there should only be a single normalisation for the same path.
161 Decision: All functions remove trailing slashes in their results.
164 <summary>Arguments</summary>
166 - (+) It allows normalisations to be unique, in that there's only a single normalisation for the same path. If trailing slashes were preserved, both `foo/bar` and `foo/bar/` would be valid but different normalisations for the same path.
167 - Comparison to other frameworks to figure out the least surprising behavior:
168 - (+) Nix itself doesn't support trailing slashes when parsing and doesn't preserve them when appending paths.
169 - (-) [Rust's std::path](https://doc.rust-lang.org/std/path/index.html) does preserve them during [construction](https://doc.rust-lang.org/std/path/struct.Path.html#method.new).
170 - (+) Doesn't preserve them when returning individual [components](https://doc.rust-lang.org/std/path/struct.Path.html#method.components).
171 - (+) Doesn't preserve them when [canonicalizing](https://doc.rust-lang.org/std/path/struct.Path.html#method.canonicalize).
172 - (+) [Python 3's pathlib](https://docs.python.org/3/library/pathlib.html#module-pathlib) doesn't preserve them during [construction](https://docs.python.org/3/library/pathlib.html#pathlib.PurePath).
173 - Notably it represents the individual components as a list internally.
174 - (-) [Haskell's filepath](https://hackage.haskell.org/package/filepath-1.4.100.0) has [explicit support](https://hackage.haskell.org/package/filepath-1.4.100.0/docs/System-FilePath.html#g:6) for handling trailing slashes.
175 - (-) Does preserve them for [normalisation](https://hackage.haskell.org/package/filepath-1.4.100.0/docs/System-FilePath.html#v:normalise).
176 - (-) [NodeJS's Path library](https://nodejs.org/api/path.html) preserves trailing slashes for [normalisation](https://nodejs.org/api/path.html#pathnormalizepath).
177 - (+) For [parsing a path](https://nodejs.org/api/path.html#pathparsepath) into its significant elements, trailing slashes are not preserved.
178 - (+) Nix's builtin function `dirOf` gives an unexpected result for paths with trailing slashes: `dirOf "foo/bar/" == "foo/bar"`.
179 Inconsistently, `baseNameOf` works correctly though: `baseNameOf "foo/bar/" == "bar"`.
180 - (-) We are writing a path library to improve handling of paths though, so we shouldn't use these functions and discourage their use.
181 - (-) Unexpected result when normalising intermediate paths, like `relative.normalise ("foo" + "/") + "bar" == "foobar"`.
182 - (+) This is not a practical use case though.
183 - (+) Don't use `+` to append paths, this library has a `join` function for that.
184 - (-) Users might use `+` out of habit though.
185 - (+) The `realpath` command also removes trailing slashes.
186 - (+) Even with a trailing slash, the path is the same, it's only an indication that it's a directory.
190 ## Other implementations and references
192 - [Rust](https://doc.rust-lang.org/std/path/struct.Path.html)
193 - [Python](https://docs.python.org/3/library/pathlib.html)
194 - [Haskell](https://hackage.haskell.org/package/filepath-1.4.100.0/docs/System-FilePath.html)
195 - [Nodejs](https://nodejs.org/api/path.html)
196 - [POSIX.1-2017](https://pubs.opengroup.org/onlinepubs/9699919799/nframe.html)