1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7
8let
9 cfg = config.services.ocsinventory-agent;
10
11 settingsFormat = pkgs.formats.keyValue {
12 mkKeyValue = lib.generators.mkKeyValueDefault { } "=";
13 };
14
15in
16{
17 meta = {
18 doc = ./ocsinventory-agent.md;
19 maintainers = with lib.maintainers; [ anthonyroussel ];
20 };
21
22 options = {
23 services.ocsinventory-agent = {
24 enable = lib.mkEnableOption "OCS Inventory Agent";
25
26 package = lib.mkPackageOption pkgs "ocsinventory-agent" { };
27
28 settings = lib.mkOption {
29 type = lib.types.submodule {
30 freeformType = settingsFormat.type.nestedTypes.elemType;
31
32 options = {
33 server = lib.mkOption {
34 type = lib.types.nullOr lib.types.str;
35 example = "https://ocsinventory.localhost:8080/ocsinventory";
36 default = null;
37 description = ''
38 The URI of the OCS Inventory server where to send the inventory file.
39
40 This option is ignored if {option}`services.ocsinventory-agent.settings.local` is set.
41 '';
42 };
43
44 local = lib.mkOption {
45 type = lib.types.nullOr lib.types.path;
46 example = "/var/lib/ocsinventory-agent/reports";
47 default = null;
48 description = ''
49 If specified, the OCS Inventory Agent will run in offline mode
50 and the resulting inventory file will be stored in the specified path.
51 '';
52 };
53
54 ca = lib.mkOption {
55 type = lib.types.path;
56 default = config.security.pki.caBundle;
57 defaultText = lib.literalExpression "config.security.pki.caBundle";
58 description = ''
59 Path to CA certificates file in PEM format, for server
60 SSL certificate validation.
61 '';
62 };
63
64 tag = lib.mkOption {
65 type = lib.types.nullOr lib.types.str;
66 default = null;
67 example = "01234567890123";
68 description = "Tag for the generated inventory.";
69 };
70
71 debug = lib.mkEnableOption "debug mode";
72 };
73 };
74 default = { };
75 example = {
76 debug = true;
77 server = "https://ocsinventory.localhost:8080/ocsinventory";
78 tag = "01234567890123";
79 };
80 description = ''
81 Configuration for /etc/ocsinventory-agent/ocsinventory-agent.cfg.
82
83 Refer to
84 {manpage}`ocsinventory-agent(1)` for available options.
85 '';
86 };
87
88 interval = lib.mkOption {
89 type = lib.types.str;
90 default = "daily";
91 example = "06:00";
92 description = ''
93 How often we run the ocsinventory-agent service. Runs by default every daily.
94
95 The format is described in
96 {manpage}`systemd.time(7)`.
97 '';
98 };
99 };
100 };
101
102 config =
103 let
104 configFile = settingsFormat.generate "ocsinventory-agent.cfg" cfg.settings;
105
106 in
107 lib.mkIf cfg.enable {
108 # Path of the configuration file is hard-coded and cannot be changed
109 # https://github.com/OCSInventory-NG/UnixAgent/blob/v2.10.0/lib/Ocsinventory/Agent/Config.pm#L78
110 #
111 environment.etc."ocsinventory-agent/ocsinventory-agent.cfg".source = configFile;
112
113 systemd.services.ocsinventory-agent = {
114 description = "OCS Inventory Agent service";
115 wantedBy = [ "multi-user.target" ];
116 after = [ "network.target" ];
117
118 reloadTriggers = [ configFile ];
119
120 serviceConfig = {
121 ExecStart = lib.getExe cfg.package;
122 ConfigurationDirectory = "ocsinventory-agent";
123 StateDirectory = "ocsinventory-agent";
124 };
125 };
126
127 systemd.timers.ocsinventory-agent = {
128 description = "Launch OCS Inventory Agent regularly";
129 wantedBy = [ "timers.target" ];
130
131 timerConfig = {
132 OnCalendar = cfg.interval;
133 AccuracySec = "1h";
134 RandomizedDelaySec = 240;
135 Persistent = true;
136 Unit = "ocsinventory-agent.service";
137 };
138 };
139 };
140}