Skip to content

Commit

Permalink
generalize
Browse files Browse the repository at this point in the history
  • Loading branch information
srid committed Jun 21, 2024
1 parent 64c914e commit 29c70e3
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 31 deletions.
4 changes: 4 additions & 0 deletions example/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
inputs.github-ci-nix.darwinModules.default
{
nixpkgs.hostPlatform = "aarch64-darwin";
# TODO: Add this, and add fake ssh keys for agenix to work during nix build.
#github-ci-nix = {
# orgRunners.juspay.num = 2;
#};
services.nix-daemon.enable = true;
}
];
Expand Down
109 changes: 78 additions & 31 deletions nix/module.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{ pkgs, lib, config, ... }:
top@{ pkgs, lib, config, ... }:

let
inherit (pkgs.stdenv) isLinux;
inherit (lib) types;

host = builtins.toString config.networking.hostName;
# The list of systems that this host can build for.
Expand All @@ -16,6 +17,7 @@ let
in
lib.unique ([ host-system ] ++ extra-systems);
for = lib.flip builtins.map;
forAttr = lib.flip lib.mapAttrsToList;
# For input n, return [1..n]
range =
lib.genList (i: i + 1);
Expand Down Expand Up @@ -67,41 +69,86 @@ let
user = "github-runner";
group = "github-runner";

# Create 'num' runners for the given GitHub org
mkOrgRunners = { orgName, num }:
lib.listToAttrs (for (range num)
(n: {
name = "${host}-${orgName}-${paddedNum n}";
value = common // {
tokenFile = config.age.secrets."github-runner-tokens/${orgName}".path;
url = "https://github.com/${orgName}";
};
})
);

# Like mkOrgRunner but for personal repos
mkPersonalRunners = { user, repo, num ? 1 }:
lib.listToAttrs (for (range num)
(n: {
name = "${host}-${user}-${repo}-${paddedNum n}";
value = common // {
tokenFile = config.age.secrets."github-runner-tokens/${user}".path;
url = "https://github.com/${user}/${repo}";
};
})
);
in
{
options = { };
options = {
github-ci-nix = lib.mkOption {
type = types.submodule {
options = {
orgRunners = lib.mkOption {
type = types.attrsOf (types.submodule ({ config, name, ... }: {
options = {
num = lib.mkOption {
type = types.int;
};

output.name = lib.mkOption {
type = types.str;
default = "${host}-${name}-${paddedNum config.num}";
};
output.runner = lib.mkOption {
type = types.raw;
default = common // {
tokenFile = top.config.age.secrets."github-runner-tokens/${name}".path;
url = "https://github.com/${name}";
};
};
};
}));
default = { };
};

personalRunners = lib.mkOption {
type = types.attrsOf (types.submodule ({ config, name, ... }: {
options = {
num = lib.mkOption {
type = types.int;
};

output.user = lib.mkOption {
type = types.str;
default =
let parts = lib.splitString "/" name;
in if lib.length parts == 2 then builtins.elemAt parts 0 else builtins.abort "Invalid user/repo";
};
output.repo = lib.mkOption {
type = types.str;
default =
let parts = lib.splitString "/" name;
in if lib.length parts == 2 then builtins.elemAt parts 1 else builtins.abort "Invalid user/repo";
};

output.name = lib.mkOption {
type = types.str;
default = "${host}-${config.output.user}-${config.output.repo}-${paddedNum config.num}";
};
output.runner = lib.mkOption {
type = types.raw;
default = common // {
tokenFile = top.config.age.secrets."github-runner-tokens/${config.output.user}".path;
url = "https://github.com/${config.output.user}";
};
};
};
}));
default = { };
};
};
};
default = { };
};
};
config = {
# Each org gets its own set of runners. There will be at max `num` parallels
# CI builds for this org / host combination.
services.github-runners = lib.mkMerge [
# Example: org runners
# (mkOrgRunners { orgName = "juspay"; num = 10; })
# Example: personal runners
# (mkPersonalRunners { user = "srid"; repo = "emanote"; })
];
services.github-runners = lib.listToAttrs
(forAttr config.github-ci-nix.orgRunners
(name: cfg:
lib.nameValuePair cfg.output.name cfg.output.runner)
++
forAttr config.github-ci-nix.personalRunners (name: cfg:
lib.nameValuePair cfg.output.name cfg.output.runner)
);

# User (Linux only)
users.users.${user} = lib.mkIf isLinux {
Expand Down

0 comments on commit 29c70e3

Please sign in to comment.