= Path operations

Any expression that contains a forward slash (`/`) _not_ followed by a space
is interpreted as a path.
To refer to a file or directory relative to the current directory, prefix it with `./`.
You can specify the current directory as `./.`

[source]
....
nix-repl> ./file.txt
/home/amy/codeberg/nix-book/file.txt

nix-repl> ./.
/home/amy/codeberg/nix-book
....

== Concatenating paths

Paths can be concatenated to produce a new path.

[source]
....
nix-repl> /home/wombat + /bin/sh
/home/wombat/bin/sh

nix-repl> :t /home/wombat + /bin/sh
a path
....

[IMPORTANT]
====
Relative paths are made absolute when they are parsed, which occurs before concatenation.
This is why the result in the example below is not `/home/wombat/file.txt`.

[source]
....
nix-repl> /home/wombat + ./file.txt
/home/wombat/home/amy/codeberg/nix-book/file.txt
....
====

== Concatenating a path + a string

A path can be concatenated with a string to produce a new path.

[source]
....
nix-repl> /home/wombat + "/file.txt"
/home/wombat/file.txt

nix-repl> :t /home/wombat + "/file.txt"
a path
....

[NOTE]
====
The Nix reference manual says that the string must not "have a string context" that refers to a store path.
String contexts are beyond the scope of this book;
for more information see https://nixos.org/manual/nix/stable/language/operators#path-concatenation.
====

== Concatenating a string + a path

Strings can be concatenated with paths, but with a side-effect that may surprise you:
if the path exists, the file is copied to the Nix store!
The result is a string, not a path.

In the example below, the file `file.txt` is copied to `/nix/store/gp8ba25gpwvbqizqfr58jr014gmv1hd8-file.txt`
(not, as you might expect, to `/home/wombat/nix/store/gp8ba25gpwvbqizqfr58jr014gmv1hd8-file.txt`).

[source]
....
nix-repl> "/home/wombat" + ./file.txt
"/home/wombat/nix/store/gp8ba25gpwvbqizqfr58jr014gmv1hd8-file.txt"
....

The path must exist.

[source]
....
nix-repl> "/home/wombat" + ./no-such-file.txt
error (ignored): error: end of string reached
error: getting status of '/home/amy/codeberg/nix-book/no-such-file.txt': No such file or directory
....

== Useful built-in functions for paths

Nix provides some built-in functions for working with paths;
a few examples are shown below.
For more information on these and other built-in functions, see the Nix Manual
(https://nixos.org/manual/nix/stable/language/builtins).

Does the path exist?

[source]
....
nix-repl> builtins.pathExists ./index.html
true

nix-repl> builtins.pathExists /no/such/path
false
....

Get a list of the files in a directory, along with the type of each file.

[source]
....
nix-repl> builtins.readDir ./.
{ ".envrc" = "regular"; ".git" = "directory"; ".gitignore" = "regular"; Makefile = "regular"; images = "directory"; "index.html" = "regular"; "shell.nix" = "regular"; source = "directory"; themes = "directory"; "wombats-book-of-nix.pdf" = "regular"; }
....

Read the contents of a file into a string.

[source]
....
nix-repl> builtins.readFile ./.envrc
"use nix\n"
....
