nixos-servers/nixos-modules/k3s/default.nix

57 lines
1.6 KiB
Nix
Raw Normal View History

2024-03-19 21:56:52 +00:00
{ pkgs, lib, config, kubenix, ... }:
let cfg = config.lab.k3s;
in {
2024-04-15 21:28:23 +00:00
options.lab.k3s = {
enable = lib.mkOption {
default = false;
type = lib.types.bool;
description = ''
Whether to run k3s on this server.
'';
};
serverAddr = lib.mkOption {
default = null;
type = with lib.types; nullOr str;
description = ''
Address of the server whose cluster this server should join.
Leaving this empty will make the server initialize the cluster.
'';
};
2024-03-19 21:56:52 +00:00
};
config = lib.mkIf cfg.enable {
environment.systemPackages = with pkgs; [ k3s ];
networking = {
nftables.enable = lib.mkForce false;
firewall.enable = lib.mkForce false;
};
services.k3s = {
enable = true;
role = "server";
2024-04-15 21:28:23 +00:00
tokenFile = config.age.secrets.k3s-server-token.path;
extraFlags = "--tls-san ${config.networking.fqdn} --disable servicelb";
2024-04-15 21:28:23 +00:00
clusterInit = cfg.serverAddr == null;
serverAddr = lib.mkIf (! (cfg.serverAddr == null)) cfg.serverAddr;
2024-03-19 21:56:52 +00:00
};
2024-04-15 21:28:23 +00:00
system = lib.mkIf (cfg.serverAddr == null) {
activationScripts.k3s-bootstrap.text = (
let
k3sBootstrapFile = (kubenix.evalModules.x86_64-linux {
module = import ./bootstrap.nix;
}).config.kubernetes.result;
in
''
mkdir -p /var/lib/rancher/k3s/server/manifests
ln -sf ${k3sBootstrapFile} /var/lib/rancher/k3s/server/manifests/k3s-bootstrap.json
''
);
};
age.secrets.k3s-server-token.file = ../../secrets/k3s-server-token.age;
2024-03-19 21:56:52 +00:00
};
}