tweag / rules_nixpkgs

Rules for importing Nixpkgs packages into Bazel.
Apache License 2.0
285 stars 80 forks source link

How to toggle optional dependencies #572

Closed blackliner closed 1 month ago

blackliner commented 2 months ago

Is it possible to use nixpkgs_package and change the optional dependencies? See for example: https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/libraries/opencv/default.nix#L46-L54

something like

load("@rules_nixpkgs_core//:nixpkgs.bzl", "nixpkgs_package")
load("//packages/thirdparty/nix:defs.bzl", "NIX_REPOSITORIES")

def opencv():
    nixpkgs_package(
        name = "opencv4",
        repositories = NIX_REPOSITORIES,
        build_file = "//packages/thirdparty/opencv:BUILD.bazel.tmpl",
        optionals = [ {"enableGtk2": False}, {"enableJPEG": True} ]
    )
aherrmann commented 1 month ago

I think it should be possible to achieve this using the existing nixopts attribute. Something along the lines of the following (not tested)

    nixpkgs_package(
        name = "opencv4",
        repositories = NIX_REPOSITORIES,
        build_file = "//packages/thirdparty/opencv:BUILD.bazel.tmpl",
        nixopts = [ "--arg", "enableGtk2", "false", "--arg", "enableJPEG", "true" ]
    )
blackliner commented 1 month ago

Doesn't seem to work. Looking at https://ryantm.github.io/nixpkgs/using/overrides/, it mentions .override that can be used in derivations. Is there a way to use the command line to provide overrides? Or other means with nixpkgs_package

Would https://github.com/NixOS/nix/issues/5766 be the missing part?

Could we write a proxy default.nix for the time being to set the override? This does NOT do the trick yet :-/

{ pkgs ? import <nixpkgs> {} }:

let
  opencv4 = pkgs.opencv4.overrideAttrs (previousAttrs: {
    enableEXR = false;
  });
in
opencv4
aherrmann commented 1 month ago

I see, in that case this should be doable using the nix_file_content attribute to provide a Nix expression, see example in tests. Something like

    nixpkgs_package(
        name = "opencv4",
        repositories = NIX_REPOSITORIES,
        build_file = "//packages/thirdparty/opencv:BUILD.bazel.tmpl",
        nix_file_content = "let pkgs = import <nixpkgs> { config = {}; overlays = []; }; in pkgs.opencv.override { enableEXR = false; }",
    )

Would https://github.com/NixOS/nix/issues/5766 be the missing part?

That would make it possible to configure the override using nixopts. But, nix_file_content should also work.

blackliner commented 1 month ago

Thank you, this workaround did it!