about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/monitoring/cockpit.nix
blob: 64e26ce4e127da2ebd069ec7e0b9164267de9952 (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
{ pkgs, config, lib, ... }:

let
  cfg = config.services.cockpit;
  inherit (lib) types mkEnableOption mkOption mkIf literalMD mkPackageOption;
  settingsFormat = pkgs.formats.ini {};
in {
  options = {
    services.cockpit = {
      enable = mkEnableOption "Cockpit";

      package = mkPackageOption pkgs "Cockpit" {
        default = [ "cockpit" ];
      };

      settings = lib.mkOption {
        type = settingsFormat.type;

        default = {};

        description = ''
          Settings for cockpit that will be saved in /etc/cockpit/cockpit.conf.

          See the [documentation](https://cockpit-project.org/guide/latest/cockpit.conf.5.html), that is also available with `man cockpit.conf.5` for details.
        '';
      };

      port = mkOption {
        description = "Port where cockpit will listen.";
        type = types.port;
        default = 9090;
      };

      openFirewall = mkOption {
        description = "Open port for cockpit.";
        type = types.bool;
        default = false;
      };
    };
  };
  config = mkIf cfg.enable {

    # expose cockpit-bridge system-wide
    environment.systemPackages = [ cfg.package ];

    # allow cockpit to find its plugins
    environment.pathsToLink = [ "/share/cockpit" ];

    # generate cockpit settings
    environment.etc."cockpit/cockpit.conf".source = settingsFormat.generate "cockpit.conf" cfg.settings;

    security.pam.services.cockpit = {};

    networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.port ];

    # units are in reverse sort order if you ls $out/lib/systemd/system
    # all these units are basically verbatim translated from upstream

    # Translation from $out/lib/systemd/system/systemd-cockpithttps.slice
    systemd.slices.system-cockpithttps = {
      description = "Resource limits for all cockpit-ws-https@.service instances";
      sliceConfig = {
        TasksMax = 200;
        MemoryHigh = "75%";
        MemoryMax = "90%";
      };
    };

    # Translation from $out/lib/systemd/system/cockpit-wsinstance-https@.socket
    systemd.sockets."cockpit-wsinstance-https@" = {
      unitConfig = {
        Description = "Socket for Cockpit Web Service https instance %I";
        BindsTo = [ "cockpit.service" "cockpit-wsinstance-https@%i.service" ];
        # clean up the socket after the service exits, to prevent fd leak
        # this also effectively prevents a DoS by starting arbitrarily many sockets, as
        # the services are resource-limited by system-cockpithttps.slice
        Documentation = "man:cockpit-ws(8)";
      };
      socketConfig = {
        ListenStream = "/run/cockpit/wsinstance/https@%i.sock";
        SocketUser = "root";
        SocketMode = "0600";
      };
    };

    # Translation from $out/lib/systemd/system/cockpit-wsinstance-https@.service
    systemd.services."cockpit-wsinstance-https@" = {
      description = "Cockpit Web Service https instance %I";
      bindsTo = [ "cockpit.service"];
      path = [ cfg.package ];
      documentation = [ "man:cockpit-ws(8)" ];
      serviceConfig = {
        Slice = "system-cockpithttps.slice";
        ExecStart = "${cfg.package}/libexec/cockpit-ws --for-tls-proxy --port=0";
        User = "root";
        Group = "";
      };
    };

    # Translation from $out/lib/systemd/system/cockpit-wsinstance-http.socket
    systemd.sockets.cockpit-wsinstance-http = {
      unitConfig = {
        Description = "Socket for Cockpit Web Service http instance";
        BindsTo = "cockpit.service";
        Documentation = "man:cockpit-ws(8)";
      };
      socketConfig = {
        ListenStream = "/run/cockpit/wsinstance/http.sock";
        SocketUser = "root";
        SocketMode = "0600";
      };
    };

    # Translation from $out/lib/systemd/system/cockpit-wsinstance-https-factory.socket
    systemd.sockets.cockpit-wsinstance-https-factory = {
      unitConfig = {
        Description = "Socket for Cockpit Web Service https instance factory";
        BindsTo = "cockpit.service";
        Documentation = "man:cockpit-ws(8)";
      };
      socketConfig = {
        ListenStream = "/run/cockpit/wsinstance/https-factory.sock";
        Accept = true;
        SocketUser = "root";
        SocketMode = "0600";
      };
    };

    # Translation from $out/lib/systemd/system/cockpit-wsinstance-https-factory@.service
    systemd.services."cockpit-wsinstance-https-factory@" = {
      description = "Cockpit Web Service https instance factory";
      documentation = [ "man:cockpit-ws(8)" ];
      path = [ cfg.package ];
      serviceConfig = {
        ExecStart = "${cfg.package}/libexec/cockpit-wsinstance-factory";
        User = "root";
      };
    };

    # Translation from $out/lib/systemd/system/cockpit-wsinstance-http.service
    systemd.services."cockpit-wsinstance-http" = {
      description = "Cockpit Web Service http instance";
      bindsTo = [ "cockpit.service" ];
      path = [ cfg.package ];
      documentation = [ "man:cockpit-ws(8)" ];
      serviceConfig = {
        ExecStart = "${cfg.package}/libexec/cockpit-ws --no-tls --port=0";
        User = "root";
        Group = "";
      };
    };

    # Translation from $out/lib/systemd/system/cockpit.socket
    systemd.sockets."cockpit" = {
      unitConfig = {
        Description = "Cockpit Web Service Socket";
        Documentation = "man:cockpit-ws(8)";
        Wants = "cockpit-motd.service";
      };
      socketConfig = {
        ListenStream = cfg.port;
        ExecStartPost = [
          "-${cfg.package}/share/cockpit/motd/update-motd \"\" localhost"
          "-${pkgs.coreutils}/bin/ln -snf active.motd /run/cockpit/motd"
        ];
        ExecStopPost = "-${pkgs.coreutils}/bin/ln -snf inactive.motd /run/cockpit/motd";
      };
      wantedBy = [ "sockets.target" ];
    };

    # Translation from $out/lib/systemd/system/cockpit.service
    systemd.services."cockpit" = {
      description = "Cockpit Web Service";
      documentation = [ "man:cockpit-ws(8)" ];
      restartIfChanged = true;
      path = with pkgs; [ coreutils cfg.package ];
      requires = [ "cockpit.socket" "cockpit-wsinstance-http.socket" "cockpit-wsinstance-https-factory.socket" ];
      after = [ "cockpit-wsinstance-http.socket" "cockpit-wsinstance-https-factory.socket" ];
      environment = {
        G_MESSAGES_DEBUG = "cockpit-ws,cockpit-bridge";
      };
      serviceConfig = {
        RuntimeDirectory="cockpit/tls";
        ExecStartPre = [
          # cockpit-tls runs in a more constrained environment, these + means that these commands
          # will run with full privilege instead of inside that constrained environment
          # See https://www.freedesktop.org/software/systemd/man/systemd.service.html#ExecStart= for details
          "+${cfg.package}/libexec/cockpit-certificate-ensure --for-cockpit-tls"
        ];
        ExecStart = "${cfg.package}/libexec/cockpit-tls";
        User = "root";
        Group = "";
        NoNewPrivileges = true;
        ProtectSystem = "strict";
        ProtectHome = true;
        PrivateTmp = true;
        PrivateDevices = true;
        ProtectKernelTunables = true;
        RestrictAddressFamilies = [ "AF_UNIX" "AF_INET" "AF_INET6" ];
        MemoryDenyWriteExecute = true;
      };
    };

    # Translation from $out/lib/systemd/system/cockpit-motd.service
    # This part basically implements a motd state machine:
    # - If cockpit.socket is enabled then /run/cockpit/motd points to /run/cockpit/active.motd
    # - If cockpit.socket is disabled then /run/cockpit/motd points to /run/cockpit/inactive.motd
    # - As cockpit.socket is disabled by default, /run/cockpit/motd points to /run/cockpit/inactive.motd
    # /run/cockpit/active.motd is generated dynamically by cockpit-motd.service
    systemd.services."cockpit-motd" = {
      path = with pkgs; [ nettools ];
      serviceConfig = {
        Type = "oneshot";
        ExecStart = "${cfg.package}/share/cockpit/motd/update-motd";
      };
      description = "Cockpit motd updater service";
      documentation = [ "man:cockpit-ws(8)" ];
      wants = [ "network.target" ];
      after = [ "network.target" "cockpit.socket" ];
    };

    systemd.tmpfiles.rules = [ # From $out/lib/tmpfiles.d/cockpit-tmpfiles.conf
      "C /run/cockpit/inactive.motd 0640 root root - ${cfg.package}/share/cockpit/motd/inactive.motd"
      "f /run/cockpit/active.motd   0640 root root -"
      "L+ /run/cockpit/motd - - - - inactive.motd"
      "d /etc/cockpit/ws-certs.d 0600 root root 0"
    ];
  };

  meta.maintainers = pkgs.cockpit.meta.maintainers;
}