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"; networking.hostName = "mac-demarco-mini";
imports = [ (import ./gitea-actions-runner.nix) ]; imports = [ (import ./gitea-actions-runner.nix) ];
services.gitea-actions-runner = { services.gitea-actions-runner = {
user = "gitearunner"; user = "gitea-runner";
instances.nix = { instances.nix = {
enable = true; enable = true;
name = config.networking.hostName; name = config.networking.hostName;
@ -55,7 +55,9 @@
curl curl
direnv direnv
gawk gawk
just
git-lfs git-lfs
isort
gitFull gitFull
gnused gnused
ncdu ncdu
@ -70,7 +72,44 @@
launchd.daemons.gitea-runner-nix.serviceConfig.EnvironmentVariables = { launchd.daemons.gitea-runner-nix.serviceConfig.EnvironmentVariables = {
XDG_CONFIG_HOME = "/var/lib/gitea-runner"; XDG_CONFIG_HOME = "/var/lib/gitea-runner";
XDG_CACHE_HOME = "/var/lib/gitea-runner/.cache"; 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)]; nixpkgs.overlays = [(import ../nixos/overlay.nix)];
environment.systemPackages = with pkgs; [ environment.systemPackages = with pkgs; [
python-with-my-packages python-with-my-packages
@ -150,6 +189,7 @@
programs.starship = { programs.starship = {
enable = true; enable = true;
}; };
programs.zsh.enable = true;
home.stateVersion = "24.05"; home.stateVersion = "24.05";
}; };
}; };

View File

@ -1,8 +1,10 @@
{ config, lib, pkgs, ... }: {
config,
with lib; lib,
pkgs,
let ...
}:
with lib; let
cfg = config.services.gitea-actions-runner; cfg = config.services.gitea-actions-runner;
settingsFormat = pkgs.formats.yaml {}; settingsFormat = pkgs.formats.yaml {};
@ -14,9 +16,8 @@ let
hasHostScheme = instance: any (label: hasSuffix ":host" label) instance.labels; hasHostScheme = instance: any (label: hasSuffix ":host" label) instance.labels;
tokenXorTokenFile = instance: 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 { in {
options.services.gitea-actions-runner = { options.services.gitea-actions-runner = {
package = mkOption { package = mkOption {
@ -111,25 +112,49 @@ in {
description = "Gitea Actions Runner user"; description = "Gitea Actions Runner user";
}; };
launchd.daemons = mapAttrs' (name: instance: launchd.daemons =
(mapAttrs' (
name: instance:
nameValuePair "gitea-runner-${name}" { nameValuePair "gitea-runner-${name}" {
serviceConfig = { serviceConfig = {
ProgramArguments = [ ProgramArguments = [
"${pkgs.writeShellScript "gitea-runner-start-${name}" '' "/usr/bin/env"
echo "home is $HOME" "bash"
mkdir -p /var/log/gitea-runner/ "-c"
chown -R ${cfg.user} /var/log/gitea-runner ''
chmod 755 /var/log/gitea-runner 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} mkdir -p /var/lib/gitea-runner/${name}
chown -R ${cfg.user} /var/lib/gitea-runner cd /var/lib/gitea-runner/${name}
chmod 755 /var/lib/gitea-runner if [ ! -e "/var/lib/gitea-runner/${name}/.runner" ]; then
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 \ ${cfg.package}/bin/act_runner register --no-interactive \
--instance ${escapeShellArg instance.url} \ --instance ${escapeShellArg instance.url} \
--token "$TOKEN" \ --token "$TOKEN" \
@ -139,36 +164,13 @@ in {
fi fi
# Start the runner # Start the runner
exec ${cfg.package}/bin/act_runner daemon --config ${settingsFormat.generate "config.yaml" instance.settings} chown -R ${cfg.user} /var/lib/gitea-runner
chown -R ${cfg.user} /var/log/gitea-runner
''}" ''}"
]; ];
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; )
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
'';
};
}; };
} }