summaryrefslogtreecommitdiff
path: root/openwrt/etc.nix
blob: b9f83247f922d4f27bbb6cc46417191284353c6a (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
{ config, lib, ... }:

let
  cfg = config.etc;
in

{
  options.etc = lib.mkOption {
    type = lib.types.attrsOf (lib.types.submoduleWith {
      description = "`/etc` file description";
      modules = [
        ({ name, ... }: {
          options = {
            enable = lib.mkEnableOption "this `/etc` file" // {
              default = true;
            };

            text = lib.mkOption {
              type = lib.types.lines;
              description = ''
                Contents of the file.
              '';
            };
          };
        })
      ];
    });
    default = {};
    description = ''
      Extra files to *create* in the target `/etc`. It is not currently possible to
      *delete* files from the target.

      This option should usually not be used if there's a UCI way to achieve the
      same effect.
    '';
  };

  config = lib.mkIf (cfg != {}) {
    deploySteps.etc = {
      priority = 8000;
      apply =
        lib.concatStrings
          (lib.mapAttrsToList
            (name: file: lib.optionalString (file.enable) ''
              ${lib.optionalString (dirOf name != ".") ''
                mkdir -p ${lib.escapeShellArg (dirOf name)}
              ''}
              echo ${lib.escapeShellArg file.text} >${lib.escapeShellArg "/etc/${name}"}
            '')
            cfg);
    };
  };
}