NixTutorial/path.nix

103 lines
2.2 KiB
Nix
Raw Permalink Normal View History

2024-04-28 16:56:35 +00:00
{ lib, config, ... }:
let
pathType = lib.types.submodule {
options = {
locations = lib.mkOption {
type = lib.types.listOf lib.types.str;
};
2024-04-28 19:13:23 +00:00
style = lib.mkOption {
type = pathStyleType;
default = {};
};
};
};
pathStyleType = lib.types.submodule {
options = {
weight = lib.mkOption {
type = lib.types.ints.between 1 20;
default = 5;
};
2024-04-28 20:00:44 +00:00
color = lib.mkOption {
type = colorType;
default = "blue";
};
geodesic = lib.mkOption {
type = lib.types.bool;
default = false;
};
2024-04-28 16:56:35 +00:00
};
};
2024-04-28 20:00:44 +00:00
colorType = lib.types.either
(lib.types.strMatching "0x[0-9A-F]{3}([0-9A-F]([0-9A-F]{2}([0-9A-F]{2})?)?)?")
(
lib.types.enum [
"black"
"blue"
"brown"
"gray"
"green"
"orange"
"purple"
"red"
"white"
"yellow"
]);
2024-04-28 16:56:35 +00:00
in
{
options = {
2024-04-28 19:13:23 +00:00
users = lib.mkOption {
type = lib.types.attrsOf (
lib.types.submodule {
options.pathStyle = lib.mkOption {
type = pathStyleType;
default = {};
};
});
};
2024-04-28 16:56:35 +00:00
map.paths = lib.mkOption {
type = lib.types.listOf pathType;
};
};
config = {
map.paths = builtins.map (
user: {
locations = [
user.departure.location
user.arrival.location
];
2024-04-28 19:13:23 +00:00
style = user.pathStyle;
2024-04-28 16:56:35 +00:00
}) (lib.filter (
user:
user.departure.location != null
&& user.arrival.location != null
) (lib.attrValues config.users));
requestParams =
let
attrForLocation =
loc: "$(${config.scripts.geocode}/bin/geocode ${lib.escapeShellArg loc})";
paramForPath =
path:
let
2024-04-28 19:13:23 +00:00
attributes =
[
"weight:${toString path.style.weight}"
2024-04-28 20:00:44 +00:00
"color:${path.style.color}"
"geodesic:${lib.boolToString path.style.geodesic}"
2024-04-28 19:13:23 +00:00
]
++ builtins.map attrForLocation path.locations;
2024-04-28 16:56:35 +00:00
in
''path="${lib.concatStringsSep "|" attributes}"'';
in
builtins.map paramForPath config.map.paths;
};
}