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