summaryrefslogtreecommitdiff
path: root/bootle/package.nix
blob: 822cf8999ea82793172cd0883ec4741af2793dcf (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
{
  callPackage,
  rust-bin,
  naersk,
  bootler,
  qemu,
  ...
}: let
  rust-toolchain = rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
  naersk' = callPackage naersk {
    cargo = rust-toolchain;
    rustc = rust-toolchain;
    clippy = rust-toolchain;
  };
in (naersk'.buildPackage {
  src = ./.;

  # deps for rust-src
  additionalCargoLock = "${rust-toolchain.availableComponents.rust-src}/lib/rustlib/src/rust/library/Cargo.lock";

  # just library build
  copyBins = false;
  copyLibs = true;
  release = true;

  # build std
  cargoBuildOptions = x:
    x
    ++ [
      "-Zbuild-std"
    ];

  postInstall = ''
    ld --oformat binary \
      -o kernel.bin \
      -T src/linker.ld \
      -e _start \
      target/x86_64-unknown-none/release/libbootle.a

    dd if=/dev/zero of=disk bs=512 count=2880
    dd if=${bootler}/bin/boot.bin of=disk conv=notrunc
    dd if=kernel.bin of=disk bs=512 seek=1 conv=notrunc

    mkdir -p $out/lib
    mkdir -p $out/bin

    cat<<EOF>$out/bin/bootle
    #!/usr/bin/env bash

    mkdir -p ./.bootle
    cp $(echo $out)/bin/disk ./.bootle/disk
    chmod a+w ./.bootle/disk

    ${qemu}/bin/qemu-system-x86_64 \
      -nographic \
      -drive file=./.bootle/disk,format=raw,index=0,media=disk

    rm ./.bootle -r

    EOF

    chmod +x $out/bin/bootle

    cp target/x86_64-unknown-none/release/libbootle.a $out/lib
    cp kernel.bin $out/bin
    cp disk $out/bin
  '';
})