{ pkgs, lib, fenix, selfPkgs, ... }: let version = "0.15.0"; mkServicepoint = { rustPlatform, pkgs, }: rustPlatform.buildRustPackage (finalAttrs: { inherit version; pname = "servicepoint-binding-c"; src = lib.filter { root = ./.; include = [ ./Cargo.lock ./Cargo.toml ./src ./build.rs ./LICENSE ./cbindgen.toml ]; }; cargoLock.lockFile = ./Cargo.lock; useFetchCargoVendor = true; meta = { description = "C bindings for the servicepoint crate."; homepage = "https://git.berlin.ccc.de/servicepoint/servicepoint-binding-c"; license = lib.licenses.gpl3Plus; pkgConfigModules = [ "servicepoint" ]; }; nativeBuildInputs = [ pkgs.pkg-config ]; buildInputs = [ pkgs.xz ]; preBuild = '' mkdir -p include export SERVICEPOINT_HEADER_OUT=$(realpath "include") echo "Rust version: $(rustc --version)" echo "preBuild hook: set SERVICEPOINT_HEADER_OUT to $SERVICEPOINT_HEADER_OUT" ''; postInstall = '' cp -r include $out mkdir -p $out/lib/pkgconfig sed "s:\$out:$out:g" ${./servicepoint.pc.in} | sed "s:\$version:$version:g" > $out/lib/pkgconfig/servicepoint.pc ''; }); mkExample = { name, servicepointBinding, pkgs, }: pkgs.gccStdenv.mkDerivation { inherit version; pname = "servicepoint-c-example-${name}"; nativeBuildInputs = [ pkgs.pkg-config ]; buildInputs = [ servicepointBinding pkgs.xz ]; src = ./example/src; buildPhase = '' set -e set -x mkdir -p $out/bin $CC ${name}.c $CFLAGS $EXTRA_CFLAGS $(pkg-config --libs --cflags servicepoint) $(pkg-config --libs --cflags liblzma) -o $out/bin/${name} ''; }; rustPlatform-stable = pkgs.rustPlatform; rustPlatform-unstable = pkgs.makeRustPlatform { cargo = fenix.minimal.cargo; rustc = fenix.minimal.rustc; }; rustPlatform-musl-stable = pkgs.pkgsMusl.rustPlatform; rustPlatform-musl-unstable = pkgs.pkgsMusl.makeRustPlatform { cargo = fenix.minimal.cargo; rustc = fenix.minimal.rustc; }; examples = [ "announce" "brightness_tester" "header_logger" "moving_line" "random_stuff" "wiping_clear" ]; size-cflags = [ "-Oz" "-fwrapv" "-fomit-frame-pointer" "-fno-stack-protector" "-fno-unroll-loops" "-fno-unwind-tables" "-fno-asynchronous-unwind-tables" "-fmerge-all-constants" "-fvisibility=hidden" "-Bsymbolic" "-fno-ident" "-fno-exceptions" "-ffunction-sections" "-fdata-sections" "-Wl,-z,norelro" "-Wl,--hash-style=gnu" "-Wl,--gc-sections" "-Wl,--exclude-libs,ALL" ]; servicepoint-stable-size-args = { buildType = "size_optimized"; buildNoDefaultFeatures = true; }; servicepoint-unstable-size-args = { # TODO: do these override the nix flags? CARGOFLAGS = ''-Zbuild-std="core,std,alloc,proc_macro,panic_abort" -Zbuild-std-features="panic_immediate_abort"''; # TODO: those override the nix flags # NIX_CFLAGS_COMPILE = builtins.toString ["-Oz" "-fwrapv" "-fomit-frame-pointer" "-fno-stack-protector" "-fno-unroll-loops" "-fno-unwind-tables" "-fno-asynchronous-unwind-tables" "-fmerge-all-constants" "-fvisibility=hidden" "-Bsymbolic" "-fno-ident" "-fno-exceptions" "-ffunction-sections" "-fdata-sections"]; # NIX_CFLAGS_LINK = builtins.toString ["Wl,-z,norelro" "-Wl,--hash-style=gnu" "-Wl,--gc-sections" "-Wl,--exclude-libs,ALL"]; }; mkAllExamples = suffix: pkgs.symlinkJoin { name = "servicepoint-all-examples"; paths = builtins.map (e: selfPkgs."${e}${suffix}") examples; }; in rec { servicepoint-binding-c-stable-release = mkServicepoint { inherit pkgs; rustPlatform = rustPlatform-stable; }; servicepoint-binding-c-nightly-release = mkServicepoint { inherit pkgs; rustPlatform = rustPlatform-unstable; }; servicepoint-binding-c-musl-stable-release = mkServicepoint { pkgs = pkgs.pkgsMusl; rustPlatform = rustPlatform-musl-stable; }; servicepoint-binding-c-musl-nightly-release = mkServicepoint { pkgs = pkgs.pkgsMusl; rustPlatform = rustPlatform-musl-unstable; }; servicepoint-binding-c-stable-size = servicepoint-binding-c-stable-release // servicepoint-stable-size-args; servicepoint-binding-c-nightly-size = servicepoint-binding-c-nightly-release // servicepoint-stable-size-args // servicepoint-unstable-size-args; servicepoint-binding-c-musl-stable-size = servicepoint-binding-c-musl-stable-release // servicepoint-stable-size-args; servicepoint-binding-c-musl-nightly-size = servicepoint-binding-c-musl-nightly-release // servicepoint-stable-size-args // servicepoint-unstable-size-args; # default variants servicepoint-binding-c = servicepoint-binding-c-stable-release; servicepoint-binding-c-musl = servicepoint-binding-c-musl-stable-release; all-examples = mkAllExamples ""; all-examples-size = mkAllExamples "-size"; # TODO: musl targets do not work on darwin all-examples-musl = mkAllExamples "-musl"; all-examples-musl-static = mkAllExamples "-musl-static"; all-examples-musl-static-size = mkAllExamples "-musl-static-size"; } # construct one package per example // (lib.genAttrs examples ( name: mkExample { inherit name pkgs; servicepointBinding = selfPkgs.servicepoint-binding-c; } )) # construct another pakage per example, but optimized for size with unstable rust // (lib.mapAttrs' ( name: value: lib.nameValuePair ("${name}-size") ( value // { EXTRA_CFLAGS = builtins.toString size-cflags; } ) ) ( lib.genAttrs examples ( name: mkExample { inherit name pkgs; servicepointBinding = selfPkgs.servicepoint-binding-c-nightly-size; } ) ) ) # construct another pakage per example, but with musl // (lib.mapAttrs' (name: value: lib.nameValuePair ("${name}-musl") value) ( lib.genAttrs examples ( name: mkExample { inherit name; pkgs = pkgs.pkgsMusl; servicepointBinding = selfPkgs.servicepoint-binding-c-musl; } ) )) # construct another pakage per example, but statically linked with musl // (lib.mapAttrs' (name: value: lib.nameValuePair ("${name}-musl-static") value) ( lib.genAttrs examples ( name: mkExample { inherit name; pkgs = pkgs.pkgsMusl; servicepointBinding = selfPkgs.servicepoint-binding-c-musl; } // { EXTRA_CFLAGS = "-static"; } ) )) # construct another pakage per example, but statically linked with musl and size optimized // (lib.mapAttrs' (name: value: lib.nameValuePair ("${name}-musl-static-size") value) ( lib.genAttrs examples ( name: mkExample { inherit name; pkgs = pkgs.pkgsMusl; servicepointBinding = selfPkgs.servicepoint-binding-c-musl; } // { EXTRA_CFLAGS = "-static" + builtins.toString size-cflags; } ) ))