yumeami/modules/nixos/services/ci/github-runner/default.nix

128 lines
3.5 KiB
Nix

{
config,
lib,
namespace,
pkgs,
...
}: let
inherit (lib) types mkEnableOption mkOption mkIf;
inherit (lib.${namespace}) getAttrByNamespace mkOptionsWithNamespace;
inherit (config.networking) hostName;
inherit (config.sops) secrets;
base = "${namespace}.services.ci.github-runner";
cfg = getAttrByNamespace config base;
nvdaCfg = getAttrByNamespace config "${namespace}.hardware.nvidia";
in {
options = with types;
mkOptionsWithNamespace base {
enable = mkEnableOption "Github self-hosted runner";
runners = mkOption {
description = "Definition of runners to enable to the device";
type = attrsOf (submodule {
options = {
instances = mkOption {
type = types.int;
default = 1;
description = "Number of instances of the runner to spawn for this configuration.";
};
tokenFile = mkOption {
type = nullOr path;
default = null;
description = "Path to the token file to utilize for authentication";
};
url = mkOption {
type = str;
default = "";
description = "URL of the repository for which to add the self-hosted runner";
};
};
});
default = [];
};
};
config = mkIf cfg.enable {
services.github-runners = let
inherit (config.users.users) github-runner;
inherit (lib) attrValues concatLists genList listToAttrs mapAttrs optional replicate;
inherit (builtins) stringLength concatStringsSep;
padIndex = idx: concatStringsSep "" (replicate (3 - stringLength (toString idx)) "0") + toString idx;
mkRunnerConfig = {
index,
name,
runner,
count,
}: {
name = "${hostName}-${name}-${padIndex index}";
value = {
enable = true;
name = "${hostName}-${padIndex index}";
replace = true;
ephemeral = true;
tokenFile =
if runner.tokenFile == null
then secrets."github/runner".path
else runner.tokenFile;
url = runner.url;
extraPackages = with pkgs; let
gtar = pkgs.runCommandNoCC "gtar" {} ''
mkdir -p $out/bin
ln -s ${lib.getExe pkgs.gnutar} $out/bin/gtar
'';
in [
nix
cachix
awscli2
coreutils
docker
gh
gtar
jq
nodejs_20
openssl
unzip
which
];
extraLabels = ["nix"] ++ optional nvdaCfg.enable "gpu";
user = github-runner.name;
group = github-runner.group;
};
};
in
cfg.runners
|> mapAttrs (name: runner:
genList (idx: {
index = idx;
name = name;
runner = runner;
})
runner.instances)
|> attrValues
|> concatLists
|> map mkRunnerConfig
|> listToAttrs;
users = {
users.github-runner = {
isSystemUser = true;
group = "github-runner";
extraGroups = ["docker" "podman"];
};
groups.github-runner = {};
};
sops.secrets = let
inherit (config.users.users) github-runner;
in {
"github/runner" = {
owner = github-runner.name;
group = github-runner.group;
};
};
${namespace}.services.storage.impermanence.folders = ["/var/lib/github-runner"];
};
}