~ruther/nix-tmpactivator

ref: 36039e24788d5577829ef2aa357cad7b423f7487 nix-tmpactivator/lib/default.nix -rw-r--r-- 3.0 KiB
36039e24 — Rutherther feat: add removal records for home files 1 year, 5 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
{ pkgs, lib, ... }:

rec {
  escapeTmpFileContents = contents: builtins.replaceStrings ["\n"] ["\\n"] contents;
  mkTmpFile = { type ? "f", target, mode ? "0700", user, group, contents }: "${type} \"${target}\" ${mode} ${user} ${group} - ${escapeTmpFileContents contents}";
  mkRmTmpFile = { type ? "f", target }: "${if type == "d" then "R" else "r"} ${target}";

  tmpFileType = lib.types.submodule ({ config, ... }: {
    options = {
      enable = lib.mkOption {
        type = lib.types.bool;
        default = true;
      };

      type = lib.mkOption { type = lib.types.str; default = "f"; };
      mode = lib.mkOption { type = lib.types.str; default = "0400"; };
      user = lib.mkOption { type = lib.types.nullOr lib.types.str; default = null; };
      group = lib.mkOption { type = lib.types.nullOr lib.types.str; default = null; };

      executable = lib.mkOption {
        type = lib.types.bool;
        default = false;
      };

      target = lib.mkOption {
        type = lib.types.str;
      };
      source = lib.mkOption {
        type = lib.types.nullOr lib.types.path;
        default = null;
      };
      text = lib.mkOption {
        type = lib.types.nullOr lib.types.lines;
        default = null;
      };
    };
  });

  homeFileType = lib.types.attrsOf (lib.types.submodule ({ config, name, ... }: {
    options = {
      enable = lib.mkOption {
        type = lib.types.bool;
        default = true;
      };

      type = lib.mkOption { type = lib.types.str; default = "f"; };
      mode = lib.mkOption { type = lib.types.str; default = "0400"; };
      user = lib.mkOption { type = lib.types.nullOr lib.types.str; default = null; };
      group = lib.mkOption { type = lib.types.nullOr lib.types.str; default = null; };

      executable = lib.mkOption {
        type = lib.types.bool;
        default = false;
      };

      target = lib.mkOption {
        type = lib.types.str;
      };
      source = lib.mkOption {
        type = lib.types.nullOr lib.types.path;
        default = null;
      };
      text = lib.mkOption {
        type = lib.types.nullOr lib.types.lines;
        default = null;
      };
    };

    config = {
      target = lib.mkDefault name;
      source = lib.mkIf (config.text != null) (lib.mkDefault (pkgs.writeTextFile {
        inherit (config) text;
        executable = config.executable == true;
        name = storeFileName config.target;
      }));
    };
  }));

  # Figures out a valid Nix store name for the given path.
  storeFileName = path:
    let
      # All characters that are considered safe. Note "-" is not
      # included to avoid "-" followed by digit being interpreted as a
      # version.
      safeChars = [ "+" "." "_" "?" "=" ] ++ lib.lowerChars ++ lib.upperChars
        ++ lib.stringToCharacters "0123456789";

      empties = l: lib.genList (x: "") (lib.length l);

      unsafeInName =
        lib.stringToCharacters (lib.replaceStrings safeChars (empties safeChars) path);

      safeName = lib.replaceStrings unsafeInName (empties unsafeInName) path;
    in "home_" + safeName;
}
Do not follow this link