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

99 lines
2.8 KiB
Text

= Nixpkgs
As discussed in <<#type-path>>, enclosing a path in angle brackets, e.g. <nixpkgs> causes the directories
listed in the environment variable NIX_PATH to be searched for the given
file or directory name.
In the REPL, the command `:l <nixpkgs>` will load `nixpkgs`.
[source]
.Example
....
nix-repl> :l <nixpkgs>
Added 25694 variables.
....
This gives you access to many more functions and tools.
Alternatively, you can automatically load nixpkgs when you enter the REPL
using the command `nix repl '<nixpkgs>'`.
Within a Nix flake
////
TODO: Add a cross-reference to where we discuss the `lib` functions.
TODO: discuss the `lib` functions
TODO: discuss the `import` directive
TODO: Mention that... In a file, the counterpart to :l <nixpkgs> is with (import <nixpkgs> {}); at the beginning of the file.
////
== Nixpkgs library function
In this section, we will look at a few especially useful Nixpkgs library functions.
You can find a full list in the
https://nixos.org/manual/nixpkgs/unstable/#sec-functions-library[Nixpkgs manual].
[#genAttrs]
=== `lib.genAttrs`
The function
https://nixos.org/manual/nixpkgs/stable/#function-library-lib.attrsets.genAttrs[`lib.genAttrs`]
generates an attribute set by mapping a function over a list of attribute names.
It is an alias for `lib.attrsets.genAttrs`.
It takes two arguments:
- names of values in the resulting attribute set
- a function, given the name of the attribute, returns the attribute's value
[source]
.Example
....
nix-repl> lib.genAttrs [ "x86_64-linux" "aarch64-linux" ] (system: "some definitions for ${system}")
{
aarch64-linux = "some definitions for aarch64-linux";
x86_64-linux = "some definitions for x86_64-linux";
}
....
As we shall see later, this is very useful when writing flakes.
[#getExe]
=== `lib.getExe` and `lib.getExe'`
The function
https://nixos.org/manual/nixpkgs/stable/#function-library-lib.meta.getExe[`lib.getExe`]
returns the path to the main program of a package.
It is an alias for `lib.meta.getExe`.
[source]
.Example
....
nix-repl> system = "x86_64-linux"
nix-repl> pkgs = import <nixpkgs> { inherit system; }
nix-repl> lib.getExe pkgs.hello
"/nix/store/s9p0adfpzarzfa5kcnqhwargfwiq8qmj-hello-2.12.2/bin/hello"
....
The function
https://nixos.org/manual/nixpkgs/stable/#function-library-lib.meta.getExe[`lib.getExe'`]
returns the path to the specified program of a package.
It is an alias for `lib.meta.getExe'`.
[source]
.Example
....
nix-repl> lib.getExe' pkgs.imagemagick "convert"
"/nix/store/rn6ck85zkpkgdnm00jmn762z22wz86w6-imagemagick-7.1.2-3/bin/convert"
....
[#mkShell]
=== `pkgs.mkShell`
The function
https://nixos.org/manual/nixpkgs/stable/#sec-pkgs-mkShell[`pkgs.mkShell`]
is a wrapper around `stenv.mkDerivation`
[#flakeExposed]
=== `lib.systems.flakeExposed`
[#mkDerivation]
=== `stdenv.mkDerivation`