Optional deps are added automatic

When I override a dep with some optional deps enabled, I thought I need to enable those deps manully, e.g.

somePkg.override {
  inherit openssl;
  enableOpenssl = true;
};

But I just need to set the enableOpenssl part. Are those deps enabled automatic?

When you are overriding, you are not starting from the base derivation function, you are starting from the values set by somePkg. You only need to redefine the parameters you want to change. If somePkg already sets optional parameters with values you want, you don’t need to redefine those with the same values.

These values are set as openssl ? null so I expected that it’s null by default. Am I missing something?

Maybe we should talk about a specific package but in general, derivation definitions (mostly “default.nix” files) are actually a function that takes an attribute set as a parameter and then return the actual derivation. What we call parameters (openssl ? null in your example) are actually an attribute of that single attribute set parameter. The Nixpkgs exposes the actual derivations by calling their function and by passing the expression in the pkgs scope with the same name as the parameter name as the parameter value (for example, openssl = pkgs.openssl). So what you are referring as somePkg is usually the derivation function called with callPackage^1. So I would expect openssl to be not null.

That is default value of the attribute when you call the function without passing it a value – for example, if you do import ./path/to/some/pkg { }. Packages are typically called using callPackage function, which imports the path, finds the arguments of the function in the imported file, and fills in all the attributes that match existing attributes in the package set callPackage is from.

Thanks for your help! In my somePkg I have

{
...
, openssl ? null
, enableOpenssl ? false
...
}:

assert enableOpenssl -> openssl != null;
...

I expected the openssl is disabled by default. Am I wrong? When I override it with

somePkg.override {
  openssl = null;
  enableOpenssl = true;
};

the assert does raise an error. But

somePkg.override {
  enableOpenssl = true;
};

works.

Do you mean the openssl is filled with pkg.openssl automatic even if the ? null and I need to pass a null explicitly when using callPackage?

Indeed.

But unless enableOpenssl exists in pkgs, it will have the default value of false.

So as long as you properly avoid the use of openssl when disabled, it should not really matter whether openssl is null or not.

buildInputs = [
  /* … */
] ++ lib.optionals enableOpenssl [
  openssl
];
1 Like

Yes openssl will be set to pkgs.openssl if it exists regardless of if openssl parameter is optional or not. This might be confusing but I think this is done because you may call the derivation function without using callPackage. In that case I would expect the derivation to build even without providing openssl (since
enableOpenssl is false by default) so ? null part is useful.

Also, you probably shouldn’t need to set openssl to null even if you don’t use it because Nix is lazy by default.

1 Like

Thanks for your help!