diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..d5db07a --- /dev/null +++ b/.cargo/config.toml @@ -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" diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..a5dbbcb --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake . diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2d5df85 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +.direnv diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..0170448 --- /dev/null +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..35853d1 --- /dev/null +++ b/Cargo.toml @@ -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" diff --git a/flake.lock b/flake.lock index 53ac97b..6dd8246 100644 --- a/flake.lock +++ b/flake.lock @@ -2,11 +2,11 @@ "nodes": { "crane": { "locked": { - "lastModified": 1736898272, - "narHash": "sha256-D10wlrU/HCpSRcb3a7yk+bU3ggpMD1kGbseKtO+7teo=", + "lastModified": 1737250794, + "narHash": "sha256-bdIPhvsAKyYQzqAIeay4kOxTHGwLGkhM+IlBIsmMYFI=", "owner": "ipetkov", "repo": "crane", - "rev": "6a589f034202a7c6e10bce6c5d1d392d7bc0f340", + "rev": "c5b7075f4a6d523fe8204618aa9754e56478c0e0", "type": "github" }, "original": { @@ -15,27 +15,6 @@ "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": { "inputs": { "systems": "systems" @@ -54,7 +33,19 @@ "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, "locked": { "lastModified": 1736681776, @@ -73,11 +64,11 @@ "libpcap-src": { "flake": false, "locked": { - "lastModified": 1736964815, - "narHash": "sha256-qXgILw6Br5kanQzTmPC5P0JBZFyO7jSdKKyl4UdtD4Q=", + "lastModified": 1737231768, + "narHash": "sha256-cZpHFjy0cqLeP4TJR80aAMi0V4qzPcEmwU8ouhxZ0sM=", "ref": "refs/heads/master", - "rev": "515022131fa902b2641bcf3bf40f323b0dd726f7", - "revCount": 6396, + "rev": "1fd4281671fb78533bfb3cfac31db78a4015cdcd", + "revCount": 6400, "type": "git", "url": "https://github.com/the-tcpdump-group/libpcap" }, @@ -88,11 +79,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1736867362, - "narHash": "sha256-i/UJ5I7HoqmFMwZEH6vAvBxOrjjOJNU739lnZnhUln8=", + "lastModified": 1737165118, + "narHash": "sha256-s40Kk/OulP3J/1JvC3VT16U4r/Xw6Qdi7SRw3LYkPWs=", "owner": "nixos", "repo": "nixpkgs", - "rev": "9c6b49aeac36e2ed73a8c472f1546f6d9cf1addc", + "rev": "6a3ae7a5a12fb8cac2d59d7df7cbd95f9b2f0566", "type": "github" }, "original": { @@ -105,30 +96,14 @@ "root": { "inputs": { "crane": "crane", - "fenix": "fenix", "flake-utils": "flake-utils", - "libnl": "libnl", + "freebsd-libs-packed": "freebsd-libs-packed", + "libnl-src": "libnl-src", "libpcap-src": "libpcap-src", "nixpkgs": "nixpkgs", "rust-overlay": "rust-overlay", - "winpcap": "winpcap" - } - }, - "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" + "winpcap-installer": "winpcap-installer", + "winpcap-libs": "winpcap-libs" } }, "rust-overlay": { @@ -138,11 +113,11 @@ ] }, "locked": { - "lastModified": 1736907983, - "narHash": "sha256-fw55wVwpJW36Md2HZBKuxX3YHGeqsGsspPLtCMVr1Y8=", + "lastModified": 1737253755, + "narHash": "sha256-T1FIl8yuQ0cJx2DBMvWH73O/igJGaumNNyZkea2m3a0=", "owner": "oxalica", "repo": "rust-overlay", - "rev": "eaa365c911441e07e387ff6acc596619fc50b156", + "rev": "9e4b97a04063ff39a37b63e8fb31cc2b4adf9227", "type": "github" }, "original": { @@ -166,7 +141,19 @@ "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, "locked": { "lastModified": 1277475020, diff --git a/flake.nix b/flake.nix index 17c4d83..0513595 100644 --- a/flake.nix +++ b/flake.nix @@ -3,78 +3,101 @@ inputs = { nixpkgs.url = "github:nixos/nixpkgs/nixos-24.11"; + rust-overlay = { url = "github:oxalica/rust-overlay"; inputs.nixpkgs.follows = "nixpkgs"; }; crane.url = "github:ipetkov/crane"; flake-utils.url = "github:numtide/flake-utils"; - fenix = { - url = "github:nix-community/fenix"; - inputs.nixpkgs.follows = "nixpkgs"; - }; libpcap-src = { url = "git+https://github.com/the-tcpdump-group/libpcap"; flake = false; }; - libnl = { + libnl-src = { url = "git+https://github.com/thom311/libnl"; flake = false; }; - winpcap = { + winpcap-libs = { url = "https://www.winpcap.org/install/bin/WpdPack_4_1_2.zip"; flake = false; }; - - #freebsd = { - # url = - # "https://download.freebsd.org/releases/ISO-IMAGES/14.1/FreeBSD-14.1-RELEASE-amd64-dvd1.iso"; - # flake = false; - #}; + winpcap-installer = { + url = "https://www.winpcap.org/install/bin/WinPcap_4_1_3.exe"; + 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 - , libpcap-src, libnl, winpcap }: - flake-utils.lib.eachDefaultSystem (system: + outputs = { self, nixpkgs, flake-utils, crane, rust-overlay, libpcap-src + , libnl-src, winpcap-libs, winpcap-installer, freebsd-libs-packed }: + flake-utils.lib.eachSystem [ "x86_64-linux" ] (system: let pkgs = import nixpkgs { inherit system; - overlays = [ (import rust-overlay) fenix.overlays.default ]; - config.allowUnfree = true; + overlays = [ (import rust-overlay) ]; }; - linuxBuildTools = with pkgs; [ - autoconf - automake - bison - cmake - flex - libtool - libpcap - musl - pkg-config - ]; + system-libs = import ./system-libs.nix { + inherit pkgs libpcap-src winpcap-installer freebsd-libs-packed; + }; + inherit (system-libs) + bsd-toolchain winpcap-drivers freebsd-libs libnl libpcap-linux + libpcap-freebsd; - windowsBuildTools = linuxBuildTools ++ (with pkgs; [ + buildTools = with pkgs; [ + mold + lld + zig + clang + glibc + + pkgsCross.x86_64-freebsd.buildPackages.clang pkgsCross.mingwW64.stdenv.cc pkgsCross.mingwW64.windows.pthreads - ]); + ]; - toolchain = - pkgs.pkgsMusl.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml; + devShellTools = with pkgs; [ rust-analyzer ]; - 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 { - devShells.default = craneLib.devShell { - name = "sparse"; + devShells = { + default = craneLib.devShell { + name = "sparse-default"; - buildInputs = windowsBuildTools - ++ (with pkgs; [ rust-analyzer packer ]); + packages = buildTools ++ devShellTools; - SPARSE_BUILD_WINPCAP = "${winpcap}/Lib"; - SPARSE_BUILD_LIBPCAP = "${libpcap-src}"; - SPARSE_BUILD_LIBNL = "${libnl}"; + SPARSE_BUILD_WINPCAP_LIBS = "${winpcap-libs}/Lib"; + SPARSE_BUILD_WINPCAP_DRIVERS = "${winpcap-drivers}/npf.sys"; + SPARSE_BUILD_LIBPCAP = libpcap-src; + SPARSE_BUILD_LIBNL = libnl-src; + + FREEBSD_LIBS = freebsd-libs; + + RUSTFLAGS = "-Ctarget-feature=+crt-static"; + }; }; }); } diff --git a/sparse-beacon/Cargo.toml b/sparse-beacon/Cargo.toml new file mode 100644 index 0000000..82cefbb --- /dev/null +++ b/sparse-beacon/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "sparse-beacon" +version = "0.7.0" +edition = "2021" diff --git a/sparse-beacon/build.rs b/sparse-beacon/build.rs new file mode 100644 index 0000000..cc35c3d --- /dev/null +++ b/sparse-beacon/build.rs @@ -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"); + } +} diff --git a/sparse-beacon/src/main.rs b/sparse-beacon/src/main.rs new file mode 100644 index 0000000..ac6bb53 --- /dev/null +++ b/sparse-beacon/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("hi there"); +} diff --git a/system-libs.nix b/system-libs.nix new file mode 100644 index 0000000..1ebab9a --- /dev/null +++ b/system-libs.nix @@ -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 + ''; + }; +} diff --git a/toolchain.toml b/toolchain.toml deleted file mode 100644 index 090ed9d..0000000 --- a/toolchain.toml +++ /dev/null @@ -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"