aboutsummaryrefslogtreecommitdiff
path: root/modules/lib/file-type.nix
blob: f02daa1294cb5ca89d1de9c5998c31cc69c643ef (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
{ writeTextFile, linkFarm, lib, ... }:
{ name, config, ... }:
with builtins // lib;
let
  sanitize = p:
  let
    safeChars = [ "+" "." "_" "?" "=" ] ++ lowerChars ++ upperChars ++ stringToCharacters "0123456789";
    empties = l: genList (_: "") (length l);
    unsafeInName = stringToCharacters (replaceStrings safeChars (empties safeChars) p);
  in "kobo_" + replaceStrings unsafeInName (empties unsafeInName) p;
in {
  options = {
    enable = mkEnableOption name // { default = true; };
    name = mkOption {
      type = types.str;
    };
    target = mkOption {
      type = types.str;
      default = name;
      apply = x: if hasPrefix "/" x then x else "/${x}";
    };
    executable = mkOption {
      type = with types; nullOr bool;
      default = null;
    };
    text = mkOption {
      type = with types; nullOr lines;
      default = null;
    };
    source = mkOption {
      type = types.path;
    };
    _package = mkOption {
      type = types.path;
      readOnly = true;
      internal = true;
      default = linkFarm (sanitize name) [{
        name = removePrefix "/" config.target;
        # let
        #   t = config.target;
        # in if
        path = config.source;
      }];
    };
  };

  config = {
    source = mkIf (config.text != null) (mkDefault (writeTextFile {
      inherit (config) text;
      name = sanitize name;
      executable = config.executable == true;
    }));
  };
}