From 21b6115ddfcf9e4603f7462014a700dcae5ed8e3 Mon Sep 17 00:00:00 2001 From: Matthew Bauer Date: Sat, 26 Nov 2016 01:05:43 -0600 Subject: [PATCH] Add nix-bundle.sh. --- README.md | 2 +- arx.nix | 22 +++++++++------------- default.nix | 33 +-------------------------------- makebootstrap.nix | 14 +++++++------- maketar.nix | 42 +++++++++++++++++++++++++++--------------- nix-bootstrap.nix | 11 +++++------ nix-bundle.sh | 29 +++++++++++++++++++++++++++++ nix-installer.nix | 22 +++++++++++----------- nix-user-chroot.c | 7 +++++++ nix-user-chroot.nix | 34 +++++++++++++++++++++------------- 10 files changed, 118 insertions(+), 98 deletions(-) create mode 100755 nix-bundle.sh diff --git a/README.md b/README.md index 6f6b6b6..1eee443 100644 --- a/README.md +++ b/README.md @@ -3,5 +3,5 @@ to run: ```sh -nix-build +./nix-bundle.sh TARGET EXECUTABLE ``` diff --git a/arx.nix b/arx.nix index 8236eef..2988ec2 100644 --- a/arx.nix +++ b/arx.nix @@ -1,15 +1,11 @@ { stdenv, arx }: +{ name, archive, startup}: -{ name -, archive -, startup -}: - - stdenv.mkDerivation { - inherit name; - nativeBuildInputs = [ arx ]; - buildCommand = '' - ${arx}/bin/arx tmpx ${archive} -o $out // ${startup} - chmod +x $out - ''; - } +stdenv.mkDerivation { + inherit name; + nativeBuildInputs = [ arx ]; + buildCommand = '' + ${arx}/bin/arx tmpx ${archive} -o $out // ${startup} + chmod +x $out + ''; +} diff --git a/default.nix b/default.nix index 8784396..c00d5d9 100644 --- a/default.nix +++ b/default.nix @@ -2,7 +2,7 @@ with nixpkgs; -let +rec { arx = callPackage ./arx.nix { inherit (haskellPackages) arx; @@ -22,35 +22,4 @@ let inherit nix-user-chroot makebootstrap; }; -in { - - hello = nix-bootstrap { - name = "hello"; - target = hello; - run = "/bin/hello"; - }; - - firefox = nix-bootstrap { - name = "firefox"; - target = firefox; - run = "/bin/firefox"; - }; - - nano = nix-bootstrap { - name = "nano"; - target = nano; - run = "/bin/nano"; - }; - - emacs = nix-bootstrap { - name = "emacs"; - target = emacs; - run = "/bin/emacs"; - }; - - nixInstaller = makebootstrap { - name = "nix-installer.sh"; - targets = [ nix-installer ]; - startup = ".${nix-installer}/install"; - }; } diff --git a/makebootstrap.nix b/makebootstrap.nix index 09b0212..b78b00e 100644 --- a/makebootstrap.nix +++ b/makebootstrap.nix @@ -1,9 +1,9 @@ { arx, maketar }: +{ name, targets, startup }: - { name, targets, startup }: - arx { - inherit name startup; - archive = maketar { - inherit name targets; - }; - } +arx { + inherit name startup; + archive = maketar { + inherit name targets; + }; +} diff --git a/maketar.nix b/maketar.nix index 723bf5d..b7cff8d 100644 --- a/maketar.nix +++ b/maketar.nix @@ -1,17 +1,29 @@ -{ stdenv, perl, pathsFromGraph }: - +{ stdenv, gcc, perl, pathsFromGraph }: { name, targets }: - stdenv.mkDerivation { - inherit name; - exportReferencesGraph = map (x: [("closure-" + baseNameOf x) x]) targets; - nativeBuildInputs = [ perl ]; - buildCommand = '' - storePaths=$(${perl}/bin/perl ${pathsFromGraph} ./closure-*) - # printRegistration=1 ${perl}/bin/perl ${pathsFromGraph} ./closure-* > .reginfo - tar cvfj $out \ - --owner=0 --group=0 --mode=u+rw,uga+r \ - --hard-dereference \ - $storePaths - ''; - } +stdenv.mkDerivation { + inherit name; + exportReferencesGraph = map (x: [("closure-" + baseNameOf x) x]) targets; + nativeBuildInputs = [ perl ]; + buildCommand = '' + storePaths=$(${perl}/bin/perl ${pathsFromGraph} ./closure-*) + + # remove "unused" stdenv store paths + # these need to be adjusted and made more intelligent + # this should create a "runtime stdenv" + storePaths=$(echo $storePaths | tr ' ' '\n' | \ + grep -Ev '/nix/store/[a-z0-9]+-linux-headers-[0-9.]+' | \ + grep -v ${stdenv.cc.libc.dev} | \ + grep -v ${stdenv.cc.libc.bin} | \ + grep -v ${stdenv.cc.cc} | \ + grep -v ${stdenv.cc.cc.lib} | \ + tr '\n' ' ') + # grep -Ev '/nix/store/[a-z0-9]+-zlib-[0-9.]+' | \ + + # printRegistration=1 ${perl}/bin/perl ${pathsFromGraph} ./closure-* > .reginfo + tar cfj $out \ + --owner=0 --group=0 --mode=u+rw,uga+r \ + --hard-dereference \ + $storePaths + ''; +} diff --git a/nix-bootstrap.nix b/nix-bootstrap.nix index e4d2c78..cb6ea07 100644 --- a/nix-bootstrap.nix +++ b/nix-bootstrap.nix @@ -1,9 +1,8 @@ { stdenv, writeText, nix-user-chroot, makebootstrap }: - { name, target, run }: - makebootstrap { - inherit name; - startup = ".${nix-user-chroot}/bin/nix-user-chroot ./nix ${target}${run}"; - targets = [ nix-user-chroot target ]; - } +makebootstrap { + inherit name; + startup = ".${nix-user-chroot}/bin/nix-user-chroot ./nix ${target}${run}"; + targets = [ nix-user-chroot target ]; +} diff --git a/nix-bundle.sh b/nix-bundle.sh new file mode 100755 index 0000000..be9b805 --- /dev/null +++ b/nix-bundle.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env sh + +if [ "$#" -lt 2 ]; then + cat < + */ + #define _GNU_SOURCE #include #include diff --git a/nix-user-chroot.nix b/nix-user-chroot.nix index acec1bb..c8403c9 100644 --- a/nix-user-chroot.nix +++ b/nix-user-chroot.nix @@ -1,14 +1,22 @@ -{ stdenv, fetchFromGitHub }: +{ stdenv, fetchFromGitHub, patchelf }: - stdenv.mkDerivation { - name = "nix-user-chroot"; - phases = [ "buildPhase" "installPhase" "fixupPhase" ]; - buildPhase = '' - cp ${./nix-user-chroot.c} nix-user-chroot.c - $CC nix-user-chroot.c -o nix-user-chroot - ''; - installPhase = '' - mkdir -p $out/bin/ - cp nix-user-chroot $out/bin/nix-user-chroot - ''; - } +stdenv.mkDerivation { + name = "nix-user-chroot"; + phases = [ "buildPhase" "fixupPhase" "installPhase" ]; + + buildPhase = '' + cp ${./nix-user-chroot.c} nix-user-chroot.c + $CC nix-user-chroot.c -o nix-user-chroot + ''; + + # setup local libc interpreter + fixupPhase = '' + patchelf --set-interpreter .$(patchelf --print-interpreter nix-user-chroot) nix-user-chroot + patchelf --set-rpath $(patchelf --print-rpath nix-user-chroot | sed 's|/nix/store/|./nix/store/|g') nix-user-chroot + ''; + + installPhase = '' + mkdir -p $out/bin/ + cp nix-user-chroot $out/bin/nix-user-chroot + ''; +}