about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/misc/homepage-dashboard.nix
blob: 02f1378cb0d59fc08da0b248c09121169729e6a6 (plain) (blame)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
{ config
, pkgs
, lib
, ...
}:

let
  cfg = config.services.homepage-dashboard;
  # Define the settings format used for this program
  settingsFormat = pkgs.formats.yaml { };
in
{
  options = {
    services.homepage-dashboard = {
      enable = lib.mkEnableOption (lib.mdDoc "Homepage Dashboard");

      package = lib.mkPackageOption pkgs "homepage-dashboard" { };

      openFirewall = lib.mkOption {
        type = lib.types.bool;
        default = false;
        description = lib.mdDoc "Open ports in the firewall for Homepage.";
      };

      listenPort = lib.mkOption {
        type = lib.types.int;
        default = 8082;
        description = lib.mdDoc "Port for Homepage to bind to.";
      };

      environmentFile = lib.mkOption {
        type = lib.types.str;
        description = ''
          The path to an environment file that contains environment variables to pass
          to the homepage-dashboard service, for the purpose of passing secrets to
          the service.

          See the upstream documentation:

          https://gethomepage.dev/latest/installation/docker/#using-environment-secrets
        '';
        default = "";
      };

      customCSS = lib.mkOption {
        type = lib.types.lines;
        description = lib.mdDoc ''
          Custom CSS for styling Homepage.

          See https://gethomepage.dev/latest/configs/custom-css-js/.
        '';
        default = "";
      };

      customJS = lib.mkOption {
        type = lib.types.lines;
        description = lib.mdDoc ''
          Custom Javascript for Homepage.

          See https://gethomepage.dev/latest/configs/custom-css-js/.
        '';
        default = "";
      };

      bookmarks = lib.mkOption {
        inherit (settingsFormat) type;
        description = lib.mdDoc ''
          Homepage bookmarks configuration.

          See https://gethomepage.dev/latest/configs/bookmarks/.
        '';
        # Defaults: https://github.com/gethomepage/homepage/blob/main/src/skeleton/bookmarks.yaml
        example = [
          {
            Developer = [
              { Github = [{ abbr = "GH"; href = "https://github.com/"; }]; }
            ];
          }
          {
            Entertainment = [
              { YouTube = [{ abbr = "YT"; href = "https://youtube.com/"; }]; }
            ];
          }
        ];
        default = [ ];
      };

      services = lib.mkOption {
        inherit (settingsFormat) type;
        description = lib.mdDoc ''
          Homepage services configuration.

          See https://gethomepage.dev/latest/configs/services/.
        '';
        # Defaults: https://github.com/gethomepage/homepage/blob/main/src/skeleton/services.yaml
        example = [
          {
            "My First Group" = [
              {
                "My First Service" = {
                  href = "http://localhost/";
                  description = "Homepage is awesome";
                };
              }
            ];
          }
          {
            "My Second Group" = [
              {
                "My Second Service" = {
                  href = "http://localhost/";
                  description = "Homepage is the best";
                };
              }
            ];
          }
        ];
        default = [ ];
      };

      widgets = lib.mkOption {
        inherit (settingsFormat) type;
        description = lib.mdDoc ''
          Homepage widgets configuration.

          See https://gethomepage.dev/latest/configs/service-widgets/.
        '';
        # Defaults: https://github.com/gethomepage/homepage/blob/main/src/skeleton/widgets.yaml
        example = [
          {
            resources = {
              cpu = true;
              memory = true;
              disk = "/";
            };
          }
          {
            search = {
              provider = "duckduckgo";
              target = "_blank";
            };
          }
        ];
        default = [ ];
      };

      kubernetes = lib.mkOption {
        inherit (settingsFormat) type;
        description = lib.mdDoc ''
          Homepage kubernetes configuration.

          See https://gethomepage.dev/latest/configs/kubernetes/.
        '';
        default = { };
      };

      docker = lib.mkOption {
        inherit (settingsFormat) type;
        description = lib.mdDoc ''
          Homepage docker configuration.

          See https://gethomepage.dev/latest/configs/docker/.
        '';
        default = { };
      };

      settings = lib.mkOption {
        inherit (settingsFormat) type;
        description = lib.mdDoc ''
          Homepage settings.

          See https://gethomepage.dev/latest/configs/settings/.
        '';
        # Defaults: https://github.com/gethomepage/homepage/blob/main/src/skeleton/settings.yaml
        default = { };
      };
    };
  };

  config =
    let
      # If homepage-dashboard is enabled, but none of the configuration values have been updated,
      # then default to "unmanaged" configuration which is manually updated in
      # var/lib/homepage-dashboard. This is to maintain backwards compatibility, and should be
      # deprecated in a future release.
      managedConfig = !(
        cfg.bookmarks == [ ] &&
        cfg.customCSS == "" &&
        cfg.customJS == "" &&
        cfg.docker == { } &&
        cfg.kubernetes == { } &&
        cfg.services == [ ] &&
        cfg.settings == { } &&
        cfg.widgets == [ ]
      );

      configDir = if managedConfig then "/etc/homepage-dashboard" else "/var/lib/homepage-dashboard";

      msg = "using unmanaged configuration for homepage-dashboard is deprecated and will be removed"
        + " in 24.05. please see the NixOS documentation for `services.homepage-dashboard' and add"
        + " your bookmarks, services, widgets, and other configuration using the options provided.";
    in
    lib.mkIf cfg.enable {
      warnings = lib.optional (!managedConfig) msg;

      environment.etc = lib.mkIf managedConfig {
        "homepage-dashboard/custom.css".text = cfg.customCSS;
        "homepage-dashboard/custom.js".text = cfg.customJS;

        "homepage-dashboard/bookmarks.yaml".source = settingsFormat.generate "bookmarks.yaml" cfg.bookmarks;
        "homepage-dashboard/docker.yaml".source = settingsFormat.generate "docker.yaml" cfg.docker;
        "homepage-dashboard/kubernetes.yaml".source = settingsFormat.generate "kubernetes.yaml" cfg.kubernetes;
        "homepage-dashboard/services.yaml".source = settingsFormat.generate "services.yaml" cfg.services;
        "homepage-dashboard/settings.yaml".source = settingsFormat.generate "settings.yaml" cfg.settings;
        "homepage-dashboard/widgets.yaml".source = settingsFormat.generate "widgets.yaml" cfg.widgets;
      };

      systemd.services.homepage-dashboard = {
        description = "Homepage Dashboard";
        after = [ "network.target" ];
        wantedBy = [ "multi-user.target" ];

        environment = {
          HOMEPAGE_CONFIG_DIR = configDir;
          PORT = toString cfg.listenPort;
          LOG_TARGETS = lib.mkIf managedConfig "stdout";
        };

        serviceConfig = {
          Type = "simple";
          DynamicUser = true;
          EnvironmentFile = lib.mkIf (cfg.environmentFile != null) cfg.environmentFile;
          StateDirectory = lib.mkIf (!managedConfig) "homepage-dashboard";
          ExecStart = lib.getExe cfg.package;
          Restart = "on-failure";
        };
      };

      networking.firewall = lib.mkIf cfg.openFirewall {
        allowedTCPPorts = [ cfg.listenPort ];
      };
    };
}