Skip to content

Commit 21b6115

Browse files
committed
Add nix-bundle.sh.
1 parent 6dc15df commit 21b6115

10 files changed

+119
-99
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
to run:
44

55
```sh
6-
nix-build
6+
./nix-bundle.sh TARGET EXECUTABLE
77
```

arx.nix

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
{ stdenv, arx }:
2+
{ name, archive, startup}:
23

3-
{ name
4-
, archive
5-
, startup
6-
}:
7-
8-
stdenv.mkDerivation {
9-
inherit name;
10-
nativeBuildInputs = [ arx ];
11-
buildCommand = ''
12-
${arx}/bin/arx tmpx ${archive} -o $out // ${startup}
13-
chmod +x $out
14-
'';
15-
}
4+
stdenv.mkDerivation {
5+
inherit name;
6+
nativeBuildInputs = [ arx ];
7+
buildCommand = ''
8+
${arx}/bin/arx tmpx ${archive} -o $out // ${startup}
9+
chmod +x $out
10+
'';
11+
}

default.nix

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
with nixpkgs;
44

5-
let
5+
rec {
66

77
arx = callPackage ./arx.nix {
88
inherit (haskellPackages) arx;
@@ -22,35 +22,4 @@ let
2222
inherit nix-user-chroot makebootstrap;
2323
};
2424

25-
in {
26-
27-
hello = nix-bootstrap {
28-
name = "hello";
29-
target = hello;
30-
run = "/bin/hello";
31-
};
32-
33-
firefox = nix-bootstrap {
34-
name = "firefox";
35-
target = firefox;
36-
run = "/bin/firefox";
37-
};
38-
39-
nano = nix-bootstrap {
40-
name = "nano";
41-
target = nano;
42-
run = "/bin/nano";
43-
};
44-
45-
emacs = nix-bootstrap {
46-
name = "emacs";
47-
target = emacs;
48-
run = "/bin/emacs";
49-
};
50-
51-
nixInstaller = makebootstrap {
52-
name = "nix-installer.sh";
53-
targets = [ nix-installer ];
54-
startup = ".${nix-installer}/install";
55-
};
5625
}

makebootstrap.nix

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{ arx, maketar }:
2+
{ name, targets, startup }:
23

3-
{ name, targets, startup }:
4-
arx {
5-
inherit name startup;
6-
archive = maketar {
7-
inherit name targets;
8-
};
9-
}
4+
arx {
5+
inherit name startup;
6+
archive = maketar {
7+
inherit name targets;
8+
};
9+
}

maketar.nix

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
1-
{ stdenv, perl, pathsFromGraph }:
2-
1+
{ stdenv, gcc, perl, pathsFromGraph }:
32
{ name, targets }:
43

5-
stdenv.mkDerivation {
6-
inherit name;
7-
exportReferencesGraph = map (x: [("closure-" + baseNameOf x) x]) targets;
8-
nativeBuildInputs = [ perl ];
9-
buildCommand = ''
10-
storePaths=$(${perl}/bin/perl ${pathsFromGraph} ./closure-*)
11-
# printRegistration=1 ${perl}/bin/perl ${pathsFromGraph} ./closure-* > .reginfo
12-
tar cvfj $out \
13-
--owner=0 --group=0 --mode=u+rw,uga+r \
14-
--hard-dereference \
15-
$storePaths
16-
'';
17-
}
4+
stdenv.mkDerivation {
5+
inherit name;
6+
exportReferencesGraph = map (x: [("closure-" + baseNameOf x) x]) targets;
7+
nativeBuildInputs = [ perl ];
8+
buildCommand = ''
9+
storePaths=$(${perl}/bin/perl ${pathsFromGraph} ./closure-*)
10+
11+
# remove "unused" stdenv store paths
12+
# these need to be adjusted and made more intelligent
13+
# this should create a "runtime stdenv"
14+
storePaths=$(echo $storePaths | tr ' ' '\n' | \
15+
grep -Ev '/nix/store/[a-z0-9]+-linux-headers-[0-9.]+' | \
16+
grep -v ${stdenv.cc.libc.dev} | \
17+
grep -v ${stdenv.cc.libc.bin} | \
18+
grep -v ${stdenv.cc.cc} | \
19+
grep -v ${stdenv.cc.cc.lib} | \
20+
tr '\n' ' ')
21+
# grep -Ev '/nix/store/[a-z0-9]+-zlib-[0-9.]+' | \
22+
23+
# printRegistration=1 ${perl}/bin/perl ${pathsFromGraph} ./closure-* > .reginfo
24+
tar cfj $out \
25+
--owner=0 --group=0 --mode=u+rw,uga+r \
26+
--hard-dereference \
27+
$storePaths
28+
'';
29+
}

nix-bootstrap.nix

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
{ stdenv, writeText, nix-user-chroot, makebootstrap }:
2-
32
{ name, target, run }:
43

5-
makebootstrap {
6-
inherit name;
7-
startup = ".${nix-user-chroot}/bin/nix-user-chroot ./nix ${target}${run}";
8-
targets = [ nix-user-chroot target ];
9-
}
4+
makebootstrap {
5+
inherit name;
6+
startup = ".${nix-user-chroot}/bin/nix-user-chroot ./nix ${target}${run}";
7+
targets = [ nix-user-chroot target ];
8+
}

nix-bundle.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env sh
2+
3+
if [ "$#" -lt 2 ]; then
4+
cat <<EOF
5+
6+
Usage: $0 TARGET EXECUTABLE
7+
8+
Create a single-file bundle from the nixpkgs attribute "TARGET".
9+
EXECUTABLE should be relative to the TARGET's output path.
10+
11+
For example:
12+
13+
$ $0 hello /bin/hello
14+
$ ./hello
15+
Hello, world!
16+
17+
EOF
18+
19+
exit 1
20+
fi
21+
22+
target="$1"
23+
exec="$2"
24+
25+
expr="with import <nixpkgs> {}; with import ./. {}; nix-bootstrap { name = \"$target\"; target = $target; run = \"$exec\"; }"
26+
27+
out=$(nix-store -r $(nix-instantiate -E "$expr"))
28+
29+
cp -f $out $target

nix-installer.nix

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{ stdenv, fetchFromGitHub, writeText, nix, cacert }:
22

3-
stdenv.mkDerivation {
4-
name = "nix-installer";
3+
stdenv.mkDerivation {
4+
name = "nix-installer";
55

6-
propagatedBuildInputs = [ nix.out cacert ];
6+
propagatedBuildInputs = [ nix.out cacert ];
77

8-
buildCommand = ''
9-
mkdir -p $out/bin/
10-
substitute ${./install-nix-from-closure.sh} $out/install \
11-
--subst-var-by nix ${nix.out} \
12-
--subst-var-by cacert ${cacert}
13-
chmod +x $out/install
14-
'';
15-
}
8+
buildCommand = ''
9+
mkdir -p $out/bin/
10+
substitute ${./install-nix-from-closure.sh} $out/install \
11+
--subst-var-by nix ${nix.out} \
12+
--subst-var-by cacert ${cacert}
13+
chmod +x $out/install
14+
'';
15+
}

nix-user-chroot.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/*
2+
* This file is based on @lethalman's nix-user-chroot. This file has
3+
* diverged from it though.
4+
*
5+
* Usage: nix-user-chroot <nixpath> <command>
6+
*/
7+
18
#define _GNU_SOURCE
29
#include <sched.h>
310
#include <unistd.h>

nix-user-chroot.nix

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1-
{ stdenv, fetchFromGitHub }:
2-
3-
stdenv.mkDerivation {
4-
name = "nix-user-chroot";
5-
phases = [ "buildPhase" "installPhase" "fixupPhase" ];
6-
buildPhase = ''
7-
cp ${./nix-user-chroot.c} nix-user-chroot.c
8-
$CC nix-user-chroot.c -o nix-user-chroot
9-
'';
10-
installPhase = ''
11-
mkdir -p $out/bin/
12-
cp nix-user-chroot $out/bin/nix-user-chroot
13-
'';
14-
}
1+
{ stdenv, fetchFromGitHub, patchelf }:
2+
3+
stdenv.mkDerivation {
4+
name = "nix-user-chroot";
5+
phases = [ "buildPhase" "fixupPhase" "installPhase" ];
6+
7+
buildPhase = ''
8+
cp ${./nix-user-chroot.c} nix-user-chroot.c
9+
$CC nix-user-chroot.c -o nix-user-chroot
10+
'';
11+
12+
# setup local libc interpreter
13+
fixupPhase = ''
14+
patchelf --set-interpreter .$(patchelf --print-interpreter nix-user-chroot) nix-user-chroot
15+
patchelf --set-rpath $(patchelf --print-rpath nix-user-chroot | sed 's|/nix/store/|./nix/store/|g') nix-user-chroot
16+
'';
17+
18+
installPhase = ''
19+
mkdir -p $out/bin/
20+
cp nix-user-chroot $out/bin/nix-user-chroot
21+
'';
22+
}

0 commit comments

Comments
 (0)