Skip to content
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

[wip] pythonPackages.bcc-python: init at 0.5 #41196

Closed
wants to merge 1 commit into from

Conversation

teto
Copy link
Member

@teto teto commented May 29, 2018

Add bcc python bindings (https://github.com/iovisor/bcc), aka tools to
play with BPF and kernel.

Motivation for this change

I want to load BPF programs via python.

I haven't properly tested everything yet but the bindings seemed to work (got an error when loading my bpf filter but unrelated I think)
@RAGGE
@Mic92

Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option build-use-sandbox in nix.conf on non-NixOS)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nox --run "nox-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Fits CONTRIBUTING.md.

Add bcc python bindings (https://github.com/iovisor/bcc), aka tools to
play with BPF and kernel.
@Mic92
Copy link
Member

Mic92 commented May 29, 2018

I also did this here: https://github.com/NixOS/nixpkgs/pull/40426/files :)

@Mic92
Copy link
Member

Mic92 commented May 29, 2018

Your pull request has some changes, I should incorporate like the output.

@Mic92
Copy link
Member

Mic92 commented May 29, 2018

Mhm. I think we cannot importe bcc into python packages since it depends on the kernel version.

@Mic92
Copy link
Member

Mic92 commented May 29, 2018

Do we really need a dedicated python output? Bcc already depends on python anyway.

@@ -47,13 +58,17 @@ stdenv.mkDerivation rec {

find $out/share/bcc/tools -type f -executable -print0 | \
while IFS= read -r -d ''$'\0' f; do
pythonLibs="$out/lib/python2.7/site-packages:${pythonPackages.netaddr}/lib/${python.libPrefix}/site-packages"
pythonLibs="${if pythonSupport then "$py" else "$out"}/lib/python2.7/site-packages:${pythonPackages.netaddr}/lib/${python.libPrefix}/site-packages"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use ${python.sitePackages}

'';
''
+ lib.optionalString pythonSupport ''
moveToOutput lib/python2.7 "$py"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

${python.libPrefix} ?

@teto
Copy link
Member Author

teto commented May 29, 2018

ah nice that you put up a package. I am not that knowledgeable with bcc, I wanted to use it to trace and collect statistics on some kernel functions rather than adding printk's. Your python.pkgs.buildPythonApplication might be a better approach then. Feel free to close this

@teto
Copy link
Member Author

teto commented May 29, 2018

on second thought with the buildPythonApplication, will I be able to import bcc and write my own apps ?

@FRidh
Copy link
Member

FRidh commented May 29, 2018

on second thought with the buildPythonApplication, will I be able to import bcc and write my own apps ?

No.

@Mic92
Copy link
Member

Mic92 commented May 29, 2018

@FRidh are you sure? the PYTHONPATH was set correctly in a nix-shell:

$ nix-shell -p linuxPackages.bcc
bash: /run/user/1000/env-vars: cannot overwrite existing file
[nix-shell:~/git/nixpkgs]$ echo $PYTHONPATH
/nix/store/d90chi5qw76bbncy4b1b0qb2m065c7yh-bcc-0.5.0/lib/python3.6/site-packages:...
[nix-shell:~/git/nixpkgs]$ python
Python 3.6.5 (default, Mar 28 2018, 10:24:30)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import bcc

@Mic92 Mic92 closed this May 29, 2018
@Mic92
Copy link
Member

Mic92 commented May 29, 2018

Also the following snippet works fine:

with import <nixpkgs> {};
python3.pkgs.buildPythonApplication {
  name = "bcc-app";
  format = "other";
  unpackPhase = ":";
  installPhase = ''
    mkdir -p $out/bin/
    cat > $out/bin/script <<EOF
    #!/usr/bin/env python
    import bcc
    print("hello")
    EOF
    chmod +x $out/bin/script
  '';
  propagatedBuildInputs = [ linuxPackages.bcc ];
}
$ nix-build
$ result/bin/script
hello

So I would say, yes, you can use my version to build your own applications on top of bcc.

@FRidh
Copy link
Member

FRidh commented May 29, 2018

Technically it works with the current implementation but it is not supposed to (#11423). How would one use this with withPackages, which is the preferred solution? We should not be importing from top-level packages.

@FRidh
Copy link
Member

FRidh commented May 29, 2018

Note that with withPackages/buildEnv I intentionally broke support for it. Until 3 years ago we had a lot of Python modules in the top-level package set, and that caused a lot of trouble.

@Mic92
Copy link
Member

Mic92 commented May 29, 2018

I suppose toPythonModule can be used for withPackages. We cannot easily import bcc into pythonPackages. It strongly depend on the kernel version/header that is used as it compiles at runtime bpf code that is executed in the kernel.

@FRidh
Copy link
Member

FRidh commented May 29, 2018

We cannot easily import bcc into pythonPackages. It strongly depend on the kernel version/header that is used as it compiles at runtime bpf code that is executed in the kernel.

How is that any different for buildPythonPackage than for buildPythonApplication?

@Mic92
Copy link
Member

Mic92 commented May 29, 2018

I thought your concern was about importing from top-level.

@FRidh
Copy link
Member

FRidh commented May 29, 2018

It is, I thought you were referring to having to use buildPythonPackage.

But, the actual issue is, as you pointed out, that it is made for a certain kernel version. Is it a problem that users of the Python library would have to use override in order to pass in a different kernel? There is no way around that, really, anyway.

@teto
Copy link
Member Author

teto commented May 30, 2018

I would have liked to use nix-shell -p python.withPackages as a shebang but I suppose it won't work.
I've cherry-picked #40426 and ran sudo nix-shell -p python '(linuxPackagesFor my_lenovo_kernel).bcc my_lenovo_kernel.dev (my_lenovo_kernel being a custom kernel). Is that the correct way to get bcc as a package ?
Then I successfully started the bcc hello world.


nix-shell:/home/teto/testbed]# python hello_world.py
      python3.6m-1781  [003] ....  5249.621283: 0: Hello, World!
              sh-18805 [000] ....  5249.627813: 0: Hello, World!
              sh-18805 [000] ....  5249.627883: 0: Hello, World!
      python3.6m-1781  [003] ....  5254.626520: 0: Hello, World!
              sh-18808 [000] ....  5254.632796: 0: Hello, World!
              sh-18808 [000] ....  5254.632863: 0: Hello, World!

@teto teto deleted the bcc_bindings branch May 13, 2020 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants