generated from nix-community/nur-packages-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpodman.nix
153 lines (150 loc) · 4.91 KB
/
podman.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
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
{
config,
lib,
pkgs,
...
}:
with lib;
let
cfg = config.services.prometheus.exporters.${name};
name = "podman";
port = 9882;
in
{
options.services.prometheus.exporters.${name} = {
enable = mkEnableOption (lib.mdDoc "the prometheus ${name} exporter");
port = mkOption {
type = types.port;
default = port;
description = lib.mdDoc ''
Port to listen on.
'';
};
listenAddress = mkOption {
type = types.str;
default = "0.0.0.0";
description = lib.mdDoc ''
Address to listen on.
'';
};
extraFlags = mkOption {
type = types.listOf types.str;
default = [ ];
description = lib.mdDoc ''
Extra commandline options to pass to the ${name} exporter.
'';
};
openFirewall = mkOption {
type = types.bool;
default = false;
description = lib.mdDoc ''
Open port in firewall for incoming connections.
'';
};
firewallFilter = mkOption {
type = types.nullOr types.str;
default = null;
example = literalExpression ''
"-i eth0 -p tcp -m tcp --dport ${toString port}"
'';
description = lib.mdDoc ''
Specify a filter for iptables to use when
{option}`services.prometheus.exporters.${name}.openFirewall`
is true. It is used as `ip46tables -I nixos-fw firewallFilter -j nixos-fw-accept`.
'';
};
user = mkOption {
type = types.str;
default = "root";
description = lib.mdDoc ''
User name under which the ${name} exporter shall be run.
'';
};
group = mkOption {
type = types.str;
default = "root";
description = lib.mdDoc ''
Group under which the ${name} exporter shall be run.
'';
};
enabledCollectors = mkOption {
type = types.listOf types.str;
default = [ ];
example = [ "pod" ];
description = lib.mdDoc ''
Collectors to enable. The collectors listed here are enabled in addition to the default ones.
'';
};
};
config =
let
containersStorage = config.virtualisation.containers.storage.settings.storage.graphroot;
in
mkIf cfg.enable {
virtualisation.podman.enable = true;
virtualisation.containers.containersConf.settings = {
engine.helper_binaries_dir = [ "${config.virtualisation.podman.package}/libexec/podman" ];
};
networking.firewall.extraCommands = mkIf cfg.openFirewall (concatStrings [
"ip46tables -A nixos-fw ${cfg.firewallFilter} "
"-m comment --comment ${name}-exporter -j nixos-fw-accept"
]);
systemd.services."prometheus-${name}-exporter" = {
description = "Prometheus ${name} exporter service";
wantedBy = [ "multi-user.target" ];
wants = [ "podman.socket" ];
after = [
"podman.socket"
"network.target"
];
path = with pkgs; [
runc
crun
conmon
];
environment.HOME = "/tmp";
serviceConfig.ExecStart = ''
${pkgs.prometheus-podman-exporter}/bin/prometheus-podman-exporter \
${concatMapStringsSep " " (x: "--collector." + x) cfg.enabledCollectors} \
--web.listen-address ${cfg.listenAddress}:${toString cfg.port} \
${concatStringsSep " " cfg.extraFlags}
'';
serviceConfig.ExecReload = "${pkgs.procps}/bin/kill -HUP $MAINPID";
serviceConfig.TimeoutStopSec = "20s";
serviceConfig.SendSIGKILL = "no";
serviceConfig.WorkingDirectory = "/tmp";
serviceConfig.Restart = "always";
serviceConfig.PrivateTmp = true;
serviceConfig.DynamicUser = false;
serviceConfig.User = cfg.user;
serviceConfig.Group = cfg.group;
# Hardening
serviceConfig.AmbientCapabilities = [ "CAP_SYS_ADMIN" ];
serviceConfig.CapabilityBoundingSet = [ "CAP_SYS_ADMIN" ];
serviceConfig.DeviceAllow = [ "" ];
serviceConfig.LockPersonality = true;
serviceConfig.MemoryDenyWriteExecute = true;
serviceConfig.NoNewPrivileges = true;
serviceConfig.PrivateDevices = true;
serviceConfig.ProtectClock = true;
serviceConfig.ProtectControlGroups = true;
serviceConfig.ProtectHome = true;
serviceConfig.ProtectHostname = true;
serviceConfig.ProtectKernelLogs = true;
serviceConfig.ProtectKernelModules = true;
serviceConfig.ProtectKernelTunables = true;
serviceConfig.ProtectSystem = "strict";
serviceConfig.ReadWritePaths = [ containersStorage ];
serviceConfig.RemoveIPC = true;
serviceConfig.RestrictAddressFamilies = [
"AF_INET"
"AF_INET6"
];
serviceConfig.RestrictNamespaces = true;
serviceConfig.RestrictRealtime = true;
serviceConfig.RestrictSUIDSGID = true;
serviceConfig.SystemCallArchitectures = "native";
serviceConfig.UMask = "0077";
};
};
}