Skip to content

Commit 4ea7a7c

Browse files
committed
Improvements and amendaments (to be rebased/squashed)
1 parent 6b216c2 commit 4ea7a7c

1 file changed

Lines changed: 33 additions & 18 deletions

File tree

internal/nix/nix-darwin/multi.nix

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ systemArgs@{ pkgs, config, lib, ... }:
66
let
77
inherit (lib) mkIf mkDefault types mkOption;
88
inherit (lib.strings) match;
9+
topConfig = config;
910
literalDocBook = lib.literalDocBook or lib.literalExample;
1011
literalExpression = lib.literalExpression or lib.literalExample;
1112

@@ -16,7 +17,7 @@ let
1617
command = "${config.package}/bin/hercules-ci-agent --config ${configFile}";
1718
testCommand = "${command} --test-configuration";
1819
suffix = if name == "" then "" else "-${name}";
19-
user = if name == "" then "hercules-ci-agent" else "hci-${name}";
20+
user = topConfig.users.users.${config.user};
2021
in
2122
{
2223
options = {
@@ -36,6 +37,18 @@ let
3637
type = types.str;
3738
default = "_hercules-ci-agent";
3839
};
40+
group = mkOption {
41+
type = types.str;
42+
default = "_hercules-ci-agent";
43+
};
44+
logFile = mkOption {
45+
type = types.str;
46+
default = "/var/log/hci-agent${suffix}.log";
47+
};
48+
baseDirectory = mkOption {
49+
type = types.str;
50+
default = "/var/lib/hercules-ci-agent${if name == "" then "" else "/${name}"}"; # Almost a `suffix` logic, but yield subdir
51+
};
3952
} // makeSettingsOptions { cfg = config; opt = options; };
4053
config = let cfg = config; in
4154
{
@@ -44,23 +57,23 @@ let
4457
packageOption = options.package;
4558
inherit pkgs;
4659
};
47-
baseDirectory = "/var/lib/hercules-ci-agent${if name == "" then "" else "/${name}"}"; # Almost a `suffix` logic, but yield subdir
60+
baseDirectory = cfg.baseDirectory;
4861
nixUserIsTrusted = true;
4962
labels =
5063
let
5164
mkIfNotNull = x: mkIf (x != null) x;
5265
in
5366
{
54-
darwin.label = config.system.darwinLabel;
55-
darwin.revision = config.system.darwinRevision;
56-
darwin.version = config.system.darwinVersion;
57-
darwin.nix.daemon = config.nix.useDaemon;
58-
darwin.nix.sandbox = config.nix.settings.sandbox;
67+
darwin.label = topConfig.system.darwinLabel;
68+
darwin.revision = topConfig.system.darwinRevision;
69+
darwin.version = topConfig.system.darwinVersion;
70+
darwin.nix.daemon = topConfig.nix.useDaemon;
71+
darwin.nix.sandbox = topConfig.nix.settings.sandbox;
5972
};
6073
};
6174
systemConfig = { config, ... }: {
62-
launchd.daemons.hercules-ci-agent = {
63-
script = "exec ${cfg.package}/bin/hercules-ci-agent --config ${cfg.jsonFile}";
75+
launchd.daemons."hci-agent${suffix}" = {
76+
script = "exec ${command}";
6477

6578
path = [ config.nix.package ];
6679
environment = {
@@ -71,21 +84,24 @@ let
7184
serviceConfig.RunAtLoad = true;
7285
serviceConfig.StandardErrorPath = cfg.logFile;
7386
serviceConfig.StandardOutPath = cfg.logFile;
74-
serviceConfig.GroupName = "_hercules-ci-agent";
75-
serviceConfig.UserName = "_hercules-ci-agent";
76-
serviceConfig.WorkingDirectory = user.home;
87+
serviceConfig.GroupName = cfg.user;
88+
serviceConfig.UserName = cfg.group;
89+
serviceConfig.WorkingDirectory = cfg.baseDirectory;
7790
serviceConfig.WatchPaths = [
7891
cfg.settings.staticSecretsDirectory
7992
];
8093
};
94+
95+
nix.settings.trusted-users = [ cfg.user ];
8196

8297
system.activationScripts.preActivation.text = ''
8398
touch '${cfg.logFile}'
8499
chown ${toString user.uid}:${toString user.gid} '${cfg.logFile}'
100+
if ! test -d ${cfg.baseDirectory}; then
101+
mkdir -p ${cfg.baseDirectory}
102+
chown ${toString user.uid}:${toString user.gid} ${cfg.baseDirectory}
103+
fi
85104
'';
86-
# Trusted user allows simplified configuration and better performance
87-
# when operating in a cluster.
88-
nix.settings.trusted-users = [ config.systemd.services."hercules-ci-agent${suffix}".serviceConfig.User ];
89105
};
90106
};
91107
};
@@ -107,7 +123,7 @@ in {
107123
- Default base directory: `/var/lib/hercules-ci-agent`
108124
109125
Otherwise:
110-
- User: `hci-''${name}`
126+
- User: `hercules-ci-agent` (on darwin we use same username)
111127
- Default base directory: `/var/lib/hercules-ci-agent-''${name}`
112128
'';
113129
};
@@ -117,8 +133,7 @@ in {
117133
{
118134
nix = mergeSub (c: c.nix);
119135
launchd = mergeSub (c: c.launchd);
120-
# FIXME: no per-agent user support
121-
# users = mergeSub (c: c.users);
136+
system = mergeSub (c: c.system);
122137
}
123138
{
124139
nix.extraOptions = lib.mkIf (config.services.hercules-ci-agents != { }) ''

0 commit comments

Comments
 (0)