The pipe operator '<|' was added in Nix 2.24 as experimental. In typical nix community fashion, an eternity has passed, it's still experimental, and they're still arguing about it. Thus, in order not to burden any users of this flake, they have been removed. To get this done quickly a utility function has been added called 'compose'. It is 'lib.trivial.pipe' written in reverse. This eliminates any bugs and performance regressions from unnecessary thunk evaluation.
120 lines
2.8 KiB
Nix
120 lines
2.8 KiB
Nix
{ lib, sLib, ... }:
|
|
|
|
let
|
|
submodule =
|
|
{ config, ... }:
|
|
{
|
|
options = {
|
|
default = lib.mkOption {
|
|
type = lib.types.nullOr lib.types.str;
|
|
default = null;
|
|
example = "ddg";
|
|
description = "The default search engine used in the address bar and search bar.";
|
|
};
|
|
|
|
engines = lib.mkOption {
|
|
type = lib.types.attrsOf (lib.types.submodule engineSubmodule);
|
|
default = { };
|
|
};
|
|
};
|
|
};
|
|
|
|
engineSubmodule =
|
|
{ name, ... }:
|
|
{
|
|
options = {
|
|
name = lib.mkOption {
|
|
type = lib.types.str;
|
|
default = name;
|
|
};
|
|
|
|
searchUri = {
|
|
base = lib.mkOption {
|
|
type = lib.types.strMatching "[a-z]+://[-A-Za-z0-9.]+(:[0-9]+)?(/[^?]*)?";
|
|
};
|
|
|
|
params = lib.mkOption {
|
|
type = lib.types.listOf (
|
|
lib.types.submodule {
|
|
options = {
|
|
key = lib.mkOption { type = lib.types.str; };
|
|
value = lib.mkOption { type = lib.types.str; };
|
|
};
|
|
}
|
|
);
|
|
default = [ ];
|
|
};
|
|
};
|
|
|
|
method = lib.mkOption {
|
|
type = lib.types.enum [
|
|
"GET"
|
|
"POST"
|
|
];
|
|
default = "GET";
|
|
};
|
|
|
|
alias = lib.mkOption {
|
|
type = lib.types.nullOr lib.types.str;
|
|
default = null;
|
|
};
|
|
|
|
icon = lib.mkOption {
|
|
type = lib.types.nullOr (
|
|
lib.types.oneOf [
|
|
lib.types.package
|
|
lib.types.str
|
|
]
|
|
);
|
|
default = null;
|
|
};
|
|
};
|
|
};
|
|
|
|
buildPolicyFromEngine =
|
|
{
|
|
name,
|
|
searchUri,
|
|
method,
|
|
alias,
|
|
icon,
|
|
}:
|
|
let
|
|
params = sLib.compose [
|
|
(builtins.concatStringsSep "&")
|
|
(builtins.map (x: "${x.key}=${x.value}"))
|
|
] searchUri.params;
|
|
in
|
|
{
|
|
Name = name;
|
|
URLTemplate =
|
|
if method == "POST" then
|
|
searchUri.base
|
|
else if params == "" then
|
|
searchUri.base
|
|
else
|
|
"${searchUri.base}?${params}";
|
|
Method = method;
|
|
${if method == "POST" then "PostData" else null} = params;
|
|
${if icon != null then "IconURL" else null} = "file://${builtins.toString icon}";
|
|
${if alias != null then "Alias" else null} = alias;
|
|
};
|
|
in
|
|
{
|
|
options = {
|
|
search = lib.mkOption {
|
|
type = lib.types.submodule submodule;
|
|
default = { };
|
|
description = "Declarative search engine configuration.";
|
|
};
|
|
};
|
|
|
|
process =
|
|
{ search, ... }:
|
|
{
|
|
policies.SearchEngines = {
|
|
${if search.default == null then null else "Default"} = search.default;
|
|
Add = lib.mapAttrsToList (_: buildPolicyFromEngine) search.engines;
|
|
};
|
|
};
|
|
}
|