Why do some *.package options have no default?

Is there a reason to not have the package as a default inside an option?

In particular i stumbled upon the new hardware.graphics.package and hardware.graphics.package32 options which are only set at runtime, if hardware.graphics.enable = true;.

config = lib.mkIf cfg.enable {
    # ...
    hardware.graphics.package = lib.mkDefault pkgs.mesa;
    hardware.graphics.package32 = lib.mkDefault pkgs.pkgsi686Linux.mesa;
};

If you reference config.hardware.graphics.package without setting it and enabling hardware.graphics.enable you will get an error.

Wouldn’t it be better to put them into the default in mkOption like so:?

    package = lib.mkOption {
      description = ''
        The package that provides the default driver set.
      '';
      type = lib.types.package;
      default = pkgs.mesa;
    };

or so:?

    package = lib.mkPackageOption pkgs "mesa" { };

This way it would be documented, which package is set by default in search.nixos.org/options and you wouldn’t get an error if referencing config.hardware.graphics.package.

It doesn’t make sense to reference config.hardware.graphics.package without checking config.hardware.graphics.enable first. That’s what the enable option is for - to allow that check in the first place.

The module seems fine as-is IMO, if you want to document the default you could set defaultText instead.

1 Like

Okay, that’s true.

But if also using defaultText, and the package changes for some reason, the editor would have to change / keep track of two lines, which is bad practice (in my opinion). As a developer wouldn’t you want as few duplicates as possible?