on master help sign in
11 files changed+132−54
since splitsnapshots
unifiedsplit
AppRun.c+5 README.md+14−1 appdir.nix+2−2 appimagetool.nix+2−2 default.nix+25−8 flake.nix+26 nix-bootstrap.sh+13−13 nix-bundle.sh+11−6 nix-run.sh+23−21 release.nix+1−1 test.sh+10
AppRun.c+5
@@ -207,6 +207,11 @@ int main(int argc, char *argv[]) {
207207 err_exit("mount(%s, %s)", usrdir, path_buf);
208208 }
209209
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+
210215 // fixes issue #1 where writing to /proc/self/gid_map fails
211216 // see user_namespaces(7) for more documentation
212217 int fd_setgroups = open("/proc/self/setgroups", O_WRONLY);
README.md+14−1
@@ -35,14 +35,27 @@ Some others to try:
3535./nix-bundle.sh nano /bin/nano
3636```
3737
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+
3849```sh
3950./nix-bundle.sh firefox /bin/firefox
4051```
4152
4253```sh
43−./nix-bundle.sh emacs /bin/emacs
54+# SDL-based game. ~228MB
55+./nix-bundle.sh ivan /bin/ivan
4456```
4557
58+
4659## Self-bundling (meta)
4760
4861Starting with v0.1.3, you can bundle nix-bundle! To do this, just use nix-bundle normally:
appdir.nix+2−2
@@ -1,4 +1,4 @@
1−{ stdenv, fetchurl, muslPkgs, perl, pathsFromGraph, fetchFromGitHub, coreutils, bash }:
1+{ stdenv, lib, fetchurl, muslPkgs, perl, pathsFromGraph, fetchFromGitHub, coreutils, bash }:
22
33let
44 AppRun = targets: muslPkgs.stdenv.mkDerivation {
@@ -8,7 +8,7 @@ let
88
99 buildPhase = ''
1010 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}"'
1212 '';
1313
1414 installPhase = ''
appimagetool.nix+2−2
@@ -1,4 +1,4 @@
1−{ stdenv, fetchurl, fuse, zlib, squashfsTools, glib }:
1+{ stdenv, lib, fetchurl, fuse, zlib, squashfsTools, glib }:
22
33# This is from some binaries.
44
@@ -37,7 +37,7 @@ in stdenv.mkDerivation rec {
3737 for x in $out/bin/*; do
3838 patchelf \
3939 --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 ]} \
4141 $x
4242 done
4343 '';
default.nix+25−8
@@ -10,6 +10,16 @@ let
1010 '';
1111 });
1212in 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+
1323 arx = { archive, startup}:
1424 stdenv.mkDerivation {
1525 name = "arx";
@@ -35,7 +45,7 @@ in rec {
3545 };
3646
3747 # TODO: eventually should this go in nixpkgs?
38− nix-user-chroot = stdenv.lib.makeOverridable stdenv.mkDerivation {
48+ nix-user-chroot = lib.makeOverridable stdenv.mkDerivation {
3949 name = "nix-user-chroot-2c52b5f";
4050 src = ./nix-user-chroot;
4151
@@ -58,6 +68,8 @@ in rec {
5868
5969 runHook postInstall
6070 '';
71+
72+ meta.platforms = lib.platforms.linux;
6173 };
6274
6375 makebootstrap = { targets, startup }:
@@ -68,15 +80,20 @@ in rec {
6880 };
6981 };
7082
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
7288 writeScript "startup" ''
7389 #!/bin/sh
74− .${nix-user-chroot'}/bin/nix-user-chroot -n ./nix ${nixUserChrootFlags} -- ${target}${run} $@
90+ ${initScript}
91+ .${nix-user-chroot'}/bin/nix-user-chroot -n ./nix ${nixUserChrootFlags} -- ${path}${run} "$@"
7592 '';
7693
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 ? "" }:
7895 let
79− script = makeStartup { inherit target nixUserChrootFlags nix-user-chroot' run; };
96+ script = makeStartup { inherit target nixUserChrootFlags nix-user-chroot' run initScript; };
8097 in makebootstrap {
8198 startup = ".${script} '\"$@\"'";
8299 targets = [ "${script}" ] ++ extraTargets;
@@ -93,10 +110,10 @@ in rec {
93110 nix-user-chroot'' = targets: nix-user-chroot.overrideDerivation (o: {
94111 buildInputs = o.buildInputs ++ targets;
95112 makeFlags = o.makeFlags ++ [
96− ''ENV_PATH="${stdenv.lib.makeBinPath targets}"''
113+ ''ENV_PATH="${lib.makeBinPath targets}"''
97114 ];
98− }); in { target, extraTargets ? [], run }: nix-bootstrap {
99− inherit target extraTargets run;
115+ }); in { target, extraTargets ? [], run, initScript ? "" }: nix-bootstrap {
116+ inherit target extraTargets run initScript;
100117 nix-user-chroot' = nix-user-chroot'' extraTargets;
101118 };
102119}
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 @@
11#!/bin/sh
22
3−cmd=sh
4−if ! [ $# -eq 0 ]; then
5− cmd=$@
3+if [ $# -gt 0 ]; then
4+ set -- sh
65fi
76
87# should download this in the future
98# but the mirror is down
10−proot=`dirname $0`/proot-`uname -p`
9+proot=$(dirname "$0")/proot-$(uname -p)
1110export PROOT_NO_SECCOMP=1
1211
1312nixdir=$HOME/.nix
1413
1514OLD_NIX_PATH=$NIX_PATH
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
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
17+if [ -n "$OLD_NIX_PATH" ]; then NIX_PATH="$OLD_NIX_PATH"; fi
1818
1919if [ -z "$IN_PROOT" ]; then
2020 export IN_PROOT=1
2121
22− if ! [ -d $nixdir ]; then
23− mkdir -p $nixdir
22+ if ! [ -d "$nixdir" ]; then
23+ mkdir -p "$nixdir"
2424 s=$(mktemp)
25− curl https://nixos.org/nix/install -o $s
26− $proot -b $nixdir:/nix $0 sh $s
25+ curl https://nixos.org/nix/install -o "$s"
26+ $proot -b "$nixdir:/nix" "$0" sh "$s"
2727 fi
2828
29− $proot -b $nixdir:/nix $0 $cmd
29+ $proot -b "$nixdir:/nix" "$0" "$@"
3030
3131 export IN_PROOT=
3232 exit
33−elif ! [ $# -eq 0 ]; then
34− exec $cmd
33+elif [ $# -gt 0 ]; then
34+ exec "$@"
3535fi
nix-bundle.sh+11−6
@@ -8,6 +8,9 @@ Usage: $0 TARGET EXECUTABLE
88Create a single-file bundle from the nixpkgs attribute "TARGET".
99EXECUTABLE should be relative to the TARGET's output path.
1010
11+The TARGET is either an attribute in nixpkgs, or an absolute path to the
12+store.
13+
1114For example:
1215
1316$ $0 hello /bin/hello
@@ -19,7 +22,7 @@ EOF
1922 exit 1
2023fi
2124
22−nix_file=`dirname $0`/default.nix
25+nix_file=$(dirname "$0")/default.nix
2326
2427target="$1"
2528shift
@@ -38,21 +41,23 @@ shift
3841bootstrap=nix-bootstrap
3942if [ "$target" = "nix-bundle" ] || [ "$target" = "nixStable" ] || [ "$target" = "nixUnstable" ] || [ "$target" = "nix" ]; then
4043 bootstrap=nix-bootstrap-nix
41−elif ! [ -z "$extraTargets" ]; then
44+elif [ -n "$extraTargets" ]; then
4245 bootstrap=nix-bootstrap-path
4346fi
4447
4548expr="with import <nixpkgs> {}; with import $nix_file {}; $bootstrap { target = $target; extraTargets = [ $extraTargets ]; run = \"$exec\"; }"
4649
47−out=$(nix-store --no-gc-warning -r $(nix-instantiate --no-gc-warning -E "$expr"))
50+drv=$(nix-instantiate --no-gc-warning -E "$expr")
51+
52+out=$(nix-store --no-gc-warning --realize "$drv")
4853
4954if [ -z "$out" ]; then
5055 >&2 echo "$0 failed. Exiting."
5156 exit 1
5257elif [ -t 1 ]; then
53− filename=$(basename $exec)
58+ filename=$(basename "$exec")
5459 echo "Nix bundle created at $filename."
55− cp -f $out $filename
60+ cp -f "$out" "$filename"
5661else
57− cat $out
62+ echo "$out"
5863fi
nix-run.sh+23−21
@@ -18,11 +18,11 @@ if [ -z "$1" ]; then
1818 exit 1
1919fi
2020
21−pkg=$1
21+pkg="$1"
2222shift
2323
2424# A second argument will provide a hint to run
25−if ! [ -z "$1" ]; then
25+if [ -n "$1" ]; then
2626 name="$1"
2727 shift
2828else
@@ -31,7 +31,7 @@ fi
3131
3232expr="with import <nixpkgs> {}; let x = ($pkg); in x"
3333path=$(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")
3535
3636if [ -z "$out" ]; then
3737 >&2 echo "Could not evaluate $pkg to a Nix drv."
@@ -43,7 +43,7 @@ run_darwin_app () {
4343 dir="$1"
4444 shift
4545
46− open -a "$dir" --args $@
46+ open -a "$dir" --args "$@"
4747}
4848
4949# Run FILE as a Freedesktop application
@@ -53,14 +53,16 @@ run_linux_desktop_app () {
5353 file="$1"
5454 shift
5555
56− cmd=$(grep '^Exec' $file | tail -1 | \
57− sed 's/Exec=//;s/^"//;s/" *$/')
56+ cmd=$(grep '^Exec' "$file" | tail -1 | \
57+ sed 's/Exec=//;s/^"//;s/" *$//')
5858
59− if ! [ -z "$@" ]; then
60− cmd=$(echo "$cmd" | sed "s/%[fu]/$1/;s/%[FU]/$@/")
59+ if [ "$#" -gt 0 ]; then
60+ cmd=$(echo "$cmd" | sed "s/%[fu]/$1/;s/%[FU]/$*/")
6161 fi
6262
6363 cmd=$(echo "$cmd" | sed "s/%k/$desktop/;s/%.//")
64+
65+ "$cmd" "$@"
6466}
6567
6668# Run FILE as an ordinary binary
@@ -68,30 +70,30 @@ run_bin () {
6870 file="$1"
6971 shift
7072
71− $file $@
73+ "$file" "$@"
7274}
7375
7476if [ -x "$out/nix-support/run" ]; then
75− run_bin "$out/nix-support/run" $@
77+ run_bin "$out/nix-support/run" "$@"
7678elif [ -x "$out/bin/run" ]; then
77− run_bin "$out/bin/run" $@
79+ run_bin "$out/bin/run" "$@"
7880elif [ "$(uname)" = Darwin ] && [ -d "$out/Applications/$name.app" ]; then
79− run_darwin_app "$out/Applications/$name.app" $@
80−elif [ "$(uname)" = Darwin ] && [ -d $out/Applications/*.app ]; then
81− for f in $out/Applications/*.app; do
82− run_darwin_app "$f" $@
81+ run_darwin_app "$out/Applications/$name.app" "$@"
82+elif [ "$(uname)" = Darwin ] && [ -d "$out"/Applications/*.app ]; then
83+ for f in "$out"/Applications/*.app; do
84+ run_darwin_app "$f" "$@"
8385 done
8486elif [ -f "$out/share/applications/$name.desktop" ]; then
85− run_linux_desktop_app "$out/share/applications/$name.desktop" $@
86−elif [ -d $out/share/applications ]; then
87− for f in $out/share/applications/*.desktop; do
87+ run_linux_desktop_app "$out/share/applications/$name.desktop" "$@"
88+elif [ -d "$out"/share/applications ]; then
89+ for f in "$out"/share/applications/*.desktop; do
8890 run_linux_desktop_app "$f"
8991 done
9092elif [ -x "$out/bin/$name" ]; then
91− run_bin "$out/bin/$name" $@
93+ run_bin "$out/bin/$name" "$@"
9294elif [ -d "$out/bin" ]; then
93− for bin in $out/bin/*; do
94− run_bin "$bin" $@
95+ for bin in "$out"/bin/*; do
96+ run_bin "$bin" "$@"
9597 done
9698else
9799 >&2 echo "Cannot find a way to run path $out."
release.nix+1−1
@@ -4,7 +4,7 @@
44stdenv.mkDerivation rec {
55 pname = "nix-bundle";
66 name = "${pname}-${version}";
7− version = "0.3.0";
7+ version = "0.4.0";
88
99 src = ./.;
1010
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