Refactor apps to use types

This commit is contained in:
Archit Gupta 2024-02-20 01:25:27 -08:00
parent afa2a89bae
commit bac4759bcb

View File

@ -4,25 +4,40 @@
{ config, lib, flakelight, genSystems, ... }: { config, lib, flakelight, genSystems, ... }:
let let
inherit (lib) isFunction mapAttrs mkIf mkMerge mkOption; inherit (lib) isStringLike mapAttrs mkIf mkMerge mkOption mkOptionType;
inherit (lib.types) lazyAttrsOf raw; inherit (lib.types) coercedTo lazyAttrsOf;
inherit (lib.options) mergeEqualOption;
inherit (flakelight.types) nullable optFunctionTo; inherit (flakelight.types) nullable optFunctionTo;
isApp = x: (x ? type) && (x.type == "app") && (x ? program); app = mkOptionType {
name = "app";
description = "flake app";
descriptionClass = "noun";
check = x: (x ? type) && (x.type == "app") && (x ? program);
merge = mergeEqualOption;
};
mkApp = pkgs: app: stringLike = mkOptionType {
let app' = if isFunction app then app pkgs else app; in name = "stringLike";
if isApp app' then app' else { type = "app"; program = "${app'}"; }; description = "string-convertible value";
descriptionClass = "noun";
check = isStringLike;
merge = mergeEqualOption;
};
mkApp = app: { type = "app"; program = "${app}"; };
appType = optFunctionTo (coercedTo stringLike mkApp app);
in in
{ {
options = { options = {
app = mkOption { app = mkOption {
type = nullable raw; type = nullable appType;
default = null; default = null;
}; };
apps = mkOption { apps = mkOption {
type = nullable (optFunctionTo (lazyAttrsOf raw)); type = nullable (optFunctionTo (lazyAttrsOf appType));
default = null; default = null;
}; };
}; };
@ -34,7 +49,7 @@ in
(mkIf (config.apps != null) { (mkIf (config.apps != null) {
outputs.apps = genSystems (pkgs: outputs.apps = genSystems (pkgs:
mapAttrs (_: mkApp pkgs) (config.apps pkgs)); mapAttrs (_: v: v pkgs) (config.apps pkgs));
}) })
]; ];
} }