on master help sign in
Cleanup nix-run script
685f1ceMatthew Bauer
nix-run.sh+38−15
@@ -1,14 +1,27 @@
1#!/bin/sh
1#!/bin/sh
2
2
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+
3if [ -z "$1" ]; then
10if [ -z "$1" ]; then
4 >&2 echo "Need more than one argument."
11 >&2 echo "Need more than one argument."
12+ >&2 echo
13+ >&2 echo "Try:"
5− >&2 echo "Try: $ nix-run hello"
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"
6 exit 1
18 exit 1
7fi
19fi
8
20
9pkg=$1
21pkg=$1
10shift
22shift
11
23
24+# A second argument will provide a hint to run
12if ! [ -z "$1" ]; then
25if ! [ -z "$1" ]; then
13 name="$1"
26 name="$1"
14 shift
27 shift
@@ -16,7 +29,7 @@ else
16 name="$pkg"
29 name="$pkg"
17fi
30fi
18
31
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)
22
35
@@ -25,34 +38,44 @@ if [ -z "$out" ]; then
25 exit 1
38 exit 1
26fi
39fi
27
40
41+# Run DIR as a Darwin application
28−run_darwin_app() {
42+run_darwin_app () {
29− app="$1"
43+ dir="$1"
30 shift
44 shift
31− echo 'darwin'
32− open -a "$app" --args $@
33−}
34
45
35−run_linux_desktop_app() {
46+ open -a "$dir" --args $@
36− # taken from:
47+}
37− # https://askubuntu.com/questions/5172/running-a-desktop-file-in-the-terminal/5174#5174
38
48
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"
40 shift
54 shift
55+
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/" *$/')
58+
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]/$@/")
45 fi
61 fi
62+
46 cmd=$(echo "$cmd" | sed "s/%k/$desktop/;s/%.//")
63 cmd=$(echo "$cmd" | sed "s/%k/$desktop/;s/%.//")
47}
64}
48
65
66+# Run FILE as an ordinary binary
49run_bin () {
67run_bin () {
50− bin="$1"
68+ file="$1"
51 shift
69 shift
52− $bin $@
70+
71+ $file $@
53}
72}
54
73
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