summaryrefslogtreecommitdiff
path: root/modules/xnet/persist.nix
blob: 5e69bae348fbe05e1a95bfd95d09893f25a92b5f (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
{ config, lib, ... }:
let
  inherit (lib)
    concatLists
    isString
    mkOption
    types
    ;

in
{
  options.xnet.persist = mkOption {
    type = types.listOf (
      types.either types.str (
        types.submodule {
          options = {
            path = mkOption {
              type = types.str;
              description = "Path to persist";
            };
            user = mkOption {
              type = types.nullOr types.str;
              default = null;
              description = "User ID to set on the persisted directory";
            };
            group = mkOption {
              type = types.nullOr types.str;
              default = null;
              description = "Group ID to set on the persisted directory";
            };

            mode = mkOption {
              type = types.nullOr types.str;
              default = null;
              description = "Permissions to set";
            };
          };
        }
      )
    );
    default = [ ];
    description = "Automatically persisted state.";
  };

  config.systemd.tmpfiles.rules =
    let
      mkEntry =
        entry:
        let
          path = if isString entry then entry else entry.path;
          user = if isString entry then "-" else entry.user;
          group = if isString entry then "-" else entry.group;
          mode = if isString entry then "-" else entry.mode;

          dent = "d /persist${path} ${mode} ${user} ${group} - -";
          link = "L+ ${path} - - - - /persist${path}";
          perm = "Z ${mode} ${user} ${group} - -";
        in
        [
          dent
          link
          perm
        ];

      entries = map mkEntry config.xnet.persist;
    in
    concatLists entries;
}