@@ -0,0 +1,106 @@
1+{ config, pkgs, lib, ... }:
2+
3+with lib;
4+
5+let
6+ cfg = config.services.cadvisor;
7+
8+in {
9+ options = {
10+ services.cadvisor = {
11+ enable = mkOption {
12+ default = false;
13+ type = types.bool;
14+ description = "Wherther to enable cadvisor service.";
15+ };
16+
17+ host = mkOption {
18+ default = "127.0.0.1";
19+ type = types.str;
20+ description = "Cadvisor listening host";
21+ };
22+
23+ port = mkOption {
24+ default = 8080;
25+ type = types.int;
26+ description = "Cadvisor listening port";
27+ };
28+
29+ storageDriver = mkOption {
30+ default = null;
31+ type = types.nullOr types.str;
32+ example = "influxdb";
33+ description = "Cadvisor storage driver.";
34+ };
35+
36+ storageDriverHost = mkOption {
37+ default = "localhost:8086";
38+ type = types.str;
39+ description = "Cadvisor storage driver host.";
40+ };
41+
42+ storageDriverDb = mkOption {
43+ default = "root";
44+ type = types.str;
45+ description = "Cadvisord storage driver database name.";
46+ };
47+
48+ storageDriverUser = mkOption {
49+ default = "root";
50+ type = types.str;
51+ description = "Cadvisor storage driver username.";
52+ };
53+
54+ storageDriverPassword = mkOption {
55+ default = "root";
56+ type = types.str;
57+ description = "Cadvisor storage driver password.";
58+ };
59+
60+ storageDriverSecure = mkOption {
61+ default = false;
62+ type = types.bool;
63+ description = "Cadvisor storage driver, enable secure communication.";
64+ };
65+ };
66+ };
67+
68+ config = mkIf cfg.enable {
69+ systemd.services.cadvisor = {
70+ wantedBy = [ "multi-user.target" ];
71+ after = [ "network.target" "docker.service" "influxdb.service" ];
72+
73+ postStart = mkBefore ''
74+ until ${pkgs.curl}/bin/curl -s -o /dev/null 'http://${cfg.host}:${toString cfg.port}/containers/'; do
75+ sleep 1;
76+ done
77+ '';
78+
79+ serviceConfig = {
80+ ExecStart = ''${pkgs.cadvisor}/bin/cadvisor \
81+ -logtostderr=true \
82+ -listen_ip=${cfg.host} \
83+ -port=${toString cfg.port} \
84+ ${optionalString (cfg.storageDriver != null) ''
85+ -storage_driver ${cfg.storageDriver} \
86+ -storage_driver_user ${cfg.storageDriverHost} \
87+ -storage_driver_db ${cfg.storageDriverDb} \
88+ -storage_driver_user ${cfg.storageDriverUser} \
89+ -storage_driver_password ${cfg.storageDriverPassword} \
90+ ${optionalString cfg.storageDriverSecure "-storage_driver_secure"}
91+ ''}
92+ '';
93+ User = "cadvisor";
94+ };
95+ };
96+
97+ virtualisation.docker.enable = true;
98+
99+ users.extraUsers = singleton {
100+ name = "cadvisor";
101+ uid = config.ids.uids.cadvisor;
102+ description = "Cadvisor user";
103+ extraGroups = [ "docker" ];
104+ };
105+ };
106+}