NixTutorial/path.nix

75 lines
1.6 KiB
Nix
Raw 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 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}"
]
++ 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;
};
}