1{ lib, rust, rustPlatform, fetchFromGitHub }:
2
3let
4 mkBlogOsTest = target: rustPlatform.buildRustPackage rec {
5 name = "blog_os-sysroot-test";
6
7 src = fetchFromGitHub {
8 owner = "phil-opp";
9 repo = "blog_os";
10 rev = "4e38e7ddf8dd021c3cd7e4609dfa01afb827797b";
11 sha256 = "0k9ipm9ddm1bad7bs7368wzzp6xwrhyfzfpckdax54l4ffqwljcg";
12 };
13
14 cargoSha256 = "1x8iwgy1irgfkv2yjkxm6479nwbrk82b0c80jm7y4kw0s32r01lg";
15
16 inherit target;
17
18 RUSTFLAGS = "-C link-arg=-nostartfiles";
19
20 # Tests don't work for `no_std`. See https://os.phil-opp.com/testing/
21 doCheck = false;
22
23 meta = with lib; {
24 description = "Test for using custom sysroots with buildRustPackage";
25 maintainers = with maintainers; [ aaronjanse ];
26 platforms = lib.platforms.x86_64;
27 };
28 };
29
30 # The book uses rust-lld for linking, but rust-lld is not currently packaged for NixOS.
31 # The justification in the book for using rust-lld suggests that gcc can still be used for testing:
32 # > Instead of using the platform's default linker (which might not support Linux targets),
33 # > we use the cross platform LLD linker that is shipped with Rust for linking our kernel.
34 # https://github.com/phil-opp/blog_os/blame/7212ffaa8383122b1eb07fe1854814f99d2e1af4/blog/content/second-edition/posts/02-minimal-rust-kernel/index.md#L157
35 targetContents = {
36 "llvm-target" = "x86_64-unknown-none";
37 "data-layout" = "e-m:e-i64:64-f80:128-n8:16:32:64-S128";
38 "arch" = "x86_64";
39 "target-endian" = "little";
40 "target-pointer-width" = "64";
41 "target-c-int-width" = "32";
42 "os" = "none";
43 "executables" = true;
44 "linker-flavor" = "gcc";
45 "panic-strategy" = "abort";
46 "disable-redzone" = true;
47 "features" = "-mmx,-sse,+soft-float";
48 };
49
50in {
51 blogOS-targetByFile = mkBlogOsTest (builtins.toFile "x86_64-blog_os.json" (builtins.toJSON targetContents));
52 blogOS-targetByNix = let
53 plat = lib.systems.elaborate { config = "x86_64-none"; } // {
54 rustc = {
55 config = "x86_64-blog_os";
56 platform = targetContents;
57 };
58 };
59 in mkBlogOsTest (rust.toRustTargetSpec plat);
60}