@@ -1,14 +1,27 @@
3+# nix-run.sh provides an easy way to run executables from Nix derivations
4+# without installing them. It will try to determine how to run the application
5+# based on what files are installable. Currently, macOS apps, Freedesktop apps,
6+# and ordinary binaries are handled.
3if [ -z "$1" ]; then
10if [ -z "$1" ]; then
4 >&2 echo "Need more than one argument."
11 >&2 echo "Need more than one argument."
5− >&2 echo "Try: $ nix-run hello"
14+ >&2 echo "$ nix-run hello"
16+ >&2 echo "To run the hello program"
17+ >&2 echo "or substitute hello with another package in Nixpkgs"
24+# A second argument will provide a hint to run
12if ! [ -z "$1" ]; then
25if ! [ -z "$1" ]; then
@@ -16,7 +29,7 @@ else
16 name="$pkg"
29 name="$pkg"
19−expr="with import <nixpkgs> {}; let x = $pkg; in x"
32+expr="with import <nixpkgs> {}; let x = ($pkg); in x"
20path=$(nix-instantiate --no-gc-warning -E "$expr")
33path=$(nix-instantiate --no-gc-warning -E "$expr")
21out=$(nix-store --no-gc-warning -r $path)
34out=$(nix-store --no-gc-warning -r $path)
@@ -25,34 +38,44 @@ if [ -z "$out" ]; then
41+# Run DIR as a Darwin application
28−run_darwin_app() {
42+run_darwin_app () {
32− open -a "$app" --args $@
35−run_linux_desktop_app() {
46+ open -a "$dir" --args $@
37− # https://askubuntu.com/questions/5172/running-a-desktop-file-in-the-terminal/5174#5174
39− desktop="$1"
49+# Run FILE as a Freedesktop application
51+# https://askubuntu.com/questions/5172/running-a-desktop-file-in-the-terminal/5174
52+run_linux_desktop_app () {
41− cmd=$(grep '^Exec' $desktop | tail -1 | \
56+ cmd=$(grep '^Exec' $file | tail -1 | \
42 sed 's/Exec=//;s/^"//;s/" *$/')
57 sed 's/Exec=//;s/^"//;s/" *$/')
43 if ! [ -z "$@" ]; then
59 if ! [ -z "$@" ]; then
44 cmd=$(echo "$cmd" | sed "s/%[fu]/$1/;s/%[FU]/$@/")
60 cmd=$(echo "$cmd" | sed "s/%[fu]/$1/;s/%[FU]/$@/")
46 cmd=$(echo "$cmd" | sed "s/%k/$desktop/;s/%.//")
63 cmd=$(echo "$cmd" | sed "s/%k/$desktop/;s/%.//")
66+# Run FILE as an ordinary binary
49run_bin () {
67run_bin () {
50− bin="$1"
68+ file="$1"
74+if [ -x "$out/nix-support/run" ]; then
75+ run_bin "$out/nix-support/run" $@
76+elif [ -x "$out/bin/run" ]; then
77+ run_bin "$out/bin/run" $@
55−if [ "$(uname)" = Darwin ] && [ -d "$out/Applications/$name.app" ]; then
78+elif [ "$(uname)" = Darwin ] && [ -d "$out/Applications/$name.app" ]; then
56 run_darwin_app "$out/Applications/$name.app" $@
79 run_darwin_app "$out/Applications/$name.app" $@
57elif [ "$(uname)" = Darwin ] && [ -d $out/Applications/*.app ]; then
80elif [ "$(uname)" = Darwin ] && [ -d $out/Applications/*.app ]; then
58 for f in $out/Applications/*.app; do
81 for f in $out/Applications/*.app; do