{ pkgs, buildTools, buildEnvironment, craneLib, advisory-db, winpcap-libs , winpcap-drivers, freebsd-libs, libnl, libpcap-linux-musl, libpcap-linux-gnu , libpcap-freebsd }: let patch-elf = header: drv: path: let hdr = toString header; in pkgs.stdenv.mkDerivation { name = "elf-patch-${hdr}"; buildInputs = with pkgs; [ vim ]; src = drv; buildPhase = '' mkdir -p $out/$(dirname ${path}) cp ${drv}/${path} $out/${path} echo '000007: ${hdr}' xxd -r - $out/${path} ''; }; patch-freebsd-elf = patch-elf 9; freebsd-zig-libc = pkgs.writeText "freebsd-libc.txt" '' include_dir=${freebsd-libs}/usr/include sys_include_dir=${freebsd-libs}/usr/include crt_dir=${freebsd-libs}/usr/lib msvc_lib_dir= kernel32_lib_dir= gcc_dir= ''; src = craneLib.cleanCargoSource ./.; commonArgs = buildEnvironment // { inherit src; strictDeps = true; nativeBuildInputs = buildTools; buildInputs = buildTools; }; fileSetForBeaconCrate = pkgs.lib.fileset.toSource { root = ./.; fileset = pkgs.lib.fileset.unions [ ./.cargo/config.toml ./Cargo.toml ./Cargo.lock ./build_common.rs (craneLib.fileset.commonCargoSources ./sparse-actions) (craneLib.fileset.commonCargoSources ./pcap-sys) (craneLib.fileset.commonCargoSources ./nl-sys) ./nl-sys/src/bridge.c (craneLib.fileset.commonCargoSources ./packets) (craneLib.fileset.commonCargoSources ./sparse-beacon) (craneLib.fileset.commonCargoSources ./sparse-unix-beacon) (craneLib.fileset.commonCargoSources ./sparse-windows-beacon) ]; }; fileSetForInstallerCrate = pkgs.lib.fileset.toSource { root = ./.; fileset = pkgs.lib.fileset.unions [ ./.cargo/config.toml ./Cargo.toml ./Cargo.lock ./build_common.rs (craneLib.fileset.commonCargoSources ./sparse-actions) (craneLib.fileset.commonCargoSources ./pcap-sys) (craneLib.fileset.commonCargoSources ./nl-sys) ./nl-sys/src/bridge.c (craneLib.fileset.commonCargoSources ./packets) (craneLib.fileset.commonCargoSources ./sparse-installer) ]; }; fileSetForWebCrate = pkgs.lib.fileset.toSource { root = ./.; fileset = pkgs.lib.fileset.unions [ ./.cargo/config.toml ./Cargo.toml ./Cargo.lock ./build_common.rs (craneLib.fileset.commonCargoSources ./pcap-sys) (craneLib.fileset.commonCargoSources ./nl-sys) ./nl-sys/src/bridge.c (craneLib.fileset.commonCargoSources ./packets) (craneLib.fileset.commonCargoSources ./sparse-actions) (craneLib.fileset.commonCargoSources ./sparse-server) ./sparse-server/style ./sparse-server/public ]; }; freebsdArgs = commonArgs // { # Sigh... # For some reason, crane and cargo don't run the build script for FreeBSD # It runs it with the dev shell for all 3 targets, and runs it in the # nix packages for Linux and Windows... just not FreeBSD CARGO_BUILD_RUSTFLAGS = "-Ctarget-feature=+crt-static -Clink-args=--target=x86_64-unknown-freebsd -Clink-args=--sysroot=${freebsd-libs} -Clink-args=-L${freebsd-libs}/lib -Clink-args=-L${freebsd-libs}/usr/lib -Clink-args=-L${libpcap-freebsd}/lib"; doCheck = false; }; windowsArgs = commonArgs // { src = fileSetForBeaconCrate; doCheck = false; }; linuxCargoArtifacts = craneLib.buildDepsOnly (commonArgs // { name = "sparse-deps-linux"; cargoExtraArgs = "--target=x86_64-unknown-linux-musl --locked"; }); freebsdCargoArtifacts = craneLib.buildDepsOnly (freebsdArgs // { name = "sparse-deps-freebsd"; cargoExtraArgs = "--target=x86_64-unknown-freebsd --locked -p sparse-beacon"; }); windowsCargoArtifacts = craneLib.buildDepsOnly (windowsArgs // { name = "sparse-deps-windows"; cargoExtraArgs = "--target=x86_64-pc-windows-gnu --locked -p sparse-beacon"; }); sparse-beacon-linux = craneLib.buildPackage (commonArgs // { cargoArtifacts = linuxCargoArtifacts; name = "sparse-beacon-linux"; cargoExtraArgs = "-p sparse-unix-beacon"; src = fileSetForBeaconCrate; CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl"; CARGO_BUILD_RUSTFLAGS = "-Ctarget-feature=+crt-static"; }); sparse-beacon-freebsd-sysv = craneLib.buildPackage (freebsdArgs // { src = fileSetForBeaconCrate; cargoArtifacts = freebsdCargoArtifacts; name = "sparse-beacon-freebsd"; cargoExtraArgs = "-p sparse-unix-beacon"; nativeBuildInputs = buildTools; CARGO_BUILD_TARGET = "x86_64-unknown-freebsd"; CARGO_BUILD_RUSTFLAGS = "-Ctarget-feature=+crt-static"; }); sparse-beacon-freebsd = patch-freebsd-elf sparse-beacon-freebsd-sysv "bin/sparse-beacon"; sparse-beacon-windows = craneLib.buildPackage (windowsArgs // { cargoArtifacts = windowsCargoArtifacts; name = "sparse-beacon-windows"; cargoExtraArgs = "-p sparse-windows-beacon"; CARGO_BUILD_TARGET = "x86_64-pc-windows-gnu"; CARGO_BUILD_RUSTFLAGS = "-Ctarget-feature=+crt-static"; }); linux-loader = pkgs.stdenv.mkDerivation { name = "sparse-linux-loader"; buildInputs = with pkgs; [ zig ]; src = ./unix-loader; buildPhase = '' mkdir $out export XDG_CACHE_HOME=$(mktemp -d) zig build \ --summary all \ --prefix $out \ --release=small \ -Dbeacon=${sparse-beacon-linux}/bin/sparse-unix-beacon \ -Dtarget=x86_64-linux-musl \ --verbose ''; }; freebsd-loader = pkgs.stdenv.mkDerivation { name = "sparse-freebsd-loader"; buildInputs = with pkgs; [ zig ]; src = ./unix-loader; buildPhase = '' mkdir $out export XDG_CACHE_HOME=$(mktemp -d) zig build \ --summary all \ --prefix $out \ --release=small \ -Dtarget=x86_64-freebsd \ -Dbeacon=${sparse-beacon-freebsd}/bin/sparse-unix-beacon \ --sysroot ${freebsd-libs} \ --libc ${freebsd-zig-libc} \ --verbose ''; }; sparse-installer-linux = craneLib.buildPackage (commonArgs // { cargoArtifacts = linuxCargoArtifacts; name = "sparse-installer-linux"; cargoExtraArgs = "-p sparse-installer"; src = fileSetForInstallerCrate; CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl"; CARGO_BUILD_RUSTFLAGS = "-Ctarget-feature=+crt-static"; SPARSE_INSTALLER_LINUX = "${linux-loader}/lib/libunix-loader.so"; }); sparse-installer-freebsd-sysv = craneLib.buildPackage (commonArgs // { cargoArtifacts = linuxCargoArtifacts; name = "sparse-installer-linux"; cargoExtraArgs = "-p sparse-installer"; src = fileSetForInstallerCrate; CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl"; CARGO_BUILD_RUSTFLAGS = "-Ctarget-feature=+crt-static"; SPARSE_INSTALLER_FREEBSD = "${freebsd-loader}/lib/libunix-loader.so"; }); sparse-installer-freebsd = patch-freebsd-elf sparse-installer-freebsd-sysv "bin/sparse-installer"; sparse-server = craneLib.buildPackage (commonArgs // { src = fileSetForWebCrate; cargoArtifacts = linuxCargoArtifacts; name = "sparse-server-webclient"; buildPhaseCargoCommand = '' cargo leptos build \ --release \ --project=sparse-server ''; doNotPostBuildInstallCargoBinaries = true; installPhase = '' mkdir -p $out/bin cp target/x86_64-unknown-linux-musl/release/sparse-server $out/bin ''; SPARSE_INSTALLER_LINUX = "${sparse-installer-linux}/bin/sparse-installer"; SPARSE_INSTALLER_FREEBSD = "${sparse-installer-freebsd}/bin/sparse-installer"; SPARSE_BEACON_WINDOWS = "${sparse-beacon-windows}/bin/sparse-beacon.exe"; }); outputs = rec { packages = { inherit sparse-server sparse-beacon-linux sparse-beacon-windows sparse-installer-linux sparse-installer-freebsd linux-loader freebsd-loader sparse-beacon-freebsd; inherit freebsd-zig-libc; default = sparse-server; }; checks = outputs.packages // { rs-fmt = craneLib.cargoFmt { inherit src; }; rs-audit = craneLib.cargoAudit { inherit src advisory-db; }; rs-toml-fmt = craneLib.taploFmt { src = pkgs.lib.sourceFilesBySuffices src [ ".toml" ]; }; rs-deny = craneLib.cargoDeny { inherit src; }; rs-clippy = craneLib.cargoClippy (commonArgs // { cargoArtifacts = linuxCargoArtifacts; cargoClippyExtraArgs = "--all-targets -- --deny warnings"; }); zig-fmt = pkgs.stdenv.mkDerivation { name = "zig-fmt-check"; src = ./.; checkPhase = '' zig fmt --check ''; }; }; }; in outputs