Skip to content

Commit

Permalink
clarify side notes
Browse files Browse the repository at this point in the history
  • Loading branch information
fricklerhandwerk committed Sep 5, 2024
1 parent 33950c3 commit bbbed74
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
5 changes: 3 additions & 2 deletions source/tutorials/first-steps/ad-hoc-shell-environments.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ $ echo no chance | lolcat
The program ‘lolcat’ is currently not installed.
```

Use `nix-shell` with the `-p` (`--packages`) option to specify that we need the `cowsay` and `lolcat` packages.
Use `nix-shell` with the `-p` (`--packages`) option to specify that we need the `cowsay` and `lolcat` packages:

:::{note}
The first invocation of `nix-shell` for these packages may take a while to download all dependencies.
Expand Down Expand Up @@ -162,6 +162,7 @@ Python 3.10.11

Exit the shell as usual to return to the previous environment.

(towards-reproducibility)=
## Towards reproducibility

These shell environments are very convenient, but the examples so far are not reproducible yet.
Expand All @@ -175,7 +176,7 @@ The following example creates a fully reproducible environment.
You can run it anywhere, anytime to obtain the exact same version of the `git`.

```shell-session
$ nix-shell -p git --run "git --version" --pure -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/2a601aafdc5605a5133a2ca506a34a3a73377247.tar.gz
$ nix-shell -p git --run "git --version" --pure -I nixpkgs=https://github.com/NixOS/nixpkgs/tarball/2a601aafdc5605a5133a2ca506a34a3a73377247
...
git version 2.33.1
```
Expand Down
25 changes: 18 additions & 7 deletions source/tutorials/first-steps/declarative-shell.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ A better solution is to create our shell environment from a `shell.nix` file.

Create a file called `shell.nix` with these contents:

{lineno-start=1}
```nix
let
nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/tarball/nixos-24.05";
Expand All @@ -67,19 +68,29 @@ pkgs.mkShellNoCC {
```

::::{dropdown} Detailed explanation
We use a version of [Nixpkgs pinned to a release branch](<ref-pinning-nixpkgs>), and explicitly set configuration options and overlays to avoid them being inadvertently overridden by [global configuration](https://nixos.org/manual/nixpkgs/stable/#chap-packageconfig).
We use a version of [Nixpkgs pinned to a release branch](<ref-pinning-nixpkgs>).
If you followed the [](ad-hoc-envs) tutorial and don't want to to download all dependencies again, specify the exact same revision as in the section [](towards-reproducibility):

`nix-shell` was originally conceived as a way to construct a shell environment containing the [tools needed to debug package builds](https://nixos.org/manual/nixpkgs/stable/#sec-tools-of-stdenv), such as Make or GCC.
Only later it became widely used as a general way to make temporary environments for other purposes.
`mkShellNoCC` is a function that produces such an environment, but without a compiler toolchain.
{lineno-start=1 emphasize-lines="2"}
```nix
let
nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/tarball/2a601aafdc5605a5133a2ca506a34a3a73377247";
pkgs = import nixpkgs { config = {}; overlays = []; };
in
```

We explicitly set `config` and `overlays` to avoid them being inadvertently overridden by [global configuration](https://nixos.org/manual/nixpkgs/stable/#chap-packageconfig).

`mkShellNoCC` takes as argument an attribute set.
`mkShellNoCC` is a function that takes as argument an attribute set.
Here we give it an attribute `packages` with a list containing two items from the `pkgs` attribute set.

:::{Dropdown} Side note on `packages` and `buildInputs`
You may encounter examples of `mkShell` or `mkShellNoCC` that add packages to the `buildInputs` or `nativeBuildInputs` attributes instead.
:::{Dropdown} Side note on `mkShell`

`nix-shell` and `mkShell` were originally conceived as a way to construct a shell environment containing the [tools needed to debug package builds](https://nixos.org/manual/nixpkgs/stable/#sec-tools-of-stdenv), such as Make or GCC.
Only later it became widely used as a general way to make temporary environments for other purposes.
`mkShellNoCC` is a function that produces such an environment, but without a compiler toolchain.

You may encounter examples of `mkShell` or `mkShellNoCC` that add packages to the `buildInputs` or `nativeBuildInputs` attributes instead.
`mkShellNoCC` is a [wrapper around `mkDerivation`](https://nixos.org/manual/nixpkgs/stable/#sec-pkgs-mkShell), so it takes the same arguments as `mkDerivation`, such as `buildInputs` or `nativeBuildInputs`.
The `packages` attribute argument to `mkShellNoCC` is simply an alias for `nativeBuildInputs`.
:::
Expand Down

0 comments on commit bbbed74

Please sign in to comment.