aboutsummaryrefslogtreecommitdiff
path: root/nixpkgs/nixos/modules/programs/tsm-client.nix
blob: 7ac4086d5f09472c43fea204f49d78e549f999ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
{ config, lib, pkgs, ... }:

let

  inherit (builtins) length map;
  inherit (lib.attrsets) attrNames filterAttrs hasAttr mapAttrs mapAttrsToList optionalAttrs;
  inherit (lib.modules) mkDefault mkIf;
  inherit (lib.options) literalExample mkEnableOption mkOption;
  inherit (lib.strings) concatStringsSep optionalString toLower;
  inherit (lib.types) addCheck attrsOf lines nullOr package path port str strMatching submodule;

  # Checks if given list of strings contains unique
  # elements when compared without considering case.
  # Type: checkIUnique :: [string] -> bool
  # Example: checkIUnique ["foo" "Foo"] => false
  checkIUnique = lst:
    let
      lenUniq = l: length (lib.lists.unique l);
    in
      lenUniq lst == lenUniq (map toLower lst);

  # TSM rejects servername strings longer than 64 chars.
  servernameType = strMatching ".{1,64}";

  serverOptions = { name, config, ... }: {
    options.name = mkOption {
      type = servernameType;
      example = "mainTsmServer";
      description = ''
        Local name of the IBM TSM server,
        must be uncapitalized and no longer than 64 chars.
        The value will be used for the
        <literal>server</literal>
        directive in <filename>dsm.sys</filename>.
      '';
    };
    options.server = mkOption {
      type = strMatching ".+";
      example = "tsmserver.company.com";
      description = ''
        Host/domain name or IP address of the IBM TSM server.
        The value will be used for the
        <literal>tcpserveraddress</literal>
        directive in <filename>dsm.sys</filename>.
      '';
    };
    options.port = mkOption {
      type = addCheck port (p: p<=32767);
      default = 1500;  # official default
      description = ''
        TCP port of the IBM TSM server.
        The value will be used for the
        <literal>tcpport</literal>
        directive in <filename>dsm.sys</filename>.
        TSM does not support ports above 32767.
      '';
    };
    options.node = mkOption {
      type = strMatching ".+";
      example = "MY-TSM-NODE";
      description = ''
        Target node name on the IBM TSM server.
        The value will be used for the
        <literal>nodename</literal>
        directive in <filename>dsm.sys</filename>.
      '';
    };
    options.genPasswd = mkEnableOption ''
      automatic client password generation.
      This option influences the
      <literal>passwordaccess</literal>
      directive in <filename>dsm.sys</filename>.
      The password will be stored in the directory
      given by the option <option>passwdDir</option>.
      <emphasis>Caution</emphasis>:
      If this option is enabled and the server forces
      to renew the password (e.g. on first connection),
      a random password will be generated and stored
    '';
    options.passwdDir = mkOption {
      type = path;
      example = "/home/alice/tsm-password";
      description = ''
        Directory that holds the TSM
        node's password information.
        The value will be used for the
        <literal>passworddir</literal>
        directive in <filename>dsm.sys</filename>.
      '';
    };
    options.includeExclude = mkOption {
      type = lines;
      default = "";
      example = ''
        exclude.dir     /nix/store
        include.encrypt /home/.../*
      '';
      description = ''
        <literal>include.*</literal> and
        <literal>exclude.*</literal> directives to be
        used when sending files to the IBM TSM server.
        The lines will be written into a file that the
        <literal>inclexcl</literal>
        directive in <filename>dsm.sys</filename> points to.
      '';
    };
    options.extraConfig = mkOption {
      # TSM option keys are case insensitive;
      # we have to ensure there are no keys that
      # differ only by upper and lower case.
      type = addCheck
        (attrsOf (nullOr str))
        (attrs: checkIUnique (attrNames attrs));
      default = {};
      example.compression = "yes";
      example.passwordaccess = null;
      description = ''
        Additional key-value pairs for the server stanza.
        Values must be strings, or <literal>null</literal>
        for the key not to be used in the stanza
        (e.g. to overrule values generated by other options).
      '';
    };
    options.text = mkOption {
      type = lines;
      example = literalExample
        ''lib.modules.mkAfter "compression no"'';
      description = ''
        Additional text lines for the server stanza.
        This option can be used if certion configuration keys
        must be used multiple times or ordered in a certain way
        as the <option>extraConfig</option> option can't
        control the order of lines in the resulting stanza.
        Note that the <literal>server</literal>
        line at the beginning of the stanza is
        not part of this option's value.
      '';
    };
    options.stanza = mkOption {
      type = str;
      internal = true;
      visible = false;
      description = "Server stanza text generated from the options.";
    };
    config.name = mkDefault name;
    # Client system-options file directives are explained here:
    # https://www.ibm.com/support/knowledgecenter/SSEQVQ_8.1.8/client/c_opt_usingopts.html
    config.extraConfig =
      mapAttrs (lib.trivial.const mkDefault) (
        {
          commmethod = "v6tcpip";  # uses v4 or v6, based on dns lookup result
          tcpserveraddress = config.server;
          tcpport = builtins.toString config.port;
          nodename = config.node;
          passwordaccess = if config.genPasswd then "generate" else "prompt";
          passworddir = ''"${config.passwdDir}"'';
        } // optionalAttrs (config.includeExclude!="") {
          inclexcl = ''"${pkgs.writeText "inclexcl.dsm.sys" config.includeExclude}"'';
        }
      );
    config.text =
      let
        attrset = filterAttrs (k: v: v!=null) config.extraConfig;
        mkLine = k: v: k + optionalString (v!="") "  ${v}";
        lines = mapAttrsToList mkLine attrset;
      in
        concatStringsSep "\n" lines;
    config.stanza = ''
      server  ${config.name}
      ${config.text}
    '';
  };

  options.programs.tsmClient = {
    enable = mkEnableOption ''
      IBM Spectrum Protect (Tivoli Storage Manager, TSM)
      client command line applications with a
      client system-options file "dsm.sys"
    '';
    servers = mkOption {
      type = attrsOf (submodule [ serverOptions ]);
      default = {};
      example.mainTsmServer = {
        server = "tsmserver.company.com";
        node = "MY-TSM-NODE";
        extraConfig.compression = "yes";
      };
      description = ''
        Server definitions ("stanzas")
        for the client system-options file.
      '';
    };
    defaultServername = mkOption {
      type = nullOr servernameType;
      default = null;
      example = "mainTsmServer";
      description = ''
        If multiple server stanzas are declared with
        <option>programs.tsmClient.servers</option>,
        this option may be used to name a default
        server stanza that IBM TSM uses in the absence of
        a user-defined <filename>dsm.opt</filename> file.
        This option translates to a
        <literal>defaultserver</literal> configuration line.
      '';
    };
    dsmSysText = mkOption {
      type = lines;
      readOnly = true;
      description = ''
        This configuration key contains the effective text
        of the client system-options file "dsm.sys".
        It should not be changed, but may be
        used to feed the configuration into other
        TSM-depending packages used on the system.
      '';
    };
    package = mkOption {
      type = package;
      default = pkgs.tsm-client;
      defaultText = "pkgs.tsm-client";
      example = literalExample "pkgs.tsm-client-withGui";
      description = ''
        The TSM client derivation to be
        added to the system environment.
        It will called with <literal>.override</literal>
        to add paths to the client system-options file.
      '';
    };
    wrappedPackage = mkOption {
      type = package;
      readOnly = true;
      description = ''
        The TSM client derivation, wrapped with the path
        to the client system-options file "dsm.sys".
        This option is to provide the effective derivation
        for other modules that want to call TSM executables.
      '';
    };
  };

  cfg = config.programs.tsmClient;

  assertions = [
    {
      assertion = checkIUnique (mapAttrsToList (k: v: v.name) cfg.servers);
      message = ''
        TSM servernames contain duplicate name
        (note that case doesn't matter!)
      '';
    }
    {
      assertion = (cfg.defaultServername!=null)->(hasAttr cfg.defaultServername cfg.servers);
      message = "TSM defaultServername not found in list of servers";
    }
  ];

  dsmSysText = ''
    ****  IBM Spectrum Protect (Tivoli Storage Manager)
    ****  client system-options file "dsm.sys".
    ****  Do not edit!
    ****  This file is generated by NixOS configuration.

    ${optionalString (cfg.defaultServername!=null) "defaultserver  ${cfg.defaultServername}"}

    ${concatStringsSep "\n" (mapAttrsToList (k: v: v.stanza) cfg.servers)}
  '';

in

{

  inherit options;

  config = mkIf cfg.enable {
    inherit assertions;
    programs.tsmClient.dsmSysText = dsmSysText;
    programs.tsmClient.wrappedPackage = cfg.package.override rec {
      dsmSysCli = pkgs.writeText "dsm.sys" cfg.dsmSysText;
      dsmSysApi = dsmSysCli;
    };
    environment.systemPackages = [ cfg.wrappedPackage ];
  };

  meta.maintainers = [ lib.maintainers.yarny ];

}