2022-11-21 04:44:04 -06:00
|
|
|
{
|
2023-09-14 04:22:22 -05:00
|
|
|
description = "Secure Boot for NixOS";
|
2022-11-21 04:44:04 -06:00
|
|
|
|
|
|
|
inputs = {
|
2022-12-17 17:31:09 -06:00
|
|
|
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable-small";
|
2022-11-26 09:41:48 -06:00
|
|
|
|
2023-01-07 04:09:35 -06:00
|
|
|
flake-parts.url = "github:hercules-ci/flake-parts";
|
2023-04-23 04:55:04 -05:00
|
|
|
flake-parts.inputs.nixpkgs-lib.follows = "nixpkgs";
|
2023-01-07 04:09:35 -06:00
|
|
|
|
2023-01-07 13:21:03 -06:00
|
|
|
pre-commit-hooks-nix = {
|
|
|
|
url = "github:cachix/pre-commit-hooks.nix";
|
|
|
|
inputs.nixpkgs.follows = "nixpkgs";
|
|
|
|
inputs.flake-utils.follows = "flake-utils";
|
2023-04-23 04:55:04 -05:00
|
|
|
inputs.flake-compat.follows = "flake-compat";
|
2023-01-07 13:21:03 -06:00
|
|
|
};
|
|
|
|
|
2023-01-07 04:09:35 -06:00
|
|
|
# We only have this input to pass it to other dependencies and
|
2023-01-21 03:27:34 -06:00
|
|
|
# avoid having multiple versions in our dependencies.
|
2023-01-07 04:09:35 -06:00
|
|
|
flake-utils.url = "github:numtide/flake-utils";
|
|
|
|
|
2023-07-21 17:45:50 -05:00
|
|
|
crane = {
|
|
|
|
url = "github:ipetkov/crane";
|
|
|
|
inputs.nixpkgs.follows = "nixpkgs";
|
|
|
|
};
|
|
|
|
|
|
|
|
rust-overlay = {
|
|
|
|
url = "github:oxalica/rust-overlay";
|
|
|
|
inputs.nixpkgs.follows = "nixpkgs";
|
|
|
|
inputs.flake-utils.follows = "flake-utils";
|
|
|
|
};
|
|
|
|
|
2022-12-25 18:22:13 -06:00
|
|
|
flake-compat = {
|
|
|
|
url = "github:edolstra/flake-compat";
|
|
|
|
flake = false;
|
|
|
|
};
|
2022-11-21 04:44:04 -06:00
|
|
|
};
|
|
|
|
|
2023-07-21 17:45:50 -05:00
|
|
|
outputs = inputs@{ self, nixpkgs, crane, rust-overlay, flake-parts, ... }:
|
2023-11-10 08:15:48 -06:00
|
|
|
let
|
|
|
|
# Systems supported in CI
|
|
|
|
supportedSystems = [ "x86_64-linux" ];
|
|
|
|
fixupFlakes = outputs: nixpkgs.lib.updateManyAttrsByPath [
|
|
|
|
# Apply post-flakeparts massaging for limited supported systems, e.g. systems for which
|
|
|
|
# we don't have KVM support and cannot test in CI, but we still can meaningfully
|
|
|
|
# build packages.
|
|
|
|
{
|
|
|
|
path = [ "checks" ];
|
|
|
|
update = nixpkgs.lib.filterAttrs (name: _: builtins.elem name supportedSystems);
|
|
|
|
}
|
|
|
|
]
|
|
|
|
outputs;
|
|
|
|
in
|
|
|
|
fixupFlakes (flake-parts.lib.mkFlake { inherit inputs; } ({ moduleWithSystem, ... }: {
|
2023-01-07 04:52:49 -06:00
|
|
|
imports = [
|
|
|
|
# Derive the output overlay automatically from all packages that we define.
|
|
|
|
inputs.flake-parts.flakeModules.easyOverlay
|
2023-01-07 13:21:03 -06:00
|
|
|
|
|
|
|
# Formatting and quality checks.
|
|
|
|
inputs.pre-commit-hooks-nix.flakeModule
|
2023-01-07 04:52:49 -06:00
|
|
|
];
|
|
|
|
|
2023-07-21 17:45:50 -05:00
|
|
|
flake.nixosModules.lanzaboote = moduleWithSystem (
|
|
|
|
perSystem@{ config }:
|
2023-01-07 04:52:49 -06:00
|
|
|
{ ... }: {
|
2023-07-21 17:45:50 -05:00
|
|
|
imports = [
|
|
|
|
./nix/modules/lanzaboote.nix
|
|
|
|
];
|
2023-01-07 04:52:49 -06:00
|
|
|
|
2023-01-13 15:10:40 -06:00
|
|
|
boot.lanzaboote.package = perSystem.config.packages.tool;
|
2023-07-21 17:45:50 -05:00
|
|
|
}
|
|
|
|
);
|
2023-01-07 04:52:49 -06:00
|
|
|
|
2023-07-21 17:45:50 -05:00
|
|
|
flake.nixosModules.uki = moduleWithSystem (
|
|
|
|
perSystem@{ config }:
|
2023-05-22 16:28:59 -05:00
|
|
|
{ lib, ... }: {
|
2023-07-21 17:45:50 -05:00
|
|
|
imports = [
|
|
|
|
./nix/modules/uki.nix
|
|
|
|
];
|
2023-05-22 16:28:59 -05:00
|
|
|
|
2023-07-21 17:45:50 -05:00
|
|
|
boot.loader.uki.stub = lib.mkDefault "${perSystem.config.packages.fatStub}/bin/lanzaboote_stub.efi";
|
|
|
|
}
|
|
|
|
);
|
2023-05-22 16:28:59 -05:00
|
|
|
|
2023-01-07 04:52:49 -06:00
|
|
|
systems = [
|
|
|
|
"x86_64-linux"
|
|
|
|
|
|
|
|
# Not actively tested, but may work:
|
2022-12-20 21:54:38 -06:00
|
|
|
"aarch64-linux"
|
2023-01-07 04:52:49 -06:00
|
|
|
];
|
|
|
|
|
2023-01-08 07:42:30 -06:00
|
|
|
perSystem = { config, system, pkgs, ... }:
|
|
|
|
let
|
2023-09-29 15:31:47 -05:00
|
|
|
rustTarget = "${pkgs.stdenv.hostPlatform.qemuArch}-unknown-uefi";
|
2023-07-21 17:45:50 -05:00
|
|
|
pkgs = import nixpkgs {
|
|
|
|
system = system;
|
|
|
|
overlays = [
|
|
|
|
rust-overlay.overlays.default
|
|
|
|
];
|
2023-05-22 16:28:59 -05:00
|
|
|
};
|
|
|
|
|
2023-04-12 15:13:12 -05:00
|
|
|
inherit (pkgs) lib;
|
2023-01-07 04:52:49 -06:00
|
|
|
|
2023-09-13 10:38:17 -05:00
|
|
|
uefi-rust-stable = pkgs.rust-bin.fromRustupToolchainFile ./rust/uefi/rust-toolchain.toml;
|
2023-09-14 09:55:11 -05:00
|
|
|
craneLib = crane.lib.${system}.overrideToolchain uefi-rust-stable;
|
2023-07-21 17:45:50 -05:00
|
|
|
|
|
|
|
# Build attributes for a Rust application.
|
|
|
|
buildRustApp = lib.makeOverridable (
|
2023-09-13 10:38:17 -05:00
|
|
|
{ pname
|
|
|
|
, src
|
2023-07-21 17:45:50 -05:00
|
|
|
, target ? null
|
|
|
|
, doCheck ? true
|
|
|
|
, extraArgs ? { }
|
|
|
|
}:
|
|
|
|
let
|
|
|
|
commonArgs = {
|
2023-09-13 10:38:17 -05:00
|
|
|
inherit pname;
|
2023-07-21 17:45:50 -05:00
|
|
|
inherit src;
|
|
|
|
CARGO_BUILD_TARGET = target;
|
|
|
|
inherit doCheck;
|
|
|
|
|
|
|
|
# Workaround for https://github.com/ipetkov/crane/issues/262.
|
|
|
|
dummyrs = pkgs.writeText "dummy.rs" ''
|
|
|
|
#![allow(unused)]
|
|
|
|
|
|
|
|
#![cfg_attr(
|
|
|
|
any(target_os = "none", target_os = "uefi"),
|
|
|
|
no_std,
|
|
|
|
no_main,
|
|
|
|
)]
|
|
|
|
|
|
|
|
#[cfg_attr(any(target_os = "none", target_os = "uefi"), panic_handler)]
|
|
|
|
fn panic(_info: &::core::panic::PanicInfo<'_>) -> ! {
|
|
|
|
loop {}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(any(target_os = "none", target_os = "uefi"), export_name = "efi_main")]
|
|
|
|
fn main() {}
|
|
|
|
'';
|
|
|
|
} // extraArgs;
|
|
|
|
|
|
|
|
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
package = craneLib.buildPackage (commonArgs // {
|
|
|
|
inherit cargoArtifacts;
|
|
|
|
});
|
|
|
|
|
|
|
|
clippy = craneLib.cargoClippy (commonArgs // {
|
|
|
|
inherit cargoArtifacts;
|
|
|
|
cargoClippyExtraArgs = "-- --deny warnings";
|
|
|
|
});
|
|
|
|
|
|
|
|
rustfmt = craneLib.cargoFmt (commonArgs // { inherit cargoArtifacts; });
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
stubCrane = buildRustApp {
|
2023-09-13 10:38:17 -05:00
|
|
|
pname = "lanzaboote-stub";
|
|
|
|
src = craneLib.cleanCargoSource ./rust/uefi;
|
2023-09-14 09:55:11 -05:00
|
|
|
target = rustTarget;
|
2023-07-21 17:45:50 -05:00
|
|
|
doCheck = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
fatStubCrane = stubCrane.override {
|
|
|
|
extraArgs = {
|
|
|
|
cargoExtraArgs = "--no-default-features --features fat";
|
2023-08-09 18:42:51 -05:00
|
|
|
};
|
|
|
|
};
|
2023-07-21 17:45:50 -05:00
|
|
|
|
|
|
|
stub = stubCrane.package;
|
|
|
|
fatStub = fatStubCrane.package;
|
|
|
|
|
2023-09-14 04:41:29 -05:00
|
|
|
# TODO: when we will have more backends
|
|
|
|
# let's generalize this properly.
|
2023-07-21 17:45:50 -05:00
|
|
|
toolCrane = buildRustApp {
|
2023-09-14 04:41:29 -05:00
|
|
|
pname = "lzbt-systemd";
|
2023-07-21 17:45:50 -05:00
|
|
|
src = ./rust/tool;
|
|
|
|
extraArgs = {
|
|
|
|
TEST_SYSTEMD = pkgs.systemd;
|
|
|
|
nativeCheckInputs = with pkgs; [
|
|
|
|
binutils-unwrapped
|
|
|
|
sbsigntool
|
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
tool = toolCrane.package;
|
2023-01-13 15:10:40 -06:00
|
|
|
|
|
|
|
wrappedTool = pkgs.runCommand "lzbt"
|
|
|
|
{
|
|
|
|
nativeBuildInputs = [ pkgs.makeWrapper ];
|
|
|
|
} ''
|
|
|
|
mkdir -p $out/bin
|
|
|
|
|
|
|
|
# Clean PATH to only contain what we need to do objcopy. Also
|
|
|
|
# tell lanzatool where to find our UEFI binaries.
|
2023-09-14 04:41:29 -05:00
|
|
|
makeWrapper ${tool}/bin/lzbt-systemd $out/bin/lzbt \
|
2023-07-21 17:45:50 -05:00
|
|
|
--set PATH ${lib.makeBinPath [ pkgs.binutils-unwrapped pkgs.sbsigntool ]} \
|
2023-01-13 15:10:40 -06:00
|
|
|
--set LANZABOOTE_STUB ${stub}/bin/lanzaboote_stub.efi
|
|
|
|
'';
|
2023-01-08 07:42:30 -06:00
|
|
|
in
|
|
|
|
{
|
|
|
|
packages = {
|
2023-05-22 16:28:59 -05:00
|
|
|
inherit stub fatStub;
|
2023-01-13 15:10:40 -06:00
|
|
|
tool = wrappedTool;
|
|
|
|
lzbt = wrappedTool;
|
2023-01-08 07:42:30 -06:00
|
|
|
};
|
2023-01-08 07:41:01 -06:00
|
|
|
|
2023-07-21 17:45:50 -05:00
|
|
|
overlayAttrs = {
|
|
|
|
inherit (config.packages) tool;
|
|
|
|
};
|
2023-01-07 13:21:03 -06:00
|
|
|
|
2023-05-28 17:09:07 -05:00
|
|
|
checks =
|
|
|
|
let
|
|
|
|
nixosLib = import (pkgs.path + "/nixos/lib") { };
|
2023-07-21 17:45:50 -05:00
|
|
|
runTest = module: nixosLib.runTest {
|
|
|
|
imports = [ module ];
|
|
|
|
hostPkgs = pkgs;
|
|
|
|
};
|
2023-05-28 17:09:07 -05:00
|
|
|
in
|
|
|
|
{
|
2023-07-21 17:45:50 -05:00
|
|
|
toolClippy = toolCrane.clippy;
|
|
|
|
stubClippy = stubCrane.clippy;
|
|
|
|
fatStubClippy = fatStubCrane.clippy;
|
|
|
|
toolFmt = toolCrane.rustfmt;
|
|
|
|
stubFmt = stubCrane.rustfmt;
|
2023-05-28 17:09:07 -05:00
|
|
|
} // (import ./nix/tests/lanzaboote.nix {
|
|
|
|
inherit pkgs;
|
|
|
|
lanzabooteModule = self.nixosModules.lanzaboote;
|
|
|
|
}) // (import ./nix/tests/stub.nix {
|
|
|
|
inherit pkgs runTest;
|
|
|
|
ukiModule = self.nixosModules.uki;
|
|
|
|
});
|
2023-01-08 07:42:30 -06:00
|
|
|
|
|
|
|
pre-commit = {
|
|
|
|
check.enable = true;
|
2023-01-07 13:21:03 -06:00
|
|
|
|
2023-01-08 07:42:30 -06:00
|
|
|
settings.hooks = {
|
|
|
|
nixpkgs-fmt.enable = true;
|
2023-01-21 03:28:02 -06:00
|
|
|
typos.enable = true;
|
2023-01-07 04:52:49 -06:00
|
|
|
};
|
2023-01-08 07:42:30 -06:00
|
|
|
};
|
2023-01-07 04:09:35 -06:00
|
|
|
|
2023-01-08 07:42:30 -06:00
|
|
|
devShells.default = pkgs.mkShell {
|
2023-10-29 13:39:28 -05:00
|
|
|
shellHook = ''
|
|
|
|
${config.pre-commit.installationScript}
|
|
|
|
'';
|
2023-01-08 07:42:30 -06:00
|
|
|
|
2023-04-12 15:13:12 -05:00
|
|
|
packages = [
|
|
|
|
pkgs.nixpkgs-fmt
|
|
|
|
pkgs.statix
|
|
|
|
pkgs.cargo-release
|
2023-07-23 11:47:02 -05:00
|
|
|
pkgs.cargo-machete
|
2023-10-29 13:39:28 -05:00
|
|
|
|
|
|
|
# Convenience for test fixtures in nix/tests.
|
|
|
|
pkgs.openssl
|
|
|
|
(pkgs.sbctl.override { databasePath = "pki"; })
|
|
|
|
|
|
|
|
# Needed for `cargo test` in rust/tool. We also need
|
|
|
|
# TEST_SYSTEMD below for that.
|
|
|
|
pkgs.sbsigntool
|
2023-01-08 07:42:30 -06:00
|
|
|
];
|
2023-01-07 04:09:35 -06:00
|
|
|
|
2023-07-21 17:45:50 -05:00
|
|
|
inputsFrom = [
|
|
|
|
config.packages.stub
|
|
|
|
config.packages.tool
|
|
|
|
];
|
2023-04-12 15:13:12 -05:00
|
|
|
|
2023-01-08 07:42:30 -06:00
|
|
|
TEST_SYSTEMD = pkgs.systemd;
|
|
|
|
};
|
2023-01-07 04:09:35 -06:00
|
|
|
};
|
2023-11-10 08:15:48 -06:00
|
|
|
}));
|
2022-11-21 04:44:04 -06:00
|
|
|
}
|