1/*
2 This file is for options that NixOS and nix-darwin have in common.
3
4 Platform-specific code is in the respective default.nix files.
5*/
6
7{
8 config,
9 lib,
10 options,
11 pkgs,
12 ...
13}:
14let
15 inherit (lib)
16 filterAttrs
17 literalExpression
18 mkIf
19 mkOption
20 mkRemovedOptionModule
21 mkRenamedOptionModule
22 types
23 mkPackageOption
24 ;
25
26 cfg = config.services.hercules-ci-agent;
27
28 inherit (import ./settings.nix { inherit pkgs lib; }) format settingsModule;
29
30in
31{
32 imports = [
33 (mkRenamedOptionModule
34 [ "services" "hercules-ci-agent" "extraOptions" ]
35 [ "services" "hercules-ci-agent" "settings" ]
36 )
37 (mkRenamedOptionModule
38 [ "services" "hercules-ci-agent" "baseDirectory" ]
39 [ "services" "hercules-ci-agent" "settings" "baseDirectory" ]
40 )
41 (mkRenamedOptionModule
42 [ "services" "hercules-ci-agent" "concurrentTasks" ]
43 [ "services" "hercules-ci-agent" "settings" "concurrentTasks" ]
44 )
45 (mkRemovedOptionModule [ "services" "hercules-ci-agent" "patchNix" ]
46 "Nix versions packaged in this version of Nixpkgs don't need a patched nix-daemon to work correctly in Hercules CI Agent clusters."
47 )
48 ];
49
50 options.services.hercules-ci-agent = {
51 enable = mkOption {
52 type = types.bool;
53 default = false;
54 description = ''
55 Enable to run Hercules CI Agent as a system service.
56
57 [Hercules CI](https://hercules-ci.com) is a
58 continuous integation service that is centered around Nix.
59
60 Support is available at [help@hercules-ci.com](mailto:help@hercules-ci.com).
61 '';
62 };
63 package = mkPackageOption pkgs "hercules-ci-agent" { };
64 settings = mkOption {
65 description = ''
66 These settings are written to the `agent.toml` file.
67
68 Not all settings are listed as options, can be set nonetheless.
69
70 For the exhaustive list of settings, see <https://docs.hercules-ci.com/hercules-ci/reference/agent-config/>.
71 '';
72 type = types.submoduleWith { modules = [ settingsModule ]; };
73 };
74
75 /*
76 Internal and/or computed values.
77
78 These are written as options instead of let binding to allow sharing with
79 default.nix on both NixOS and nix-darwin.
80 */
81 tomlFile = mkOption {
82 type = types.path;
83 internal = true;
84 defaultText = lib.literalMD "generated `hercules-ci-agent.toml`";
85 description = ''
86 The fully assembled config file.
87 '';
88 };
89 };
90
91 config = mkIf cfg.enable {
92 # Make sure that nix.extraOptions does not override trusted-users
93 assertions = [
94 {
95 assertion =
96 (cfg.settings.nixUserIsTrusted or false)
97 -> builtins.match ".*(^|\n)[ \t]*trusted-users[ \t]*=.*" config.nix.extraOptions == null;
98 message = ''
99 hercules-ci-agent: Please do not set `trusted-users` in `nix.extraOptions`.
100
101 The hercules-ci-agent module by default relies on `nix.settings.trusted-users`
102 to be effectful, but a line like `trusted-users = ...` in `nix.extraOptions`
103 will override the value set in `nix.settings.trusted-users`.
104
105 Instead of setting `trusted-users` in the `nix.extraOptions` string, you should
106 set an option with additive semantics, such as
107 - the NixOS option `nix.settings.trusted-users`, or
108 - the Nix option in the `extraOptions` string, `extra-trusted-users`
109 '';
110 }
111 ];
112 nix.extraOptions = ''
113 # A store path that was missing at first may well have finished building,
114 # even shortly after the previous lookup. This *also* applies to the daemon.
115 narinfo-cache-negative-ttl = 0
116 '';
117 services.hercules-ci-agent = {
118 tomlFile = format.generate "hercules-ci-agent.toml" cfg.settings;
119 settings.config._module.args = {
120 packageOption = options.services.hercules-ci-agent.package;
121 inherit pkgs;
122 };
123 };
124 };
125}