@@ -1,14 +1,27 @@
11#!/bin/sh
22
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.
7+
8+# Usage
9+
310if [ -z "$1" ]; then
411 >&2 echo "Need more than one argument."
5− >&2 echo "Try: $ nix-run hello"
12+ >&2 echo
13+ >&2 echo "Try:"
14+ >&2 echo "$ nix-run hello"
15+ >&2 echo
16+ >&2 echo "To run the hello program"
17+ >&2 echo "or substitute hello with another package in Nixpkgs"
618 exit 1
719fi
820
921pkg=$1
1022shift
1123
24+# A second argument will provide a hint to run
1225if ! [ -z "$1" ]; then
1326 name="$1"
1427 shift
@@ -16,7 +29,7 @@ else
1629 name="$pkg"
1730fi
1831
19−expr="with import <nixpkgs> {}; let x = $pkg; in x"
32+expr="with import <nixpkgs> {}; let x = ($pkg); in x"
2033path=$(nix-instantiate --no-gc-warning -E "$expr")
2134out=$(nix-store --no-gc-warning -r $path)
2235
@@ -25,34 +38,44 @@ if [ -z "$out" ]; then
2538 exit 1
2639fi
2740
28−run_darwin_app() {
29− app="$1"
41+# Run DIR as a Darwin application
42+run_darwin_app () {
43+ dir="$1"
3044 shift
31− echo 'darwin'
32− open -a "$app" --args $@
33−}
3445
35−run_linux_desktop_app() {
36− # taken from:
37− # https://askubuntu.com/questions/5172/running-a-desktop-file-in-the-terminal/5174#5174
46+ open -a "$dir" --args $@
47+}
3848
39− desktop="$1"
49+# Run FILE as a Freedesktop application
50+# taken from:
51+# https://askubuntu.com/questions/5172/running-a-desktop-file-in-the-terminal/5174
52+run_linux_desktop_app () {
53+ file="$1"
4054 shift
41− cmd=$(grep '^Exec' $desktop | tail -1 | \
55+
56+ cmd=$(grep '^Exec' $file | tail -1 | \
4257 sed 's/Exec=//;s/^"//;s/" *$/')
58+
4359 if ! [ -z "$@" ]; then
4460 cmd=$(echo "$cmd" | sed "s/%[fu]/$1/;s/%[FU]/$@/")
4561 fi
62+
4663 cmd=$(echo "$cmd" | sed "s/%k/$desktop/;s/%.//")
4764}
4865
66+# Run FILE as an ordinary binary
4967run_bin () {
50− bin="$1"
68+ file="$1"
5169 shift
52− $bin $@
70+
71+ $file $@
5372}
5473
55−if [ "$(uname)" = Darwin ] && [ -d "$out/Applications/$name.app" ]; then
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" $@
78+elif [ "$(uname)" = Darwin ] && [ -d "$out/Applications/$name.app" ]; then
5679 run_darwin_app "$out/Applications/$name.app" $@
5780elif [ "$(uname)" = Darwin ] && [ -d $out/Applications/*.app ]; then
5881 for f in $out/Applications/*.app; do