Skip to content

Commit 5b33020

Browse files
committed
Add Nix flake with development shell
This also includes a couple of minor touches to other things in order to make them work more nicely in a nix devshell
1 parent 6264bb0 commit 5b33020

File tree

5 files changed

+200
-4
lines changed

5 files changed

+200
-4
lines changed

dev/verify-msrv.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22
set -Eeuo pipefail
33
cargo install -q jaq
44
for CRATE in "$@"; do

flake.lock

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
{
2+
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
3+
inputs.nixpkgs-mozilla.url = "github:mozilla/nixpkgs-mozilla/master";
4+
outputs = { self, nixpkgs, nixpkgs-mozilla, ... } @ inputs:
5+
{
6+
devShells.x86_64-linux.default =
7+
let pkgs = import nixpkgs {
8+
system = "x86_64-linux";
9+
overlays = [ (import (nixpkgs-mozilla + "/rust-overlay.nix")) ];
10+
};
11+
in with pkgs; let
12+
# Work around the nixpkgs-mozilla equivalent of
13+
# https://github.com/NixOS/nixpkgs/issues/278508 and an
14+
# incompatibility between nixpkgs-mozilla and makeRustPlatform
15+
rustChannelOf = args: let
16+
orig = pkgs.rustChannelOf args;
17+
patchRustPkg = pkg: (pkg.overrideAttrs (oA: {
18+
buildCommand = builtins.replaceStrings
19+
[ "rustc,rustdoc" ]
20+
[ "rustc,rustdoc,clippy-driver,cargo-clippy" ]
21+
oA.buildCommand;
22+
})) // {
23+
targetPlatforms = [ "x86_64-linux" ];
24+
badTargetPlatforms = [ ];
25+
};
26+
overrideRustPkg = pkg: lib.makeOverridable (origArgs:
27+
patchRustPkg (pkg.override origArgs)
28+
) {};
29+
in builtins.mapAttrs (_: overrideRustPkg) orig;
30+
31+
customisedRustChannelOf = args:
32+
lib.flip builtins.mapAttrs (rustChannelOf args) (_: pkg: pkg.override {
33+
targets = [
34+
"x86_64-unknown-linux-gnu"
35+
"x86_64-pc-windows-msvc" "x86_64-unknown-none"
36+
"wasm32-wasip1" "wasm32-wasip2" "wasm32-unknown-unknown"
37+
];
38+
extensions = [ "rust-src" ];
39+
});
40+
41+
# Hyperlight needs a variety of toolchains, since we use Nightly
42+
# for rustfmt and old toolchains to verify MSRV
43+
toolchains = lib.mapAttrs (_: customisedRustChannelOf) {
44+
stable = {
45+
# Stay on 1.87 for development due to the
46+
# quickly-reversed default enablement of
47+
# #[warn(clippy::uninlined_format_args)]
48+
date = "2025-05-15";
49+
channel = "stable";
50+
sha256 = "sha256-KUm16pHj+cRedf8vxs/Hd2YWxpOrWZ7UOrwhILdSJBU=";
51+
};
52+
nightly = {
53+
date = "2025-07-29";
54+
channel = "nightly";
55+
sha256 = "sha256-6D2b7glWC3jpbIGCq6Ta59lGCKN9sTexhgixH4Y7Nng=";
56+
};
57+
"1.85" = {
58+
date = "2025-02-20";
59+
channel = "stable";
60+
sha256 = "sha256-AJ6LX/Q/Er9kS15bn9iflkUwcgYqRQxiOIL2ToVAXaU=";
61+
};
62+
};
63+
64+
rust-platform = makeRustPlatform {
65+
cargo = toolchains.stable.rust;
66+
rustc = toolchains.stable.rust;
67+
};
68+
69+
# Hyperlight scripts use cargo in a bunch of ways that don't
70+
# make sense for Nix cargo, including the `rustup +toolchain`
71+
# syntax to use a specific toolchain and `cargo install`, so we
72+
# build wrappers for rustc and cargo that enable this. The
73+
# scripts also use `rustup toolchain install` in some cases, in
74+
# order to work in CI, so we provide a fake rustup that does
75+
# nothing as well.
76+
rustup-like-wrapper = name: pkgs.writeShellScriptBin name
77+
(let
78+
clause = name: toolchain:
79+
"+${name}) base=\"${toolchain.rust}\"; shift 1; ;;";
80+
clauses = lib.strings.concatStringsSep "\n"
81+
(lib.mapAttrsToList clause toolchains);
82+
in ''
83+
base="${toolchains.stable.rust}"
84+
case "$1" in
85+
${clauses}
86+
install) exit 0; ;;
87+
esac
88+
export PATH="$base/bin:$PATH"
89+
exec "$base/bin/${name}" "$@"
90+
'');
91+
fake-rustup = pkgs.symlinkJoin {
92+
name = "fake-rustup";
93+
paths = [
94+
(pkgs.writeShellScriptBin "rustup" "")
95+
(rustup-like-wrapper "rustc")
96+
(rustup-like-wrapper "cargo")
97+
];
98+
};
99+
100+
buildRustPackageClang = rust-platform.buildRustPackage.override { stdenv = clangStdenv; };
101+
in (buildRustPackageClang rec {
102+
pname = "hyperlight";
103+
version = "0.0.0";
104+
src = lib.cleanSource ./.;
105+
cargoHash = "sha256-F8ntdieOstUyh7qKxENKOWYUyponFcMoeGBgeRpuCt0=";
106+
107+
nativeBuildInputs = [
108+
azure-cli
109+
just
110+
dotnet-sdk_9
111+
llvmPackages_18.llvm
112+
gh
113+
lld
114+
valgrind
115+
pkg-config
116+
ffmpeg
117+
mkvtoolnix
118+
wasm-tools
119+
jq
120+
jaq
121+
gdb
122+
];
123+
buildInputs = [
124+
pango
125+
cairo
126+
openssl
127+
];
128+
129+
auditable = false;
130+
131+
LIBCLANG_PATH = "${pkgs.llvmPackages_18.libclang.lib}/lib";
132+
# Use unwrapped clang for compiling guests
133+
HYPERLIGHT_GUEST_clang = "${clang.cc}/bin/clang";
134+
135+
RUST_NIGHTLY = "${toolchains.nightly.rust}";
136+
# Set this through shellHook rather than nativeBuildInputs to be
137+
# really sure that it overrides the real cargo.
138+
shellHook = ''
139+
export PATH="${fake-rustup}/bin:$PATH"
140+
'';
141+
}).overrideAttrs(oA: {
142+
hardeningDisable = [ "all" ];
143+
});
144+
};
145+
}

src/hyperlight_guest_bin/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ and third-party code used by our C-API needed to build a native hyperlight-guest
1616
[features]
1717
default = ["libc", "printf"]
1818
libc = [] # compile musl libc
19-
printf = [] # compile printf
19+
printf = [ "libc" ] # compile printf
2020
trace_guest = ["hyperlight-common/trace_guest", "dep:hyperlight-guest-tracing", "hyperlight-guest/trace_guest"]
2121
mem_profile = ["hyperlight-common/unwind_guest","hyperlight-common/mem_profile"]
2222

src/hyperlight_guest_bin/build.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ fn cargo_main() {
9797
cfg.flag("-fno-stack-protector");
9898
cfg.flag("-fstack-clash-protection");
9999
cfg.flag("-mstack-probe-size=4096");
100-
cfg.compiler("clang");
100+
cfg.compiler(
101+
env::var("HYPERLIGHT_GUEST_clang")
102+
.as_deref()
103+
.unwrap_or("clang"),
104+
);
101105

102106
if cfg!(windows) {
103107
unsafe { env::set_var("AR_x86_64_unknown_none", "llvm-ar") };
@@ -197,7 +201,10 @@ impl From<&std::ffi::OsStr> for Tool {
197201
}
198202

199203
fn find_next(root_dir: &Path, tool_name: &str) -> PathBuf {
200-
let path = env::var_os("PATH").expect("$PATH should exist");
204+
if let Some(path) = env::var_os(format!("HYPERLIGHT_GUEST_{tool_name}")) {
205+
return path.into();
206+
}
207+
let path = env::var_os("PATH)").expect("$PATH should exist");
201208
let paths: Vec<_> = env::split_paths(&path).collect();
202209
for path in &paths {
203210
let abs_path = fs::canonicalize(path);

0 commit comments

Comments
 (0)