{ pkgs, buildTools, buildEnvironment, craneLib, advisory-db, winpcap-libs , winpcap-drivers, win-openssl, freebsd-libs, libnl, libcap, 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 ./.; fileSetForBeaconCrate = beacon: infector: pkgs.lib.fileset.toSource { root = ./.; fileset = pkgs.lib.fileset.unions [ ./.cargo/config.toml ./Cargo.toml ./Cargo.lock ./build_common.rs ./unix-loader/src/abi.h (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 infector) (craneLib.fileset.commonCargoSources ./sparse-beacon) (craneLib.fileset.commonCargoSources beacon) ]; }; fileSetForInstallerCrate = installer: infector: pkgs.lib.fileset.toSource { root = ./.; fileset = pkgs.lib.fileset.unions [ ./.cargo/config.toml ./Cargo.toml ./Cargo.lock ./build_common.rs ./unix-loader/src/abi.h (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 infector) (craneLib.fileset.commonCargoSources installer) ]; }; fileSetForWebCrate = pkgs.lib.fileset.toSource { root = ./.; fileset = pkgs.lib.fileset.unions [ ./.cargo/config.toml ./Cargo.toml ./Cargo.lock ./build_common.rs ./unix-loader/src/abi.h (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-handler) ./sparse-server/style ./sparse-server/public ./sparse-server/migrations ./sparse-server/.sqlx ./sparse-handler/.sqlx (craneLib.fileset.commonCargoSources ./sparse-server) ]; }; commonArgs = buildEnvironment // { inherit src; #strictDeps = true; nativeBuildInputs = buildTools.linux; buildInputs = buildTools.all; }; linuxArgs = commonArgs // { nativeBuildInputs = buildTools.linux ++ (with pkgs; [ musl ]); RUSTFLAGS = "-Ctarget-feature=+crt-static"; }; webCrateBuildArgs = commonArgs // { nativeBuildInputs = buildTools.linux ++ (with pkgs; [ glibc.static ]); SPARSE_INSTALLER_LINUX = "${sparse-installer-linux}/bin/sparse-unix-installer"; SPARSE_INSTALLER_FREEBSD = "${sparse-installer-freebsd}/bin/sparse-unix-installer"; SPARSE_INSTALLER_WINDOWS = "${sparse-installer-windows}/bin/sparse-windows-installer.exe"; SPARSE_BEACON_LINUX = "${sparse-beacon-linux}/bin/sparse-unix-beacon"; SPARSE_BEACON_LINUX_LOADER = "${linux-loader}/bin/unix-loader"; SPARSE_BEACON_FREEBSD = "${sparse-beacon-freebsd}/bin/sparse-unix-beacon"; SPARSE_BEACON_FREEBSD_LOADER = "${freebsd-loader}/bin/unix-loader"; SPARSE_BEACON_WINDOWS = "${sparse-beacon-windows}/bin/sparse-windows-beacon.exe"; SPARSE_BEACON_WINDOWS_SVC = "${sparse-beacon-windows-svc}/bin/sparse-windows-beacon.exe"; }; 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; nativeBuildInputs = buildTools.linux ++ buildTools.freebsd; }; windowsArgs = commonArgs // { doCheck = false; nativeBuildInputs = buildTools.linux ++ buildTools.windows; OPENSSL_DIR = win-openssl; }; gnuLinuxCargoArtifacts = craneLib.buildDepsOnly (webCrateBuildArgs // { name = "sparse-deps-gnu-linux"; cargoExtraArgs = "--target=x86_64-unknown-linux-gnu --locked -p sparse-server"; }); linuxCargoArtifacts = craneLib.buildDepsOnly (linuxArgs // { name = "sparse-deps-linux"; cargoExtraArgs = "--target=x86_64-unknown-linux-musl --locked -p sparse-unix-beacon -p sparse-unix-installer"; RUSTFLAGS = "-Ctarget-feature=+crt-static"; }); freebsdCargoArtifacts = craneLib.buildDepsOnly (freebsdArgs // { name = "sparse-deps-freebsd"; cargoExtraArgs = "--target=x86_64-unknown-freebsd --locked -p sparse-unix-beacon -p sparse-unix-installer"; }); windowsCargoArtifacts = craneLib.buildDepsOnly (windowsArgs // { name = "sparse-deps-windows"; cargoExtraArgs = "--target=x86_64-pc-windows-gnu --locked -p sparse-windows-beacon -p sparse-windows-installer"; }); sparse-beacon-linux = craneLib.buildPackage (linuxArgs // { cargoArtifacts = linuxCargoArtifacts; name = "sparse-beacon-linux"; cargoExtraArgs = "-p sparse-unix-beacon"; src = fileSetForBeaconCrate ./sparse-unix-beacon ./sparse-unix-infector; CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl"; CARGO_BUILD_RUSTFLAGS = "-Ctarget-feature=+crt-static"; }); sparse-beacon-freebsd-sysv = craneLib.buildPackage (freebsdArgs // { cargoArtifacts = freebsdCargoArtifacts; name = "sparse-beacon-freebsd"; cargoExtraArgs = "-p sparse-unix-beacon"; src = fileSetForBeaconCrate ./sparse-unix-beacon ./sparse-unix-infector; nativeBuildInputs = buildTools.linux ++ buildTools.freebsd; 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-unix-beacon"; sparse-beacon-windows = craneLib.buildPackage (windowsArgs // { cargoArtifacts = windowsCargoArtifacts; name = "sparse-beacon-windows"; cargoExtraArgs = "-p sparse-windows-beacon"; src = fileSetForBeaconCrate ./sparse-windows-beacon ./sparse-windows-infector; CARGO_BUILD_TARGET = "x86_64-pc-windows-gnu"; # CARGO_BUILD_RUSTFLAGS = "-Ctarget-feature=+crt-static"; }); sparse-beacon-windows-svc = craneLib.buildPackage (windowsArgs // { cargoArtifacts = windowsCargoArtifacts; name = "sparse-beacon-windows"; cargoExtraArgs = "-p sparse-windows-beacon --features=service"; src = fileSetForBeaconCrate ./sparse-windows-beacon ./sparse-windows-infector; 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 \ --color off \ --summary all \ --prefix $out \ --release=small \ -Dfork \ -Dos=linux \ -Dbeacon=${sparse-beacon-linux}/bin/sparse-unix-beacon \ -Dtarget=x86_64-linux-musl \ --verbose ''; }; freebsd-loader-sysv = 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 \ --color off \ --summary all \ --prefix $out \ --release=small \ -Dfork \ -Dos=freebsd \ -Dtarget=x86_64-freebsd \ -Dbeacon=${sparse-beacon-freebsd}/bin/sparse-unix-beacon \ --sysroot ${freebsd-libs} \ --libc ${freebsd-zig-libc} \ --verbose ''; }; freebsd-loader = patch-freebsd-elf freebsd-loader-sysv "bin/unix-loader"; sparse-installer-linux = craneLib.buildPackage (linuxArgs // { cargoArtifacts = linuxCargoArtifacts; name = "sparse-installer-linux"; cargoExtraArgs = "-p sparse-unix-installer"; src = fileSetForInstallerCrate ./sparse-unix-installer ./sparse-unix-infector; CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl"; CARGO_BUILD_RUSTFLAGS = "-Ctarget-feature=+crt-static"; SPARSE_LOADER = "${linux-loader}/lib/libunix-loader-linux.so"; }); sparse-installer-freebsd-sysv = craneLib.buildPackage (freebsdArgs // { cargoArtifacts = linuxCargoArtifacts; name = "sparse-installer-freebsd"; cargoExtraArgs = "-p sparse-unix-installer"; src = fileSetForInstallerCrate ./sparse-unix-installer ./sparse-unix-infector; CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl"; CARGO_BUILD_RUSTFLAGS = "-Ctarget-feature=+crt-static"; SPARSE_LOADER = "${freebsd-loader-sysv}/lib/libunix-loader-freebsd.so"; }); sparse-installer-freebsd = patch-freebsd-elf sparse-installer-freebsd-sysv "bin/sparse-unix-installer"; sparse-installer-windows = craneLib.buildPackage (windowsArgs // { cargoArtifacts = windowsCargoArtifacts; name = "sparse-installer-windows"; cargoExtraArgs = "-p sparse-windows-installer"; src = fileSetForInstallerCrate ./sparse-windows-installer ./sparse-windows-infector; CARGO_BUILD_TARGET = "x86_64-pc-windows-gnu"; CARGO_BUILD_RUSTFLAGS = "-Ctarget-feature=+crt-static"; SPARSE_LIBRARY = "${sparse-beacon-windows}/lib/sparse_windows_beacon.dll"; }); sparse-server = craneLib.buildPackage (webCrateBuildArgs // { src = fileSetForWebCrate; cargoArtifacts = gnuLinuxCargoArtifacts; name = "sparse-server"; pname = "sparse-server"; buildPhaseCargoCommand = '' runHook preBuild cargo leptos build \ -vv \ --release \ --project=sparse-server runHook postBuild ''; doNotPostBuildInstallCargoBinaries = true; doInstallCargoArtifacts = false; installPhaseCommand = '' mkdir -p $out/bin cp target/x86_64-unknown-linux-gnu/release/sparse-server $out/bin ''; doCheck = false; }); sparse-server-docker = pkgs.dockerTools.buildImage { name = "sparse-server"; tag = "latest"; config = { Cmd = [ "${sparse-server}/bin/sparse-server" "serve" "--file-store" "/sparse-server/files" "--management-address" "0.0.0.0:3000" "-i" ]; Expose = { "3000" = ""; }; Env = [ "DATABASE_URL=sqlite:///sparse-server/db.sqlite" ]; }; }; outputs = rec { packages = { inherit sparse-server sparse-server-docker sparse-beacon-linux sparse-beacon-freebsd sparse-beacon-windows sparse-beacon-windows-svc linux-loader freebsd-loader-sysv sparse-installer-linux sparse-installer-freebsd sparse-installer-windows; 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