aboutsummaryrefslogtreecommitdiff
path: root/lib/lib.nix
blob: 9dd60d4c71f455cfe0a8e340a7fc3db1bfe5f64c (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
55
56
57
58
59
60
61
62
let
  optionalAttrs = cond: as:
    if cond
    then as
    else {};

  mkIfNull = cond: as:
    if cond
    then as
    else null;

  globimport = {
    lazyImport = path: optionalAttrs (builtins.pathExists path) (import path);

    getSubdirs = dir:
      builtins.filter (x: x != null)
      (builtins.attrValues
        (builtins.mapAttrs
          (name: value: mkIfNull (value == "directory") name)
          (builtins.readDir dir)));

    getSubfiles = dir:
      builtins.filter (x: x != null)
      (builtins.attrValues
        (builtins.mapAttrs
          (name: value: mkIfNull (value == "file") name)
          (builtins.readDir dir)));
  };

  netTypes = {lib, ...}: {
    ipAddr = lib.types.strMatching "^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$";
    ipAddrCidr = lib.types.strMatching "^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])/(3[0-2]|[12]?[0-9])$";
  };

  options = {
    lib,
    config,
  }: let
    inherit (lib) mkOption mkEnableOption;

    mkThemeOption = name:
      mkOption {
        description = "Pre-made theme for ${name}";
        type = lib.types.enum ["catppuccin" "adwaita" "kanagawa" "terminal"];
        default = config.collinux.theme;
        defaultText = "config.collinux.theme";
      };

    mkProgramOption = name: {
      enable = mkEnableOption "whether to enable ${name}";
      theme = mkThemeOption name;
    };
  in {
    inherit mkProgramOption mkThemeOption;
  };
in {
  inherit globimport;
  inherit options;
  inherit netTypes;

  flatten = builtins.foldl' (a: b: a ++ b) [];
}