Nix-shell shebang: Difference between revisions

imported>Mausch
Add link to new CLI docs
N0099 (talk | contribs)
 
(7 intermediate revisions by 4 users not shown)
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>
</syntaxHighlight>


Line 123: Line 144:
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 134: Line 167:
== 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]]