nixos/services.cadvisor: remove with lib;

This commit is contained in:
Felix Buehler 2024-08-30 00:46:55 +02:00
parent 278fc7501c
commit 5ced735a89

View File

@ -1,55 +1,52 @@
{ config, pkgs, lib, ... }:
with lib;
let
cfg = config.services.cadvisor;
in {
options = {
services.cadvisor = {
enable = mkEnableOption "Cadvisor service";
enable = lib.mkEnableOption "Cadvisor service";
listenAddress = mkOption {
listenAddress = lib.mkOption {
default = "127.0.0.1";
type = types.str;
type = lib.types.str;
description = "Cadvisor listening host";
};
port = mkOption {
port = lib.mkOption {
default = 8080;
type = types.port;
type = lib.types.port;
description = "Cadvisor listening port";
};
storageDriver = mkOption {
storageDriver = lib.mkOption {
default = null;
type = types.nullOr types.str;
type = lib.types.nullOr lib.types.str;
example = "influxdb";
description = "Cadvisor storage driver.";
};
storageDriverHost = mkOption {
storageDriverHost = lib.mkOption {
default = "localhost:8086";
type = types.str;
type = lib.types.str;
description = "Cadvisor storage driver host.";
};
storageDriverDb = mkOption {
storageDriverDb = lib.mkOption {
default = "root";
type = types.str;
type = lib.types.str;
description = "Cadvisord storage driver database name.";
};
storageDriverUser = mkOption {
storageDriverUser = lib.mkOption {
default = "root";
type = types.str;
type = lib.types.str;
description = "Cadvisor storage driver username.";
};
storageDriverPassword = mkOption {
storageDriverPassword = lib.mkOption {
default = "root";
type = types.str;
type = lib.types.str;
description = ''
Cadvisor storage driver password.
@ -60,8 +57,8 @@ in {
'';
};
storageDriverPasswordFile = mkOption {
type = types.str;
storageDriverPasswordFile = lib.mkOption {
type = lib.types.str;
description = ''
File that contains the cadvisor storage driver password.
@ -75,14 +72,14 @@ in {
'';
};
storageDriverSecure = mkOption {
storageDriverSecure = lib.mkOption {
default = false;
type = types.bool;
type = lib.types.bool;
description = "Cadvisor storage driver, enable secure communication.";
};
extraOptions = mkOption {
type = types.listOf types.str;
extraOptions = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [];
description = ''
Additional cadvisor options.
@ -93,23 +90,23 @@ in {
};
};
config = mkMerge [
{ services.cadvisor.storageDriverPasswordFile = mkIf (cfg.storageDriverPassword != "") (
mkDefault (toString (pkgs.writeTextFile {
config = lib.mkMerge [
{ services.cadvisor.storageDriverPasswordFile = lib.mkIf (cfg.storageDriverPassword != "") (
lib.mkDefault (toString (pkgs.writeTextFile {
name = "cadvisor-storage-driver-password";
text = cfg.storageDriverPassword;
}))
);
}
(mkIf cfg.enable {
(lib.mkIf cfg.enable {
systemd.services.cadvisor = {
wantedBy = [ "multi-user.target" ];
after = [ "network.target" "docker.service" "influxdb.service" ];
path = optionals config.boot.zfs.enabled [ pkgs.zfs ];
path = lib.optionals config.boot.zfs.enabled [ pkgs.zfs ];
postStart = mkBefore ''
postStart = lib.mkBefore ''
until ${pkgs.curl.bin}/bin/curl -s -o /dev/null 'http://${cfg.listenAddress}:${toString cfg.port}/containers/'; do
sleep 1;
done
@ -120,14 +117,14 @@ in {
-logtostderr=true \
-listen_ip="${cfg.listenAddress}" \
-port="${toString cfg.port}" \
${escapeShellArgs cfg.extraOptions} \
${optionalString (cfg.storageDriver != null) ''
${lib.escapeShellArgs cfg.extraOptions} \
${lib.optionalString (cfg.storageDriver != null) ''
-storage_driver "${cfg.storageDriver}" \
-storage_driver_host "${cfg.storageDriverHost}" \
-storage_driver_db "${cfg.storageDriverDb}" \
-storage_driver_user "${cfg.storageDriverUser}" \
-storage_driver_password "$(cat "${cfg.storageDriverPasswordFile}")" \
${optionalString cfg.storageDriverSecure "-storage_driver_secure"}
${lib.optionalString cfg.storageDriverSecure "-storage_driver_secure"}
''}
'';