nix-book/source/nix-language/import.adoc
Amy de Buitléir 3d8f0e2b8b initial commit
2025-10-12 15:55:01 +01:00

70 lines
954 B
Text

= Import
The built-in `import` function provides a way to parse a `.nix` file.
[source,nix]
.a.nix
....
{
message = "You successfully imported me!";
b = 12;
}
....
[source]
.Example
....
nix-repl> a = import ./a.nix
nix-repl> a.message
"You successfully imported me!"
nix-repl> a.b
12
....
The scope of the imported file does not inherit the scope of the importer.
[source,nix]
.b.nix
....
x + 7
....
[source]
.Example
....
nix-repl> x = 12
nix-repl> y = import ./b.nix
nix-repl> y
error:
… while calling the 'import' builtin
at «string»:1:2:
1| import ./b.nix
| ^
error: undefined variable 'x'
at /home/amy/codeberg/nix-book/b.nix:1:1:
1| x + 7
| ^
2|
....
So to pass information when importing something, use a function.
[source,nix]
.c.nix
....
x: x + 7
....
[source]
.Example
....
nix-repl> f = import ./c.nix
nix-repl> f 12
19
....