grafana-alloy: don't build the frontend twice
[NixPkgs.git] / nixos / modules / services / misc / homepage-dashboard.nix
blobb29a53d2f1cff56358d8f70def437ffc65465a24
1 { config
2 , pkgs
3 , lib
4 , ...
5 }:
7 let
8   cfg = config.services.homepage-dashboard;
9   # Define the settings format used for this program
10   settingsFormat = pkgs.formats.yaml { };
13   options = {
14     services.homepage-dashboard = {
15       enable = lib.mkEnableOption "Homepage Dashboard, a highly customizable application dashboard";
17       package = lib.mkPackageOption pkgs "homepage-dashboard" { };
19       openFirewall = lib.mkOption {
20         type = lib.types.bool;
21         default = false;
22         description = "Open ports in the firewall for Homepage.";
23       };
25       listenPort = lib.mkOption {
26         type = lib.types.int;
27         default = 8082;
28         description = "Port for Homepage to bind to.";
29       };
31       environmentFile = lib.mkOption {
32         type = lib.types.str;
33         description = ''
34           The path to an environment file that contains environment variables to pass
35           to the homepage-dashboard service, for the purpose of passing secrets to
36           the service.
38           See the upstream documentation:
40           https://gethomepage.dev/latest/installation/docker/#using-environment-secrets
41         '';
42         default = "";
43       };
45       customCSS = lib.mkOption {
46         type = lib.types.lines;
47         description = ''
48           Custom CSS for styling Homepage.
50           See https://gethomepage.dev/latest/configs/custom-css-js/.
51         '';
52         default = "";
53       };
55       customJS = lib.mkOption {
56         type = lib.types.lines;
57         description = ''
58           Custom Javascript for Homepage.
60           See https://gethomepage.dev/latest/configs/custom-css-js/.
61         '';
62         default = "";
63       };
65       bookmarks = lib.mkOption {
66         inherit (settingsFormat) type;
67         description = ''
68           Homepage bookmarks configuration.
70           See https://gethomepage.dev/latest/configs/bookmarks/.
71         '';
72         # Defaults: https://github.com/gethomepage/homepage/blob/main/src/skeleton/bookmarks.yaml
73         example = [
74           {
75             Developer = [
76               { Github = [{ abbr = "GH"; href = "https://github.com/"; }]; }
77             ];
78           }
79           {
80             Entertainment = [
81               { YouTube = [{ abbr = "YT"; href = "https://youtube.com/"; }]; }
82             ];
83           }
84         ];
85         default = [ ];
86       };
88       services = lib.mkOption {
89         inherit (settingsFormat) type;
90         description = ''
91           Homepage services configuration.
93           See https://gethomepage.dev/latest/configs/services/.
94         '';
95         # Defaults: https://github.com/gethomepage/homepage/blob/main/src/skeleton/services.yaml
96         example = [
97           {
98             "My First Group" = [
99               {
100                 "My First Service" = {
101                   href = "http://localhost/";
102                   description = "Homepage is awesome";
103                 };
104               }
105             ];
106           }
107           {
108             "My Second Group" = [
109               {
110                 "My Second Service" = {
111                   href = "http://localhost/";
112                   description = "Homepage is the best";
113                 };
114               }
115             ];
116           }
117         ];
118         default = [ ];
119       };
121       widgets = lib.mkOption {
122         inherit (settingsFormat) type;
123         description = ''
124           Homepage widgets configuration.
126           See https://gethomepage.dev/latest/configs/service-widgets/.
127         '';
128         # Defaults: https://github.com/gethomepage/homepage/blob/main/src/skeleton/widgets.yaml
129         example = [
130           {
131             resources = {
132               cpu = true;
133               memory = true;
134               disk = "/";
135             };
136           }
137           {
138             search = {
139               provider = "duckduckgo";
140               target = "_blank";
141             };
142           }
143         ];
144         default = [ ];
145       };
147       kubernetes = lib.mkOption {
148         inherit (settingsFormat) type;
149         description = ''
150           Homepage kubernetes configuration.
152           See https://gethomepage.dev/latest/configs/kubernetes/.
153         '';
154         default = { };
155       };
157       docker = lib.mkOption {
158         inherit (settingsFormat) type;
159         description = ''
160           Homepage docker configuration.
162           See https://gethomepage.dev/latest/configs/docker/.
163         '';
164         default = { };
165       };
167       settings = lib.mkOption {
168         inherit (settingsFormat) type;
169         description = ''
170           Homepage settings.
172           See https://gethomepage.dev/latest/configs/settings/.
173         '';
174         # Defaults: https://github.com/gethomepage/homepage/blob/main/src/skeleton/settings.yaml
175         default = { };
176       };
177     };
178   };
180   config =
181     let
182       # If homepage-dashboard is enabled, but none of the configuration values have been updated,
183       # then default to "unmanaged" configuration which is manually updated in
184       # var/lib/homepage-dashboard. This is to maintain backwards compatibility, and should be
185       # deprecated in a future release.
186       managedConfig = !(
187         cfg.bookmarks == [ ] &&
188         cfg.customCSS == "" &&
189         cfg.customJS == "" &&
190         cfg.docker == { } &&
191         cfg.kubernetes == { } &&
192         cfg.services == [ ] &&
193         cfg.settings == { } &&
194         cfg.widgets == [ ]
195       );
197       configDir = if managedConfig then "/etc/homepage-dashboard" else "/var/lib/homepage-dashboard";
199       msg = "using unmanaged configuration for homepage-dashboard is deprecated and will be removed"
200         + " in 24.05. please see the NixOS documentation for `services.homepage-dashboard' and add"
201         + " your bookmarks, services, widgets, and other configuration using the options provided.";
202     in
203     lib.mkIf cfg.enable {
204       warnings = lib.optional (!managedConfig) msg;
206       environment.etc = lib.mkIf managedConfig {
207         "homepage-dashboard/custom.css".text = cfg.customCSS;
208         "homepage-dashboard/custom.js".text = cfg.customJS;
210         "homepage-dashboard/bookmarks.yaml".source = settingsFormat.generate "bookmarks.yaml" cfg.bookmarks;
211         "homepage-dashboard/docker.yaml".source = settingsFormat.generate "docker.yaml" cfg.docker;
212         "homepage-dashboard/kubernetes.yaml".source = settingsFormat.generate "kubernetes.yaml" cfg.kubernetes;
213         "homepage-dashboard/services.yaml".source = settingsFormat.generate "services.yaml" cfg.services;
214         "homepage-dashboard/settings.yaml".source = settingsFormat.generate "settings.yaml" cfg.settings;
215         "homepage-dashboard/widgets.yaml".source = settingsFormat.generate "widgets.yaml" cfg.widgets;
216       };
218       systemd.services.homepage-dashboard = {
219         description = "Homepage Dashboard";
220         after = [ "network.target" ];
221         wantedBy = [ "multi-user.target" ];
223         environment = {
224           HOMEPAGE_CONFIG_DIR = configDir;
225           HOMEPAGE_CACHE_DIR = "/var/cache/homepage-dashboard";
226           PORT = toString cfg.listenPort;
227           LOG_TARGETS = lib.mkIf managedConfig "stdout";
228         };
230         serviceConfig = {
231           Type = "simple";
232           DynamicUser = true;
233           EnvironmentFile = lib.mkIf (cfg.environmentFile != null) cfg.environmentFile;
234           StateDirectory = lib.mkIf (!managedConfig) "homepage-dashboard";
235           CacheDirectory = "homepage-dashboard";
236           ExecStart = lib.getExe cfg.package;
237           Restart = "on-failure";
238         };
239       };
241       networking.firewall = lib.mkIf cfg.openFirewall {
242         allowedTCPPorts = [ cfg.listenPort ];
243       };
244     };