modules/dependencies: introduce top-level (internal) __depPackages option

This commit is contained in:
Gaetan Lepage 2025-04-08 15:46:22 +02:00
parent 710f9cbd52
commit 6c4e2d9279

View file

@ -5,9 +5,71 @@
...
}:
let
inherit (lib) types;
cfg = config.dependencies;
packages = {
mkDependencyOption = name: properties: {
enable = lib.mkEnableOption "Add ${name} to dependencies.";
package =
lib.mkPackageOption pkgs name properties
# Handle example manually so that we can embed the original attr-path within
# the literalExpression object. This simplifies testing the examples.
// lib.optionalAttrs (properties.example != null) {
example =
if properties.example._type or null == "literalExpression" then
properties.example
else
rec {
_type = "literalExpression";
text = "pkgs.${lib.showAttrPath path}";
path = lib.toList properties.example;
};
};
};
in
{
options = {
__depPackages = lib.mkOption {
type = types.attrsOf (
types.submodule {
options = {
default = lib.mkOption {
type = with types; either str (listOf str);
description = ''
Default name (or path) for this package.
'';
example = "git";
};
example = lib.mkOption {
type = with types; nullOr str;
description = ''
Example of another package to use instead of the default.
'';
example = "gitMinimal";
default = null;
};
};
}
);
default = { };
internal = true;
visible = false;
};
dependencies = lib.mapAttrs mkDependencyOption config.__depPackages;
};
config = {
extraPackages = lib.pipe cfg [
builtins.attrValues
(builtins.filter (p: p.enable))
(builtins.map (p: p.package))
];
__depPackages = {
bat.default = "bat";
codeium.default = "codeium";
coreutils = {
@ -67,35 +129,5 @@ let
yq.default = "yq";
zk.default = "zk";
};
mkDependencyOption = name: properties: {
enable = lib.mkEnableOption "Add ${name} to dependencies.";
package =
lib.mkPackageOption pkgs name properties
# Handle example manually so that we can embed the original attr-path within
# the literalExpression object. This simplifies testing the examples.
// lib.optionalAttrs (properties ? example) {
example =
if properties.example._type or null == "literalExpression" then
properties.example
else
rec {
_type = "literalExpression";
text = "pkgs.${lib.showAttrPath path}";
path = lib.toList properties.example;
};
};
};
in
{
options.dependencies = lib.mapAttrs mkDependencyOption packages;
config = {
extraPackages = lib.pipe cfg [
builtins.attrValues
(builtins.filter (p: p.enable))
(builtins.map (p: p.package))
];
};
}