NixTutorial/marker.nix

122 lines
2.5 KiB
Nix
Raw Normal View History

2024-04-26 16:17:35 +00:00
{ lib, config, ... }:
let
2024-04-28 16:04:17 +00:00
# Returns the uppercased first label or number of a string
firstUpperAlnum =
str:
lib.mapNullable
lib.head (builtins.match "[^A-Z0-9]*([A-Z0-9]).*" (lib.toUpper str));
userType = lib.types.submodule (
{ name, ... }: {
options = {
departure = lib.mkOption {
type = markerType;
default = {};
};
};
config = {
departure.style.label = lib.mkDefault (firstUpperAlnum name);
};
});
2024-04-26 16:17:35 +00:00
markerType = lib.types.submodule {
options = {
location = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
};
2024-04-26 22:30:42 +00:00
2024-04-28 16:21:30 +00:00
style = {
label = lib.mkOption {
type = lib.types.nullOr
(lib.types.strMatching "[0-9A-Z]");
default = null;
};
color = lib.mkOption {
type = colorType;
default = "red";
};
2024-04-26 22:30:42 +00:00
};
2024-04-26 16:17:35 +00:00
};
};
2024-04-28 16:21:30 +00:00
colorType = lib.types.either
(lib.types.strMatching "0x[0-9A-F]{3}[0-9A-F]{3}?")
(lib.types.enum [
"black"
"blue"
"brown"
"gray"
"green"
"orange"
"purple"
"red"
"white"
"yellow"
]);
2024-04-26 16:17:35 +00:00
in
{
options = {
users = lib.mkOption {
type = lib.types.attrsOf userType;
};
2024-04-26 16:17:35 +00:00
map.markers = lib.mkOption {
type = lib.types.listOf markerType;
};
};
config = {
users = {
2024-04-26 22:30:42 +00:00
manuel.departure = {
location = "Switzerland";
};
ganondorf.departure = {
location = "Argentinia";
2024-04-28 16:21:30 +00:00
style = {
color = "green";
};
2024-04-26 22:30:42 +00:00
};
};
2024-04-26 16:17:35 +00:00
map = {
markers = lib.filter
(marker: marker.location != null)
(
lib.concatMap (user: [
user.departure
]) (lib.attrValues config.users));
center = lib.mkIf
(lib.length config.map.markers >= 1)
null;
zoom = lib.mkIf
(lib.length config.map.markers >= 2)
null;
2024-04-26 16:17:35 +00:00
};
requestParams =
let
2024-04-26 22:30:42 +00:00
paramForMarker =
marker:
let
attributes =
lib.optional (marker.style.label != null)
"label:${marker.style.label}"
++ [
2024-04-28 16:21:30 +00:00
"color:${marker.style.color}"
2024-04-26 22:30:42 +00:00
"$(${config.scripts.geocode}/bin/geocode ${
lib.escapeShellArg marker.location
})"
];
in
"markers=\"${lib.concatStringsSep "|" attributes}\"";
in builtins.map paramForMarker config.map.markers;
2024-04-26 16:17:35 +00:00
};
}