{ pkgs, lib, config, ... }: let inherit (lib) mkEnableOption mkOption mkIf; inherit (lib.attrsets) mapAttrsToList; inherit (builtins) filter toString attrNames attrValues length; inherit (pkgs) writeShellScript writeShellScriptBin coreutils; cfg = config.snowhawk.brave-apps; in { options.snowhawk.brave-apps = { enable = mkEnableOption "brave-apps home-manager module"; chromium-package = mkOption { description = '' chromium package to use for the chromium apps ''; type = with lib.types; package; default = pkgs.brave; }; apps = mkOption { description = '' chromium based desktop apps configuration ''; type = with lib.types; attrsOf (submodule { options = { url = mkOption { description = '' the url to open as a chromium window only one of `url` `urlEval` or `urlFile` may be set ''; default = null; type = with lib.types; nullOr str; }; urlEval = mkOption { description = '' the bash script to evaluate to get url to open as chromium window. May be useful for secrets management only one of `url` `urlEval` or `urlFile` may be set ''; default = null; type = with lib.types; nullOr str; }; urlFile = mkOption { description = '' file to read which contains the url to open as a chromium window. May be useful for secrets management only one of `url` `urlEval` or `urlFile` may be set ''; default = null; type = with lib.types; nullOr str; }; }; }); }; }; config = mkIf cfg.enable { home.packages = let evalString = s: "\$(${s})"; readFile = f: evalString "${coreutils}/bin/cat ${f}"; writeChromiumDesktopApp = name: app: writeShellScriptBin name '' ${lib.getExe cfg.chromium-package} --new-window --app=${app} ''; in mapAttrsToList (n: v: assert lib.assertMsg (length (filter (v: v != null) (attrValues v)) == 1) "only one of `url` `urlEval` or `urlFile` may be set: ${toString n} has multiple urls set"; (writeChromiumDesktopApp n ( if v.url != null then v.url else if v.urlEval != null then evalString v.urlEval else if v.urlFile != null then readFile v.urlFile else throw "grr >:( -- this should be unreachable" ))) cfg.apps; }; }