Nixpkgs: How to build package with dependency, which is not in nixpkgs.

Created on 9 Nov 2017  Â·  7Comments  Â·  Source: NixOS/nixpkgs

Issue description

Dependency nix file - https://gist.github.com/MrSorcus/29012dde638ad7ad790f0eee05cf2635

Insert it in configuration.nix:

environment.systemPackages = [
  (import ./example_pkg.nix)
];

Package nix file - https://gist.github.com/MrSorcus/1a172342cdb3df5aa11354e047b5b92c

But nix-shell tells me about undefined variable.

error: undefined variable ‘example_pkg’ at ./src/default.nix:10:3
(use ‘--show-trace’ to show detailed location information)

Replacement example_pkg to import ./example_pkg.nix does not change anything.
How can i add/linking package (my_package) with dependency (example_pkg)?

P.S. To some extent, Nixos does not simplify, but complicates.

Most helpful comment

Here you are, it works now: mrsorcus.tar.gz

Problems that I've noticed in your code:

  1. binding.cr, first line: should be without lib prefix, i.e. @[Link("libbinn")] -> @[Link("binn")], this goes straight into the linker as -lbinn.
  2. binn.nix, src: better to use fetchFromGitHub when you fetch from GitHub. It's both faster and more neat. Also, you don't have a hash (sha256) or revision (rev) in src: both should be present in order for fetcher to work.
  3. binn.nix, buildInputs: binn actually uses GCC even if Clang is installed, see their Makefile.
  4. binn.nix, preBuild: it's cleaner to just substitute /usr to $out, see: https://nixos.org/nixpkgs/manual/#fun-substituteInPlace

Besides that, I've cleaned up default.nix and removed unused stuff to make it more straightforward. Both nix-shell and nix-build work:

[chronos@yegatool:~/Desktop/mrsorcus]$ nix-build
/nix/store/5vr6d9hbm03x167rjwl721d55hwhha33-binn-crystal-binding

[chronos@yegatool:~/Desktop/mrsorcus]$ ./result
Pointer(Binn::Binn)@0x18c62d0

[chronos@yegatool:~/Desktop/mrsorcus]$ nix-shell

[nix-shell:~/Desktop/mrsorcus]$ crystal build binding.cr

[nix-shell:~/Desktop/mrsorcus]$ ./binding 
Pointer(Binn::Binn)@0xedc2d0

All 7 comments

Doesn't help...
/nix/store/v5464abdvkql4cikkpm24kks5ih5j678-binutils-2.28.1/bin/ld: cannot find -lexample_pkg

I could try to help you out if you'd show me complete set of Nix expressions that you are trying to build.

https://gist.github.com/MrSorcus/df0ae0d794c8500f470bd944ce64be9c - Dependency (Binn library).
https://gist.github.com/MrSorcus/56b4307e1d242b531c74ea080532c9a8 - Package (Nix expr for crystal script).
https://gist.github.com/MrSorcus/a907ca926b5d7cac313b97037efc7a0f - Crystal script

Directory - ./test and files ./test/binn.nix, ./test/default.nix, ./test/binding.cr.
Go to directory ./test/ and call nix-shell.

Got error:

root@nixos_laptop> nix-shell
error: undefined variable ‘binn’ at ./test/default.nix:20:3
(use ‘--show-trace’ to show detailed location information)

Ok, change binn to self.binn

Now i can be opened nix-shell, but cannot linking with library. Logs here -
https://gist.github.com/MrSorcus/f329c892e2574e1d74626a3eb2eb50f0

Here you are, it works now: mrsorcus.tar.gz

Problems that I've noticed in your code:

  1. binding.cr, first line: should be without lib prefix, i.e. @[Link("libbinn")] -> @[Link("binn")], this goes straight into the linker as -lbinn.
  2. binn.nix, src: better to use fetchFromGitHub when you fetch from GitHub. It's both faster and more neat. Also, you don't have a hash (sha256) or revision (rev) in src: both should be present in order for fetcher to work.
  3. binn.nix, buildInputs: binn actually uses GCC even if Clang is installed, see their Makefile.
  4. binn.nix, preBuild: it's cleaner to just substitute /usr to $out, see: https://nixos.org/nixpkgs/manual/#fun-substituteInPlace

Besides that, I've cleaned up default.nix and removed unused stuff to make it more straightforward. Both nix-shell and nix-build work:

[chronos@yegatool:~/Desktop/mrsorcus]$ nix-build
/nix/store/5vr6d9hbm03x167rjwl721d55hwhha33-binn-crystal-binding

[chronos@yegatool:~/Desktop/mrsorcus]$ ./result
Pointer(Binn::Binn)@0x18c62d0

[chronos@yegatool:~/Desktop/mrsorcus]$ nix-shell

[nix-shell:~/Desktop/mrsorcus]$ crystal build binding.cr

[nix-shell:~/Desktop/mrsorcus]$ ./binding 
Pointer(Binn::Binn)@0xedc2d0

It's a very good answer, thank you so much.

Also, feel free to add Binn to Nixpkgs if you want :-)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sid-kap picture sid-kap  Â·  3Comments

tomberek picture tomberek  Â·  3Comments

ob7 picture ob7  Â·  3Comments

ghost picture ghost  Â·  3Comments

ayyess picture ayyess  Â·  3Comments