on master help sign in
AppRun.c+9−1
since splitsnapshots
unifiedsplit
@@ -207,6 +207,11 @@ int main(int argc, char *argv[]) {
207 err_exit("mount(%s, %s)", usrdir, path_buf);
207 err_exit("mount(%s, %s)", usrdir, path_buf);
208 }
208 }
209
209
210+ snprintf(path_buf, sizeof(path_buf), "%s/bin", rootdir);
211+ if (symlink("/usr/bin", path_buf) < 0) {
212+ err_exit("symlink(/usr/bin, %s)", path_buf);
213+ }
214+
210 // fixes issue #1 where writing to /proc/self/gid_map fails
215 // fixes issue #1 where writing to /proc/self/gid_map fails
211 // see user_namespaces(7) for more documentation
216 // see user_namespaces(7) for more documentation
212 int fd_setgroups = open("/proc/self/setgroups", O_WRONLY);
217 int fd_setgroups = open("/proc/self/setgroups", O_WRONLY);
@@ -226,8 +231,10 @@ int main(int argc, char *argv[]) {
226 err_exit("chroot(%s)", rootdir);
231 err_exit("chroot(%s)", rootdir);
227 }
232 }
228
233
234+ char *pwddir = getenv("PWD");
229− chdir("/");
235+ chdir(pwddir);
230
236
237+ SAVE_ENV_VAR(PWD);
231 SAVE_ENV_VAR(DBUS_SESSION_BUS_ADDRESS);
238 SAVE_ENV_VAR(DBUS_SESSION_BUS_ADDRESS);
232 SAVE_ENV_VAR(USER);
239 SAVE_ENV_VAR(USER);
233 SAVE_ENV_VAR(HOSTNAME);
240 SAVE_ENV_VAR(HOSTNAME);
@@ -243,6 +250,7 @@ int main(int argc, char *argv[]) {
243
250
244 clearenv();
251 clearenv();
245
252
253+ LOAD_ENV_VAR(PWD);
246 LOAD_ENV_VAR(DBUS_SESSION_BUS_ADDRESS);
254 LOAD_ENV_VAR(DBUS_SESSION_BUS_ADDRESS);
247 LOAD_ENV_VAR(USER);
255 LOAD_ENV_VAR(USER);
248 LOAD_ENV_VAR(HOSTNAME);
256 LOAD_ENV_VAR(HOSTNAME);
README.md+14−1
@@ -35,14 +35,27 @@ Some others to try:
35./nix-bundle.sh nano /bin/nano
35./nix-bundle.sh nano /bin/nano
36```
36```
37
37
38+```sh
39+./nix-bundle.sh emacs /bin/emacs
40+```
41+
42+Or if you want to try graphical applications:
43+
44+```sh
45+# Simple X game. Very few dependencies. Quick to build and load. ~13MB
46+./nix-bundle.sh xskat /bin/xskat
47+```
48+
38```sh
49```sh
39./nix-bundle.sh firefox /bin/firefox
50./nix-bundle.sh firefox /bin/firefox
40```
51```
41
52
42```sh
53```sh
54+# SDL-based game. ~228MB
43−./nix-bundle.sh emacs /bin/emacs
55+./nix-bundle.sh ivan /bin/ivan
44```
56```
45
57
58+
46## Self-bundling (meta)
59## Self-bundling (meta)
47
60
48Starting with v0.1.3, you can bundle nix-bundle! To do this, just use nix-bundle normally:
61Starting with v0.1.3, you can bundle nix-bundle! To do this, just use nix-bundle normally:
appdir.nix+3−3
@@ -1,4 +1,4 @@
1−{ stdenv, fetchurl, muslPkgs, perl, pathsFromGraph, fetchFromGitHub, coreutils, bash }:
1+{ stdenv, lib, fetchurl, muslPkgs, perl, pathsFromGraph, fetchFromGitHub, coreutils, bash }:
2
2
3let
3let
4 AppRun = targets: muslPkgs.stdenv.mkDerivation {
4 AppRun = targets: muslPkgs.stdenv.mkDerivation {
@@ -8,7 +8,7 @@ let
8
8
9 buildPhase = ''
9 buildPhase = ''
10 CC="$CC -O2 -Wall -Wno-deprecated-declarations -Wno-unused-result -static"
10 CC="$CC -O2 -Wall -Wno-deprecated-declarations -Wno-unused-result -static"
11− $CC ${./AppRun.c} -o AppRun -DENV_PATH='"${stdenv.lib.makeBinPath targets}"'
11+ $CC ${./AppRun.c} -o AppRun -DENV_PATH='"${lib.makeBinPath targets}"'
12 '';
12 '';
13
13
14 installPhase = ''
14 installPhase = ''
@@ -42,7 +42,7 @@ in
42 cd $out/${name}.AppDir
42 cd $out/${name}.AppDir
43
43
44 mkdir -p nix/store
44 mkdir -p nix/store
45− cp -Lr $storePaths nix/store
45+ cp -r $storePaths nix/store
46
46
47 ln -s .${target} usr
47 ln -s .${target} usr
48
48
appimage-bundle.nix+22−18
@@ -1,5 +1,7 @@
1# use like this:
1# use like this:
2# nix-build appimage-bundle.nix --argstr package hello --argstr exec hello
2# nix-build appimage-bundle.nix --argstr package hello --argstr exec hello
3+# nix-build appimage-bundle.nix --arg package 'with import <nixpkgs>{}; writers.writePython3Bin "helloThere.py" {} "print(1)\n"' --argstr exec helloThere.py
4+
3
5
4{nixpkgs ? import <nixpkgs>{},
6{nixpkgs ? import <nixpkgs>{},
5package,
7package,
@@ -15,26 +17,18 @@ let
15 };
17 };
16 src = env;
18 src = env;
17 inherit exec;
19 inherit exec;
18− buildInputs = [ drv nixpkgs.coreutils nixpkgs.gnutar nixpkgs.xz ];
20+ buildInputs = [ drv ];
19− usr_fonts = buildEnv {
20− name = "fonts";
21− paths = [noto-fonts];
22− };
23 buildCommand = ''
21 buildCommand = ''
24− source $stdenv/setup
22+ mkdir -p $out/share/icons/hicolor/256x256/apps
25− mkdir -p $out/bin
23+ mkdir -p $out/share/applications
26− cp -rL ${env}/* $out/
27− chmod +w -R $out/
28
24
29− mkdir -p $out/share/fonts
25+ shopt -s extglob
26+ ln -s ${env}/!(share) $out/
30− cp ${usr_fonts}/share/fonts/* $out/share/fonts -R
27+ ln -s ${env}/share/* $out/share/
31
28
32− mkdir -p $out/share/icons
33− mkdir -p $out/share/icons/hicolor/256x256/apps
34 touch $out/share/icons/hicolor/256x256/apps/${drv.name}.png
29 touch $out/share/icons/hicolor/256x256/apps/${drv.name}.png
35 touch $out/share/icons/${drv.name}.png
30 touch $out/share/icons/${drv.name}.png
36
31
37− mkdir -p $out/share/applications
38 cat <<EOF > $out/share/applications/${drv.name}.desktop
32 cat <<EOF > $out/share/applications/${drv.name}.desktop
39 [Desktop Entry]
33 [Desktop Entry]
40 Type=Application
34 Type=Application
@@ -50,8 +44,18 @@ let
50 };
44 };
51
45
52in
46in
47+ let results =
48+ if (nixpkgs.lib.isDerivation package && !(nixpkgs.lib.isString package))
49+ then {
50+ name = package.name;
51+ target = appimage_src package "${exec}";
52+ extraTargets = [];
53+ }
54+ else {
55+ name = nixpkgs."${package}".name;
56+ target = appimage_src (nixpkgs."${package}") "${exec}";
57+ extraTargets = [];
58+ };
59+ in
53 with (import (./appimage-top.nix){nixpkgs' = nixpkgs.path;});
60 with (import (./appimage-top.nix){nixpkgs' = nixpkgs.path;});
54− (appimage (appdir {
55− name = package;
56− target = appimage_src nixpkgs."${package}" "${exec}";
57− })).overrideAttrs (old: {name = package;})
61+ (appimage (appdir results )).overrideAttrs (old: {name = results.name;})
appimagetool.nix+2−2
@@ -1,4 +1,4 @@
1−{ stdenv, fetchurl, fuse, zlib, squashfsTools, glib }:
1+{ stdenv, lib, fetchurl, fuse, zlib, squashfsTools, glib }:
2
2
3# This is from some binaries.
3# This is from some binaries.
4
4
@@ -37,7 +37,7 @@ in stdenv.mkDerivation rec {
37 for x in $out/bin/*; do
37 for x in $out/bin/*; do
38 patchelf \
38 patchelf \
39 --set-interpreter ${dynamicLinker} \
39 --set-interpreter ${dynamicLinker} \
40− --set-rpath ${stdenv.lib.makeLibraryPath [ zlib stdenv.glibc.out fuse glib ]} \
40+ --set-rpath ${lib.makeLibraryPath [ zlib stdenv.glibc.out fuse glib ]} \
41 $x
41 $x
42 done
42 done
43 '';
43 '';
default.nix+25−8
@@ -10,6 +10,16 @@ let
10 '';
10 '';
11 });
11 });
12in rec {
12in rec {
13+ toStorePath = target:
14+ # If a store path has been given but is not a derivation, add the missing context
15+ # to it so it will be propagated properly as a build input.
16+ if !(lib.isDerivation target) && lib.isStorePath target then
17+ let path = toString target; in
18+ builtins.appendContext path { "${path}" = { path = true; }; }
19+ # Otherwise, add to the store. This takes care of appending the store path
20+ # in the context automatically.
21+ else "${target}";
22+
13 arx = { archive, startup}:
23 arx = { archive, startup}:
14 stdenv.mkDerivation {
24 stdenv.mkDerivation {
15 name = "arx";
25 name = "arx";
@@ -35,7 +45,7 @@ in rec {
35 };
45 };
36
46
37 # TODO: eventually should this go in nixpkgs?
47 # TODO: eventually should this go in nixpkgs?
38− nix-user-chroot = stdenv.lib.makeOverridable stdenv.mkDerivation {
48+ nix-user-chroot = lib.makeOverridable stdenv.mkDerivation {
39 name = "nix-user-chroot-2c52b5f";
49 name = "nix-user-chroot-2c52b5f";
40 src = ./nix-user-chroot;
50 src = ./nix-user-chroot;
41
51
@@ -58,6 +68,8 @@ in rec {
58
68
59 runHook postInstall
69 runHook postInstall
60 '';
70 '';
71+
72+ meta.platforms = lib.platforms.linux;
61 };
73 };
62
74
63 makebootstrap = { targets, startup }:
75 makebootstrap = { targets, startup }:
@@ -68,15 +80,20 @@ in rec {
68 };
80 };
69 };
81 };
70
82
71− makeStartup = { target, nixUserChrootFlags, nix-user-chroot', run }:
83+ makeStartup = { target, nixUserChrootFlags, nix-user-chroot', run, initScript }:
84+ let
85+ # Avoid re-adding a store path into the store
86+ path = toStorePath target;
87+ in
72 writeScript "startup" ''
88 writeScript "startup" ''
73 #!/bin/sh
89 #!/bin/sh
90+ ${initScript}
74− .${nix-user-chroot'}/bin/nix-user-chroot -n ./nix ${nixUserChrootFlags} -- ${target}${run} $@
91+ .${nix-user-chroot'}/bin/nix-user-chroot -n ./nix ${nixUserChrootFlags} -- ${path}${run} "$@"
75 '';
92 '';
76
93
77− nix-bootstrap = { target, extraTargets ? [], run, nix-user-chroot' ? nix-user-chroot, nixUserChrootFlags ? "" }:
94+ nix-bootstrap = { target, extraTargets ? [], run, nix-user-chroot' ? nix-user-chroot, nixUserChrootFlags ? "", initScript ? "" }:
78 let
95 let
79− script = makeStartup { inherit target nixUserChrootFlags nix-user-chroot' run; };
96+ script = makeStartup { inherit target nixUserChrootFlags nix-user-chroot' run initScript; };
80 in makebootstrap {
97 in makebootstrap {
81 startup = ".${script} '\"$@\"'";
98 startup = ".${script} '\"$@\"'";
82 targets = [ "${script}" ] ++ extraTargets;
99 targets = [ "${script}" ] ++ extraTargets;
@@ -93,10 +110,10 @@ in rec {
93 nix-user-chroot'' = targets: nix-user-chroot.overrideDerivation (o: {
110 nix-user-chroot'' = targets: nix-user-chroot.overrideDerivation (o: {
94 buildInputs = o.buildInputs ++ targets;
111 buildInputs = o.buildInputs ++ targets;
95 makeFlags = o.makeFlags ++ [
112 makeFlags = o.makeFlags ++ [
96− ''ENV_PATH="${stdenv.lib.makeBinPath targets}"''
113+ ''ENV_PATH="${lib.makeBinPath targets}"''
97 ];
114 ];
98− }); in { target, extraTargets ? [], run }: nix-bootstrap {
115+ }); in { target, extraTargets ? [], run, initScript ? "" }: nix-bootstrap {
99− inherit target extraTargets run;
116+ inherit target extraTargets run initScript;
100 nix-user-chroot' = nix-user-chroot'' extraTargets;
117 nix-user-chroot' = nix-user-chroot'' extraTargets;
101 };
118 };
102}
119}
flake.nixnew file+26
@@ -0,0 +1,26 @@
1+{
2+ description = "The purely functional package manager";
3+
4+ inputs.nixpkgs.url = "nixpkgs/nixos-20.03-small";
5+
6+ outputs = { self, nixpkgs }: let
7+ systems = [ "x86_64-linux" "i686-linux" "aarch64-linux" ];
8+ forAllSystems = f: nixpkgs.lib.genAttrs systems (system: f system);
9+ in {
10+ bundlers = {
11+ nix-bundle = { program, system }: let
12+ nixpkgs' = nixpkgs.legacyPackages.${system};
13+ nix-bundle = import self { nixpkgs = nixpkgs'; };
14+ script = nixpkgs'.writeScript "startup" ''
15+ #!/bin/sh
16+ .${nix-bundle.nix-user-chroot}/bin/nix-user-chroot -n ./nix -- ${program} "$@"
17+ '';
18+ in nix-bundle.makebootstrap {
19+ targets = [ script ];
20+ startup = ".${builtins.unsafeDiscardStringContext script} '\"$@\"'";
21+ };
22+ };
23+
24+ defaultBundler = self.bundlers.nix-bundle;
25+ };
26+}
nix-bootstrap.sh+13−13
@@ -1,35 +1,35 @@
1#!/bin/sh
1#!/bin/sh
2
2
3−cmd=sh
4−if ! [ $# -eq 0 ]; then
3+if [ $# -gt 0 ]; then
5− cmd=$@
4+ set -- sh
6fi
5fi
7
6
8# should download this in the future
7# should download this in the future
9# but the mirror is down
8# but the mirror is down
10−proot=`dirname $0`/proot-`uname -p`
9+proot=$(dirname "$0")/proot-$(uname -p)
11export PROOT_NO_SECCOMP=1
10export PROOT_NO_SECCOMP=1
12
11
13nixdir=$HOME/.nix
12nixdir=$HOME/.nix
14
13
15OLD_NIX_PATH=$NIX_PATH
14OLD_NIX_PATH=$NIX_PATH
15+# shellcheck disable=SC1090
16−if [ -e $HOME/.nix-profile/etc/profile.d/nix.sh ]; then . $HOME/.nix-profile/etc/profile.d/nix.sh; fi
16+if [ -e "$HOME/.nix-profile/etc/profile.d/nix.sh" ]; then . "$HOME/.nix-profile/etc/profile.d/nix.sh"; fi
17−if ! [ -z "$OLD_NIX_PATH" ]; then NIX_PATH="$OLD_NIX_PATH"; fi
17+if [ -n "$OLD_NIX_PATH" ]; then NIX_PATH="$OLD_NIX_PATH"; fi
18
18
19if [ -z "$IN_PROOT" ]; then
19if [ -z "$IN_PROOT" ]; then
20 export IN_PROOT=1
20 export IN_PROOT=1
21
21
22− if ! [ -d $nixdir ]; then
22+ if ! [ -d "$nixdir" ]; then
23− mkdir -p $nixdir
23+ mkdir -p "$nixdir"
24 s=$(mktemp)
24 s=$(mktemp)
25− curl https://nixos.org/nix/install -o $s
25+ curl https://nixos.org/nix/install -o "$s"
26− $proot -b $nixdir:/nix $0 sh $s
26+ $proot -b "$nixdir:/nix" "$0" sh "$s"
27 fi
27 fi
28
28
29− $proot -b $nixdir:/nix $0 $cmd
29+ $proot -b "$nixdir:/nix" "$0" "$@"
30
30
31 export IN_PROOT=
31 export IN_PROOT=
32 exit
32 exit
33−elif ! [ $# -eq 0 ]; then
33+elif [ $# -gt 0 ]; then
34− exec $cmd
34+ exec "$@"
35fi
35fi
nix-bundle.sh+39−156
@@ -1,180 +1,63 @@
1#!/usr/bin/env sh
1#!/usr/bin/env sh
2
2
3−fail() {
3+if [ "$#" -lt 2 ]; then
4− echo "Try '$0 --help'." >&2
5− exit 2
6−}
7−
8−build() {
9− linkdir=$(mktemp -d)
10− nix build "$@" --out-link "$linkdir/result"
11− readlink "$linkdir/result"
12−}
13−
14−check_arg() {
15− if [ $# -le 1 ]
16− then
17− echo "error: flag '$1' requires an argument" >&2
18− fail
19− fi
20−}
21−
22−nix_file=`dirname $0`/default.nix
23−
24−# Read arguments
25−
26−build_flags=()
27−unset bin
28−targets=()
29−
30−while [ $# -ne 0 ]
31−do
32− case "$1" in
33− -f|--file|-I|--include)
34− check_arg "$@"
35− build_flags+=("$1")
36− shift
37− build_flags+=("$1")
38− shift
39− ;;
40− --arg|--argstr)
41− if [ $# -le 2 ]
42− then
43− echo "error: flag '$1' requires two arguments" >&2
44− fail
45− fi
46− build_flags+=("$1")
47− shift
48− build_flags+=("$1")
49− shift
50− build_flags+=("$1")
51− shift
52− ;;
53− --bin)
54− check_arg "$@"
55− shift
56− bin="$1"
57− shift
58− ;;
59− -h|--help)
60− cat <<EOF >&2
4+ cat <<EOF
5+
61−Usage: $0 <FLAGS>... [<TARGET>] <targets>...
6+Usage: $0 TARGET EXECUTABLE
62−
7+
63−Summary: Similar to nix build, but creates a single-file bundle.
8+Create a single-file bundle from the nixpkgs attribute "TARGET".
64−
65−Flags:
66− --arg <NAME> <EXPR> argument to be passed to Nix functions
67− --argstr <NAME> <STRING> string-valued argument to be passed to Nix functions
68− -f, --file <FILE> evaluate FILE rather than the default
69− -I, --include <PATH> add a path to the list of locations used to look up <...> file names
70− --bin <BINARY> the binary to bundle, relative to TARGET's output path; defaults to
9+EXECUTABLE should be relative to the TARGET's output path.
71− the lexicographically first file inside /bin
10+
72−
11+The TARGET is either an attribute in nixpkgs, or an absolute path to the
73−Examples:
12+store.
74−
13+
75− To bundle GNU Hello from NixOS 19.03:
14+For example:
76− $ $0 -f channel:nixos-19.03 hello --bin /bin/hello
15+
77−
16+$ $0 hello /bin/hello
78− To bundle the executable from within a project that has a default.nix for build:
17+$ ./hello
79− $ $0 --bin /bin/name-of-the-executable
18+Hello, world!
19+
80EOF
20EOF
81− exit 0
82− ;;
83− --)
84− shift
85− break
86− ;;
87− -*)
88− echo "error: unrecognized flag '$1'" >&2
89− fail
90− ;;
91− *)
92− targets+=("$1")
93− shift
94− ;;
95− esac
96−done
97−
98−while [ $# -ne 0 ]
99−do
100− targets+=("$1")
101− shift
102−done
103−
104−# Add application-specific extra targets
105−
106−if [ "${targets[0]}" = "nix-bundle" ] || [ "${targets[0]}" = "nixStable" ] || [ "${targets[0]}" = "nixUnstable" ] || [ "${targets[0]}" = "nix" ]
107−then
108− targets+=("bash")
109− targets+=("bzip2")
110− targets+=("coreutils")
111− targets+=("gnutar")
112− targets+=("gzip")
113− targets+=("xz")
114−fi
115
21
116−# Create tmp directory for the links produced by `nix build`
22+ exit 1
117−
118−linkdir=$(mktemp -d)
119−build_flags+=("--out-link")
120−build_flags+=("$linkdir/result")
121−
122−# Build targets
123−
124−target_path=
125−extra_target_paths=()
126−
127−for target in "${targets[@]}"
128−do
129− nix build "${build_flags[@]}" "$target" || exit $?
130− out_link="$linkdir/$(ls "$linkdir")"
131− if [ -z "$target_path" ]
132− then
133− target_path="$(readlink "$out_link")"
134− else
135− extra_target_paths+=("\"$(readlink "$out_link")\"")
136− fi
137− rm "$out_link"
138−done
139−
140−if [ -z "$target_path" ]
141−then
142− nix build "${build_flags[@]}" || exit $?
143− out_link="$(ls "$linkdir")"
144− target_path="$(readlink "$out_link")"
145fi
23fi
146
24
147−# Apply default for --bin
25+nix_file=$(dirname "$0")/default.nix
26+
27+target="$1"
28+shift
148
29
149−if [ -z ${bin+x} ]
30+extraTargets=
150−then
31+if [ "$#" -gt 1 ]; then
151− bin=$(ls "$target_path/bin" | head -1)
32+ while [ "$#" -gt 1 ]; do
33+ extraTargets="$extraTargets $1"
34+ shift
35+ done
152fi
36fi
153
37
154−# Determine bootstrap function -- This seems like a total hack!
38+exec="$1"
39+shift
155
40
156bootstrap=nix-bootstrap
41bootstrap=nix-bootstrap
157−if ! [ -z "$extra_target_paths" ]; then
42+if [ "$target" = "nix-bundle" ] || [ "$target" = "nixStable" ] || [ "$target" = "nixUnstable" ] || [ "$target" = "nix" ]; then
43+ bootstrap=nix-bootstrap-nix
44+elif [ -n "$extraTargets" ]; then
158 bootstrap=nix-bootstrap-path
45 bootstrap=nix-bootstrap-path
159fi
46fi
160
47
161−# Run bootstrap function
162−
163−expr="(with import $nix_file {}; $bootstrap { target = \"$target_path\"; extraTargets = [ ${extra_target_paths[@]} ]; run = \"$bin\"; })"
48+expr="with import <nixpkgs> {}; with import $nix_file {}; $bootstrap { target = $target; extraTargets = [ $extraTargets ]; run = \"$exec\"; }"
164−
165−echo $expr
166
49
167−out=$(nix-store --no-gc-warning -r $(nix-instantiate --no-gc-warning -E "$expr"))
50+drv=$(nix-instantiate --no-gc-warning -E "$expr")
168
51
169−# Copy the created bundle to the working directory
52+out=$(nix-store --no-gc-warning --realize "$drv")
170
53
171if [ -z "$out" ]; then
54if [ -z "$out" ]; then
172 >&2 echo "$0 failed. Exiting."
55 >&2 echo "$0 failed. Exiting."
173 exit 1
56 exit 1
174elif [ -t 1 ]; then
57elif [ -t 1 ]; then
175− filename=$(basename $bin)
58+ filename=$(basename "$exec")
176 echo "Nix bundle created at $filename."
59 echo "Nix bundle created at $filename."
177− cp -f $out $filename
60+ cp -f "$out" "$filename"
178else
61else
179− cat $out
62+ echo "$out"
180fi
63fi
nix-run.sh+23−21
@@ -18,11 +18,11 @@ if [ -z "$1" ]; then
18 exit 1
18 exit 1
19fi
19fi
20
20
21−pkg=$1
21+pkg="$1"
22shift
22shift
23
23
24# A second argument will provide a hint to run
24# A second argument will provide a hint to run
25−if ! [ -z "$1" ]; then
25+if [ -n "$1" ]; then
26 name="$1"
26 name="$1"
27 shift
27 shift
28else
28else
@@ -31,7 +31,7 @@ fi
31
31
32expr="with import <nixpkgs> {}; let x = ($pkg); in x"
32expr="with import <nixpkgs> {}; let x = ($pkg); in x"
33path=$(nix-instantiate --no-gc-warning -E "$expr")
33path=$(nix-instantiate --no-gc-warning -E "$expr")
34−out=$(nix-store --no-gc-warning -r $path)
34+out=$(nix-store --no-gc-warning -r "$path")
35
35
36if [ -z "$out" ]; then
36if [ -z "$out" ]; then
37 >&2 echo "Could not evaluate $pkg to a Nix drv."
37 >&2 echo "Could not evaluate $pkg to a Nix drv."
@@ -43,7 +43,7 @@ run_darwin_app () {
43 dir="$1"
43 dir="$1"
44 shift
44 shift
45
45
46− open -a "$dir" --args $@
46+ open -a "$dir" --args "$@"
47}
47}
48
48
49# Run FILE as a Freedesktop application
49# Run FILE as a Freedesktop application
@@ -53,14 +53,16 @@ run_linux_desktop_app () {
53 file="$1"
53 file="$1"
54 shift
54 shift
55
55
56− cmd=$(grep '^Exec' $file | tail -1 | \
56+ cmd=$(grep '^Exec' "$file" | tail -1 | \
57− sed 's/Exec=//;s/^"//;s/" *$/')
57+ sed 's/Exec=//;s/^"//;s/" *$//')
58
58
59− if ! [ -z "$@" ]; then
59+ if [ "$#" -gt 0 ]; then
60− cmd=$(echo "$cmd" | sed "s/%[fu]/$1/;s/%[FU]/$@/")
60+ cmd=$(echo "$cmd" | sed "s/%[fu]/$1/;s/%[FU]/$*/")
61 fi
61 fi
62
62
63 cmd=$(echo "$cmd" | sed "s/%k/$desktop/;s/%.//")
63 cmd=$(echo "$cmd" | sed "s/%k/$desktop/;s/%.//")
64+
65+ "$cmd" "$@"
64}
66}
65
67
66# Run FILE as an ordinary binary
68# Run FILE as an ordinary binary
@@ -68,30 +70,30 @@ run_bin () {
68 file="$1"
70 file="$1"
69 shift
71 shift
70
72
71− $file $@
73+ "$file" "$@"
72}
74}
73
75
74if [ -x "$out/nix-support/run" ]; then
76if [ -x "$out/nix-support/run" ]; then
75− run_bin "$out/nix-support/run" $@
77+ run_bin "$out/nix-support/run" "$@"
76elif [ -x "$out/bin/run" ]; then
78elif [ -x "$out/bin/run" ]; then
77− run_bin "$out/bin/run" $@
79+ run_bin "$out/bin/run" "$@"
78elif [ "$(uname)" = Darwin ] && [ -d "$out/Applications/$name.app" ]; then
80elif [ "$(uname)" = Darwin ] && [ -d "$out/Applications/$name.app" ]; then
79− run_darwin_app "$out/Applications/$name.app" $@
81+ run_darwin_app "$out/Applications/$name.app" "$@"
80−elif [ "$(uname)" = Darwin ] && [ -d $out/Applications/*.app ]; then
82+elif [ "$(uname)" = Darwin ] && [ -d "$out"/Applications/*.app ]; then
81− for f in $out/Applications/*.app; do
83+ for f in "$out"/Applications/*.app; do
82− run_darwin_app "$f" $@
84+ run_darwin_app "$f" "$@"
83 done
85 done
84elif [ -f "$out/share/applications/$name.desktop" ]; then
86elif [ -f "$out/share/applications/$name.desktop" ]; then
85− run_linux_desktop_app "$out/share/applications/$name.desktop" $@
87+ run_linux_desktop_app "$out/share/applications/$name.desktop" "$@"
86−elif [ -d $out/share/applications ]; then
88+elif [ -d "$out"/share/applications ]; then
87− for f in $out/share/applications/*.desktop; do
89+ for f in "$out"/share/applications/*.desktop; do
88 run_linux_desktop_app "$f"
90 run_linux_desktop_app "$f"
89 done
91 done
90elif [ -x "$out/bin/$name" ]; then
92elif [ -x "$out/bin/$name" ]; then
91− run_bin "$out/bin/$name" $@
93+ run_bin "$out/bin/$name" "$@"
92elif [ -d "$out/bin" ]; then
94elif [ -d "$out/bin" ]; then
93− for bin in $out/bin/*; do
95+ for bin in "$out"/bin/*; do
94− run_bin "$bin" $@
96+ run_bin "$bin" "$@"
95 done
97 done
96else
98else
97 >&2 echo "Cannot find a way to run path $out."
99 >&2 echo "Cannot find a way to run path $out."
release.nix+1−1
@@ -4,7 +4,7 @@
4stdenv.mkDerivation rec {
4stdenv.mkDerivation rec {
5 pname = "nix-bundle";
5 pname = "nix-bundle";
6 name = "${pname}-${version}";
6 name = "${pname}-${version}";
7− version = "0.3.0";
7+ version = "0.4.0";
8
8
9 src = ./.;
9 src = ./.;
10
10
test.shnew file+10
@@ -0,0 +1,10 @@
1+#!/usr/bin/env bash
2+set -euo pipefail
3+export NIX_PATH=channel:nixos-20.09
4+
5+echo "Test with attribute name"
6+./nix-bundle.sh hello /bin/hello
7+
8+echo "Test with store path"
9+out=$(nix-build --no-out-link --expr '(import <nixpkgs> {})' -A hello)
10+./nix-bundle.sh "$out" /bin/hello