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

working on #PR 325 - nix shell and shellbang (#427)

* changing to non-mobile wiki link

* adding link to nix-shell pure option.

* mentioning shebang just under the heading.

* adding bash in the listed included packages.

* corrected link snippet

* address review comments

Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
This commit is contained in:
Henrik 2023-02-16 18:23:23 +00:00 committed by GitHub
parent 710a10af81
commit fda85e22d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,7 +2,7 @@
# Reproducible interpreted scripts
In this tutorial, you will learn how to use Nix to create and run reproducible interpreted scripts.
In this tutorial, you will learn how to use Nix to create and run reproducible interpreted scripts, also known as [shebang] scripts.
## Requirements
@ -31,7 +31,7 @@ A [shebang] is the first line of a script starting with `#!`.
It determines which program to use for running the script.
[Bash]: https://www.gnu.org/software/bash/
[shebang]: https://en.m.wikipedia.org/wiki/Shebang_(Unix)
[shebang]: https://en.wikipedia.org/wiki/Shebang_(Unix)
We will use the shebang line `#! /usr/bin/env nix-shell`.
@ -42,9 +42,12 @@ We use [`nix-shell` as a shebang interpreter].
It takes the following parameters relevant for our use case:
- `-i` tells which program to use for interpreting the rest of the file
- `--pure` excludes most environment variables when the script is run
- `-p` lists packages that should be present in the interpreter's environment
- `-I` explicitly sets [the search path] for packages
More details on the options can be found in the [`nix-shell` reference documentation](https://nixos.org/manual/nix/stable/command-ref/nix-shell.html#options).
[`nix-shell` as a shebang interpreter]: https://nixos.org/manual/nix/stable/command-ref/nix-shell.html#use-as-a--interpreter
[the search path]: https://nixos.org/manual/nix/unstable/command-ref/opt-common.html#opt-I
@ -52,16 +55,18 @@ Create a file named `nixpkgs-releases.sh` with the following content:
```shell
#!/usr/bin/env nix-shell
#! nix-shell -i bash
#! nix-shell -p curl jq python3Packages.xmljson
#! nix-shell -i bash --pure
#! nix-shell -p bash cacert curl jq python3Packages.xmljson
#! nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/2a601aafdc5605a5133a2ca506a34a3a73377247.tar.gz
curl https://github.com/NixOS/nixpkgs/releases.atom | xml2json | jq .
```
We specify `bash` as the interpreter.
We specify the `bash` program as the interpreter with the `-i` option.
We enable the `--pure` option to prevent the script from implicitly using programs that may already exist on the system that will run the script.
The command `xml2json` is provided by the package `python3Packages.xmljson`, while the commands `jq` and `curl` are provided by packages of the same name.
With the `-p` option we specify the packages required for the script to run.
The command `xml2json` is provided by the package `python3Packages.xmljson`, while `bash`, `jq`, and `curl` are provided by packages of the same name. `cacert` must be present for SSL authentication to work. Use [search.nixos.org](https://search.nixos.org/packages) to find packages providing the program you need.
The parameter of `-I` refers to a specific Git commit of the Nixpkgs repository.
This ensures that the script will always run with the exact same packages versions, everywhere.