@@ -0,0 +1,81 @@
1+{ callPackage
2+, cmake
3+, fetchFromGitHub
4+, lib
5+, protobuf
6+, python3
7+, stdenv
8+}:
9+
10+let
11+ pythonRuntime = python3.withPackages(ps: [ ps.protobuf ]);
12+in stdenv.mkDerivation rec {
13+ pname = "nanopb";
14+ version = "0.4.1";
15+
16+ src = fetchFromGitHub {
17+ owner = pname;
18+ repo = pname;
19+ rev = version;
20+ sha256 = "16zxk42wzn519bpxf4578qn97k0h1cnbkvqqkqvka9sl0n3lz2dp";
21+ };
22+
23+ nativeBuildInputs = [ cmake python3 ];
24+
25+ cmakeFlags = [
26+ "-DBUILD_SHARED_LIBS=ON" # generate $out/lib/libprotobuf-nanopb.so{.0,}
27+ "-DBUILD_STATIC_LIBS=ON" # generate $out/lib/libprotobuf-nanopb.a
28+ "-Dnanopb_PROTOC_PATH=${protobuf}/bin/protoc"
29+ ];
30+
31+ # On a case-insensitive filesystem (such as on Darwin), CMake fails to create
32+ # the build directory because of the existence of the BUILD file.
33+ # TODO: This can be removed once https://github.com/nanopb/nanopb/pull/537 is merged.
34+ preConfigure = "rm BUILD";
35+
36+ # install the generator which requires Python3 with the protobuf package. It
37+ # also requires the nanopb module that's generated by CMake to be in a
38+ # relative location to the generator itself so we move it out of the
39+ # python.sitePackages into the shared generator folder.
40+ postInstall = ''
41+ mkdir -p $out/share/nanopb/generator/proto
42+ cp ../generator/nanopb_generator.py $out/share/nanopb/generator/nanopb_generator.py
43+ cp ../generator/proto/_utils.py $out/share/nanopb/generator/proto/_utils.py
44+ cp ../generator/proto/nanopb.proto $out/share/nanopb/generator/proto/nanopb.proto
45+ mv $out/${python3.sitePackages}/nanopb_pb2.py $out/share/nanopb/generator/proto
46+ rm -rf $out/${python3.sitePackages}
47+
48+ mkdir $out/bin
49+ substitute ${./protoc-gen-nanopb} $out/bin/protoc-gen-nanopb \
50+ --subst-var-by python ${pythonRuntime}/bin/python \
51+ --subst-var-by out $out
52+ chmod +x $out/bin/protoc-gen-nanopb
53+ '';
54+
55+ meta = with lib; {
56+ inherit (protobuf.meta) platforms;
57+
58+ description = "Protocol Buffers with small code size";
59+ homepage = "https://jpa.kapsi.fi/nanopb/";
60+ license = licenses.zlib;
61+ maintainers = with maintainers; [ kalbasit ];
62+
63+ longDescription = ''
64+ Nanopb is a small code-size Protocol Buffers implementation in ansi C. It
65+ is especially suitable for use in microcontrollers, but fits any memory
66+ restricted system.
67+
68+ - Homepage: jpa.kapsi.fi/nanopb
69+ - Documentation: jpa.kapsi.fi/nanopb/docs
70+ - Downloads: jpa.kapsi.fi/nanopb/download
71+ - Forum: groups.google.com/forum/#!forum/nanopb
72+
73+ In order to use the nanopb options in your proto files, you'll need to
74+ tell protoc where to find the nanopb.proto file.
75+ You can do so with the --proto_path (-I) option to add the directory
76+ ''${nanopb}/share/nanopb/generator/proto like so:
77+
78+ protoc --proto_path=. --proto_path=''${nanopb}/share/nanopb/generator/proto --plugin=protoc-gen-nanopb=''${nanopb}/bin/protoc-gen-nanopb --nanopb_out=out file.proto
79+ '';
80+ };
81+}