/ lib / default.nix
default.nix
 1  { lib, super, ... }: let
 2    pars = min: val:
 3      if val > min then
 4        "[${toString min}-${toString val}]"
 5      else "${toString min}";
 6  in {
 7    genRegex = x: assert lib.isInt x && x <= 100; let
 8      dig  = builtins.floor (x / 10);
 9      rest = lib.mod x 10;
10    in lib.optionalString (dig >= 1) "[0-9]|"
11     + lib.optionalString (dig > 1) "${pars 1 (dig - 1)}[0-9]|"
12     + lib.optionalString (rest == 0) "${toString x}"
13     + lib.optionalString (rest != 0 && dig != 0) (toString dig)
14     + lib.optionalString (rest != 0) "[0-${toString rest}]";
15    mkFishPath = pkgs:
16      lib.pipe pkgs [
17        (lib.makeBinPath)
18        (lib.splitString ":")
19        (map (x: "fish_add_path ${x}"))
20        (lib.concatStringsSep "\n")
21      ];
22  
23    genUser = name: assert lib.isString name; args @ {
24      description ? name,
25      isNormalUser ? true,
26      home ? "/home/${name}",
27      extraGroups ? [
28        "networkmanager"
29        "docker"
30        "wheel"
31        "video"
32        "gdm"
33        "dialout"
34        "kvm"
35        "adbusers"
36        "vboxusers"
37        "fwupd-refresh"
38      ],
39      ...
40    }: assert (
41      lib.isString description &&
42      lib.isBool isNormalUser &&
43      lib.isString home &&
44      lib.isList extraGroups &&
45      lib.all lib.isString extraGroups
46    ); {
47      ${name} = args // {
48        inherit description isNormalUser home extraGroups;
49      };
50    };
51  
52    # users :: lists, options :: ( attrs | str -> attrs )
53    genUsers = users: options:
54      assert (
55        lib.isList users &&
56        lib.length users != 0 &&
57        lib.all lib.isString users &&
58        (lib.isAttrs options || (lib.isFunction options && lib.isAttrs (options "test")))
59      );
60      lib.foldl' (acc: name: let
61        opts = if lib.isAttrs options then options else options name;
62      in  acc // lib.genUser name opts) {} users;
63  
64    nixvim = super.nixvim.extend (se: su: {
65      toLuaObject' = x: if isNull x then "" else se.toLuaObject x;
66    });
67  }