Skip to content

Commit

Permalink
Add derivation to build lightswitch with nix
Browse files Browse the repository at this point in the history
As well as a container image. This is mostly experimental and won't be
adding CI for it as of now.

Also add a rust-gdb env variable to use the nix provided gdb binary
rather than the system's as this might happen when running under the
root user.

Test Plan
=========

CI to ensure nothing is broken and

```
(nix:nix-shell-env) [javierhonduco@fedora lightswitch]$ nix build .#container
(nix:nix-shell-env) [javierhonduco@fedora lightswitch]$ docker load < result
(nix:nix-shell-env) [javierhonduco@fedora lightswitch]$ docker run -it --privileged --pid host -v /sys/:/sys/ lightswitch:xq7qmc7br6nxn5gd5064z4mns96q4kjf
```
  • Loading branch information
javierhonduco committed May 3, 2024
1 parent 07677fb commit 735d3dd
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
flame.svg
src/bpf/*_skel.rs
.vmtest.log
/result
21 changes: 21 additions & 0 deletions flake.lock

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

60 changes: 41 additions & 19 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@
flake-utils.follows = "flake-utils";
};
};

crane = {
url = "github:ipetkov/crane";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { self, nixpkgs, flake-utils, rust-overlay }:
outputs = { self, nixpkgs, flake-utils, rust-overlay, crane }:
flake-utils.lib.eachSystem [ "x86_64-linux" "aarch64-linux" ]
(system:
let
Expand All @@ -21,32 +26,48 @@
elfutils-without-zstd = pkgs.elfutils.overrideAttrs (attrs: {
configureFlags = attrs.configureFlags ++ [ "--without-zstd" ];
});
buildInputs = with pkgs; [
llvmPackages_16.clang
llvmPackages_16.libcxx
llvmPackages_16.libclang
llvmPackages_16.lld
elfutils-without-zstd
zlib.static
zlib.dev
glibc.static
];
nativeBuildInputs = with pkgs; [
pkg-config
];
craneLib = crane.lib.${system};
lightswitch = craneLib.buildPackage {
src = ./.;
doCheck = false;
buildInputs = buildInputs;
nativeBuildInputs = nativeBuildInputs;
LIBCLANG_PATH = with pkgs; lib.makeLibraryPath [ llvmPackages_16.libclang ];
LD_LIBRARY_PATH = with pkgs; lib.makeLibraryPath [ zlib.static elfutils-without-zstd ];
};
in
with pkgs;
{
formatter = pkgs.nixpkgs-fmt;

devShells.default = mkShell rec {
# https://discourse.nixos.org/t/how-to-add-pkg-config-file-to-a-nix-package/8264/4
nativeBuildInputs = with pkgs; [
pkg-config
];
buildInputs = [
packages = {
default = lightswitch;
container = pkgs.dockerTools.buildImage {
name = "lightswitch";
config = {
Cmd = [ "${lightswitch}/bin/lightswitch" ];
};
};
};
devShells.default = mkShell {
nativeBuildInputs = nativeBuildInputs;
buildInputs = buildInputs ++ [
rust-bin.stable.latest.default
llvmPackages_16.clang
# llvmPackages_16.clang-unwrapped https://github.com/NixOS/nixpkgs/issues/30670
llvmPackages_16.libcxx
llvmPackages_16.libclang
llvmPackages_16.lld
# Debugging
strace
gdb
# Native deps
glibc
glibc.static
elfutils-without-zstd
zlib.static
zlib.dev
openssl
# Other tools
cargo-edit
Expand All @@ -64,6 +85,7 @@

LIBCLANG_PATH = lib.makeLibraryPath [ llvmPackages_16.libclang ];
LD_LIBRARY_PATH = lib.makeLibraryPath [ zlib.static elfutils-without-zstd ];
RUST_GDB = "${gdb}/bin/gdb";
};
}
);
Expand Down

0 comments on commit 735d3dd

Please sign in to comment.