Nix-shell shebang: Difference between revisions
imported>Milahu add link: Nix Flakes, Part 2: Evaluation caching - Tweag |
|||
(6 intermediate revisions by 5 users not shown) | |||
Line 7: | Line 7: | ||
The first shebang line is always <code>#! /usr/bin/env nix-shell</code>.<br> | The first shebang line is always <code>#! /usr/bin/env nix-shell</code>.<br> | ||
The second shebang line declares the script language and the script dependencies. | The second shebang line declares the script language and the script dependencies. | ||
As of Nix 2.19.0 you can also use the new CLI <code>nix shell</code> and flakes to define shebangs. See [https://nixos.org/manual/nix/stable/command-ref/new-cli/nix.html?highlight=shebang#shebang-interpreter docs]. | |||
== Examples == | == Examples == | ||
Line 98: | Line 100: | ||
</syntaxHighlight> | </syntaxHighlight> | ||
=== Haskell === | |||
<syntaxHighlight lang="haskell"> | |||
#! /usr/bin/env nix-shell | |||
#! nix-shell -p "haskellPackages.ghcWithPackages (p: with p; [turtle])" -i runghc | |||
{-# LANGUAGE OverloadedStrings #-} | |||
import Turtle | |||
main = echo "Hello world!" | |||
</syntaxHighlight> | |||
== Pinning nixpkgs == | == Pinning nixpkgs == | ||
Line 106: | Line 119: | ||
#! /usr/bin/env nix-shell | #! /usr/bin/env nix-shell | ||
#! nix-shell -i bash | #! nix-shell -i bash | ||
#! nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs | #! nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/aed4b19d312525ae7ca9bceb4e1efe3357d0e2eb.tar.gz | ||
echo hello world | echo hello world | ||
</syntaxHighlight> | </syntaxHighlight> | ||
== Flake == | |||
It is also possible to make it work for flake like in: | |||
<syntaxHighlight lang="bash"> | |||
#!/usr/bin/env -S nix shell nixpkgs#bash nixpkgs#hello nixpkgs#cowsay --command bash | |||
hello | cowsay | |||
</syntaxHighlight> | |||
The [https://nix.dev/manual/nix/2.19/command-ref/new-cli/nix3-shell doc] mentions that it should be possible to run more complex commands using multiple lines, but it does not work for me as reported [https://github.com/NixOS/nixpkgs/issues/280033 here]. | |||
== Performance == | == Performance == | ||
Line 125: | Line 150: | ||
* [https://gist.github.com/travisbhartwell/f972aab227306edfcfea nix-shell and Shebang Lines] | * [https://gist.github.com/travisbhartwell/f972aab227306edfcfea nix-shell and Shebang Lines] | ||
* [https://notes.yukiisbo.red/posts/2021/07/Spice_up_with_Nix_Scripts.html Spice up with Nix: Scripts with magical dependencies] | * [https://notes.yukiisbo.red/posts/2021/07/Spice_up_with_Nix_Scripts.html Spice up with Nix: Scripts with magical dependencies] | ||
[[Category:Nix]] | |||
[[Category:Shell]] |