nix-book/source/nix-language/paths.adoc
Amy de Buitléir 96f65dd5a5 expanded
2025-10-12 15:59:19 +01:00

131 lines
3.1 KiB
Text

= 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]
.Example
....
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]
.Example
....
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]
.Example
....
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]
.Example
....
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
[IMPORTANT]
====
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, you might expect the result to be `"home/wombat/file.nix"`.
However, the file `file.txt` is copied to `/nix/store/gp8ba25gpwvbqizqfr58jr014gmv1hd8-file.txt`
before concatenating it to the string.
[source]
.Example
....
nix-repl> "/home/wombat" + ./file.txt
"/home/wombat/nix/store/gp8ba25gpwvbqizqfr58jr014gmv1hd8-file.txt"
....
When concatenating a string with a path, the path must exist.
[source]
.Example
....
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]
.Example
....
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]
.Example
....
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]
.Example
....
nix-repl> builtins.readFile ./.envrc
"use nix\n"
....