<commit_before>{ config, lib, pkgs, ... }:

with lib;
let
  cfg = config.services.k3s;
in
{
  # interface
  options.services.k3s = {
    enable = mkEnableOption "k3s";

    package = mkOption {
      type = types.package;
      default = pkgs.k3s;
      defaultText = "pkgs.k3s";
      example = literalExample "pkgs.k3s";
      description = "Package that should be used for k3s";
    };

    role = mkOption {
      description = ''
        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;
      description = "The k3s server to connect to. This option only makes sense for an agent.";
      example = "https://10.0.0.10:6443";
      default = "";
    };

    token = mkOption {
      type = types.str;
      description = "The k3s token to use when connecting to the server. This option only makes sense for an agent.";
      default = "";
    };

    docker = mkOption {
      type = types.bool;
      default = false;
      description = "Use docker to run containers rather than the built-in containerd.";
    };

    extraFlags = mkOption {
      description = "Extra flags to pass to the k3s command.";
      type = types.str;
      default = "";
      example = "--no-deploy traefik --cluster-cidr 10.24.0.0/16";
    };

    disableAgent = mkOption {
      type = types.bool;
      default = false;
      description = "Only run the server. This option only makes sense for a server.";
    };
  };

  # implementation

  config = mkIf cfg.enable {
    assertions = [
      {
        assertion = cfg.role == "agent" -> cfg.serverAddr != "";
        message = "serverAddr should be set if role is 'agent'";
      }
      {
        assertion = cfg.role == "agent" -> cfg.token != "";
        message = "token should be set if role is 'agent'";
      }
    ];

    virtualisation.docker = mkIf cfg.docker {
      enable = mkDefault true;
    };

    # TODO: disable this once k3s supports cgroupsv2, either by docker
    # supporting it, or their bundled containerd
    systemd.enableUnifiedCgroupHierarchy = false;

    systemd.services.k3s = {
      description = "k3s service";
      after = [ "network.service" "firewall.service" ] ++ (optional cfg.docker "docker.service");
      wants = [ "network.service" "firewall.service" ];
      wantedBy = [ "multi-user.target" ];
      serviceConfig = {
        # See: https://github.com/rancher/k3s/blob/dddbd16305284ae4bd14c0aade892412310d7edc/install.sh#L197
        Type = if cfg.role == "agent" then "exec" else "notify";
        KillMode = "process";
        Delegate = "yes";
        Restart = "always";
        RestartSec = "5s";
        LimitNOFILE = 1048576;
        LimitNPROC = "infinity";<commit_msg>nixos/k3s: add to environment.systemPackages for adminstration<commit_after>{ config, lib, pkgs, ... }:

with lib;
let
  cfg = config.services.k3s;
in
{
  # interface
  options.services.k3s = {
    enable = mkEnableOption "k3s";

    package = mkOption {
      type = types.package;
      default = pkgs.k3s;
      defaultText = "pkgs.k3s";
      example = literalExample "pkgs.k3s";
      description = "Package that should be used for k3s";
    };

    role = mkOption {
      description = ''
        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;
      description = "The k3s server to connect to. This option only makes sense for an agent.";
      example = "https://10.0.0.10:6443";
      default = "";
    };

    token = mkOption {
      type = types.str;
      description = "The k3s token to use when connecting to the server. This option only makes sense for an agent.";
      default = "";
    };

    docker = mkOption {
      type = types.bool;
      default = false;
      description = "Use docker to run containers rather than the built-in containerd.";
    };

    extraFlags = mkOption {
      description = "Extra flags to pass to the k3s command.";
      type = types.str;
      default = "";
      example = "--no-deploy traefik --cluster-cidr 10.24.0.0/16";
    };

    disableAgent = mkOption {
      type = types.bool;
      default = false;
      description = "Only run the server. This option only makes sense for a server.";
    };
  };

  # implementation

  config = mkIf cfg.enable {
    assertions = [
      {
        assertion = cfg.role == "agent" -> cfg.serverAddr != "";
        message = "serverAddr should be set if role is 'agent'";
      }
      {
        assertion = cfg.role == "agent" -> cfg.token != "";
        message = "token should be set if role is 'agent'";
      }
    ];

    virtualisation.docker = mkIf cfg.docker {
      enable = mkDefault true;
    };

    # TODO: disable this once k3s supports cgroupsv2, either by docker
    # supporting it, or their bundled containerd
    systemd.enableUnifiedCgroupHierarchy = false;

    environment.systemPackages = [ config.services.k3s.package ];

    systemd.services.k3s = {
      description = "k3s service";
      after = [ "network.service" "firewall.service" ] ++ (optional cfg.docker "docker.service");
      wants = [ "network.service" "firewall.service" ];
      wantedBy = [ "multi-user.target" ];
      serviceConfig = {
        # See: https://github.com/rancher/k3s/blob/dddbd16305284ae4bd14c0aade892412310d7edc/install.sh#L197
        Type = if cfg.role == "agent" then "exec" else "notify";
        KillMode = "process";
        Delegate = "yes";
        Restart = "always";
        RestartSec = "5s";
        LimitNOFILE = 1048576;
        LimitNPROC = "infinity";
{'fraction_non_alphanumeric': 0.11978810663021189, 'fraction_numerical': 0.02323991797676008, 'mean_word_length': 2.2516666666666665, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2359132', 'n_tokens_mistral': 1877, 'n_tokens_neox': 1772, 'n_words': 520}