Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ build_i686/
build_x86_64/
build_conan/

# nix
/result

# ignore log files in test tree
test/**/*.log

Expand Down
4 changes: 3 additions & 1 deletion deps/musl/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
stdenv
, pkgs
, linuxHeaders ? null
, debug ? false
}:
stdenv.mkDerivation rec {
pname = "musl-includeos";
Expand Down Expand Up @@ -40,7 +41,8 @@ stdenv.mkDerivation rec {
./configure --prefix=$out --disable-shared --enable-debug --with-malloc=oldmalloc CROSS_COMPILE=${stdenv.targetPlatform.config}-
'';

CFLAGS = "-Wno-error=int-conversion -nostdinc";
dontStrip = debug;
CFLAGS = "-Wno-error=int-conversion -nostdinc${pkgs.lib.optionalString debug " -g"}";

meta = {
description = "musl - Linux based libc, built with IncludeOS linux-like syscalls";
Expand Down
2 changes: 2 additions & 0 deletions example/vm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
63 changes: 63 additions & 0 deletions flake.lock

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

113 changes: 113 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
{
description = "IncludeOS";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/25.05";
# vmrunner.url = "github:includeos/vmrunner";
vmrunner.url = "github:mazunki/vmrunner";
};

outputs = { self, nixpkgs, vmrunner }:
let
system = "x86_64-linux";

mkIncludeos = { withCcache ? false, smp ? false, debug ? false }:
let
overlays = [
(import ./overlay.nix {
inherit withCcache smp debug;
disableTargetWarning = true;
})
];
pkgs = import nixpkgs { config = {}; inherit overlays system; };
pkgsIncludeOS = pkgs.pkgsIncludeOS;
stdenvIncludeOS = pkgs.stdenvIncludeOS;
includeos = pkgsIncludeOS.includeos;

chainloader =
let
chainloaderPkgs = import nixpkgs {
inherit system;
config = {};
overlays = [
(import ./overlay.nix { inherit withCcache; smp = false; disableTargetWarning = true; })
];
crossSystem = { config = "i686-unknown-linux-musl"; };
};
in import ./chainloader.nix {
inherit nixpkgs withCcache;
pkgs = chainloaderPkgs;
};
in {
inherit stdenvIncludeOS; # modified stdenv scope used by includeos (llvm, musl, libcxx)
inherit pkgsIncludeOS; # the musl/clang package scope used by IncludeOS
inherit pkgs; # nixpkgs with the IncludeOS overlay applied
inherit includeos; # the IncludeOS derivation to add to buildInputs
inherit chainloader; # 32-bit boot stub that hotswaps into the 64-bit unikernel
};

default = mkIncludeos {};
defaultDebug = mkIncludeos { debug = true; };
mkUnikernel = import ./mkUnikernel.nix { inherit system default defaultDebug vmrunner; };
in {
packages.${system} = {
default = default.includeos // { debug = defaultDebug.includeos; };
chainloader = default.chainloader;
example = mkUnikernel { unikernel = ./example; };
};

devShells.${system}.default = import ./develop.nix { includeos = default.includeos; };

lib.${system} = {
inherit mkIncludeos mkUnikernel;

mkChainloader = { mem }:
vmrunner.lib.${system}.mkBoot default.chainloader { inherit mem; };

boot = { kernel ? "service", mem, kvm ? false, debug ? false }:
let
chainloaders = self.lib.${system}.mkChainloader { inherit mem; };
chainloader = if kvm then chainloaders.kvm
else if debug then chainloaders.debug
else chainloaders.default;
in {
type = "app";
program = "${default.pkgs.writeShellScript "boot-unikernel" ''
set -e
dir="''${1:-./result}"
${default.pkgs.lib.optionalString debug ''echo "boot: $dir/${kernel}" >&2''}
exec ${chainloader}/bin/boot "$dir/${kernel}" "$@"
''}";
};

mkBoot1 = { src, kernel ? "service", kvm ? false, mem ? "16G", debug ? false }:
let
bootApp = self.lib.${system}.boot { inherit kernel kvm debug mem; };
in {
type = "app";
program = "${default.pkgs.writeShellScript "run-flake-package" ''
set -e
exec ${bootApp.program} ${src} "$@"
''}";
};

mkBoot = args:
let
debugSrc = args.debugSrc or args.src;
base = builtins.removeAttrs args [ "debugSrc" ];
tcg = self.lib.${system}.mkBoot1 (base // { kvm = false; debug = false; });
kvm = self.lib.${system}.mkBoot1 (base // { kvm = true; debug = false; });
debug = self.lib.${system}.mkBoot1 (base // { src = debugSrc; kvm = false; debug = true; });
in
tcg // { inherit tcg kvm debug; };
};

apps.${system} = {
default = self.apps.${system}.boot-unikernel;

boot-unikernel = self.lib.${system}.boot { mem = "128m"; };

example = self.lib.${system}.mkBoot { src = self.packages.${system}.example; kernel = "hello_includeos.elf.bin"; };
};
};
}
86 changes: 86 additions & 0 deletions mkUnikernel.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# mkUnikernel.nix
{ system, default, defaultDebug, vmrunner }:
args:
let
debug = args.debug or false;
ios = if args ? includeos then args.includeos
else if debug then defaultDebug
else default;
vmrunnerPkg = if args ? vmrunner then args.vmrunner
else vmrunner.packages.${system}.default;

unikernel = args.unikernel or ./example;
test = args.test or "test.py";
doCheck = args.doCheck or false;
arch = args.arch or "x86_64";
forProduction = args.forProduction or false;

src = unikernel;
in
ios.includeos.stdenv.mkDerivation {
pname = "includeos_unikernel";
version = "dev";
dontStrip = true;
inherit doCheck;
inherit src;

nativeBuildInputs = [
ios.pkgs.buildPackages.nasm
ios.pkgs.buildPackages.cmake
ios.pkgsIncludeOS.suppressTargetWarningHook
];

buildInputs = [
ios.includeos
ios.chainloader
];

cmakeFlags = [
"-DARCH=${arch}"
"-DINCLUDEOS_PACKAGE=${ios.includeos}"
"-DCMAKE_MODULE_PATH=${ios.includeos}/cmake"
"-DFOR_PRODUCTION=${if forProduction then "ON" else "OFF"}"
"-DCMAKE_BUILD_TYPE=${if debug then "Debug" else "Release"}"
] ++ ios.pkgs.lib.optionals debug [
"-DCMAKE_C_FLAGS=-ffile-prefix-map=/build/${builtins.baseNameOf src}=/build/unikernel"
"-DCMAKE_CXX_FLAGS=-ffile-prefix-map=/build/${builtins.baseNameOf src}=/build/unikernel"
];

installPhase = ''
runHook preInstall
# we want to place any files required by the test into the output
find -mindepth 1 -maxdepth 1 -type f -exec install -v -D -t "$out/" {} \;

# especially the unikernel image, in case it wasn't at the rootdir already
find -mindepth 2 -name '*.elf.bin' -exec install -v -t "$out/" {} \;
runHook postInstall
'';

nativeCheckInputs = [
vmrunnerPkg
ios.pkgs.grub2
ios.pkgs.python3
ios.pkgs.qemu
ios.pkgs.iputils
ios.pkgs.xorriso
];

checkInputs = [
ios.includeos.lest
];

checkPhase = ''
runHook preCheck
set -e
if [ -e "${src}/${test}" ]; then
echo "Running IncludeOS test: ${src}/${test}"
python3 "${src}/${test}"
else
echo "Default test script '${test}', but no test was found 😟"
echo "For a custom path, consider specifying the path to the test script:"
echo " --argstr test 'path/to/test.py'"
exit 1
fi
runHook postCheck
'';
}
8 changes: 6 additions & 2 deletions overlay.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
withCcache, # Enable ccache. Requires correct permissions, see below.
disableTargetWarning ? true, # TODO: see https://github.com/NixOS/nixpkgs/issues/395191
smp, # Enable multicore support (SMP)
debug ? false,
} :
final: prev: {

Expand All @@ -13,7 +14,7 @@ final: prev: {
musl-unpatched = self.callPackage ./deps/musl-unpatched/default.nix { linuxHeaders = prev.linuxHeaders; };

# Import IncludeOS musl which will be built and linked with IncludeOS services
musl-includeos = self.callPackage ./deps/musl/default.nix { };
musl-includeos = self.callPackage ./deps/musl/default.nix { inherit debug; };

# Clang with unpatched musl for building libcxx
clang_musl_unpatched_nolibcxx = self.llvmPkgs.clangNoLibcxx.override (old: {
Expand Down Expand Up @@ -191,7 +192,10 @@ final: prev: {

smpFlags = if smp then [ "-DSMP=ON" ] else [];

cmakeFlags = archFlags ++ smpFlags;
debugFlags = if debug then [ "-DCMAKE_BUILD_TYPE=Debug" ] else [];
dontStrip = debug;

cmakeFlags = archFlags ++ smpFlags ++ debugFlags;

# Add some pasthroughs, for easily building the dependencies (for debugging):
# $ nix-build -A NAME
Expand Down