~ruther/nix-tmpactivator

ref: f4f924ac84d7f2d5608d53ffdc68cde86b577ff5 nix-tmpactivator/modules/tmpfiles.nix -rw-r--r-- 3.0 KiB
f4f924ac — Rutherther docs: add README 1 year, 4 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
{ config, tmpLib, pkgs, lib, ... }:

let
  inherit (tmpLib) mkTmpFile;

  tmpFiles = lib.lists.filter (file: file.enable) config.tmpfiles.files;
in {
  options = {
    tmpfiles = {
      defaultUser = lib.mkOption {
        type = lib.types.nullOr lib.types.str;
      };
      defaultGroup = lib.mkOption {
        type = lib.types.nullOr lib.types.str;
      };

      files = lib.mkOption {
        type = lib.types.listOf tmpLib.tmpFileType;
        default = [];
        description = ''
          The files to configure.
        '';
      };

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

      configurationFile = lib.mkOption {
        type = lib.types.str;
      };

      removalConfigurationFile = lib.mkOption {
        type = 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 = {
    # assertions = builtins.map
    #   (file: {
    #     assertion = file.source == null || file.text == null;
    #     message = "Either text or source can be set, not both.";
    #   })
    # tmpFiles;
    #

    tmpfiles.configurationFileLines = builtins.map (file: (tmpLib.mkTmpFile ({
      type = file.type;
      target = file.target;
      mode = file.mode;
      user = if file.user == null then config.tmpfiles.defaultUser else file.user;
      group = if file.group == null then config.tmpfiles.defaultGroup else file.group;
      contents = if file.source != null then file.source else file.text;
    }))) tmpFiles;

    tmpfiles.configurationFile = pkgs.lib.concatStringsSep "\n" config.tmpfiles.configurationFileLines;

    tmpfiles.removalConfigurationFile = lib.concatStrings (builtins.map (file: tmpLib.mkRmTmpFile ({
      type = if file.type == "d" then "R" else "r";
      target = file.target;
    }) + "\n") tmpFiles);

    tmpfiles.configurationPackage = pkgs.runCommand "tmpfiles-configuration" {
      configuration = config.tmpfiles.configurationFile;
      removal = config.tmpfiles.removalConfigurationFile;
    } ''
      mkdir -p $out/share/tmpfiles $out/lib/tmpfiles.d
      echo -n "$configuration" > "$out/lib/tmpfiles.d/tmpfiles.conf"
      echo -n "$removal" > "$out/share/tmpfiles/rm-tmpfiles.conf"
    '';

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

      paths = [
        (pkgs.writeShellScriptBin "activate" ''
          systemd-tmpfiles --create "${config.tmpfiles.configurationPackage}/lib/tmpfiles.d/tmpfiles.conf"
        '')
        (pkgs.writeShellScriptBin "deactivate" ''
          systemd-tmpfiles --remove "${config.tmpfiles.configurationPackage}/share/tmpfiles/rm-tmpfiles.conf"
        '')
        config.tmpfiles.configurationPackage
      ];
    };
  };
}
Do not follow this link