-
Notifications
You must be signed in to change notification settings - Fork 52
build: create a nix flake for development #411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jordalgo
wants to merge
1
commit into
facebook:main
Choose a base branch
from
jordalgo:nix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| * @qdeslandes | ||
| *.nix @jordalgo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| name: Nix Build | ||
|
|
||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| pull_request: | ||
| branches: | ||
| - main | ||
|
|
||
| jobs: | ||
| build_nix: | ||
| name: "Build: Nix (${{ matrix.runner }})" | ||
| strategy: | ||
| matrix: | ||
| runner: [ubuntu-latest, ubuntu-24.04-arm] | ||
| runs-on: ${{ matrix.runner }} | ||
|
|
||
| steps: | ||
| - name: Checkout bpfilter | ||
| uses: actions/checkout@v4 | ||
| - uses: nixbuild/nix-quick-install-action@v34 | ||
| with: | ||
| nix_conf: | | ||
| keep-env-derivations = true | ||
| keep-outputs = true | ||
| - name: Restore and save Nix store | ||
| uses: nix-community/cache-nix-action@v7 | ||
| with: | ||
| # restore and save a cache using this key | ||
| primary-key: nix-${{ runner.os }}-${{ hashFiles('**/*.nix', '**/flake.lock') }} | ||
| # if there's no cache hit, restore a cache by this prefix | ||
| restore-prefixes-first-match: nix-${{ runner.os }}- | ||
| # collect garbage until the Nix store size (in bytes) is at most this number | ||
| # before trying to save a new cache | ||
| # 1G = 1073741824 | ||
| gc-max-store-size-linux: 1G | ||
| # do purge caches | ||
| purge: true | ||
| # purge all versions of the cache | ||
| purge-prefixes: nix-${{ runner.os }}- | ||
| # created more than this number of seconds ago | ||
| purge-created: 0 | ||
| # or, last accessed more than this number of seconds ago | ||
| # relative to the start of the `Post Restore and save Nix store` phase | ||
| purge-last-accessed: 0 | ||
| # except any version with the key that is the same as the `primary-key` | ||
| purge-primary-key: never | ||
| - name: Build & check | ||
| run: | | ||
| nix fmt -- --check . | ||
| nix build |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| { | ||
| stdenv, | ||
| lib, | ||
| cmake, | ||
| ninja, | ||
| pkg-config, | ||
| clang, | ||
| bison, | ||
| flex, | ||
| libbpf, | ||
| libnl, | ||
| elfutils, | ||
| openssl, | ||
| testers, | ||
| zlib, | ||
| zstd, | ||
| pcre2, | ||
| xxd, | ||
| version ? "0.0.1", | ||
| }: | ||
|
|
||
| let | ||
| fs = lib.fileset; | ||
|
|
||
| # zerocallusedregs is invalid with -target bpf | ||
| hardeningDisable = [ "zerocallusedregs" ]; | ||
|
|
||
| in | ||
| { | ||
| inherit hardeningDisable; | ||
|
|
||
| package = stdenv.mkDerivation (finalAttrs: { | ||
| pname = "bpfilter"; | ||
| inherit version; | ||
|
|
||
| src = fs.toSource { | ||
| root = ./.; | ||
| fileset = fs.unions [ | ||
| ./src | ||
| ./CMakeLists.txt | ||
| ./tools/cmake | ||
| ]; | ||
| }; | ||
|
|
||
| inherit hardeningDisable; | ||
|
|
||
| nativeBuildInputs = [ | ||
| cmake | ||
| ninja | ||
| pkg-config | ||
| clang # for building codegen BPF progs | ||
| bison | ||
| flex | ||
| ]; | ||
|
|
||
| buildInputs = [ | ||
| libbpf | ||
| libnl | ||
| elfutils | ||
| openssl | ||
| zlib | ||
| zstd | ||
| pcre2 | ||
| xxd | ||
| ]; | ||
|
|
||
| cmakeFlags = [ | ||
| "-DNO_GIT_VERSION=1" | ||
| "-DDEFAULT_PROJECT_VERSION=${finalAttrs.version}" | ||
| "-DNO_DOCS=1" | ||
| "-DNO_TESTS=1" | ||
| "-DNO_CHECKS=1" | ||
| "-DNO_BENCHMARKS=1" | ||
| ]; | ||
|
|
||
| # We do not run the unit tests because the nix build sandbox doesn't | ||
| # have access to /sys/kernel/btf/vmlinux. | ||
| doCheck = false; | ||
|
|
||
| meta.pkgConfigModules = [ "bpfilter" ]; | ||
|
|
||
| passthru = { | ||
| tests.pkg-config = testers.testMetaPkgConfig finalAttrs.finalPackage; | ||
| }; | ||
|
|
||
| preFixup = '' | ||
| substituteInPlace $out/lib/systemd/system/bpfilter.service \ | ||
| --replace-fail /usr/sbin/bpfilter $out/bin/bpfilter | ||
|
|
||
| # workaround for https://github.com/NixOS/nixpkgs/issues/144170 | ||
| substituteInPlace $out/lib/pkgconfig/bpfilter.pc --replace-fail ''${prefix}/ "" | ||
| ''; | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| { | ||
| description = "bpfilter - eBPF-based packet filtering framework"; | ||
|
|
||
| inputs = { | ||
| nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; | ||
| }; | ||
|
|
||
| outputs = | ||
| { self, nixpkgs }: | ||
| let | ||
| systems = [ | ||
| "x86_64-linux" | ||
| "aarch64-linux" | ||
| ]; | ||
| forAllSystems = nixpkgs.lib.genAttrs systems; | ||
| nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; }); | ||
|
|
||
| version = "0.0.1"; | ||
|
|
||
| in | ||
| { | ||
| packages = forAllSystems ( | ||
| system: | ||
| let | ||
| pkgs = nixpkgsFor.${system}; | ||
| bpfilterLib = pkgs.callPackage ./derivation.nix { inherit version; }; | ||
| in | ||
| { | ||
| default = bpfilterLib.package; | ||
| bpfilter = bpfilterLib.package; | ||
| } | ||
| ); | ||
|
|
||
| formatter = forAllSystems (system: nixpkgsFor.${system}.nixfmt-rfc-style); | ||
|
|
||
| devShells = forAllSystems ( | ||
| system: | ||
| let | ||
| pkgs = nixpkgsFor.${system}; | ||
| bpfilterLib = pkgs.callPackage ./derivation.nix { inherit version; }; | ||
| in | ||
| { | ||
| default = pkgs.mkShell { | ||
| name = "bpfilter-dev"; | ||
|
|
||
| inherit (bpfilterLib) hardeningDisable; | ||
|
|
||
| inputsFrom = [ bpfilterLib.package ]; | ||
|
|
||
| packages = with pkgs; [ | ||
| gnumake | ||
| clang-tools # clang-tidy, clang-format | ||
| include-what-you-use | ||
| gcc | ||
| autoconf | ||
| automake | ||
| libtool | ||
|
|
||
| # Git (for GitVersion.cmake and benchmarks) | ||
| git | ||
| libgit2 | ||
|
|
||
| # BPF tools | ||
| bpftools | ||
|
|
||
| # Networking tools (for e2e tests) | ||
| iproute2 | ||
| iputils | ||
|
|
||
| # Testing | ||
| cmocka | ||
| gbenchmark | ||
|
|
||
| # Utilities | ||
| gawk | ||
| jq | ||
| gnused | ||
| procps | ||
| lcov | ||
|
|
||
| # Documentation | ||
| # TODO: this (`make doc`) is still broken but should be fixed when linuxdoc is added to nixpkgs | ||
| doxygen | ||
| python3 | ||
| python3Packages.sphinx | ||
| # python3Packages.breathe | ||
| # python3Packages.furo | ||
| # python3Packages.linuxdoc | ||
| # python3Packages.gitpython | ||
| # python3Packages.scapy | ||
| # python3Packages.python-dateutil | ||
| # python3Packages.setuptools | ||
| # glibcLocales | ||
| ]; | ||
|
|
||
| shellHook = '' | ||
| # Set locale for sphinx-build | ||
| export LOCALE_ARCHIVE="${pkgs.glibcLocales}/lib/locale/locale-archive" | ||
| export LC_ALL=C.UTF-8 | ||
|
|
||
| # Add libbpf headers to include path for clang-tidy | ||
| export CPATH="${pkgs.libbpf}/include''${CPATH:+:$CPATH}" | ||
|
|
||
| echo "bpfilter development environment" | ||
| ''; | ||
| }; | ||
| } | ||
| ); | ||
| }; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.