lanzaboote/flake.nix

133 lines
3.8 KiB
Nix
Raw Normal View History

2022-11-21 04:44:04 -06:00
{
description = "A very basic flake";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
rust-overlay.url = "github:oxalica/rust-overlay";
naersk.url = "github:nix-community/naersk";
};
outputs = { self, nixpkgs, rust-overlay, naersk }:
let
pkgs = import nixpkgs {
system = "x86_64-linux";
overlays = [
rust-overlay.overlays.default
];
};
rust-nightly = pkgs.rust-bin.fromRustupToolchainFile ./rust/lanzaboote/rust-toolchain.toml;
2022-11-21 04:44:04 -06:00
2022-11-21 17:42:41 -06:00
naersk-nightly = pkgs.callPackage naersk {
cargo = rust-nightly;
rustc = rust-nightly;
2022-11-21 04:44:04 -06:00
};
2022-11-21 08:36:39 -06:00
qemuUefi = pkgs.writeShellScriptBin "qemu-uefi" ''
exec ${pkgs.qemu}/bin/qemu-system-x86_64 \
-machine q35,accel=kvm:tcg -bios ${pkgs.OVMF.fd}/FV/OVMF.fd \
-m 4096 -serial stdio "$@"
'';
2022-11-21 17:42:41 -06:00
uefi-run = pkgs.callPackage ./nix/uefi-run.nix {
2022-11-22 17:58:52 -06:00
naersk = naersk-nightly;
2022-11-21 08:36:39 -06:00
};
systemd-boot-run = pkgs.writeShellScriptBin "systemd-boot-run" ''
${uefi-run}/bin/uefi-run lib/systemd/boot/efi/systemd-bootx64.efi
'';
add-sections = pkgs.writeShellScriptBin "add-sections" ''
set -eu
IN=$1
OSREL=$2
CMDLINE=$3
OUT=$4
stub_line=$(objdump -h "$1" | tail -2 | head -1)
stub_size=0x$(echo "$stub_line" | awk '{print $3}')
stub_offs=0x$(echo "$stub_line" | awk '{print $4}')
osrel_offs=$((stub_size + stub_offs))
cmdline_offs=$((osrel_offs + $(stat -c%s "$OSREL")))
objcopy \
--add-section .osrel="$OSREL" --change-section-vma .osrel=$(printf 0x%x $osrel_offs) \
--add-section .cmdline="$CMDLINE" \
--change-section-vma .cmdline=$(printf 0x%x $cmdline_offs) \
"$IN" "$OUT"
'';
buildRustEfiApp = src: naersk-nightly.buildPackage {
inherit src;
2022-11-21 08:36:39 -06:00
cargoBuildOptions = old: old ++ [
"--target x86_64-unknown-uefi"
];
};
buildRustLinuxApp = src: naersk-nightly.buildPackage {
inherit src;
2022-11-21 18:29:16 -06:00
};
# This is basically an empty EFI application that we use as a
# carrier for the initrd.
2022-11-23 06:05:19 -06:00
initrd-stub = buildRustEfiApp ./rust/initrd-stub;
lanzaboote = buildRustEfiApp ./rust/lanzaboote;
lanzatool = buildRustLinuxApp ./rust/lanzatool;
# A script that takes an initrd and turns it into a PE image.
wrapInitrd = pkgs.writeShellScriptBin "wrap-initrd" ''
set -eu
STUB=${initrd-stub}/bin/initrd-stub.efi
INITRD=$1
OUT=$2
stub_line=$(objdump -h "$STUB" | tail -2 | head -1)
stub_size=0x$(echo "$stub_line" | awk '{print $3}')
stub_offs=0x$(echo "$stub_line" | awk '{print $4}')
initrd_offs=$((stub_size + stub_offs))
objcopy --add-section .initrd="$INITRD" --change-section-vma .initrd=$(printf 0x%x $initrd_offs) \
"$STUB" "$OUT"
'';
2022-11-21 08:36:39 -06:00
osrel = pkgs.writeText "lanzaboote-osrel" ''
NAME=Lanzaboote
2022-11-21 18:01:11 -06:00
VERSION="${lanzaboote.version}"
2022-11-21 08:36:39 -06:00
'';
cmdline = pkgs.writeText "lanzaboote-cmdline" "console=ttyS0";
2022-11-21 08:36:39 -06:00
lanzaboote-uki = pkgs.runCommand "lanzboote-uki" {
nativeBuildInputs = [
pkgs.binutils-unwrapped
add-sections
];
} ''
mkdir -p $out/bin
add-sections ${lanzaboote}/bin/lanzaboote.efi ${osrel} ${cmdline} $out/bin/lanzaboote.efi
'';
2022-11-21 17:45:18 -06:00
in {
packages.x86_64-linux = {
inherit qemuUefi uefi-run initrd-stub lanzaboote lanzaboote-uki lanzatool wrapInitrd;
2022-11-21 17:45:18 -06:00
default = lanzaboote-uki;
};
2022-11-21 08:36:39 -06:00
2022-11-21 17:45:18 -06:00
devShells.x86_64-linux.default = pkgs.mkShell {
packages = [
2022-11-21 17:45:18 -06:00
qemuUefi
uefi-run
lanzatool
2022-11-21 19:08:35 -06:00
pkgs.openssl
wrapInitrd
2022-11-21 17:45:18 -06:00
];
inputsFrom = [
lanzatool
lanzaboote
];
2022-11-21 04:44:04 -06:00
};
2022-11-21 17:45:18 -06:00
};
2022-11-21 04:44:04 -06:00
}