summaryrefslogtreecommitdiff
path: root/pkgs/os-specific/linux/minimal-bootstrap/bash/2.nix
blob: 9b51f93799d4b7da2db6af9c539dca8ac05eaf11 (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
{
  lib,
  derivationWithMeta,
  fetchurl,
  kaem,
  tinycc,
  gnumake,
  gnupatch,
  coreutils,
  mescc-tools-extra,
  bash_2_05,
}:
let
  inherit (import ./common.nix { inherit lib; }) meta;
  pname = "bash";
  version = "2.05b";

  src = fetchurl {
    url = "mirror://gnu/bash/bash-${version}.tar.gz";
    sha256 = "1r1z2qdw3rz668nxrzwa14vk2zcn00hw7mpjn384picck49d80xs";
  };

  # Thanks to the live-bootstrap project!
  # See https://github.com/fosslinux/live-bootstrap/blob/c0494d9af84b9e8c3e76e34c6e898978013a3b39/steps/bash-2.05b/pass1.kaem
  liveBootstrap = "https://github.com/fosslinux/live-bootstrap/raw/c0494d9af84b9e8c3e76e34c6e898978013a3b39/steps/bash-2.05b";

  main_mk = fetchurl {
    url = "${liveBootstrap}/mk/main.mk";
    sha256 = "0hj29q3pq3370p18sxkpvv9flb7yvx2fs96xxlxqlwa8lkimd0j4";
  };

  common_mk = fetchurl {
    url = "${liveBootstrap}/mk/common.mk";
    sha256 = "sha256-9BzUJPz6Vx+r69i2SQlqRTH9ihgLaUp1JSYGlTbWWu8=";
  };

  builtins_mk = fetchurl {
    url = "${liveBootstrap}/mk/builtins.mk";
    sha256 = "0939dy5by1xhfmsjj6w63nlgk509fjrhpb2crics3dpcv7prl8lj";
  };

  patches = [
    # mes libc does not have locale support
    (fetchurl {
      url = "${liveBootstrap}/patches/mes-libc.patch";
      sha256 = "0zksdjf6zbb3p4hqg6plq631y76hhhgab7kdvf7cnpk8bcykn12z";
    })
    # int name, namelen; is wrong for mes libc, it is char* name, so we modify tinycc
    # to reflect this.
    (fetchurl {
      url = "${liveBootstrap}/patches/tinycc.patch";
      sha256 = "042d2kr4a8klazk1hlvphxr6frn4mr53k957aq3apf6lbvrjgcj2";
    })
    # add ifdef's for features we don't want
    (fetchurl {
      url = "${liveBootstrap}/patches/missing-defines.patch";
      sha256 = "1q0k1kj5mrvjkqqly7ki5575a5b3hy1ywnmvhrln318yh67qnkj4";
    })
    # mes libc + setting locale = not worky
    (fetchurl {
      url = "${liveBootstrap}/patches/locale.patch";
      sha256 = "1p1q1slhafsgj8x4k0dpn9h6ryq5fwfx7dicbbxhldbw7zvnnbx9";
    })
    # We do not have /dev at this stage of the bootstrap, including /dev/tty
    (fetchurl {
      url = "${liveBootstrap}/patches/dev-tty.patch";
      sha256 = "1315slv5f7ziajqyxg4jlyanf1xwd06xw14y6pq7xpm3jzjk55j9";
    })
  ];
in
kaem.runCommand "${pname}-${version}"
  {
    inherit pname version meta;

    nativeBuildInputs = [
      tinycc.compiler
      gnumake
      gnupatch
      coreutils
    ];

    passthru.runCommand =
      name: env: buildCommand:
      derivationWithMeta (
        {
          inherit name buildCommand;
          builder = "${bash_2_05}/bin/bash";
          args = [
            "-e"
            (builtins.toFile "bash-builder.sh" ''
              export CONFIG_SHELL=$SHELL

              # Normalize the NIX_BUILD_CORES variable. The value might be 0, which
              # means that we're supposed to try and auto-detect the number of
              # available CPU cores at run-time. We don't have nproc to detect the
              # number of available CPU cores so default to 1 if not set.
              NIX_BUILD_CORES="''${NIX_BUILD_CORES:-1}"
              if [ $NIX_BUILD_CORES -le 0 ]; then
                NIX_BUILD_CORES=1
              fi
              export NIX_BUILD_CORES

              bash -eux $buildCommandPath
            '')
          ];
          passAsFile = [ "buildCommand" ];

          SHELL = "${bash_2_05}/bin/bash";
          PATH = lib.makeBinPath (
            (env.nativeBuildInputs or [ ])
            ++ [
              bash_2_05
              coreutils
              # provides untar, ungz, and unbz2
              mescc-tools-extra
            ]
          );
        }
        // (removeAttrs env [ "nativeBuildInputs" ])
      );

    passthru.tests.get-version =
      result:
      kaem.runCommand "${pname}-get-version-${version}" { } ''
        ${result}/bin/bash --version
        mkdir ''${out}
      '';
  }
  ''
    # Unpack
    ungz --file ${src} --output bash.tar
    untar --file bash.tar
    rm bash.tar
    cd bash-${version}

    # Patch
    ${lib.concatMapStringsSep "\n" (f: "patch -Np0 -i ${f}") patches}

    # Configure
    cp ${main_mk} Makefile
    cp ${builtins_mk} builtins/Makefile
    cp ${common_mk} common.mk
    touch config.h
    touch include/version.h
    touch include/pipesize.h

    # Build
    make \
      CC="tcc -B ${tinycc.libs}/lib" \
      mkbuiltins
    cd builtins
    make \
      CC="tcc -B ${tinycc.libs}/lib" \
      libbuiltins.a
    cd ..
    make CC="tcc -B ${tinycc.libs}/lib"

    # Install
    install -D bash ''${out}/bin/bash
    ln -s bash ''${out}/bin/sh
  ''