Skip to content

Commit

Permalink
Bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
jmbaur committed Dec 29, 2022
0 parents commit 5cbadfd
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 0 deletions.
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target
result*
.direnv
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[workspace]

members = ["tinyboot"]
14 changes: 14 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{ pkgsStatic, lib, ... }:
let
cargoTOML = lib.importTOML ./tinyboot/Cargo.toml;
in
pkgsStatic.rustPlatform.buildRustPackage {
pname = cargoTOML.package.name;
version = cargoTOML.package.version;
src = ./.;
buildAndTestSubdir = "tinyboot";
cargoLock.lockFile = ./Cargo.lock;
postInstall = ''
ln -s $out/bin/tinyboot $out/init
'';
}
26 changes: 26 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
description = "A small '/init' for linuxboot";
inputs = {
nixpkgs.url = "nixpkgs/nixos-unstable";
};
outputs = inputs: with inputs;
let
forAllSystems = f: nixpkgs.lib.genAttrs [ "x86_64-linux" "aarch64-linux" ] (system: f {
inherit system;
pkgs = import nixpkgs { inherit system; overlays = [ self.overlays.default ]; };
});
in
{
overlays.default = final: prev: {
tinyboot = prev.callPackage ./. { };
tinyboot-initramfs = prev.callPackage ./initramfs.nix {
inherit (final) tinyboot;
};
};
devShells = forAllSystems ({ pkgs, ... }: {
default = pkgs.mkShell {
buildInputs = with pkgs; [ rustc cargo ];
};
});
packages = forAllSystems ({ pkgs, ... }: {
default = pkgs.tinyboot;
initramfs = pkgs.tinyboot-initramfs;
test = pkgs.callPackage ./test.nix { };
});
};
}
16 changes: 16 additions & 0 deletions initramfs.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{ lib, buildEnv, runCommand, cpio, xz, pkgsStatic, tinyboot, compressed ? true, ... }:
let
paths = [ /*tinyboot*/ pkgsStatic.busybox ];
in
runCommand "tinyboot-initramfs"
{ nativeBuildInputs = [ cpio ] ++ (lib.optional compressed xz); } ''
mkdir -p root; ${lib.concatMapStringsSep "; " (p: "cp -r ${p}/. root") paths}
pushd root &&
find . -print0 | cpio --null --create --format=newc >../initramfs.cpio &&
popd
${if compressed then ''
xz --check=crc32 --lzma2=dict=512KiB <initramfs.cpio >$out
'' else ''
cp initramfs.cpio $out
''}
''
14 changes: 14 additions & 0 deletions test.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{ nixosTest, lib, tinyboot-initramfs, ... }:
nixosTest {
name = "tinyboot";
nodes.machine = {
virtualisation.qemu.options = lib.mkAfter [
"-initrd ${tinyboot-initramfs}"
"-append \"console=ttyS0 init=/init\""
];
};
testScript = ''
machine.start()
machine.wait_for_console_text("Hello, world!")
'';
}
8 changes: 8 additions & 0 deletions tinyboot/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "tinyboot"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
3 changes: 3 additions & 0 deletions tinyboot/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

0 comments on commit 5cbadfd

Please sign in to comment.