1
0
Fork 0
mirror of https://github.com/NixOS/nix.dev.git synced 2024-10-18 00:06:26 -04:00

use nixShellNoCC in tutorials and guides

in our examples we never actually use the compiler toolchain stuff from
`mkShell`, so we may as well not burden users with large downloads.
This commit is contained in:
Valentin Gagarin 2024-02-06 21:16:17 +01:00
parent e0e2f95866
commit 5ec1a4967c
6 changed files with 24 additions and 23 deletions

View file

@ -38,7 +38,7 @@ Add niv to the development environment for your project to have it readily avail
build = pkgs.hello; build = pkgs.hello;
in { in {
inherit build; inherit build;
+ shell = pkgs.mkShell { + shell = pkgs.mkShellNoCC {
+ inputsFrom = [ build ]; + inputsFrom = [ build ];
+ packages = with pkgs; [ + packages = with pkgs; [
+ niv + niv

View file

@ -14,7 +14,7 @@ let
pkgs = import nixpkgs { config = {}; overlays = []; }; pkgs = import nixpkgs { config = {}; overlays = []; };
in in
pkgs.mkShell { pkgs.mkShellNoCC {
packages = with pkgs; [ packages = with pkgs; [
hello hello
]; ];
@ -45,7 +45,7 @@ Make the following addition:
pkgs = import nixpkgs { config = {}; overlays = []; }; pkgs = import nixpkgs { config = {}; overlays = []; };
in in
pkgs.mkShell { pkgs.mkShellNoCC {
packages = with pkgs; [ packages = with pkgs; [
hello hello
]; ];

View file

@ -33,7 +33,7 @@ Create a new file `shell.nix` to declare the development environment:
```{code-block} nix shell.nix ```{code-block} nix shell.nix
{ pkgs ? import (fetchTarball "https://github.com/NixOS/nixpkgs/tarball/nixos-22.11") {} }: { pkgs ? import (fetchTarball "https://github.com/NixOS/nixpkgs/tarball/nixos-22.11") {} }:
pkgs.mkShell { pkgs.mkShellNoCC {
packages = with pkgs; [ packages = with pkgs; [
(python3.withPackages (ps: [ ps.flask ])) (python3.withPackages (ps: [ ps.flask ]))
curl curl

View file

@ -7,7 +7,7 @@ How to share the package's dependencies in `default.nix` with the development en
## Summary ## Summary
Use the [`inputsFrom` attribute to `pkgs.mkShell`](https://nixos.org/manual/nixpkgs/stable/#sec-pkgs-mkShell-attributes): Use the [`inputsFrom` attribute to `pkgs.mkShellNoCC`](https://nixos.org/manual/nixpkgs/stable/#sec-pkgs-mkShell-attributes):
```nix ```nix
# default.nix # default.nix
@ -17,7 +17,7 @@ let
in in
{ {
inherit build; inherit build;
shell = pkgs.mkShell { shell = pkgs.mkShellNoCC {
inputsFrom = [ build ]; inputsFrom = [ build ];
}; };
} }
@ -67,7 +67,7 @@ Add an attribute to `default.nix` specifying an environment:
in in
{ {
build = pkgs.callPackage ./build.nix {}; build = pkgs.callPackage ./build.nix {};
+ shell = pkgs.mkShell { + shell = pkgs.mkShellNoCC {
+ }; + };
} }
``` ```
@ -84,7 +84,7 @@ Then take the package's dependencies into the environment with [`inputsFrom`](ht
{ {
- build = pkgs.callPackage ./build.nix {}; - build = pkgs.callPackage ./build.nix {};
+ inherit build; + inherit build;
shell = pkgs.mkShell { shell = pkgs.mkShellNoCC {
+ inputsFrom = [ build ]; + inputsFrom = [ build ];
}; };
} }

View file

@ -56,7 +56,7 @@ let
pkgs = import nixpkgs { config = {}; overlays = []; }; pkgs = import nixpkgs { config = {}; overlays = []; };
in in
pkgs.mkShell { pkgs.mkShellNoCC {
packages = with pkgs; [ packages = with pkgs; [
git git
neovim neovim
@ -68,18 +68,19 @@ pkgs.mkShell {
::::{dropdown} Detailed explanation ::::{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>), and explicitly set configuration options and overlays to avoid them being inadvertently overridden by [global configuration](https://nixos.org/manual/nixpkgs/stable/#chap-packageconfig).
`mkShell` is a function that produces a shell environment. `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.
It takes as argument an attribute set. Only later it became widely used as a general way to make temporary environments for other purposes.
`mkShellNoCC` is a function that produces a such an environment, but without a compiler toolchain.
`mkShellNoCC` takes as argument an attribute set.
Here we give it an attribute `packages` with a list containing one item from the `pkgs` attribute set. Here we give it an attribute `packages` with a list containing one item from the `pkgs` attribute set.
:::{Dropdown} Side note on `packages` and `buildInputs` :::{Dropdown} Side note on `packages` and `buildInputs`
You may encounter examples of `mkShell` that add packages to the `buildInputs` or `nativeBuildInputs` attributes instead. You may encounter examples of `mkShell` or `mkShellNoCC` that add packages to the `buildInputs` or `nativeBuildInputs` attributes instead.
`nix-shell` was originally conceived as a way to construct a shell environment containing the tools needed to debug package builds.
Only later it became widely used as a general way to make temporary environments for other purposes.
`mkShell` 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`. `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 `mkShell` is simply an alias for `nativeBuildInputs`. The `packages` attribute argument to `mkShellNoCC` is simply an alias for `nativeBuildInputs`.
::: :::
:::: ::::
@ -108,7 +109,7 @@ Set your `GIT_EDITOR` to use the `nvim` from the shell environment:
pkgs = import nixpkgs { config = {}; overlays = []; }; pkgs = import nixpkgs { config = {}; overlays = []; };
in in
pkgs.mkShell { pkgs.mkShellNoCC {
packages = with pkgs; [ packages = with pkgs; [
git git
neovim neovim
@ -119,7 +120,7 @@ Set your `GIT_EDITOR` to use the `nvim` from the shell environment:
} }
``` ```
Any attribute name passed to `mkShell` that is not reserved otherwise and has a value which can be coerced to a string will end up as an environment variable. Any attribute name passed to `mkShellNoCC` that is not reserved otherwise and has a value which can be coerced to a string will end up as an environment variable.
:::{dropdown} Detailed explanation :::{dropdown} Detailed explanation
@ -138,7 +139,7 @@ If you really need to override these protected environment variables, use the `s
## Startup commands ## Startup commands
You may want to run some commands before entering the shell environment. You may want to run some commands before entering the shell environment.
These commands can be placed in the `shellHook` attribute provided to `mkShell`. These commands can be placed in the `shellHook` attribute provided to `mkShellNoCC`.
Set `shellHook` to output the current repository status: Set `shellHook` to output the current repository status:
@ -148,7 +149,7 @@ Set `shellHook` to output the current repository status:
pkgs = import nixpkgs { config = {}; overlays = []; }; pkgs = import nixpkgs { config = {}; overlays = []; };
in in
pkgs.mkShell { pkgs.mkShellNoCC {
packages = with pkgs; [ packages = with pkgs; [
git git
neovim neovim

View file

@ -1935,7 +1935,7 @@ The goal of the following exercises is not to understand what the code means or
let let
message = "hello world"; message = "hello world";
in in
pkgs.mkShell { pkgs.mkShellNoCC {
buildInputs = with pkgs; [ cowsay ]; buildInputs = with pkgs; [ cowsay ];
shellHook = '' shellHook = ''
cowsay ${message} cowsay ${message}
@ -1951,9 +1951,9 @@ Explanation:
- If the argument has the attribute `pkgs`, it will be used in the function body. - If the argument has the attribute `pkgs`, it will be used in the function body.
Otherwise, by default, import the Nix expression in the file found on the lookup path `<nixpkgs>` (which is a function in this case), call the function with an empty attribute set, and use the resulting value. Otherwise, by default, import the Nix expression in the file found on the lookup path `<nixpkgs>` (which is a function in this case), call the function with an empty attribute set, and use the resulting value.
- The name `message` is bound to the string value `"hello world"`. - The name `message` is bound to the string value `"hello world"`.
- The attribute `mkShell` of the `pkgs` set is a function that is passed an attribute set as argument. - The attribute `mkShellNoCC` of the `pkgs` set is a function that is passed an attribute set as argument.
Its return value is also the result of the outer function. Its return value is also the result of the outer function.
- The attribute set passed to `mkShell` has the attributes `buildInputs` (set to a list with one element: the `cowsay` attribute from `pkgs`) and `shellHook` (set to an indented string). - The attribute set passed to `mkShellNoCC` has the attributes `buildInputs` (set to a list with one element: the `cowsay` attribute from `pkgs`) and `shellHook` (set to an indented string).
- The indented string contains an interpolated expression, which will expand the value of `message` to yield `"hello world"`. - The indented string contains an interpolated expression, which will expand the value of `message` to yield `"hello world"`.