feat: initial commit with build system

This commit is contained in:
Andrew Rioux 2025-01-19 20:46:46 -05:00
parent 8c4c6674a1
commit 6b33f1a5ba
Signed by: andrew.rioux
GPG Key ID: 9B8BAC47C17ABB94
12 changed files with 277 additions and 101 deletions

8
.cargo/config.toml Normal file
View File

@ -0,0 +1,8 @@
[target.x86_64-unknown-linux-gnu]
linker = "clang"
[target.x86_64-unknown-linux-musl]
linker = "clang"
[target.x86_64-unknown-freebsd]
linker = "x86_64-unknown-freebsd-clang"

1
.envrc Normal file
View File

@ -0,0 +1 @@
use flake .

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
.direnv

7
Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "sparse-beacon"
version = "0.7.0"

13
Cargo.toml Normal file
View File

@ -0,0 +1,13 @@
[workspace]
members = [
"sparse-beacon"
]
resolver = "2"
package.version = "2.0.0"
[profile.release]
strip = true
opt-level = "z"
lto = true
codegen-units = 1
panic = "abort"

99
flake.lock generated
View File

@ -2,11 +2,11 @@
"nodes": { "nodes": {
"crane": { "crane": {
"locked": { "locked": {
"lastModified": 1736898272, "lastModified": 1737250794,
"narHash": "sha256-D10wlrU/HCpSRcb3a7yk+bU3ggpMD1kGbseKtO+7teo=", "narHash": "sha256-bdIPhvsAKyYQzqAIeay4kOxTHGwLGkhM+IlBIsmMYFI=",
"owner": "ipetkov", "owner": "ipetkov",
"repo": "crane", "repo": "crane",
"rev": "6a589f034202a7c6e10bce6c5d1d392d7bc0f340", "rev": "c5b7075f4a6d523fe8204618aa9754e56478c0e0",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -15,27 +15,6 @@
"type": "github" "type": "github"
} }
}, },
"fenix": {
"inputs": {
"nixpkgs": [
"nixpkgs"
],
"rust-analyzer-src": "rust-analyzer-src"
},
"locked": {
"lastModified": 1736836313,
"narHash": "sha256-zdZ7/T6yG0/hzoVOiNpDiR/sW3zR6oSMrfIFJK2BrrE=",
"owner": "nix-community",
"repo": "fenix",
"rev": "056c9393c821a4df356df6ce7f14c722dc8717ec",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "fenix",
"type": "github"
}
},
"flake-utils": { "flake-utils": {
"inputs": { "inputs": {
"systems": "systems" "systems": "systems"
@ -54,7 +33,19 @@
"type": "github" "type": "github"
} }
}, },
"libnl": { "freebsd-libs-packed": {
"flake": false,
"locked": {
"narHash": "sha256-l9YQPyra0F4guqiI4rW5XsZxGP1Usy//nglUVFlqY1c=",
"type": "file",
"url": "https://download.freebsd.org/releases/amd64/14.1-RELEASE/base.txz"
},
"original": {
"type": "file",
"url": "https://download.freebsd.org/releases/amd64/14.1-RELEASE/base.txz"
}
},
"libnl-src": {
"flake": false, "flake": false,
"locked": { "locked": {
"lastModified": 1736681776, "lastModified": 1736681776,
@ -73,11 +64,11 @@
"libpcap-src": { "libpcap-src": {
"flake": false, "flake": false,
"locked": { "locked": {
"lastModified": 1736964815, "lastModified": 1737231768,
"narHash": "sha256-qXgILw6Br5kanQzTmPC5P0JBZFyO7jSdKKyl4UdtD4Q=", "narHash": "sha256-cZpHFjy0cqLeP4TJR80aAMi0V4qzPcEmwU8ouhxZ0sM=",
"ref": "refs/heads/master", "ref": "refs/heads/master",
"rev": "515022131fa902b2641bcf3bf40f323b0dd726f7", "rev": "1fd4281671fb78533bfb3cfac31db78a4015cdcd",
"revCount": 6396, "revCount": 6400,
"type": "git", "type": "git",
"url": "https://github.com/the-tcpdump-group/libpcap" "url": "https://github.com/the-tcpdump-group/libpcap"
}, },
@ -88,11 +79,11 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1736867362, "lastModified": 1737165118,
"narHash": "sha256-i/UJ5I7HoqmFMwZEH6vAvBxOrjjOJNU739lnZnhUln8=", "narHash": "sha256-s40Kk/OulP3J/1JvC3VT16U4r/Xw6Qdi7SRw3LYkPWs=",
"owner": "nixos", "owner": "nixos",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "9c6b49aeac36e2ed73a8c472f1546f6d9cf1addc", "rev": "6a3ae7a5a12fb8cac2d59d7df7cbd95f9b2f0566",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -105,30 +96,14 @@
"root": { "root": {
"inputs": { "inputs": {
"crane": "crane", "crane": "crane",
"fenix": "fenix",
"flake-utils": "flake-utils", "flake-utils": "flake-utils",
"libnl": "libnl", "freebsd-libs-packed": "freebsd-libs-packed",
"libnl-src": "libnl-src",
"libpcap-src": "libpcap-src", "libpcap-src": "libpcap-src",
"nixpkgs": "nixpkgs", "nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay", "rust-overlay": "rust-overlay",
"winpcap": "winpcap" "winpcap-installer": "winpcap-installer",
} "winpcap-libs": "winpcap-libs"
},
"rust-analyzer-src": {
"flake": false,
"locked": {
"lastModified": 1736690231,
"narHash": "sha256-g9gyxX+F6CrkT5gRIMKPnCPom0o9ZDzYnzzeNF86D6Q=",
"owner": "rust-lang",
"repo": "rust-analyzer",
"rev": "8364ef299790cb6ec22b9e09e873c97dbe9f2cb5",
"type": "github"
},
"original": {
"owner": "rust-lang",
"ref": "nightly",
"repo": "rust-analyzer",
"type": "github"
} }
}, },
"rust-overlay": { "rust-overlay": {
@ -138,11 +113,11 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1736907983, "lastModified": 1737253755,
"narHash": "sha256-fw55wVwpJW36Md2HZBKuxX3YHGeqsGsspPLtCMVr1Y8=", "narHash": "sha256-T1FIl8yuQ0cJx2DBMvWH73O/igJGaumNNyZkea2m3a0=",
"owner": "oxalica", "owner": "oxalica",
"repo": "rust-overlay", "repo": "rust-overlay",
"rev": "eaa365c911441e07e387ff6acc596619fc50b156", "rev": "9e4b97a04063ff39a37b63e8fb31cc2b4adf9227",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -166,7 +141,19 @@
"type": "github" "type": "github"
} }
}, },
"winpcap": { "winpcap-installer": {
"flake": false,
"locked": {
"narHash": "sha256-F3nzqZKR79zyV8INL3k1XndwOfWVJHYiBTI14e//ljc=",
"type": "file",
"url": "https://www.winpcap.org/install/bin/WinPcap_4_1_3.exe"
},
"original": {
"type": "file",
"url": "https://www.winpcap.org/install/bin/WinPcap_4_1_3.exe"
}
},
"winpcap-libs": {
"flake": false, "flake": false,
"locked": { "locked": {
"lastModified": 1277475020, "lastModified": 1277475020,

103
flake.nix
View File

@ -3,78 +3,101 @@
inputs = { inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-24.11"; nixpkgs.url = "github:nixos/nixpkgs/nixos-24.11";
rust-overlay = { rust-overlay = {
url = "github:oxalica/rust-overlay"; url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs"; inputs.nixpkgs.follows = "nixpkgs";
}; };
crane.url = "github:ipetkov/crane"; crane.url = "github:ipetkov/crane";
flake-utils.url = "github:numtide/flake-utils"; flake-utils.url = "github:numtide/flake-utils";
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
libpcap-src = { libpcap-src = {
url = "git+https://github.com/the-tcpdump-group/libpcap"; url = "git+https://github.com/the-tcpdump-group/libpcap";
flake = false; flake = false;
}; };
libnl = { libnl-src = {
url = "git+https://github.com/thom311/libnl"; url = "git+https://github.com/thom311/libnl";
flake = false; flake = false;
}; };
winpcap = { winpcap-libs = {
url = "https://www.winpcap.org/install/bin/WpdPack_4_1_2.zip"; url = "https://www.winpcap.org/install/bin/WpdPack_4_1_2.zip";
flake = false; flake = false;
}; };
winpcap-installer = {
#freebsd = { url = "https://www.winpcap.org/install/bin/WinPcap_4_1_3.exe";
# url = flake = false;
# "https://download.freebsd.org/releases/ISO-IMAGES/14.1/FreeBSD-14.1-RELEASE-amd64-dvd1.iso"; };
# flake = false; freebsd-libs-packed = {
#}; url = "https://download.freebsd.org/releases/amd64/14.1-RELEASE/base.txz";
flake = false;
};
# command for later:
# echo '000007: 09' | xxd -r - target/x86_64-unknown-freebsd/debug/sparse-beacon
}; };
outputs = { self, nixpkgs, flake-utils, crane, fenix, rust-overlay outputs = { self, nixpkgs, flake-utils, crane, rust-overlay, libpcap-src
, libpcap-src, libnl, winpcap }: , libnl-src, winpcap-libs, winpcap-installer, freebsd-libs-packed }:
flake-utils.lib.eachDefaultSystem (system: flake-utils.lib.eachSystem [ "x86_64-linux" ] (system:
let let
pkgs = import nixpkgs { pkgs = import nixpkgs {
inherit system; inherit system;
overlays = [ (import rust-overlay) fenix.overlays.default ]; overlays = [ (import rust-overlay) ];
config.allowUnfree = true;
}; };
linuxBuildTools = with pkgs; [ system-libs = import ./system-libs.nix {
autoconf inherit pkgs libpcap-src winpcap-installer freebsd-libs-packed;
automake };
bison inherit (system-libs)
cmake bsd-toolchain winpcap-drivers freebsd-libs libnl libpcap-linux
flex libpcap-freebsd;
libtool
libpcap
musl
pkg-config
];
windowsBuildTools = linuxBuildTools ++ (with pkgs; [ buildTools = with pkgs; [
mold
lld
zig
clang
glibc
pkgsCross.x86_64-freebsd.buildPackages.clang
pkgsCross.mingwW64.stdenv.cc pkgsCross.mingwW64.stdenv.cc
pkgsCross.mingwW64.windows.pthreads pkgsCross.mingwW64.windows.pthreads
]); ];
toolchain = devShellTools = with pkgs; [ rust-analyzer ];
pkgs.pkgsMusl.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
craneLib = (crane.mkLib pkgs).overrideToolchain toolchain; craneLib = (crane.mkLib pkgs).overrideToolchain (p:
p.rust-bin.stable.latest.default.override {
targets = [
"x86_64-unknown-linux-gnu"
"x86_64-unknown-linux-musl"
"x86_64-pc-windows-gnu"
"x86_64-unknown-freebsd"
];
});
patch-elf = header: drv: path:
pkgs.stdenv.mkDerivation {
name = "elf-patch-${header}";
buildInputs = with pkgs; [ vim ];
};
patch-freebsd-elf = patch-elf 9;
in { in {
devShells.default = craneLib.devShell { devShells = {
name = "sparse"; default = craneLib.devShell {
name = "sparse-default";
buildInputs = windowsBuildTools packages = buildTools ++ devShellTools;
++ (with pkgs; [ rust-analyzer packer ]);
SPARSE_BUILD_WINPCAP = "${winpcap}/Lib"; SPARSE_BUILD_WINPCAP_LIBS = "${winpcap-libs}/Lib";
SPARSE_BUILD_LIBPCAP = "${libpcap-src}"; SPARSE_BUILD_WINPCAP_DRIVERS = "${winpcap-drivers}/npf.sys";
SPARSE_BUILD_LIBNL = "${libnl}"; SPARSE_BUILD_LIBPCAP = libpcap-src;
SPARSE_BUILD_LIBNL = libnl-src;
FREEBSD_LIBS = freebsd-libs;
RUSTFLAGS = "-Ctarget-feature=+crt-static";
};
}; };
}); });
} }

4
sparse-beacon/Cargo.toml Normal file
View File

@ -0,0 +1,4 @@
[package]
name = "sparse-beacon"
version = "0.7.0"
edition = "2021"

8
sparse-beacon/build.rs Normal file
View File

@ -0,0 +1,8 @@
fn main() {
if std::env::var("CARGO_CFG_TARGET_OS").unwrap() == "freebsd" {
let freebsd_libs = std::env::var("FREEBSD_LIBS").unwrap();
println!("cargo:rustc-link-arg=--sysroot={freebsd_libs}");
println!("cargo:rustc-link-arg=-L{freebsd_libs}/lib");
println!("cargo:rustc-link-arg=-L{freebsd_libs}/usr/lib");
}
}

View File

@ -0,0 +1,3 @@
fn main() {
println!("hi there");
}

125
system-libs.nix Normal file
View File

@ -0,0 +1,125 @@
{ pkgs, libpcap-src, winpcap-installer, freebsd-libs-packed }:
let
freebsd-libs = pkgs.stdenv.mkDerivation {
name = "freebsd-libs";
buildInputs = with pkgs; [ gnutar ];
unpackPhase = ''
mkdir -p $out
tar xvJpf ${freebsd-libs-packed} -C $out ./usr/lib
tar xvJpf ${freebsd-libs-packed} -C $out ./usr/include
'';
};
bsd-toolchain = with pkgs.pkgsCross.x86_64-freebsd.buildPackages;
pkgs.writeText "bsd-toolchain.cmake" ''
set(CMAKE_SYSTEM_NAME FreeBSD)
set(CMAKE_C_COMPILER ${clang}/bin/x86_64-unknown-freebsd-clang)
set(CMAKE_CXX_COMPILER ${clang}/bin/x86_64-unknown-freebsd-clang++)
set(CMAKE_FIND_ROOT_PATH ${freebsd-libs})
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
'';
in {
inherit freebsd-libs;
winpcap-drivers = pkgs.stdenv.mkDerivation {
name = "winpcap-drivers";
buildInputs = with pkgs; [ p7zip ];
# extract only the 64 bit drivers
unpackPhase = ''
mkdir -p $out
printf 'Y\nN\n' | 7z e "${winpcap-installer}" '$SYSDIR/drivers/npf.sys'
mv npf.sys $out/npf.sys
'';
};
libnl = pkgs.stdenv.mkDerivation {
name = "libnl-static";
buildInputs = with pkgs; [ autoconf automake libtool ];
};
libpcap-linux = pkgs.stdenv.mkDerivation {
name = "libpcap-static-linux";
buildInputs = with pkgs; [ automake bison cmake flex ];
src = libpcap-src;
configurePhase = ''
cmake \
-DCMAKE_BUILD_TYPE=MinSizeRel \
-DBUILD_SHARED_LIBS=OFF \
-DDISABLE_BLUETOOTH=ON \
-DDISABLE_DAG=ON \
-DDISABLE_DBUS=ON \
-DDISABLE_DPDK=ON \
-DDISABLE_NETMAP=ON \
-DDISABLE_RDMA=ON \
-DDISABLE_LINUX_USBMON=ON \
-DDISABLE_SNF=ON \
-DPCAP_TYPE=linux \
.
'';
buildPhase = ''
cmake --build . --target pcap_static
'';
installPhase = ''
mkdir -p $out/lib
cp libpcap.a $out/lib
'';
};
libpcap-freebsd = pkgs.stdenv.mkDerivation {
name = "libpcap-static-freebsd";
nativeBuildInputs = with pkgs.pkgsCross.x86_64-freebsd.buildPackages; [
clang
lld
automake
bison
cmake
flex
];
src = libpcap-src;
configurePhase = ''
cmake \
-DCMAKE_TOOLCHAIN_FILE=${bsd-toolchain} \
-DCMAKE_BUILD_TYPE=MinSizeRel \
-DBUILD_SHARED_LIBS=OFF \
-DDISABLE_BLUETOOTH=ON \
-DDISABLE_DAG=ON \
-DDISABLE_DBUS=ON \
-DDISABLE_DPDK=ON \
-DDISABLE_NETMAP=ON \
-DDISABLE_RDMA=ON \
-DDISABLE_SNF=ON \
-DPCAP_TYPE=bpf \
.
'';
buildPhase = ''
cmake --build . --target pcap_static
'';
installPhase = ''
mkdir -p $out/lib
cp libpcap.a $out/lib
'';
};
}

View File

@ -1,5 +0,0 @@
[toolchain]
channel = "stable"
components = []
targets = [ "x86_64-pc-windows-gnu", "x86_64-unknown-linux-musl", "x86_64-unknown-freebsd", "aarch64-apple-darwin" ]
profile = "minimal"