diff --git a/packages.nix b/packages.nix index 24cf51f..348d98c 100644 --- a/packages.nix +++ b/packages.nix @@ -11,9 +11,12 @@ let { rustPlatform, pkgs, + buildType ? "release", + buildNoDefaultFeatures ? false, + cargoBuildFlags ? [ ], }: rustPlatform.buildRustPackage (finalAttrs: { - inherit version; + inherit version buildType cargoBuildFlags; pname = "servicepoint-binding-c"; src = lib.filter { @@ -57,9 +60,14 @@ let name, servicepointBinding, pkgs, + EXTRA_CFLAGS ? "", + static ? false, }: + let + staticPkgConfigParam = if static then "--static" else ""; + in pkgs.gccStdenv.mkDerivation { - inherit version; + inherit version EXTRA_CFLAGS; pname = "servicepoint-c-example-${name}"; nativeBuildInputs = [ pkgs.pkg-config ]; buildInputs = [ @@ -71,19 +79,12 @@ let 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} + $CC ${name}.c ${if static then "-static" else ""} $CFLAGS $EXTRA_CFLAGS \ + $(pkg-config --libs --cflags ${staticPkgConfigParam} servicepoint) \ + $(pkg-config --libs --cflags ${staticPkgConfigParam} 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" @@ -92,6 +93,12 @@ let "random_stuff" "wiping_clear" ]; + mkAllExamples = + suffix: + pkgs.symlinkJoin { + name = "servicepoint-all-examples"; + paths = builtins.map (e: selfPkgs."${e}${suffix}") examples; + }; size-cflags = [ "-Oz" "-fwrapv" @@ -112,54 +119,57 @@ let "-Wl,--gc-sections" "-Wl,--exclude-libs,ALL" ]; - servicepoint-stable-size-args = { + 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"''; + unstable-size-args = { + cargoBuildFlags = ''-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 { + rustPlatform-stable = pkgs.rustPlatform; + rustPlatform-nightly = pkgs.makeRustPlatform { + inherit (fenix.minimal) cargo rustc; + }; + rustPlatform-musl-stable = pkgs.pkgsMusl.rustPlatform; + rustPlatform-musl-nightly = pkgs.pkgsMusl.makeRustPlatform { + inherit (fenix.minimal) cargo rustc; + }; + stable-release-args = { inherit pkgs; rustPlatform = rustPlatform-stable; }; - servicepoint-binding-c-nightly-release = mkServicepoint { + nightly-release-args = { inherit pkgs; - rustPlatform = rustPlatform-unstable; + rustPlatform = rustPlatform-nightly; }; - servicepoint-binding-c-musl-stable-release = mkServicepoint { + musl-stable-release-args = { pkgs = pkgs.pkgsMusl; rustPlatform = rustPlatform-musl-stable; }; - servicepoint-binding-c-musl-nightly-release = mkServicepoint { + musl-nightly-release-args = { pkgs = pkgs.pkgsMusl; - rustPlatform = rustPlatform-musl-unstable; + rustPlatform = rustPlatform-musl-nightly; }; +in +rec { + servicepoint-binding-c-stable-release = mkServicepoint stable-release-args; + servicepoint-binding-c-nightly-release = mkServicepoint nightly-release-args; + servicepoint-binding-c-musl-stable-release = mkServicepoint musl-stable-release-args; + servicepoint-binding-c-musl-nightly-release = mkServicepoint musl-nightly-release-args; - 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; + servicepoint-binding-c-stable-size = mkServicepoint (stable-release-args // stable-size-args); + servicepoint-binding-c-nightly-size = mkServicepoint ( + nightly-release-args // stable-size-args // unstable-size-args + ); + servicepoint-binding-c-musl-stable-size = mkServicepoint ( + musl-stable-release-args // stable-size-args + ); + servicepoint-binding-c-musl-nightly-size = mkServicepoint ( + musl-nightly-release-args // stable-size-args // unstable-size-args + ); # default variants servicepoint-binding-c = servicepoint-binding-c-stable-release; @@ -181,26 +191,16 @@ rec { } )) # 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.mapAttrs' (name: value: lib.nameValuePair ("${name}-size") value) ( + lib.genAttrs examples ( + name: + mkExample { + inherit name pkgs; + servicepointBinding = selfPkgs.servicepoint-binding-c-nightly-size; + 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 ( @@ -220,9 +220,7 @@ rec { inherit name; pkgs = pkgs.pkgsMusl; servicepointBinding = selfPkgs.servicepoint-binding-c-musl; - } - // { - EXTRA_CFLAGS = "-static"; + static = true; } ) )) @@ -234,9 +232,8 @@ rec { inherit name; pkgs = pkgs.pkgsMusl; servicepointBinding = selfPkgs.servicepoint-binding-c-musl; - } - // { - EXTRA_CFLAGS = "-static" + builtins.toString size-cflags; + static = true; + EXTRA_CFLAGS = builtins.toString size-cflags; } ) ))