-
Notifications
You must be signed in to change notification settings - Fork 0
/
homepage.nix
105 lines (95 loc) · 2.99 KB
/
homepage.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
{ lib, config, ... }:
let
cfg = config.homelab.homepage;
enableDefaultTrueOption = description:
lib.mkOption {
type = lib.types.bool;
default = true;
inherit description;
};
priorityOption = lib.mkOption {
type = lib.types.int;
default = 10;
description = "Sort priority (default 10)";
};
itemConfigType = lib.types.submodule {
options = {
enable = enableDefaultTrueOption "Enable item";
priority = priorityOption;
config = lib.mkOption {
type = lib.types.attrs;
description = "Item (service, widget, etc.) configuration";
default = { };
};
};
};
itemGroupConfigType = lib.types.submodule {
options = {
enable = enableDefaultTrueOption "Enable group";
priority = priorityOption;
config = lib.mkOption {
type = lib.types.attrsOf itemConfigType;
description = "Item (services, bookmarks, etc.) group configuration";
default = { };
};
};
};
priorityComparator = a: b: a.value.priority - b.value.priority > 0;
sortByPriority = list: (builtins.sort priorityComparator list);
toHomepageConfig = itemsConfig:
let
keys = builtins.attrNames itemsConfig;
items = builtins.map (name: {
inherit name;
value = builtins.getAttr name itemsConfig;
}) keys;
sorted = sortByPriority items;
filtered = builtins.filter (item: item.value.enable) sorted;
in builtins.map (item: { "${item.name}" = item.value.config; }) filtered;
toParentHomepageConfig = itemsConfig:
let
transformed = builtins.mapAttrs
(name: item: item // { config = toHomepageConfig item.config; })
itemsConfig;
in toHomepageConfig transformed;
in {
options.homelab.homepage = {
enable = lib.mkEnableOption "Homepage dashboard";
domain = lib.mkOption {
type = lib.types.str;
default = config.homelab.domain;
description = "Homepage domain";
};
services = lib.mkOption {
type = lib.types.attrsOf itemGroupConfigType;
default = { };
};
};
# Inject a bit of declarative configuration for the homepage dashboard
options.services.homepage-dashboard = {
services-declarative = lib.mkOption {
type = lib.types.attrsOf itemGroupConfigType;
default = { };
};
bookmarks-declarative = lib.mkOption {
type = lib.types.attrsOf itemGroupConfigType;
default = { };
};
};
config = lib.mkIf cfg.enable {
services.homepage-dashboard =
let service-cfg = config.services.homepage-dashboard;
in {
enable = true;
services = toParentHomepageConfig service-cfg.services-declarative;
bookmarks = toParentHomepageConfig service-cfg.bookmarks-declarative;
};
services.nginx.virtualHosts."${cfg.domain}" = {
forceSSL = true;
useACMEHost = config.homelab.domain;
locations."/".proxyPass = "http://127.0.0.1:${
toString config.services.homepage-dashboard.listenPort
}";
};
};
}