summaryrefslogtreecommitdiffstats
path: root/lib/services/config-data-item.nix
blob: 44671b704d8d516b18800ee0804687196bcd6abc (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
# Tests in: ../../nixos/tests/modular-service-etc/test.nix
# This file is a function that returns a module.
pkgs:
{
  lib,
  name,
  config,
  options,
  ...
}:
let
  inherit (lib) mkOption types;
in
{
  options = {
    enable = mkOption {
      type = types.bool;
      default = true;
      description = ''
        Whether this configuration file should be generated.
        This option allows specific configuration files to be disabled.
      '';
    };

    name = mkOption {
      type = types.str;
      description = ''
        Name of the configuration file (relative to the service's configuration directory). Defaults to the attribute name.
      '';
    };

    path = mkOption {
      type = types.str;
      readOnly = true;
      description = ''
        The actual path where this configuration file will be available.
        This is determined by the service manager implementation.

        On NixOS it is an absolute path.
        Other service managers may provide a relative path, in order to be unprivileged and/or relocatable.
      '';
    };

    text = mkOption {
      default = null;
      type = types.nullOr types.lines;
      description = "Text content of the configuration file.";
    };

    source = mkOption {
      type = types.path;
      description = "Path of the source file.";
    };
  };

  config = {
    name = lib.mkDefault name;
    source = lib.mkIf (config.text != null) (
      let
        name' = "service-configdata-" + lib.replaceStrings [ "/" ] [ "-" ] name;
      in
      lib.mkDerivedConfig options.text (pkgs.writeText name')
    );
  };
}