Skip to content

Commit

Permalink
Improvements and amendaments (to be rebased/squashed)
Browse files Browse the repository at this point in the history
  • Loading branch information
avnik committed Mar 29, 2024
1 parent 6b216c2 commit b83ba41
Showing 1 changed file with 33 additions and 18 deletions.
51 changes: 33 additions & 18 deletions internal/nix/nix-darwin/multi.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ systemArgs@{ pkgs, config, lib, ... }:
let
inherit (lib) mkIf mkDefault types mkOption;
inherit (lib.strings) match;
topConfig = config;
literalDocBook = lib.literalDocBook or lib.literalExample;
literalExpression = lib.literalExpression or lib.literalExample;

Expand All @@ -16,7 +17,7 @@ let
command = "${config.package}/bin/hercules-ci-agent --config ${configFile}";
testCommand = "${command} --test-configuration";
suffix = if name == "" then "" else "-${name}";
user = if name == "" then "hercules-ci-agent" else "hci-${name}";
user = topConfig.users.${config.user};
in
{
options = {
Expand All @@ -36,6 +37,18 @@ let
type = types.str;
default = "_hercules-ci-agent";
};
group = mkOption {
type = types.str;
default = "_hercules-ci-agent";
};
logFile = mkOption {
type = types.str;
default = "/var/log/hci-agent${suffix}.log";
};
baseDirectory = mkOption {
type = types.str;
default = "/var/lib/hercules-ci-agent${if name == "" then "" else "/${name}"}"; # Almost a `suffix` logic, but yield subdir
};
} // makeSettingsOptions { cfg = config; opt = options; };
config = let cfg = config; in
{
Expand All @@ -44,23 +57,23 @@ let
packageOption = options.package;
inherit pkgs;
};
baseDirectory = "/var/lib/hercules-ci-agent${if name == "" then "" else "/${name}"}"; # Almost a `suffix` logic, but yield subdir
baseDirectory = cfg.baseDirectory;
nixUserIsTrusted = true;
labels =
let
mkIfNotNull = x: mkIf (x != null) x;
in
{
darwin.label = config.system.darwinLabel;
darwin.revision = config.system.darwinRevision;
darwin.version = config.system.darwinVersion;
darwin.nix.daemon = config.nix.useDaemon;
darwin.nix.sandbox = config.nix.settings.sandbox;
darwin.label = topConfig.system.darwinLabel;
darwin.revision = topConfig.system.darwinRevision;
darwin.version = topConfig.system.darwinVersion;
darwin.nix.daemon = topConfig.nix.useDaemon;
darwin.nix.sandbox = topConfig.nix.settings.sandbox;
};
};
systemConfig = { config, ... }: {
launchd.daemons.hercules-ci-agent = {
script = "exec ${cfg.package}/bin/hercules-ci-agent --config ${cfg.jsonFile}";
launchd.daemons."hci-agent${suffix}" = {
script = "exec ${command}";

path = [ config.nix.package ];
environment = {
Expand All @@ -71,21 +84,24 @@ let
serviceConfig.RunAtLoad = true;
serviceConfig.StandardErrorPath = cfg.logFile;
serviceConfig.StandardOutPath = cfg.logFile;
serviceConfig.GroupName = "_hercules-ci-agent";
serviceConfig.UserName = "_hercules-ci-agent";
serviceConfig.WorkingDirectory = user.home;
serviceConfig.GroupName = cfg.user;
serviceConfig.UserName = cfg.group;
serviceConfig.WorkingDirectory = cfg.baseDirectory;
serviceConfig.WatchPaths = [
cfg.settings.staticSecretsDirectory
];
};

nix.settings.trusted-users = [ cfg.user ];

system.activationScripts.preActivation.text = ''
touch '${cfg.logFile}'
chown ${toString user.uid}:${toString user.gid} '${cfg.logFile}'
if ! test -d ${cfg.baseDirectory}; then
mkdir -p ${cfg.baseDirectory}
chown ${toString user.uid}:${toString user.gid} ${cfg.baseDirectory}
fi
'';
# Trusted user allows simplified configuration and better performance
# when operating in a cluster.
nix.settings.trusted-users = [ config.systemd.services."hercules-ci-agent${suffix}".serviceConfig.User ];
};
};
};
Expand All @@ -107,7 +123,7 @@ in {
- Default base directory: `/var/lib/hercules-ci-agent`
Otherwise:
- User: `hci-''${name}`
- User: `hercules-ci-agent` (on darwin we use same username)
- Default base directory: `/var/lib/hercules-ci-agent-''${name}`
'';
};
Expand All @@ -117,8 +133,7 @@ in {
{
nix = mergeSub (c: c.nix);
launchd = mergeSub (c: c.launchd);
# FIXME: no per-agent user support
# users = mergeSub (c: c.users);
system = mergeSub (c: c.system);
}
{
nix.extraOptions = lib.mkIf (config.services.hercules-ci-agents != { }) ''
Expand Down

0 comments on commit b83ba41

Please sign in to comment.