Fix actions runner in macos

This commit is contained in:
Kat Huang 2024-10-08 23:23:35 -06:00
parent 9f3f835253
commit 1d31f870c5
2 changed files with 117 additions and 75 deletions

View File

@ -30,7 +30,7 @@
networking.hostName = "mac-demarco-mini";
imports = [ (import ./gitea-actions-runner.nix) ];
services.gitea-actions-runner = {
user = "gitearunner";
user = "gitea-runner";
instances.nix = {
enable = true;
name = config.networking.hostName;
@ -55,7 +55,9 @@
curl
direnv
gawk
just
git-lfs
isort
gitFull
gnused
ncdu
@ -70,7 +72,44 @@
launchd.daemons.gitea-runner-nix.serviceConfig.EnvironmentVariables = {
XDG_CONFIG_HOME = "/var/lib/gitea-runner";
XDG_CACHE_HOME = "/var/lib/gitea-runner/.cache";
XDG_RUNTIME_DIR = "/var/lib/gitea-runner/tmp";
};
# launchd.daemons.gitea-runner-restarter = {
# serviceConfig = {
# ProgramArguments = [
# "/usr/bin/env"
# "bash"
# "-c"
# ''
# SERVICE_NAME="org.nixos.gitea-runner-nix"
# while true; do
# # Check the second column of launchctl list output for our service
# EXIT_CODE=$(sudo launchctl list | grep "$SERVICE_NAME" | awk '{print $2}')
# if [ -z "$EXIT_CODE" ]; then
# echo "$(date): $SERVICE_NAME is running correctly. Terminating the restarter."
# exit 0
# else
# echo "$(date): $SERVICE_NAME is not running or in error state. Attempting to restart..."
# sudo launchctl bootout system/$SERVICE_NAME 2>/dev/null || true
# sudo launchctl load /Library/LaunchDaemons/$SERVICE_NAME.plist
# sleep 2 # Give the service some time to start
# fi
# done
# ''
# ];
# RunAtLoad = true;
# ThrottleInterval = 300;
# };
# };
launchd.daemons.does-anything-work = {
serviceConfig = {
ProgramArguments = ["/usr/bin/env" "bash" "-c" "date > /var/log/does-anything-work"];
RunAtLoad = true;
};
};
nixpkgs.overlays = [(import ../nixos/overlay.nix)];
environment.systemPackages = with pkgs; [
python-with-my-packages
@ -113,11 +152,11 @@
nixpkgs.hostPlatform = "aarch64-darwin";
users.users.kat.openssh.authorizedKeys.keys = inputs.railbird-secrets.keys.kanivanKeys;
users.users.gitea-runner = {
name = "gitea-runner";
isHidden = false;
home = "/Users/gitea-runner";
createHome = false;
};
name = "gitea-runner";
isHidden = false;
home = "/Users/gitea-runner";
createHome = false;
};
home-manager.useGlobalPkgs = true; home-manager.useUserPackages = true;
@ -150,6 +189,7 @@
programs.starship = {
enable = true;
};
programs.zsh.enable = true;
home.stateVersion = "24.05";
};
};

View File

@ -1,11 +1,13 @@
{ config, lib, pkgs, ... }:
with lib;
let
{
config,
lib,
pkgs,
...
}:
with lib; let
cfg = config.services.gitea-actions-runner;
settingsFormat = pkgs.formats.yaml { };
settingsFormat = pkgs.formats.yaml {};
hasDockerScheme = instance:
instance.labels == [] || any (label: hasInfix ":docker:" label) instance.labels;
@ -14,9 +16,8 @@ let
hasHostScheme = instance: any (label: hasSuffix ":host" label) instance.labels;
tokenXorTokenFile = instance:
(instance.token == null && instance.tokenFile != null) ||
(instance.token != null && instance.tokenFile == null);
(instance.token == null && instance.tokenFile != null)
|| (instance.token != null && instance.tokenFile == null);
in {
options.services.gitea-actions-runner = {
package = mkOption {
@ -66,7 +67,7 @@ in {
labels = mkOption {
type = types.listOf types.str;
default = [];
example = [ "macos:host" "x86_64:host" ];
example = ["macos:host" "x86_64:host"];
description = "Labels used to map jobs to their runtime environment.";
};
@ -75,7 +76,7 @@ in {
type = types.submodule {
freeformType = settingsFormat.type;
};
default = { };
default = {};
};
hostPackages = mkOption {
@ -111,64 +112,65 @@ in {
description = "Gitea Actions Runner user";
};
launchd.daemons = mapAttrs' (name: instance:
nameValuePair "gitea-runner-${name}" {
serviceConfig = {
ProgramArguments = [
"${pkgs.writeShellScript "gitea-runner-start-${name}" ''
echo "home is $HOME"
mkdir -p /var/log/gitea-runner/
chown -R ${cfg.user} /var/log/gitea-runner
chmod 755 /var/log/gitea-runner
launchd.daemons =
(mapAttrs' (
name: instance:
nameValuePair "gitea-runner-${name}" {
serviceConfig = {
ProgramArguments = [
"/usr/bin/env"
"bash"
"-c"
''
cd /var/lib/gitea-runner/${name}
exec ${cfg.package}/bin/act_runner daemon --config ${settingsFormat.generate "config.yaml" instance.settings}
''
];
KeepAlive = true;
ThrottleInterval = 5;
SessionCreate = true;
UserName = cfg.user;
GroupName = "staff";
WorkingDirectory = "/var/lib/gitea-runner/${name}";
EnvironmentVariables = {
PATH = (lib.makeBinPath (instance.hostPackages ++ [cfg.package])) + ":/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin";
};
};
}
)
cfg.instances)
// (mapAttrs' (
name: instance:
nameValuePair "gitea-runner-setup-${name}"
{
serviceConfig = {
EnvironmentVariables =
{}
// optionalAttrs (instance.token != null) {
TOKEN = instance.token;
};
RunAtLoad = true;
ProgramArguments = [
"${pkgs.writeShellScript "gitea-runner-setup-${name}" ''
mkdir -p /var/lib/gitea-runner/${name}
cd /var/lib/gitea-runner/${name}
if [ ! -e "/var/lib/gitea-runner/${name}/.runner" ]; then
${cfg.package}/bin/act_runner register --no-interactive \
--instance ${escapeShellArg instance.url} \
--token "$TOKEN" \
--name ${escapeShellArg instance.name} \
--labels ${escapeShellArg (concatStringsSep "," instance.labels)} \
--config ${settingsFormat.generate "config.yaml" instance.settings}
fi
mkdir -p /var/lib/gitea-runner/${name}
chown -R ${cfg.user} /var/lib/gitea-runner
chmod 755 /var/lib/gitea-runner
sudo su - ${cfg.user}
echo "STARTING"
# Register the runner if not already registered
if [ ! -e "$HOME/.runner" ]; then
${cfg.package}/bin/act_runner register --no-interactive \
--instance ${escapeShellArg instance.url} \
--token "$TOKEN" \
--name ${escapeShellArg instance.name} \
--labels ${escapeShellArg (concatStringsSep "," instance.labels)} \
--config ${settingsFormat.generate "config.yaml" instance.settings}
fi
# Start the runner
exec ${cfg.package}/bin/act_runner daemon --config ${settingsFormat.generate "config.yaml" instance.settings}
''}"
];
KeepAlive = true;
RunAtLoad = true;
SessionCreate = true;
UserName = cfg.user;
GroupName = "staff";
WorkingDirectory = "/var/lib/gitea-runner/${name}";
EnvironmentVariables = {
PATH = (lib.makeBinPath (instance.hostPackages ++ [ cfg.package ])) + ":/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin";
} // optionalAttrs (instance.token != null) {
TOKEN = instance.token;
};
} // optionalAttrs (instance.tokenFile != null) {
EnvironmentVariables.__TokenFile = instance.tokenFile;
};
}
) cfg.instances;
system.activationScripts.gitea-runner-setup = {
text = ''
mkdir -p /var/log/gitea-runner/
mkdir -p /var/lib/gitea-runner/${name}
chown -R ${cfg.user} /var/log/gitea-runner
chmod 755 /var/log/gitea-runner
chown -R ${cfg.user} /var/lib/gitea-runner
chmod 755 /var/lib/gitea-runner
'';
};
# Start the runner
chown -R ${cfg.user} /var/lib/gitea-runner
chown -R ${cfg.user} /var/log/gitea-runner
''}"
];
};
}
)
cfg.instances);
};
}