Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

project: move to nixpkgs Rust infrastructure #147

Merged
merged 2 commits into from
Jun 12, 2023
Merged
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
68 changes: 7 additions & 61 deletions flake.lock

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

202 changes: 51 additions & 151 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,13 @@
# avoid having multiple versions in our dependencies.
flake-utils.url = "github:numtide/flake-utils";

crane = {
url = "github:ipetkov/crane";
inputs.nixpkgs.follows = "nixpkgs";
inputs.rust-overlay.follows = "rust-overlay";
inputs.flake-utils.follows = "flake-utils";
inputs.flake-compat.follows = "flake-compat";
};

rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
inputs.flake-utils.follows = "flake-utils";
};

flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
};
};

outputs = inputs@{ self, nixpkgs, crane, rust-overlay, flake-parts, ... }:
outputs = inputs@{ self, nixpkgs, flake-parts, ... }:
flake-parts.lib.mkFlake { inherit inputs; } ({ moduleWithSystem, ... }: {
imports = [
# Derive the output overlay automatically from all packages that we define.
Expand All @@ -48,27 +34,20 @@
inputs.pre-commit-hooks-nix.flakeModule
];

flake.nixosModules.lanzaboote = moduleWithSystem (
perSystem@{ config }:
flake.nixosModules.lanzaboote = moduleWithSystem (perSystem@{ config }:
{ ... }: {
imports = [
./nix/modules/lanzaboote.nix
];
imports = [ ./nix/modules/lanzaboote.nix ];

boot.lanzaboote.package = perSystem.config.packages.tool;
}
);
});

flake.nixosModules.uki = moduleWithSystem (
perSystem@{ config }:
flake.nixosModules.uki = moduleWithSystem (perSystem@{ config }:
{ lib, ... }: {
imports = [
./nix/modules/uki.nix
];
imports = [ ./nix/modules/uki.nix ];

boot.loader.uki.stub = lib.mkDefault "${perSystem.config.packages.fatStub}/bin/lanzaboote_stub.efi";
}
);
boot.loader.uki.stub = lib.mkDefault
"${perSystem.config.packages.fatStub}/bin/lanzaboote_stub.efi";
});

systems = [
"x86_64-linux"
Expand All @@ -79,94 +58,25 @@

perSystem = { config, system, pkgs, ... }:
let
pkgs = import nixpkgs {
system = system;
overlays = [
rust-overlay.overlays.default
];
};

inherit (pkgs) lib;

uefi-rust-stable = pkgs.rust-bin.fromRustupToolchainFile ./rust/stub/rust-toolchain.toml;
craneLib = crane.lib.x86_64-linux.overrideToolchain uefi-rust-stable;

# Build attributes for a Rust application.
buildRustApp = lib.makeOverridable (
{ src
, target ? null
, doCheck ? true
, extraArgs ? { }
}:
let
commonArgs = {
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 {
src = craneLib.cleanCargoSource ./rust/stub;
target = "x86_64-unknown-uefi";
doCheck = false;
};

fatStubCrane = stubCrane.override {
extraArgs = {
cargoExtraArgs = "--no-default-features --features fat";
pkgs = import nixpkgs { inherit system; };
uefiPkgs = import nixpkgs {
inherit system;
crossSystem = {
nikstur marked this conversation as resolved.
Show resolved Hide resolved
# linuxArch is wrong here, it will yield arm64 instead of aarch64.
config = "${pkgs.hostPlatform.qemuArch}-windows";
rustc.config = "${pkgs.hostPlatform.qemuArch}-unknown-uefi";
Comment on lines +66 to +67
Copy link
Member

@SuperSandro2000 SuperSandro2000 Jun 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
config = "${pkgs.hostPlatform.qemuArch}-windows";
rustc.config = "${pkgs.hostPlatform.qemuArch}-unknown-uefi";
config = "${pkgs.stdenv.hostPlatform.qemuArch}-windows";
rustc.config = "${pkgs.stdenv.hostPlatform.qemuArch}-unknown-uefi";
       error: attribute 'hostPlatform' missing

       at /nix/store/k7h8k8whqw64rmsg6yhkmmdc82lkczc6-source/flake.nix:66:27:

           65|               # linuxArch is wrong here, it will yield arm64 instead of aarch64.
           66|               config = "${pkgs.hostPlatform.qemuArch}-windows";
             |                           ^
           67|               rustc.config = "${pkgs.hostPlatform.qemuArch}-unknown-uefi";
       Did you mean rustPlatform?

Going to test this and create a PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see #198

libc = null;
useLLVM = true;
};
};
utils = import ./nix/packages/utils.nix;

stub = stubCrane.package;
fatStub = fatStubCrane.package;

toolCrane = buildRustApp {
src = ./rust/tool;
extraArgs = {
TEST_SYSTEMD = pkgs.systemd;
nativeCheckInputs = with pkgs; [
binutils-unwrapped
sbsigntool
];
};
};
inherit (pkgs) lib;

tool = toolCrane.package;
stub = uefiPkgs.callPackage ./nix/packages/stub.nix { };
fatStub =
uefiPkgs.callPackage ./nix/packages/stub.nix { fatVariant = true; };
tool = pkgs.callPackage ./nix/packages/tool.nix { };

wrappedTool = pkgs.runCommand "lzbt"
{
Expand All @@ -177,7 +87,9 @@
# Clean PATH to only contain what we need to do objcopy. Also
# tell lanzatool where to find our UEFI binaries.
makeWrapper ${tool}/bin/lzbt $out/bin/lzbt \
--set PATH ${lib.makeBinPath [ pkgs.binutils-unwrapped pkgs.sbsigntool ]} \
--set PATH ${
lib.makeBinPath [ pkgs.binutils-unwrapped pkgs.sbsigntool ]
} \
--set LANZABOOTE_STUB ${stub}/bin/lanzaboote_stub.efi
'';
in
Expand All @@ -188,24 +100,23 @@
lzbt = wrappedTool;
};

overlayAttrs = {
inherit (config.packages) tool;
};
overlayAttrs = { inherit (config.packages) tool; };

checks =
let
nixosLib = import (pkgs.path + "/nixos/lib") { };
runTest = module: nixosLib.runTest {
imports = [ module ];
hostPkgs = pkgs;
};
runTest = module:
nixosLib.runTest {
imports = [ module ];
hostPkgs = pkgs;
};
in
{
toolClippy = toolCrane.clippy;
stubClippy = stubCrane.clippy;
fatStubClippy = fatStubCrane.clippy;
toolFmt = toolCrane.rustfmt;
stubFmt = stubCrane.rustfmt;
stubFmt = uefiPkgs.callPackage (utils.rustfmt stub) { };
toolFmt = pkgs.callPackage (utils.rustfmt tool) { };
toolClippy = pkgs.callPackage (utils.clippy tool) { };
stubClippy = uefiPkgs.callPackage (utils.clippy stub) { };
fatStubClippy = uefiPkgs.callPackage (utils.clippy fatStub) { };
} // (import ./nix/tests/lanzaboote.nix {
inherit pkgs;
lanzabooteModule = self.nixosModules.lanzaboote;
Expand Down Expand Up @@ -236,32 +147,21 @@
export PATH=$PATH:${systemdUkify}/lib/systemd
'';

packages =
let
uefi-run = pkgs.callPackage ./nix/packages/uefi-run.nix {
inherit craneLib;
};
in
[
uefi-run
pkgs.openssl
(pkgs.sbctl.override {
databasePath = "pki";
})
pkgs.sbsigntool
pkgs.efitools
pkgs.python39Packages.ovmfvartool
pkgs.qemu
pkgs.nixpkgs-fmt
pkgs.statix
pkgs.cargo-release
];

inputsFrom = [
config.packages.stub
config.packages.tool
packages = [
pkgs.uefi-run
pkgs.openssl
(pkgs.sbctl.override { databasePath = "pki"; })
pkgs.sbsigntool
pkgs.efitools
pkgs.python39Packages.ovmfvartool
pkgs.qemu
pkgs.nixpkgs-fmt
pkgs.statix
pkgs.cargo-release
];

inputsFrom = [ config.packages.stub config.packages.tool ];

TEST_SYSTEMD = pkgs.systemd;
};
};
Expand Down
Loading