NixTutorial/marker.nix

167 lines
3.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 = {};
};
2024-04-28 16:56:35 +00:00
arrival = lib.mkOption {
type = markerType;
default = {};
};
2024-04-28 16:04:17 +00:00
};
config = {
departure.style.label = lib.mkDefault (firstUpperAlnum name);
2024-04-28 16:56:35 +00:00
arrival.style.label = lib.mkDefault (firstUpperAlnum name);
2024-04-28 16:04:17 +00:00
};
});
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-28 16:32:43 +00:00
size = lib.mkOption {
type = lib.types.enum [
"tiny"
"small"
"medium"
"large"
];
default = "medium";
};
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
{
2024-04-28 16:56:35 +00:00
imports = [
./path.nix
];
2024-04-26 16:17:35 +00:00
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-28 16:56:35 +00:00
manuel = {
departure = {
location = "Switzerland";
style.size = "small";
};
arrival = {
2024-04-28 20:00:44 +00:00
location = "Chile";
2024-04-28 16:56:35 +00:00
};
2024-04-28 19:13:23 +00:00
pathStyle = {
weight = 10;
2024-04-28 20:00:44 +00:00
color = "purple";
geodesic = true;
2024-04-28 19:13:23 +00:00
};
2024-04-26 22:30:42 +00:00
};
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
2024-04-28 16:56:35 +00:00
user.arrival
]) (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
2024-04-28 16:32:43 +00:00
size = {
tiny = "tiny";
small = "small";
medium = "mid";
large = null;
}.${marker.style.size};
2024-04-26 22:30:42 +00:00
attributes =
2024-04-28 16:32:43 +00:00
lib.optional
(marker.style.label != null)
"label:${marker.style.label}"
++ lib.optional
(size != null)
"size:${size}"
2024-04-26 22:30:42 +00:00
++ [
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
};
}