on master help sign in
Add nix-run.sh
commit6159d8e3f6a15f079997e893b3dae29f4ce1c593 authorMatthew Bauer <mjbauer95@gmail.com> dateFri 13 Oct 2017, 12:14 (9 years ago) parent6d17650
2 files changed+77−1
Makefile+1−1 nix-run.sh+76
Makefile+1−1
@@ -1,5 +1,5 @@
11PREFIX ?= /usr
22
3−install: nix-bundle.sh appdir.nix appimagetool.nix appimage.nix AppRun.c appimage-top.nix default.nix appdir.sh nix2appimage.sh
3+install: nix-bundle.sh nix-run.sh appdir.nix appimagetool.nix appimage.nix AppRun.c appimage-top.nix default.nix appdir.sh nix2appimage.sh
44 mkdir -p ${PREFIX}/share/nix-bundle/
55 install $^ ${PREFIX}/share/nix-bundle/
nix-run.shnew file+76
@@ -0,0 +1,76 @@
1+#!/bin/sh
2+
3+if [ -z "$1" ]; then
4+ >&2 echo "Need more than one argument."
5+ >&2 echo "Try: $ nix-run hello"
6+ exit 1
7+fi
8+
9+pkg=$1
10+shift
11+
12+if ! [ -z "$1" ]; then
13+ name="$1"
14+ shift
15+else
16+ name="$pkg"
17+fi
18+
19+expr="with import <nixpkgs> {}; let x = $pkg; in x"
20+path=$(nix-instantiate --no-gc-warning -E "$expr")
21+out=$(nix-store --no-gc-warning -r $path)
22+
23+if [ -z "$out" ]; then
24+ >&2 echo "Could not evaluate $pkg to a Nix drv."
25+ exit 1
26+fi
27+
28+run_darwin_app() {
29+ app="$1"
30+ shift
31+ echo 'darwin'
32+ open -a "$app" --args $@
33+}
34+
35+run_linux_desktop_app() {
36+ # taken from:
37+ # https://askubuntu.com/questions/5172/running-a-desktop-file-in-the-terminal/5174#5174
38+
39+ desktop="$1"
40+ shift
41+ cmd=$(grep '^Exec' $desktop | tail -1 | \
42+ sed 's/Exec=//;s/^"//;s/" *$/')
43+ if ! [ -z "$@" ]; then
44+ cmd=$(echo "$cmd" | sed "s/%[fu]/$1/;s/%[FU]/$@/")
45+ fi
46+ cmd=$(echo "$cmd" | sed "s/%k/$desktop/;s/%.//")
47+}
48+
49+run_bin () {
50+ bin="$1"
51+ shift
52+ $bin $@
53+}
54+
55+if [ "$(uname)" = Darwin ] && [ -d "$out/Applications/$name.app" ]; then
56+ run_darwin_app "$out/Applications/$name.app" $@
57+elif [ "$(uname)" = Darwin ] && [ -d $out/Applications/*.app ]; then
58+ for f in $out/Applications/*.app; do
59+ run_darwin_app "$f" $@
60+ done
61+elif [ -f "$out/share/applications/$name.desktop" ]; then
62+ run_linux_desktop_app "$out/share/applications/$name.desktop" $@
63+elif [ -d $out/share/applications ]; then
64+ for f in $out/share/applications/*.desktop; do
65+ run_linux_desktop_app "$f"
66+ done
67+elif [ -x "$out/bin/$name" ]; then
68+ run_bin "$out/bin/$name" $@
69+elif [ -d "$out/bin" ]; then
70+ for bin in $out/bin/*; do
71+ run_bin "$bin" $@
72+ done
73+else
74+ >&2 echo "Cannot find a way to run path $out."
75+ exit 1
76+fi