~ruther/nix-tmpactivator

ref: 8fa85e7ab5259c2807913f9aa888fed4d2aa92a5 nix-tmpactivator/modules/tmpfiles.nix -rw-r--r-- 5.1 KiB
8fa85e7a — Rutherther feat: add nix registry management from nixos modules 1 year, 2 days ago
                                                                                
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
{ config, tmpLib, pkgs, lib, ... }:

let
  inherit (tmpLib) mkTmpFile;

  upperConfig = config;
  enabledInstances = lib.lists.filter (instance: config.tmpfiles.instances.files != []) config.tmpfiles.instances;
in {
  options = {
    tmpfiles = {
      # selfManagement = {
      #   enable = lib.mkEnableOption "tmpfiles self management";
      #   targetDir = lib.mkOption {
      #     type = lib.types.str;
      #   };

      #   managedInstances = lib.mkOption {
      #     type = lib.types.listOf lib.types.str;
      #   };

      #   configurationPackage = lib.mkOption {
      #     type = lib.types.package;
      #     description = ''
      #       This package contains the tmpfiles configuration package
      #     '';
      #   };
      # };

      defaultUser = lib.mkOption {
        type = lib.types.nullOr lib.types.str;
      };
      defaultGroup = lib.mkOption {
        type = lib.types.nullOr lib.types.str;
      };

      instances = lib.mkOption {
        type = lib.types.attrsOf (lib.types.submodule( { name, config, ... }: {
          options = {
            files = lib.mkOption {
              type = lib.types.listOf tmpLib.tmpFileType;
            };

            priority = lib.mkOption {
              type = lib.types.str;
              default = "100";
            };

            configurationFileLines = lib.mkOption {
              type = lib.types.listOf lib.types.str;
            };

            configurationPackage = lib.mkOption {
              type = lib.types.package;
              description = ''
                This package contains the tmpfiles configuration package
              '';
            };

            activationPackage = lib.mkOption {
              type = lib.types.package;
              description = ''
                This package contains a script for activation of the tmp files using `systemd-tmpfiles`
              '';
            };
          };

          config = let
            tmpFiles = lib.lists.filter (file: file.enable) config.files;
            tmpFilesPath = "/share/user-tmpfiles.d/${config.priority}-tmpactivator-${name}.conf";
          in {
            configurationFileLines = builtins.map (file: (tmpLib.mkTmpFile ({
              type = file.type;
              target = file.target;
              mode = file.mode;
              user = if file.user == null then upperConfig.tmpfiles.defaultUser else file.user;
              group = if file.group == null then upperConfig.tmpfiles.defaultGroup else file.group;
              contents = if file.source != null then file.source else file.text;
            }))) tmpFiles;

            configurationPackage = pkgs.writeTextFile {
              name = "${builtins.baseNameOf tmpFilesPath}";
              destination = "${tmpFilesPath}";
              text = lib.concatStringsSep "\n" config.configurationFileLines;
            };

            activationPackage = pkgs.symlinkJoin {
              name = "tmpfiles-activation";

              paths = [
                (pkgs.writeShellScriptBin "activate" ''
                  systemd-tmpfiles --create "${config.configurationPackage}/${tmpFilesPath}"
                '')
                (pkgs.writeShellScriptBin "deactivate" ''
                  systemd-tmpfiles --remove "${config.configurationPackage}/${tmpFilesPath}"
                '')
                config.configurationPackage
              ];
            };
          };
        }));
      };
    };
  };

  # config = lib.mkMerge [
    # (lib.mkIf config.tmpfiles.selfManagement.enable {
    #   tmpfiles.selfManagement.configurationPackage = pkgs.writeTextFile {
    #     name = "99-tmpactivator.conf";
    #     destination = "/share/user-tmpfiles.d/99-tmpactivator.conf";
    #     text = ''
    #       L+ ${config.tmpfiles.selfManagement.targetDir}/100-tmpactivator-files.conf - - - - ${config.tmpfiles.configurationPackage}/share/user-tmpfiles.d/100-tmpactivator-files.conf
    #       r ${config.tmpfiles.selfManagement.targetDir}/100-tmpactivator-files.conf - - - - -
    #     '';
    #   };

    #   tmpfiles.activationPackage = pkgs.symlinkJoin {
    #     name = "tmpfiles-activation";

    #     paths = [
    #       (pkgs.writeShellScriptBin "activate" ''
    #         systemd-tmpfiles --create "${config.tmpfiles.selfManagement.configurationPackage}/share/user-tmpfiles.d/99-tmpactivator.conf"
    #         systemd-tmpfiles --create "${config.tmpfiles.configurationPackage}/share/user-tmpfiles.d/100-tmpactivator-files.conf"
    #       '')
    #       (pkgs.writeShellScriptBin "deactivate" ''
    #         systemd-tmpfiles --remove "${config.tmpfiles.configurationPackage}/share/user-tmpfiles.d/100-tmpactivator-files.conf"
    #         systemd-tmpfiles --remove "${config.tmpfiles.selfManagement.configurationPackage}/share/user-tmpfiles.d/99-tmpactivator.conf"
    #       '')
    #       config.tmpfiles.configurationPackage
    #       config.tmpfiles.selfManagement.configurationPackage
    #     ];
    #   };
    # })

    # {
      # assertions = builtins.map
      #   (file: {
      #     assertion = file.source == null || file.text == null;
      #     message = "Either text or source can be set, not both.";
      #   })
      # tmpFiles;
      #
    # }
  # ];
}
Do not follow this link