about summary refs log tree commit diff
path: root/lib/default.nix
blob: e06b7ec7fecc45e370690de7cd1a76556f5a77ca (plain) (blame)
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
{
  inputs,
  lib,
  system,
  ...
}:
rec {
  my = import ./my.nix { inherit lib inputs system; };

  dns = import ./dns.nix { inherit lib inputs system; };

  packages = import ./packages.nix { inherit lib inputs system; };

  inherit ((import inputs.infuse { inherit lib; }).v1) infuse;

  isEven =
    number:
    assert (builtins.isInt number) || (builtins.isFloat number);
    builtins.div number 2 == 0;

  isOdd = number: !isEven number;

  pow =
    base: exponent:
    assert (builtins.isInt base) && (builtins.isInt exponent);
    assert exponent > 0;
    builtins.foldl' (x: _: x * base) 1 (builtins.genList (_: _) exponent);

  mkTcpMem =
    min: ini: max:
    assert min <= ini && ini <= max;
    lib.concatMapStrings (x: toString x + " ") (
      map (pow 2) [
        min
        ini
        max
      ]
    );

  # Load all files from directory (partially recursively).
  #
  # Usage: Assuming other Nix files are present in a directory:
  # ```
  # { ... }: {
  #   imports = attrValue (modulesIn ./.);
  #
  #   services.foobar.enable = true;
  # }
  # ```
  #
  modulesIn =
    dir:
    lib.pipe dir [
      builtins.readDir
      (lib.mapAttrsToList (
        name: type:
        let
          modulePath = dir + "/${name}";
        in
        if type == "regular" && lib.hasSuffix ".nix" name && name != "default.nix" then
          [ (lib.nameValuePair (lib.removeSuffix ".nix" name) modulePath) ]
        else if type == "directory" && lib.pathExists (modulePath + "/default.nix") then
          [ (lib.nameValuePair name modulePath) ]
        else
          [ ]
      ))
      lib.concatLists
      lib.listToAttrs
    ];

  # Override a module using another Nixpkgs source tree.
  #
  # Usage: Assuming we want to override "services.foobar.enable":
  # ```
  # { config, lib, ... }:
  # (lib.modulesFromRef
  #   "services/security/foobar.nix"
  #   "azahi:fix-foobar" # Can be copied form a PR.
  #   "sha256-AAA..."
  # ) // {
  #   config = {
  #     serices.foobar.enable = true;
  #   };
  # }
  # ```
  # or
  # ```
  # nixosConfigurations.machine = nixosSystem {
  #   modules = [
  #     (_: moduleFromRef "services/security/foobar.nix" "azahi:foobar-fix" "sha256-AAA...")
  #   ];
  # };
  # ```
  #
  moduleFromRef = module: ref: sha256: {
    disabledModules = [ module ];
    imports = [
      (
        let
          src = inputs.nixpkgs.legacyPackages.${system}.fetchzip {
            url =
              let
                cons = lib.splitString ":" ref;
                owner = lib.head cons;
                branch = lib.last cons;
              in
              "https://github.com/${owner}/nixpkgs/archive/refs/heads/${branch}.tar.gz";
            inherit sha256;
          };
        in
        "${src}/nixos/modules/${module}"
      )
    ];
  };
}

Consider giving Nix/NixOS a try! <3