~ruther/nix-tmpactivator

ref: 58dd1f391141cb0b92f9582d803e9510a13e1bbe nix-tmpactivator/lib/default.nix -rw-r--r-- 4.9 KiB
58dd1f39 — Rutherther feat: add self management (auto activation, deactivation) 1 year, 3 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
145
146
147
{ pkgs, lib, ... }:

rec {
  mkWrapper = { basePackage, prependFlags ? [], appendFlags ? [], pathAdd ? [], environment ? {}, extraPackages ? [] }: pkgs.symlinkJoin ({
    name = "wrapper-${basePackage.name}";
    paths = [ basePackage ] ++ extraPackages;
    nativeBuildInputs = [ pkgs.makeWrapper ];
    postBuild = let

      envToWrapperArg = name: config: let
        optionStr = attr: lib.showOption ["env" name attr];
        unsetArg =
          if !config.force
          then
            (lib.warn ''
              ${optionStr "value"} is null (indicating unsetting the variable), but ${optionStr "force"} is false. This option will have no effect
            '' [])
          else ["--unset" config.name];
        setArg = let
          arg =
            if config.force
            then "--set"
            else "--set-default";
        in [arg config.name config.value];
      in
        if config.value == null
        then unsetArg
        else setArg;

      envArgs = lib.mapAttrsToList envToWrapperArg environment;
      # Yes, the arguments are escaped later, yes, this is intended to "double escape",
      # so that they are escaped for wrapProgram and for the final binary too.
      prependFlagArgs = map (args: ["--add-flags" (lib.escapeShellArg args)]) prependFlags;
      appendFlagArgs = map (args: ["--append-flags" (lib.escapeShellArg args)]) appendFlags;
      pathArgs = map (p: ["--prefix" "PATH" ":" "${p}/bin"]) pathAdd;
      allArgs = lib.flatten (envArgs ++ prependFlagArgs ++ appendFlagArgs ++ pathArgs);
    in ''
      for file in $out/bin/*; do
        wrapProgram \
          "$file" \
          ${lib.escapeShellArgs allArgs}
      done

      ${
        lib.concatMapStringsSep "\n"
        (p:
          if lib.hasAttr "man" p
          then "${pkgs.xorg.lndir}/bin/lndir -silent ${p.man} $out"
          else "#")
        ([basePackage] ++ extraPackages)
      }
    '';
    passthru = (basePackage.passhtru or {}) // {unwrapped = basePackage;};
  });

  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.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