summary refs log tree commit diff
path: root/pkgs/build-support/build-fhs-chrootenv/default.nix
blob: e807a04628a38f91f1805c21b6bdf4f2ea3ec55f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
{stdenv, glibc, glibcLocales, gcc, coreutils, diffutils, findutils, gnused, gnugrep, gnutar, gzip, bzip2, 
bashInteractive, xz, shadow, gawk, less, buildEnv}:
{name, pkgs ? [], profile ? ""}:

let
  basePkgs = [
    glibc glibcLocales gcc coreutils diffutils findutils gnused gnugrep gnutar
    gzip bzip2 bashInteractive xz shadow gawk less
  ];

  # Compose a global profile for the chroot environment
  profilePkg = stdenv.mkDerivation {
    name = "${name}-chrootenv-profile";
    buildCommand = ''
      mkdir -p $out/etc
      cat >> $out/etc/profile << "EOF"
      export PS1='${name}-chrootenv:\u@\h:\w\$ '
      ${profile}
      EOF
    '';
  };

  paths = basePkgs ++ [ profilePkg ] ++ pkgs;

  # Composes a /usr like directory structure
  staticUsrProfile = buildEnv {
    name = "system-profile";
    inherit paths;
  };
  
  # References to shell scripts that set up or tear down the environment
  initSh = ./init.sh.in;
  mountSh = ./mount.sh.in;
  loadSh = ./load.sh.in;
  umountSh = ./umount.sh.in;
  destroySh = ./destroy.sh.in;
in
stdenv.mkDerivation {
  name = "${name}-chrootenv";
  buildCommand = ''
    mkdir -p $out/sw
    cd $out/sw
    
    for i in ${staticUsrProfile}/{etc,bin,lib{,32,64},sbin,var}
    do
        if [ -x "$i" ]
        then
            ln -s "$i"
        fi
    done
    
    ln -s ${staticUsrProfile} usr
    
    cd ..
    
    mkdir -p bin
    cd bin
    
    sed -e "s|@chrootEnv@|$out|g" \
        -e "s|@name@|${name}|g" \
        -e "s|@shell@|${stdenv.shell}|g" \
        ${initSh} > init-${name}-chrootenv
    chmod +x init-${name}-chrootenv
    
    sed -e "s|@shell@|${stdenv.shell}|g" \
        -e "s|@name@|${name}|g" \
        ${mountSh} > mount-${name}-chrootenv
    chmod +x mount-${name}-chrootenv
    
    sed -e "s|@shell@|${stdenv.shell}|g" \
        -e "s|@name@|${name}|g" \
        ${loadSh} > load-${name}-chrootenv
    chmod +x load-${name}-chrootenv
    
    sed -e "s|@shell@|${stdenv.shell}|g" \
        -e "s|@name@|${name}|g" \
        ${umountSh} > umount-${name}-chrootenv
    chmod +x umount-${name}-chrootenv
    
    sed -e "s|@chrootEnv@|$out|g" \
        -e "s|@shell@|${stdenv.shell}|g" \
        -e "s|@name@|${name}|g" \
        ${destroySh} > destroy-${name}-chrootenv
    chmod +x destroy-${name}-chrootenv
  '';
}