2020-04-24 23:36:52 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.services.k3s;
|
2022-06-26 10:26:21 +00:00
|
|
|
removeOption = config: instruction:
|
|
|
|
lib.mkRemovedOptionModule ([ "services" "k3s" ] ++ config) instruction;
|
2020-04-24 23:36:52 +00:00
|
|
|
in
|
|
|
|
{
|
2022-06-26 10:26:21 +00:00
|
|
|
imports = [
|
|
|
|
(removeOption [ "docker" ] "k3s docker option is no longer supported.")
|
|
|
|
];
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
# interface
|
|
|
|
options.services.k3s = {
|
|
|
|
enable = mkEnableOption "k3s";
|
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.k3s;
|
2021-10-06 13:57:05 +00:00
|
|
|
defaultText = literalExpression "pkgs.k3s";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Package that should be used for k3s";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
role = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Whether k3s should run as a server or agent.
|
|
|
|
Note that the server, by default, also runs as an agent.
|
|
|
|
'';
|
|
|
|
default = "server";
|
|
|
|
type = types.enum [ "server" "agent" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
serverAddr = mkOption {
|
|
|
|
type = types.str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "The k3s server to connect to. This option only makes sense for an agent.";
|
2020-04-24 23:36:52 +00:00
|
|
|
example = "https://10.0.0.10:6443";
|
|
|
|
default = "";
|
|
|
|
};
|
|
|
|
|
|
|
|
token = mkOption {
|
|
|
|
type = types.str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-07-16 19:40:57 +00:00
|
|
|
The k3s token to use when connecting to the server. This option only makes sense for an agent.
|
|
|
|
WARNING: This option will expose store your token unencrypted world-readable in the nix store.
|
|
|
|
If this is undesired use the tokenFile option instead.
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "";
|
|
|
|
};
|
|
|
|
|
2021-07-16 19:40:57 +00:00
|
|
|
tokenFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "File path containing k3s token to use when connecting to the server. This option only makes sense for an agent.";
|
2021-07-16 19:40:57 +00:00
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
extraFlags = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Extra flags to pass to the k3s command.";
|
2021-02-05 17:12:51 +00:00
|
|
|
type = types.str;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "";
|
|
|
|
example = "--no-deploy traefik --cluster-cidr 10.24.0.0/16";
|
|
|
|
};
|
|
|
|
|
|
|
|
disableAgent = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Only run the server. This option only makes sense for a server.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
2021-07-21 07:28:18 +00:00
|
|
|
|
|
|
|
configPath = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "File path containing the k3s YAML config. This is useful when the config is generated (for example on boot).";
|
2021-07-21 07:28:18 +00:00
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
# implementation
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
assertions = [
|
|
|
|
{
|
2021-07-21 07:28:18 +00:00
|
|
|
assertion = cfg.role == "agent" -> (cfg.configPath != null || cfg.serverAddr != "");
|
|
|
|
message = "serverAddr or configPath (with 'server' key) should be set if role is 'agent'";
|
2020-04-24 23:36:52 +00:00
|
|
|
}
|
|
|
|
{
|
2021-07-21 07:28:18 +00:00
|
|
|
assertion = cfg.role == "agent" -> cfg.configPath != null || cfg.tokenFile != null || cfg.token != "";
|
|
|
|
message = "token or tokenFile or configPath (with 'token' or 'token-file' keys) should be set if role is 'agent'";
|
2020-04-24 23:36:52 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2021-06-06 07:54:09 +00:00
|
|
|
environment.systemPackages = [ config.services.k3s.package ];
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
systemd.services.k3s = {
|
|
|
|
description = "k3s service";
|
2022-06-26 10:26:21 +00:00
|
|
|
after = [ "network.service" "firewall.service" ];
|
2021-04-15 00:37:46 +00:00
|
|
|
wants = [ "network.service" "firewall.service" ];
|
2020-04-24 23:36:52 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2021-06-06 07:54:09 +00:00
|
|
|
path = optional config.boot.zfs.enabled config.boot.zfs.package;
|
2020-04-24 23:36:52 +00:00
|
|
|
serviceConfig = {
|
2020-05-03 17:38:23 +00:00
|
|
|
# See: https://github.com/rancher/k3s/blob/dddbd16305284ae4bd14c0aade892412310d7edc/install.sh#L197
|
|
|
|
Type = if cfg.role == "agent" then "exec" else "notify";
|
2020-04-24 23:36:52 +00:00
|
|
|
KillMode = "process";
|
|
|
|
Delegate = "yes";
|
|
|
|
Restart = "always";
|
|
|
|
RestartSec = "5s";
|
2021-04-15 00:37:46 +00:00
|
|
|
LimitNOFILE = 1048576;
|
|
|
|
LimitNPROC = "infinity";
|
|
|
|
LimitCORE = "infinity";
|
|
|
|
TasksMax = "infinity";
|
2020-04-24 23:36:52 +00:00
|
|
|
ExecStart = concatStringsSep " \\\n " (
|
|
|
|
[
|
|
|
|
"${cfg.package}/bin/k3s ${cfg.role}"
|
2022-06-26 10:26:21 +00:00
|
|
|
]
|
2020-04-24 23:36:52 +00:00
|
|
|
++ (optional cfg.disableAgent "--disable-agent")
|
2021-07-21 07:28:18 +00:00
|
|
|
++ (optional (cfg.serverAddr != "") "--server ${cfg.serverAddr}")
|
|
|
|
++ (optional (cfg.token != "") "--token ${cfg.token}")
|
|
|
|
++ (optional (cfg.tokenFile != null) "--token-file ${cfg.tokenFile}")
|
|
|
|
++ (optional (cfg.configPath != null) "--config ${cfg.configPath}")
|
2020-04-24 23:36:52 +00:00
|
|
|
++ [ cfg.extraFlags ]
|
|
|
|
);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|