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

How to use nixpkgs_package-based binary as a tool dependency? #48

Closed
robinp opened this issue Nov 22, 2018 · 3 comments
Closed

How to use nixpkgs_package-based binary as a tool dependency? #48

robinp opened this issue Nov 22, 2018 · 3 comments

Comments

@robinp
Copy link

robinp commented Nov 22, 2018

I try to source a nixpkgs_package and use it as a tool in a genrule. I couldn't find doc about this, but I found (correctly?) that a nixpkgs_package named foo gets bound to the target //external:foo. So I try to directly build from bazel command-line such package (ghc in the example), or depend on one in a rule (hello in the example). But I get errors about non-visibility of some nix expressions.

This is odd, since ghc is surely picked up by rules_haskell as it works. But maybe Skylark code has different visibility restrictions? Not sure.

I didn't try adding the exports to the BUILD files as suggested by the warnings, since I don't know if this is a red herring. You likely have a better guess. It's also possible I'm doing something silly.

WORKSPACE:

workspace(name = "treetide")

# stripped rules_haskell stuff

http_archive(
  name = "io_tweag_rules_nixpkgs",
  strip_prefix = "rules_nixpkgs-0.4.1",
  urls = ["https://github.com/tweag/rules_nixpkgs/archive/v0.4.1.tar.gz"],
)

load("@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl", "nixpkgs_package", "nixpkgs_git_repository")

nixpkgs_git_repository(
  name = "nixpkgs",
  revision = "6a3f5bcb061e1822f50e299f5616a0731636e4e7", # "18.09"
)

nixpkgs_package(
  name = "ghc",
  repositories = { "nixpkgs": "@nixpkgs//:default.nix" },
  nix_file = "//:ghc.nix",
  build_file = "@io_tweag_rules_haskell//haskell:ghc.BUILD",
)

nixpkgs_package(
  name = "hello",
  repositories = { "nixpkgs": "@nixpkgs//:default.nix" },
)

BUILD:

genrule(
  name = "hi",
  tools = ["//external:hello"],
  outs = ["hi.txt"],
  cmd = "$(location //external:hello) > $@",
)

Failure when trying to access ghc.

 $ bazel build //external:ghc
Starting local Bazel server and connecting to it...
ERROR: ...29/external/io_tweag_rules_nixpkgs/nixpkgs/nixpkgs.bzl:136:5:
   no such target '//:ghc.nix': target 'ghc.nix' not declared in package '';
   however, a source file of this name exists. 
   (Perhaps add 'exports_files(["ghc.nix"])' to /BUILD?)
   
ERROR: ...29/external/io_tweag_rules_nixpkgs/nixpkgs/nixpkgs.bzl:136:5:
    no such target '@nixpkgs//:default.nix': target 'default.nix' not declared in package '';
    however, a source file of this name exists. 
   (Perhaps add 'exports_files(["default.nix"])' to /BUILD?)
   
ERROR: ...29/external/io_tweag_rules_nixpkgs/nixpkgs/nixpkgs.bzl:136:5:
   no such target '@io_tweag_rules_haskell//haskell:ghc.BUILD': target 'ghc.BUILD' not declared in package 'haskell';
   however, a source file of this name exists. 
   (Perhaps add 'exports_files(["ghc.BUILD"])' to haskell/BUILD?)

ERROR: Analysis of target '//external:ghc' failed; build aborted: Analysis failed

Trying on just hello:

 $ bazel build :hi
ERROR: ..29/external/io_tweag_rules_nixpkgs/nixpkgs/nixpkgs.bzl:177:9: no such target '@nixpkgs//:default.nix': target 'default.nix' not declared in package ''; however, a source file of this name exists.  (Perhaps add 'exports_files(["default.nix"])' to /BUILD?) defined by /home/ron/.cache/bazel/_bazel_ron/dea833c6b6c799258fdcca8ca5a2e429/external/nixpkgs/BUILD and referenced by '//external:hello'
ERROR: Analysis of target '//:hi' failed; build aborted: Analysis failed

Edit: in context of NixOS/nixpkgs#50765, wanting to add bazel tools from nix.

@Profpatsch
Copy link
Contributor

The external/ directory is where bazel puts all its repositories.

What you want is not "//external:hello" (if that works that is kind of weird and maybe a bazel bug‽), rather "@hello//:bin" to reference the hello binary files.

In practice nixpkgs_package creates a folder named by the name attribute and populates it with symlinks to the store files of the referenced nixpkgs attribute (which is the same as name if you omit the attribute_path argument). Then we add a default BUILD file to that folder, which exports a few basic targets, like the contents of bin (see the documentation of build_file in the README for how that works).
You can also export your own subset of files from the package, by doing something like

nixpkgs_package(
  …
  build_file_content = """\
exports_files(
  ["bin/hello"],
  visibility = ["//visibility:public"]
)
"""
)

@mboes
Copy link
Member

mboes commented Dec 9, 2018

Closing due to inactivity. @robinp feel free to reopen if you're still stuck.

@mboes mboes closed this as completed Dec 9, 2018
@robinp
Copy link
Author

robinp commented Jan 16, 2019

Thanks, this worked well!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants