on master help sign in
Make closure sizes a little bit smaller.
commit3ef86c995d747e935231092e2a119e4953b14e3b authorMatthew Bauer <mjbauer95@gmail.com> dateMon 20 Jun 2016, 20:53 (10 years ago) parentcfe7aef
6 files changed+23−33
default.nix+1−1 maketar.nix+1−1 nix-bootstrap.nix+3−3 nix-user-chroot.c+9−19 nix-user-chroot.nix+6−2 run-from-closure.sh+3−7
default.nix+1−1
@@ -45,7 +45,7 @@ in {
4545
4646 emacs = nix-bootstrap {
4747 name = "emacs";
48− target = emacs;
48+ target = emacs24-nox;
4949 run = "/bin/emacs";
5050 };
5151
maketar.nix+1−1
@@ -7,7 +7,7 @@
77 exportReferencesGraph = map (x: [("closure-" + baseNameOf x) x]) targets;
88 nativeBuildInputs = [ perl ];
99 buildCommand = ''
10− storePaths=$(${perl}/bin/perl ${pathsFromGraph} ./closure-* | grep -E -v '^/nix/store/[a-z0-9]+-(gcc|linux-headers)')
10+ storePaths=$(${perl}/bin/perl ${pathsFromGraph} ./closure-*)
1111 # printRegistration=1 ${perl}/bin/perl ${pathsFromGraph} ./closure-* > .reginfo
1212 tar cvfj $out \
1313 --owner=0 --group=0 --mode=u+rw,uga+r \
nix-bootstrap.nix+3−3
@@ -1,9 +1,9 @@
1−{ stdenv, writeText, nix-user-chroot, bash, makebootstrap }:
1+{ stdenv, writeText, nix-user-chroot, makebootstrap }:
22
33{ name, target, run }:
44
55 makebootstrap {
66 inherit name;
7− startup = ".${nix-user-chroot}/bin/nix-user-chroot ./nix .${bash}/bin/sh -c ${target}${run}";
8− targets = [ nix-user-chroot target bash ];
7+ startup = ".${nix-user-chroot}/bin/nix-user-chroot ./nix ${target}${run}";
8+ targets = [ nix-user-chroot target ];
99 }
nix-user-chroot.c+9−19
@@ -65,30 +65,29 @@ int main(int argc, char *argv[]) {
6565 usage(argv[0]);
6666 }
6767
68+ // Create skeleton root dir in temp. All dirs will be mounted here.
6869 char template[] = "/tmp/nixXXXXXX";
6970 char *rootdir = mkdtemp(template);
7071 if (!rootdir) {
7172 err_exit("mkdtemp(%s)", template);
7273 }
7374
75+ // determine absolute directory for nix dir
7476 char *nixdir = realpath(argv[1], NULL);
7577 if (!nixdir) {
7678 err_exit("realpath(%s)", argv[1]);
7779 }
7880
81+ // get uid, gid before going to new namespace
7982 uid_t uid = getuid();
8083 gid_t gid = getgid();
8184
85+ // "unshare" into new namespace
8286 if (unshare(CLONE_NEWNS | CLONE_NEWUSER) < 0) {
8387 err_exit("unshare()");
8488 }
8589
86− // bind mount all / stuff into rootdir
87− DIR* d = opendir("/");
88− if (!d) {
89− err_exit("open /");
90− }
91−
90+ // add necessary system stuff to rootdir namespace
9291 add_path("dev", rootdir);
9392 add_path("proc", rootdir);
9493 add_path("sys", rootdir);
@@ -96,13 +95,14 @@ int main(int argc, char *argv[]) {
9695 add_path("tmp", rootdir);
9796 add_path("var", rootdir);
9897
98+ // make sure nixdir exists
9999 struct stat statbuf2;
100100 if (stat(nixdir, &statbuf2) < 0) {
101101 err_exit("stat(%s)", nixdir);
102102 }
103103
104+ // mount /nix to new namespace
104105 char path_buf[PATH_MAX];
105−
106106 snprintf(path_buf, sizeof(path_buf), "%s/nix", rootdir);
107107 mkdir(path_buf, statbuf2.st_mode & ~S_IFMT);
108108 if (mount(nixdir, path_buf, "none", MS_BIND | MS_REC, NULL) < 0) {
@@ -116,29 +116,19 @@ int main(int argc, char *argv[]) {
116116 write(fd_setgroups, "deny", 4);
117117 }
118118
119− char map_buf[1024];
120−
121119 // map the original uid/gid in the new ns
120+ char map_buf[1024];
122121 snprintf(map_buf, sizeof(map_buf), "%d %d 1", uid, uid);
123122 update_map(map_buf, "/proc/self/uid_map");
124−
125123 snprintf(map_buf, sizeof(map_buf), "%d %d 1", gid, gid);
126124 update_map(map_buf, "/proc/self/gid_map");
127125
128− char cwd[PATH_MAX];
129−
130− if (!getcwd(cwd, PATH_MAX)) {
131− err_exit("getcwd()");
132− }
133−
134− chdir("/");
126+ // chroot to rootdir
135127 if (chroot(rootdir) < 0) {
136128 err_exit("chroot(%s)", rootdir);
137129 }
138− chdir(cwd);
139130
140131 // execute the command
141−
142132 execvp(argv[2], argv+2);
143133 err_exit("execvp(%s)", argv[2]);
144134}
nix-user-chroot.nix+6−2
@@ -2,9 +2,13 @@
22
33 stdenv.mkDerivation {
44 name = "nix-user-chroot";
5− buildCommand = ''
5+ phases = [ "buildPhase" "installPhase" "fixupPhase" ];
6+ buildPhase = ''
67 cp ${./nix-user-chroot.c} nix-user-chroot.c
8+ $CC nix-user-chroot.c -o nix-user-chroot
9+ '';
10+ installPhase = ''
711 mkdir -p $out/bin/
8− cc nix-user-chroot.c -o $out/bin/nix-user-chroot
12+ cp nix-user-chroot $out/bin/nix-user-chroot
913 '';
1014 }
run-from-closure.sh+3−7
@@ -1,4 +1,4 @@
1−#!/bin/sh
1+#!/usr/bin/env /sh
22
33set -e
44
@@ -6,6 +6,8 @@ self="."
66nix="@nix@"
77cacert="@cacert@"
88
9+export SSL_CERT_FILE="$cacert/etc/ssl/certs/ca-bundle.crt"
10+
911if ! [ -e $self/.reginfo ]; then
1012 echo "$0: incomplete installer (.reginfo is missing)" >&2
1113 exit 1
@@ -29,11 +31,5 @@ if ! $nix/bin/nix-env -i "$nix"; then
2931 exit 1
3032fi
3133
32−# Install an SSL certificate bundle.
33−if [ -z "$SSL_CERT_FILE" -o ! -f "$SSL_CERT_FILE" ]; then
34− $nix/bin/nix-env -i "$cacert"
35− export SSL_CERT_FILE="$HOME/.nix-profile/etc/ssl/certs/ca-bundle.crt"
36−fi
37−
3834$nix/bin/nix-channel --add https://nixos.org/channels/nixpkgs-unstable
3935$nix/bin/nix-channel --update nixpkgs