From 7b33084a3e3f5b9399fc5ab11e8401aadf1a8b18 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Fri, 30 May 2025 00:09:41 +0200 Subject: [PATCH 1/5] more configs work now ``` make LIBC=gnu LINK=dynamic make LIBC=musl LINK=dynamic CC=musl-gcc make LIBC=musl LINK=static CC=musl-gcc ``` --- example/Makefile | 11 +++++------ flake.lock | 8 ++++---- flake.nix | 50 ++++++++++++++++++++++++++---------------------- 3 files changed, 36 insertions(+), 33 deletions(-) diff --git a/example/Makefile b/example/Makefile index e2ac12e..1e615c7 100644 --- a/example/Makefile +++ b/example/Makefile @@ -36,7 +36,6 @@ THIS_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST)))) REPO_ROOT := $(realpath $(THIS_DIR)/..) CARGO_OBJDIR := cargo/$(RUST_TARGET)/$(_profile) -# TODO: check if override is needed _rust_cli_profile := $(if $(filter $(_profile),debug),dev,$(_profile)) # TODO: `make LINK=static` fails with linker error "undefined reference to `_dl_find_object'" in libgcc_eh.a @@ -57,7 +56,7 @@ CARGOFLAGS += --manifest-path=$(REPO_ROOT)/Cargo.toml \ CFLAGS += -Wall -Wextra -pedantic -fwhole-program -fPIE -pie _no_debug_cflags := -ffunction-sections -fdata-sections -Wl,--gc-sections size_optimized_CFLAGS += -Oz \ - -fwrapv -fomit-frame-pointer -fno-stack-protector\ + -fwrapv -fomit-frame-pointer -fno-stack-protector \ -fno-unroll-loops \ -fno-unwind-tables -fno-asynchronous-unwind-tables \ -fmerge-all-constants \ @@ -85,16 +84,16 @@ endif size_optimized_RUSTFLAGS += -Zlocation-detail=none \ -Zfmt-debug=none \ -C link-arg=-z,norelro \ - -C panic=abort - #-C link-arg=--hash-style=gnu -musl_RUSTFLAGS += --crate-type=staticlib -Ctarget-feature=-crt-static + -C panic=abort \ + -C link-arg=-Wl,--hash-style=gnu +static_RUSTFLAGS += --crate-type=staticlib -Ctarget-feature=+crt-static RUSTFLAGS += $($(_libc)_RUSTFLAGS) $($(_profile)_RUSTFLAGS) $($(_link_type)_RUSTFLAGS) ifneq ($(_profile), debug) RUSTFLAGS += -C link-arg=-s -C link-arg=-Wl,--gc-sections endif -# TODO: wildcard does not work with VPATH +# ADD NEW EXAMPLES HERE _c_src := src/announce.c src/brightness_tester.c src/header_logger.c \ src/moving_line.c src/random_stuff.c src/wiping_clear.c _programs := $(basename $(notdir $(_c_src))) diff --git a/flake.lock b/flake.lock index c5f76d2..9091d9f 100644 --- a/flake.lock +++ b/flake.lock @@ -2,16 +2,16 @@ "nodes": { "nixpkgs": { "locked": { - "lastModified": 1748037224, - "narHash": "sha256-92vihpZr6dwEMV6g98M5kHZIttrWahb9iRPBm1atcPk=", + "lastModified": 1748302896, + "narHash": "sha256-ixMT0a8mM091vSswlTORZj93WQAJsRNmEvqLL+qwTFM=", "owner": "nixos", "repo": "nixpkgs", - "rev": "f09dede81861f3a83f7f06641ead34f02f37597f", + "rev": "7848cd8c982f7740edf76ddb3b43d234cb80fc4d", "type": "github" }, "original": { "owner": "nixos", - "ref": "nixos-24.11", + "ref": "nixos-25.05", "repo": "nixpkgs", "type": "github" } diff --git a/flake.nix b/flake.nix index 82983f7..2adaa88 100644 --- a/flake.nix +++ b/flake.nix @@ -2,7 +2,7 @@ description = "Flake for the servicepoint library."; inputs = { - nixpkgs.url = "github:nixos/nixpkgs/nixos-24.11"; + nixpkgs.url = "github:nixos/nixpkgs/nixos-25.05"; }; outputs = @@ -31,37 +31,41 @@ { devShells = forAllSystems ( { pkgs, system }: + let + toolchain = pkgs.symlinkJoin { + name = "rust-toolchain"; + paths = with pkgs; [ + rustc + cargo + rustPlatform.rustcSrc + rustPlatform.rustLibSrc + rustfmt + clippy + cargo-expand + cargo-tarpaulin + ]; + }; + in { default = pkgs.mkShell rec { - buildInputs = with pkgs; [ + packages = with pkgs; [ + toolchain + gcc + gdb + gnumake + pkg-config xe xz libgcc libunwind + pkgsStatic.gcc + pkgsStatic.libgcc pkgsStatic.musl ]; - nativeBuildInputs = with pkgs; [ - (pkgs.symlinkJoin { - name = "rust-toolchain"; - paths = with pkgs; [ - rustc - cargo - rustPlatform.rustcSrc - rustfmt - clippy - cargo-expand - cargo-tarpaulin - ]; - }) - gcc - gdb - pkgsStatic.gcc - gnumake - pkg-config - ]; - - RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}"; + RUST_SRC_PATH = "${pkgs.rustPlatform.rustLibSrc}"; + RUST_BACKTRACE = 1; + RUST_LOG = "all"; }; } ); From e824baa4bad8a85e1d37b857cc0237e45e1ea0f7 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Fri, 30 May 2025 00:09:41 +0200 Subject: [PATCH 2/5] more configs work now ``` make LIBC=gnu LINK=dynamic make LIBC=musl LINK=dynamic CC=musl-gcc make LIBC=musl LINK=static CC=musl-gcc ``` --- example/Makefile | 18 +++++++++-------- flake.lock | 8 ++++---- flake.nix | 50 ++++++++++++++++++++++++++---------------------- 3 files changed, 41 insertions(+), 35 deletions(-) diff --git a/example/Makefile b/example/Makefile index e2ac12e..1617552 100644 --- a/example/Makefile +++ b/example/Makefile @@ -36,11 +36,8 @@ THIS_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST)))) REPO_ROOT := $(realpath $(THIS_DIR)/..) CARGO_OBJDIR := cargo/$(RUST_TARGET)/$(_profile) -# TODO: check if override is needed _rust_cli_profile := $(if $(filter $(_profile),debug),dev,$(_profile)) -# TODO: `make LINK=static` fails with linker error "undefined reference to `_dl_find_object'" in libgcc_eh.a - STRIPFLAGS += -s --strip-unneeded -R .comment -R .gnu.version -R .note -R .note.gnu.build-id -R .note.ABI-tag STATIC_LINK_LIBS += -lservicepoint_binding_c @@ -57,7 +54,7 @@ CARGOFLAGS += --manifest-path=$(REPO_ROOT)/Cargo.toml \ CFLAGS += -Wall -Wextra -pedantic -fwhole-program -fPIE -pie _no_debug_cflags := -ffunction-sections -fdata-sections -Wl,--gc-sections size_optimized_CFLAGS += -Oz \ - -fwrapv -fomit-frame-pointer -fno-stack-protector\ + -fwrapv -fomit-frame-pointer -fno-stack-protector \ -fno-unroll-loops \ -fno-unwind-tables -fno-asynchronous-unwind-tables \ -fmerge-all-constants \ @@ -81,20 +78,25 @@ CFLAGS += $($(_libc)_CFLAGS) $($(_profile)_CFLAGS) $($(_link_type)_CFLAGS) $(_se ifeq ($(LTO), 1) CFLAGS += -flto endif +ifeq ($(_libc),gnu) + ifeq ($(_link_type),static) + CFLAGS += $(error "statically linking glibc is known to be broken") + endif +endif size_optimized_RUSTFLAGS += -Zlocation-detail=none \ -Zfmt-debug=none \ -C link-arg=-z,norelro \ - -C panic=abort - #-C link-arg=--hash-style=gnu -musl_RUSTFLAGS += --crate-type=staticlib -Ctarget-feature=-crt-static + -C panic=abort \ + -C link-arg=-Wl,--hash-style=gnu +static_RUSTFLAGS += --crate-type=staticlib -Ctarget-feature=+crt-static RUSTFLAGS += $($(_libc)_RUSTFLAGS) $($(_profile)_RUSTFLAGS) $($(_link_type)_RUSTFLAGS) ifneq ($(_profile), debug) RUSTFLAGS += -C link-arg=-s -C link-arg=-Wl,--gc-sections endif -# TODO: wildcard does not work with VPATH +# ADD NEW EXAMPLES HERE _c_src := src/announce.c src/brightness_tester.c src/header_logger.c \ src/moving_line.c src/random_stuff.c src/wiping_clear.c _programs := $(basename $(notdir $(_c_src))) diff --git a/flake.lock b/flake.lock index c5f76d2..9091d9f 100644 --- a/flake.lock +++ b/flake.lock @@ -2,16 +2,16 @@ "nodes": { "nixpkgs": { "locked": { - "lastModified": 1748037224, - "narHash": "sha256-92vihpZr6dwEMV6g98M5kHZIttrWahb9iRPBm1atcPk=", + "lastModified": 1748302896, + "narHash": "sha256-ixMT0a8mM091vSswlTORZj93WQAJsRNmEvqLL+qwTFM=", "owner": "nixos", "repo": "nixpkgs", - "rev": "f09dede81861f3a83f7f06641ead34f02f37597f", + "rev": "7848cd8c982f7740edf76ddb3b43d234cb80fc4d", "type": "github" }, "original": { "owner": "nixos", - "ref": "nixos-24.11", + "ref": "nixos-25.05", "repo": "nixpkgs", "type": "github" } diff --git a/flake.nix b/flake.nix index 82983f7..2adaa88 100644 --- a/flake.nix +++ b/flake.nix @@ -2,7 +2,7 @@ description = "Flake for the servicepoint library."; inputs = { - nixpkgs.url = "github:nixos/nixpkgs/nixos-24.11"; + nixpkgs.url = "github:nixos/nixpkgs/nixos-25.05"; }; outputs = @@ -31,37 +31,41 @@ { devShells = forAllSystems ( { pkgs, system }: + let + toolchain = pkgs.symlinkJoin { + name = "rust-toolchain"; + paths = with pkgs; [ + rustc + cargo + rustPlatform.rustcSrc + rustPlatform.rustLibSrc + rustfmt + clippy + cargo-expand + cargo-tarpaulin + ]; + }; + in { default = pkgs.mkShell rec { - buildInputs = with pkgs; [ + packages = with pkgs; [ + toolchain + gcc + gdb + gnumake + pkg-config xe xz libgcc libunwind + pkgsStatic.gcc + pkgsStatic.libgcc pkgsStatic.musl ]; - nativeBuildInputs = with pkgs; [ - (pkgs.symlinkJoin { - name = "rust-toolchain"; - paths = with pkgs; [ - rustc - cargo - rustPlatform.rustcSrc - rustfmt - clippy - cargo-expand - cargo-tarpaulin - ]; - }) - gcc - gdb - pkgsStatic.gcc - gnumake - pkg-config - ]; - - RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}"; + RUST_SRC_PATH = "${pkgs.rustPlatform.rustLibSrc}"; + RUST_BACKTRACE = 1; + RUST_LOG = "all"; }; } ); From d205ed44b7829f00ffb9efad5b3914912cb0fdaa Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 1 Jun 2025 13:00:22 +0200 Subject: [PATCH 3/5] version 0.15.0 --- Cargo.lock | 2 +- Cargo.toml | 2 +- build.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f7deee5..121e01f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -536,7 +536,7 @@ dependencies = [ [[package]] name = "servicepoint_binding_c" -version = "0.14.1" +version = "0.15.0" dependencies = [ "cbindgen", "env_logger", diff --git a/Cargo.toml b/Cargo.toml index 5e29900..f6bd53b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "servicepoint_binding_c" -version = "0.14.1" +version = "0.15.0" publish = true edition = "2021" license = "GPL-3.0-or-later" diff --git a/build.rs b/build.rs index 615d00f..397341d 100644 --- a/build.rs +++ b/build.rs @@ -35,7 +35,7 @@ fn main() { if let Ok(header_out) = env::var("SERVICEPOINT_HEADER_OUT") { let header_copy = header_out + "/servicepoint.h"; - if fs::exists(&header_copy).ok().unwrap_or(false) { + if fs::exists(&header_copy).unwrap_or(false) { // check if content changed to prevent rebuild of dependents if not let mut bindings_text = Vec::new(); From 7b6b4f7e5b53151281e51a69f030502ba759c5b8 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sun, 1 Jun 2025 16:51:11 +0200 Subject: [PATCH 4/5] split flake.nix, shell per package, generate pkg-config --- devShells.nix | 51 +++++++++++++++++++++++++++++++++++++++ flake.lock | 39 ++++++++++++++++++++++++++++++ flake.nix | 56 ++++++++++--------------------------------- packages.nix | 59 ++++++++++++++++++++++++++++++++++++++++++++++ servicepoint.pc.in | 6 +++++ 5 files changed, 167 insertions(+), 44 deletions(-) create mode 100644 devShells.nix create mode 100644 packages.nix create mode 100644 servicepoint.pc.in diff --git a/devShells.nix b/devShells.nix new file mode 100644 index 0000000..367faf4 --- /dev/null +++ b/devShells.nix @@ -0,0 +1,51 @@ +{ + pkgs, + fenix, + selfPkgs, + ... +}: +(builtins.mapAttrs ( + packageName: package: + pkgs.mkShell { + inputsFrom = [ package ]; + packages = with pkgs; [ + rustfmt + clippy + cargo-expand + cargo-tarpaulin + gdb + ]; + + RUST_BACKTRACE = 1; + RUST_LOG = "all"; + } +) selfPkgs) +// { + test = pkgs.mkShell { + packages = with pkgs; [ + (pkgs.symlinkJoin { + name = "rust-toolchain"; + paths = with pkgs; [ + rustc + cargo + rustPlatform.rustcSrc + rustPlatform.rustLibSrc + rustfmt + clippy + cargo-expand + cargo-tarpaulin + ]; + }) + gcc + gnumake + xe + libgcc + libunwind + pkgsStatic.gcc + pkgsStatic.libgcc + pkgsStatic.musl + ]; + + RUST_SRC_PATH = "${pkgs.rustPlatform.rustLibSrc}"; + }; +} diff --git a/flake.lock b/flake.lock index 9091d9f..07ccbb6 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,26 @@ { "nodes": { + "fenix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ], + "rust-analyzer-src": "rust-analyzer-src" + }, + "locked": { + "lastModified": 1748759782, + "narHash": "sha256-MJNhEBsAbxRp/53qsXv6/eaWkGS8zMGX9LuCz1BLeck=", + "owner": "nix-community", + "repo": "fenix", + "rev": "9be40ad995bac282160ff374a47eed67c74f9c2a", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "fenix", + "type": "github" + } + }, "nixpkgs": { "locked": { "lastModified": 1748302896, @@ -18,8 +39,26 @@ }, "root": { "inputs": { + "fenix": "fenix", "nixpkgs": "nixpkgs" } + }, + "rust-analyzer-src": { + "flake": false, + "locked": { + "lastModified": 1748695646, + "narHash": "sha256-VwSuuRF4NvAoeHZJRRlX8zAFZ+nZyuiIvmVqBAX0Bcg=", + "owner": "rust-lang", + "repo": "rust-analyzer", + "rev": "2a388d1103450d814a84eda98efe89c01b158343", + "type": "github" + }, + "original": { + "owner": "rust-lang", + "ref": "nightly", + "repo": "rust-analyzer", + "type": "github" + } } }, "root": "root", diff --git a/flake.nix b/flake.nix index 2adaa88..d35dbd2 100644 --- a/flake.nix +++ b/flake.nix @@ -3,15 +3,19 @@ inputs = { nixpkgs.url = "github:nixos/nixpkgs/nixos-25.05"; + fenix = { + url = "github:nix-community/fenix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; }; outputs = inputs@{ self, nixpkgs, + fenix, }: let - lib = nixpkgs.lib; supported-systems = [ "x86_64-linux" "aarch64-linux" @@ -20,56 +24,20 @@ ]; forAllSystems = f: - lib.genAttrs supported-systems ( + nixpkgs.lib.genAttrs supported-systems ( system: f rec { + inherit system self; pkgs = nixpkgs.legacyPackages.${system}; - inherit system; + lib = pkgs.lib; + fenix = inputs.fenix.packages.${system}; + selfPkgs = self.packages.${system}; } ); in { - devShells = forAllSystems ( - { pkgs, system }: - let - toolchain = pkgs.symlinkJoin { - name = "rust-toolchain"; - paths = with pkgs; [ - rustc - cargo - rustPlatform.rustcSrc - rustPlatform.rustLibSrc - rustfmt - clippy - cargo-expand - cargo-tarpaulin - ]; - }; - in - { - default = pkgs.mkShell rec { - packages = with pkgs; [ - toolchain - gcc - gdb - gnumake - pkg-config - xe - xz - libgcc - libunwind - pkgsStatic.gcc - pkgsStatic.libgcc - pkgsStatic.musl - ]; - - RUST_SRC_PATH = "${pkgs.rustPlatform.rustLibSrc}"; - RUST_BACKTRACE = 1; - RUST_LOG = "all"; - }; - } - ); - + packages = forAllSystems (import ./packages.nix); + devShells = forAllSystems (import ./devShells.nix); formatter = forAllSystems ({ pkgs, ... }: pkgs.nixfmt-rfc-style); }; } diff --git a/packages.nix b/packages.nix new file mode 100644 index 0000000..02eb540 --- /dev/null +++ b/packages.nix @@ -0,0 +1,59 @@ +{ + pkgs, + lib, + fenix, + ... +}: +let + mkServicepointBindingC = + { + rustPlatform, + buildType, + }: + rustPlatform.buildRustPackage (finalAttrs: { + inherit buildType; + + pname = "servicepoint-binding-c"; + version = "0.15.0"; + src = ./.; + cargoLock.lockFile = ./Cargo.lock; + 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 = with pkgs; [ pkg-config ]; + buildInputs = with 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 + ''; + }); + rustPlatform-stable = pkgs.rustPlatform; + rustPlatform-unstable = pkgs.makeRustPlatform { + cargo = fenix.minimal.cargo; + rustc = fenix.minimal.rustc; + }; +in +rec { + servicepoint-binding-c-release = mkServicepointBindingC { + buildType = "release"; + rustPlatform = rustPlatform-stable; + }; + servicepoint-binding-c-size-optimized = mkServicepointBindingC { + buildType = "size_optimized"; + rustPlatform = rustPlatform-unstable; + }; + servicepoint-binding-c = servicepoint-binding-c-release; +} diff --git a/servicepoint.pc.in b/servicepoint.pc.in new file mode 100644 index 0000000..5f474e7 --- /dev/null +++ b/servicepoint.pc.in @@ -0,0 +1,6 @@ +Name: servicepoint +Description: C bindings for the servicepoint library +Version: $version +URL: https://git.berlin.ccc.de/servicepoint/servicepoint-binding-c +Libs: -L$out/lib -lservicepoint_binding_c +Cflags: -I$out/include From d98aec63b0b7be95623f4d914fc9870614647ad4 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Mon, 2 Jun 2025 17:22:38 +0200 Subject: [PATCH 5/5] examples work as packages in flake --- devShells.nix | 70 ++++++++++++++++++++++++++++----------------------- flake.lock | 16 ++++++++++++ flake.nix | 4 ++- packages.nix | 60 ++++++++++++++++++++++++++++++++++++++----- 4 files changed, 112 insertions(+), 38 deletions(-) diff --git a/devShells.nix b/devShells.nix index 367faf4..498b51b 100644 --- a/devShells.nix +++ b/devShells.nix @@ -4,47 +4,55 @@ selfPkgs, ... }: +let + defaultAdditionalPkgs = with pkgs; [ + rustfmt + clippy + cargo-expand + cargo-tarpaulin + gdb + ]; +in (builtins.mapAttrs ( packageName: package: pkgs.mkShell { inputsFrom = [ package ]; - packages = with pkgs; [ - rustfmt - clippy - cargo-expand - cargo-tarpaulin - gdb - ]; - + packages = defaultAdditionalPkgs; RUST_BACKTRACE = 1; RUST_LOG = "all"; } ) selfPkgs) // { - test = pkgs.mkShell { - packages = with pkgs; [ - (pkgs.symlinkJoin { - name = "rust-toolchain"; - paths = with pkgs; [ - rustc - cargo - rustPlatform.rustcSrc - rustPlatform.rustLibSrc - rustfmt - clippy - cargo-expand - cargo-tarpaulin - ]; - }) - gcc - gnumake - xe - libgcc - libunwind - pkgsStatic.gcc - pkgsStatic.libgcc - pkgsStatic.musl + default = pkgs.mkShell { + inputsFrom = [ + selfPkgs.servicepoint-binding-c + selfPkgs.announce ]; + packages = + defaultAdditionalPkgs + ++ (with pkgs; [ + (pkgs.symlinkJoin { + name = "rust-toolchain"; + paths = with pkgs; [ + rustc + cargo + rustPlatform.rustcSrc + rustPlatform.rustLibSrc + rustfmt + clippy + cargo-expand + cargo-tarpaulin + ]; + }) + gcc + gnumake + xe + libgcc + libunwind + pkgsStatic.gcc + pkgsStatic.libgcc + pkgsStatic.musl + ]); RUST_SRC_PATH = "${pkgs.rustPlatform.rustLibSrc}"; }; diff --git a/flake.lock b/flake.lock index 07ccbb6..873e7c3 100644 --- a/flake.lock +++ b/flake.lock @@ -21,6 +21,21 @@ "type": "github" } }, + "nix-filter": { + "locked": { + "lastModified": 1731533336, + "narHash": "sha256-oRam5PS1vcrr5UPgALW0eo1m/5/pls27Z/pabHNy2Ms=", + "owner": "numtide", + "repo": "nix-filter", + "rev": "f7653272fd234696ae94229839a99b73c9ab7de0", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "nix-filter", + "type": "github" + } + }, "nixpkgs": { "locked": { "lastModified": 1748302896, @@ -40,6 +55,7 @@ "root": { "inputs": { "fenix": "fenix", + "nix-filter": "nix-filter", "nixpkgs": "nixpkgs" } }, diff --git a/flake.nix b/flake.nix index d35dbd2..1501caa 100644 --- a/flake.nix +++ b/flake.nix @@ -3,6 +3,7 @@ inputs = { nixpkgs.url = "github:nixos/nixpkgs/nixos-25.05"; + nix-filter.url = "github:numtide/nix-filter"; fenix = { url = "github:nix-community/fenix"; inputs.nixpkgs.follows = "nixpkgs"; @@ -14,6 +15,7 @@ self, nixpkgs, fenix, + nix-filter }: let supported-systems = [ @@ -29,7 +31,7 @@ f rec { inherit system self; pkgs = nixpkgs.legacyPackages.${system}; - lib = pkgs.lib; + lib = pkgs.lib // nix-filter.lib; fenix = inputs.fenix.packages.${system}; selfPkgs = self.packages.${system}; } diff --git a/packages.nix b/packages.nix index 02eb540..1716250 100644 --- a/packages.nix +++ b/packages.nix @@ -2,20 +2,31 @@ pkgs, lib, fenix, + selfPkgs, ... }: let - mkServicepointBindingC = + version = "0.15.0"; + mkServicepoint = { rustPlatform, buildType, }: rustPlatform.buildRustPackage (finalAttrs: { - inherit buildType; + inherit buildType version; pname = "servicepoint-binding-c"; - version = "0.15.0"; - src = ./.; + src = lib.filter { + root = ./.; + include = [ + ./Cargo.lock + ./Cargo.toml + ./src + ./build.rs + ./LICENSE + ./cbindgen.toml + ]; + }; cargoLock.lockFile = ./Cargo.lock; meta = { description = "C bindings for the servicepoint crate."; @@ -40,20 +51,57 @@ let sed "s:\$out:$out:g" ${./servicepoint.pc.in} | sed "s:\$version:$version:g" > $out/lib/pkgconfig/servicepoint.pc ''; }); + mkExample = + { name, servicepointBinding }: + pkgs.stdenv.mkDerivation { + inherit version; + pname = "servicepoint-c-example-${name}"; + + nativeBuildInputs = with pkgs; [ pkg-config ]; + buildInputs = [ servicepointBinding ]; + src = ./example/src; + + #dontUnpack = true; + buildPhase = '' + mkdir -p $out/bin + + gcc ${name}.c $(pkg-config --libs servicepoint --cflags) -o $out/bin/${name} + ''; + }; rustPlatform-stable = pkgs.rustPlatform; rustPlatform-unstable = pkgs.makeRustPlatform { cargo = fenix.minimal.cargo; rustc = fenix.minimal.rustc; }; + examples = [ + "announce" + "brightness_tester" + "header_logger" + "moving_line" + "random_stuff" + "wiping_clear" + ]; in rec { - servicepoint-binding-c-release = mkServicepointBindingC { + servicepoint-binding-c-release = mkServicepoint { buildType = "release"; rustPlatform = rustPlatform-stable; }; - servicepoint-binding-c-size-optimized = mkServicepointBindingC { + servicepoint-binding-c-size-optimized = mkServicepoint { buildType = "size_optimized"; rustPlatform = rustPlatform-unstable; }; servicepoint-binding-c = servicepoint-binding-c-release; + + all-examples = pkgs.symlinkJoin { + name = "servicepoint-all-examples"; + paths = builtins.map (e: selfPkgs.${e}) examples; + }; } +// (lib.genAttrs examples ( + name: + mkExample { + inherit name; + servicepointBinding = selfPkgs.servicepoint-binding-c; + } +))