2021-12-10 07:24:12 -05:00
(ref-pinning-nixpkgs)=
# Pinning Nixpkgs
2023-02-16 00:23:33 -05:00
Specifying remote Nix expressions, such as the one provided by Nixpkgs, can be done in several ways:
2021-12-10 07:24:12 -05:00
2023-11-01 22:41:54 -04:00
- [`$NIX_PATH` environment variable ](https://nix.dev/manual/nix/2.18/command-ref/env-common.html#env-NIX_PATH )
- [`-I` option ](https://nix.dev/manual/nix/2.18/command-ref/opt-common.html#opt-I ) to most of commands like `nix-build` , `nix-shell` , etc.
- [`fetchurl` ](https://nix.dev/manual/nix/2.18/language/builtins.html#builtins-fetchurl ), [`fetchTarball` ](https://nix.dev/manual/nix/2.18/language/builtins.html#builtins-fetchTarball ), [`fetchGit` ](https://nix.dev/manual/nix/2.18/language/builtins.html#builtins-fetchGit ) or [Nixpkgs fetchers ](https://nixos.org/manual/nixpkgs/stable/#chap-pkgs-fetchers ) in Nix expressions
2023-05-11 23:45:19 -04:00
## Possible URL values
2021-12-10 07:24:12 -05:00
2023-05-11 23:45:19 -04:00
- Local file path:
2021-12-10 07:24:12 -05:00
2023-05-11 23:45:19 -04:00
```
./path/to/expression.nix
```
2023-02-16 00:23:33 -05:00
2023-12-05 08:24:11 -05:00
Using `./.` means that the expression is located in a file `default.nix` in the current directory.
2023-02-16 00:23:33 -05:00
2023-05-11 23:45:19 -04:00
- Pinned to a specific commit:
2023-02-16 00:23:33 -05:00
```
https://github.com/NixOS/nixpkgs/archive/eabc38219184cc3e04a974fe31857d8e0eac098d.tar.gz
```
2023-05-11 23:45:19 -04:00
- Using the latest channel version, meaning all tests have passed:
2023-02-16 00:23:33 -05:00
```
http://nixos.org/channels/nixos-22.11/nixexprs.tar.xz
```
2023-07-21 05:36:25 -04:00
- Shorthand syntax for channels:
2023-02-16 00:23:33 -05:00
```
2023-05-11 23:45:19 -04:00
channel:nixos-22.11
2023-02-16 00:23:33 -05:00
```
2023-05-11 23:45:19 -04:00
- Using the latest channel version, hosted by GitHub:
2023-02-16 00:23:33 -05:00
```
https://github.com/NixOS/nixpkgs/archive/nixos-22.11.tar.gz
```
2023-05-11 23:45:19 -04:00
- Using the latest commit on the release branch, but not tested yet:
2023-02-16 00:23:33 -05:00
```
https://github.com/NixOS/nixpkgs/archive/release-21.11.tar.gz
```
2021-12-10 07:24:12 -05:00
## Examples
2023-10-06 04:59:27 -04:00
- ```shell-session
2022-11-08 11:37:41 -05:00
$ nix-build -I ~/dev
```
2021-12-10 07:24:12 -05:00
2022-11-08 11:37:41 -05:00
- ```shell-session
2023-05-11 23:45:19 -04:00
$ nix-build -I nixpkgs=http://nixos.org/channels/nixos-22.11/nixexprs.tar.xz
2022-11-08 11:37:41 -05:00
```
2021-12-10 07:24:12 -05:00
2023-02-16 00:23:33 -05:00
- ```shell-session
2023-06-26 18:12:38 -04:00
$ nix-build -I nixpkgs=channel:nixos-22.11
2023-02-16 00:23:33 -05:00
```
2022-11-08 11:37:41 -05:00
- ```shell-session
2023-07-21 05:36:25 -04:00
$ NIX_PATH=nixpkgs=http://nixos.org/channels/nixos-22.11/nixexprs.tar.xz nix-build
2022-11-08 11:37:41 -05:00
```
2021-12-10 07:24:12 -05:00
2023-02-16 00:23:33 -05:00
- ```shell-session
2023-07-21 05:36:25 -04:00
$ NIX_PATH=nixpkgs=channel:nixos-22.11 nix-build
2023-02-16 00:23:33 -05:00
```
- In the Nix language:
2021-12-10 07:24:12 -05:00
2022-11-08 11:37:41 -05:00
```nix
2021-12-10 07:24:12 -05:00
let
2022-12-09 22:33:06 -05:00
pkgs = import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/nixos-22.11.tar.gz") {};
2022-11-08 11:37:41 -05:00
in pkgs.stdenv.mkDerivation { ... }
2021-12-10 07:24:12 -05:00
```