Jump to content

Nix-shell shebang: Difference between revisions

From Official NixOS Wiki
imported>Tobias.bora
No edit summary
Rszyma (talk | contribs)
m change link to refer to below subsection
 
(11 intermediate revisions by 7 users not shown)
Line 8: Line 8:
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].
As of Nix 2.19.0 you can also use the new CLI <code>nix shell</code> and flakes to define shebangs. See [[Nix-shell shebang#New CLI alternatives|New CLI alternatives]] section.


== Examples ==
== Examples ==
Line 32: Line 32:
convert "$1" -scale 50% "$1.s50.jpg" &&
convert "$1" -scale 50% "$1.s50.jpg" &&
cowsay "done $1.q50.jpg"
cowsay "done $1.q50.jpg"
</syntaxHighlight>
=== C# ===
Using file-based apps, new in .NET 10:
<syntaxHighlight lang="csharp">
#!/usr/bin/env nix-shell
/*
#! nix-shell -i dotnet -p dotnetCorePackages.dotnet_10.sdk
*/
#:package Humanizer@2.14.1
using Humanizer;
Environment.CurrentDirectory = (string) AppContext.GetData("EntryPointFileDirectoryPath")!; // equivalent of `cd $(dirname $0)`
var dotNet9Released = DateTimeOffset.Parse("2024-12-03");
var since = DateTimeOffset.Now - dotNet9Released;
Console.WriteLine($"It has been {since.Humanize()} since .NET 9 was released.");
</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>
=== Lua ===
Stable CLI <!-- credit fabian-thomas https://github.com/NixOS/nix/issues/604#issuecomment-2692657032 -->
<syntaxHighlight lang="lua">
#!/usr/bin/env nix-shell
--[[
#!nix-shell -i lua -p lua5_3
--]]
print("Hello, world!")
</syntaxHighlight>
Pinned and eval-cached
<syntaxHighlight lang="lua">
#!/usr/bin/env nix
--[[
#!nix shell github:NixOS/nixpkgs?ref=88a6078fc5d104f480d1b4a0b58ffec5de965b1b#lua5_3 -c lua
--]]
print("Hello, world!")
</syntaxHighlight>
</syntaxHighlight>


Line 100: Line 153:
</syntaxHighlight>
</syntaxHighlight>


=== Haskell ===
=== Wolfram Language ===
<syntaxHighlight lang="haskell">
#! /usr/bin/env nix-shell
#! nix-shell -p "haskellPackages.ghcWithPackages (p: with p; [turtle])" -i runghc


{-# LANGUAGE OverloadedStrings #-}
Using [https://woxi.ad-si.com/ Woxi], the Rust reimplementation.


import Turtle
<syntaxhighlight lang="mathematica">
#!/usr/bin/env nix-shell
(*
#! nix-shell -p woxi -i woxi
*)


main = echo "Hello world!"
RandomInteger[{1, 9}, 5] // Map[#^2&] // Map[Print]
</syntaxHighlight>
</syntaxhighlight>


== Pinning nixpkgs ==
== Pinning nixpkgs ==
Line 124: Line 178:
</syntaxHighlight>
</syntaxHighlight>


== Flake ==
== New CLI alternatives ==


It is also possible to make it work for flake like in:
It is also possible to make it work with the new [[Nix (command)|Nix command (nix3)]], like so:
<syntaxHighlight lang="bash">
<syntaxhighlight lang="bash">#! /usr/bin/env nix
#!/usr/bin/env -S nix shell nixpkgs#bash nixpkgs#hello nixpkgs#cowsay --command bash
#! nix nixpkgs#bash nixpkgs#hello nixpkgs#cowsay --command bash


hello | cowsay
hello | cowsay</syntaxhighlight>Alternatively, using a single line (using <code>env -S</code>): <syntaxhighlight lang="bash">#! /usr/bin/env -S nix shell nixpkgs#bash nixpkgs#hello nixpkgs#cowsay --command bash
</syntaxHighlight>


The [https://nixos.org/manual/nix/stable/command-ref/new-cli/nix.html?highlight=shebang#shebang-interpreter doc] mentions that it should be possible to run more complex commands using multiple lines, but it does not work for me as reported [here https://github.com/NixOS/nixpkgs/issues/280033].
hello | cowsay</syntaxhighlight>


See this [https://nix.dev/manual/nix/2.19/command-ref/new-cli/nix3-shell#use-as-a--interpreter Nix manual page] for more details on nix3 shebangs.


== Performance ==
== Performance ==
Line 146: Line 200:
== See also ==
== See also ==


* [https://nixos.org/manual/nix/unstable/command-ref/nix-shell.html?highlight=shebang#use-as-a--interpreter nix-shell: Use As a #!-Interpreter] in nix manual
* [https://nix.dev/manual/nix/stable/command-ref/nix-shell.html?highlight=shebang#use-as-a--interpreter nix-shell: Use As a #!-Interpreter] man page
* [https://www.mankier.com/1/nix-shell#Use_As_a_%23!-Interpreter nix-shell: Use As a #!-Interpreter] man page
* [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]]

Latest revision as of 13:55, 28 October 2026

You can use nix-shell as a script interpreter to

  • run scripts in arbitrary languages
  • provide dependencies with Nix

To do this, start the script with multiple shebang (#!) lines.
The first shebang line is always #! /usr/bin/env nix-shell.
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 nix shell and flakes to define shebangs. See New CLI alternatives section.

Examples

Bash

To run bash scripts, set the interpreter with -i bash

#! /usr/bin/env nix-shell
#! nix-shell -i bash -p bash

echo hello world

You can use nix-shell -p ... to add dependencies:

#! /usr/bin/env nix-shell
#! nix-shell -i bash -p imagemagick cowsay

# scale image by 50%
convert "$1" -scale 50% "$1.s50.jpg" &&
cowsay "done $1.q50.jpg"

C#

Using file-based apps, new in .NET 10:

#!/usr/bin/env nix-shell
/*
#! nix-shell -i dotnet -p dotnetCorePackages.dotnet_10.sdk
*/
#:package Humanizer@2.14.1

using Humanizer;

Environment.CurrentDirectory = (string) AppContext.GetData("EntryPointFileDirectoryPath")!; // equivalent of `cd $(dirname $0)`

var dotNet9Released = DateTimeOffset.Parse("2024-12-03");
var since = DateTimeOffset.Now - dotNet9Released;

Console.WriteLine($"It has been {since.Humanize()} since .NET 9 was released.");

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!"

Lua

Stable CLI

#!/usr/bin/env nix-shell
--[[
#!nix-shell -i lua -p lua5_3
--]]
print("Hello, world!")

Pinned and eval-cached

#!/usr/bin/env nix
--[[
#!nix shell github:NixOS/nixpkgs?ref=88a6078fc5d104f480d1b4a0b58ffec5de965b1b#lua5_3 -c lua
--]]
print("Hello, world!")

Python

#! /usr/bin/env nix-shell
#! nix-shell -i python3 -p python3

print("hello world")
#! /usr/bin/env nix-shell
#! nix-shell -i python3 -p python3Packages.pillow python3Packages.ansicolor

# scale image by 50%
import sys, PIL.Image, ansicolor
path = sys.argv[1]
image = PIL.Image.open(path)
factor = 0.5
image = image.resize((round(image.width * factor), round(image.height * factor)))
path = path + ".s50.jpg"
image.save(path)
print(ansicolor.green(f"done {path}"))

Rust

No dependencies

#!/usr/bin/env nix-shell
#![allow()] /*
#!nix-shell -i bash -p rustc
rsfile="$(readlink -f $0)"
binfile="/tmp/$(basename "$rsfile").bin"
rustc "$rsfile" -o "$binfile" --edition=2021 && exec "$binfile" $@ || exit $?
*/
fn main() {
    for argument in std::env::args().skip(1) {
        println!("{}", argument);
    };
    println!("{}", std::env::var("HOME").expect(""));
}

With dependencies

uses rust-script

#!/usr/bin/env nix-shell
//! ```cargo
//! [dependencies]
//! time = "0.1.25"
//! ```
/*
#!nix-shell -i rust-script -p rustc -p rust-script -p cargo
*/
fn main() {
    for argument in std::env::args().skip(1) {
        println!("{}", argument);
    };
    println!("{}", std::env::var("HOME").expect(""));
    println!("{}", time::now().rfc822z());
}

Wolfram Language

Using Woxi, the Rust reimplementation.

#!/usr/bin/env nix-shell
(*
#! nix-shell -p woxi -i woxi
*)

RandomInteger[{1, 9}, 5] // Map[#^2&] // Map[Print]

Pinning nixpkgs

To pin nixpkgs to a specific version, add a third shebang line:

#! /usr/bin/env nix-shell
#! nix-shell -i bash
#! nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/aed4b19d312525ae7ca9bceb4e1efe3357d0e2eb.tar.gz

echo hello world

New CLI alternatives

It is also possible to make it work with the new Nix command (nix3), like so:

#! /usr/bin/env nix
#! nix nixpkgs#bash nixpkgs#hello nixpkgs#cowsay --command bash

hello | cowsay

Alternatively, using a single line (using env -S):

#! /usr/bin/env -S nix shell nixpkgs#bash nixpkgs#hello nixpkgs#cowsay --command bash

hello | cowsay

See this Nix manual page for more details on nix3 shebangs.

Performance

TODO ... why the startup delay? how to make it faster?

See also