summaryrefslogtreecommitdiffstats
path: root/lib/types/posix.nix
blob: e8f464e4c164d7265f20f9e46278c3da25182d5e (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
{ lib }: rec {

  # RFC952, B. Lexical grammar, <hname>
  hostname = lib.mkOptionType {
    name = "hostname";
    check = x: lib.isString x && lib.all label.check (lib.splitString "." x);
    merge = lib.mergeOneOption;
  };

  # RFC952, B. Lexical grammar, <name>
  # RFC1123, 2.1  Host Names and Numbers
  label = lib.mkOptionType {
    name = "label";
    # TODO case-insensitive labels
    check = lib.test "[0-9A-Za-z]([0-9A-Za-z-]*[0-9A-Za-z])?";
    merge = lib.mergeOneOption;
  };

  # POSIX.1‐2013, 3.278 Portable Filename Character Set
  filename = lib.mkOptionType {
    name = "POSIX filename";
    check = lib.test "([0-9A-Za-z._])[0-9A-Za-z._-]*";
    merge = lib.mergeOneOption;
  };

  # POSIX.1‐2013, 3.2 Absolute Pathname
  absolute-pathname = lib.mkOptionType {
    name = "POSIX absolute pathname";
    check = x: lib.isString x && lib.substring 0 1 x == "/" && pathname.check x;
    merge = lib.mergeOneOption;
  };

  # POSIX.1‐2013, 3.267 Pathname
  pathname = lib.mkOptionType {
    name = "POSIX pathname";
    check = x:
      let
        # The filter is used to normalize paths, i.e. to remove duplicated and
        # trailing slashes.  It also removes leading slashes, thus we have to
        # check for "/" explicitly below.
        xs = lib.filter (s: lib.stringLength s > 0) (lib.splitString "/" x);
      in
        lib.isString x && (x == "/" || (lib.length xs > 0 && lib.all filename.check xs));
    merge = lib.mergeOneOption;
  };

  # POSIX.1-2013, 3.431 User Name
  username = lib.mkOptionType {
    name = "POSIX username";
    check = filename.check;
    merge = lib.mergeOneOption;
  };

}