summaryrefslogtreecommitdiff
path: root/lib/default.nix
blob: bca0a8e3af848ed24feea4811f842eb84294b4b2 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
inputs: lib: prev: with lib; rec {
  modulesAttrsIn = dir: pipe dir [
    builtins.readDir
    (mapAttrsToList (name: type:
      if type == "regular" && hasSuffix ".nix" name && name != "default.nix" then
        [ { name = removeSuffix ".nix" name; value = dir + "/${name}"; } ]
      else if type == "directory" && pathExists (dir + "/${name}/default.nix") then
        [ { inherit name; value = dir + "/${name}"; } ]
      else
        []
    ))
    concatLists
    listToAttrs
  ];

  modulesIn = dir: attrValues (modulesAttrsIn dir);

  # Collects the inputs of a flake recursively (with possible duplicates).
  collectFlakeInputs = input:
    [ input ] ++ concatMap collectFlakeInputs (builtins.attrValues (input.inputs or {}));

  my = import ./my.nix inputs.self lib;

  mkMachines = import ./machine.nix lib;

  # Gets module from ./machines/ and uses the lib to define which other modules
  # the machine needs.
  mkSystem = machines: name: systemInputs @ {
    system,
    modules,
    opts,
    ...
  }:
  let
    machine = machines.${name};
    systemClosure =
    (if hasInfix "darwin" system then
      darwinSystem
      else
      nixosSystem);
    home-manager =
      (if hasInfix "darwin" system then
      [inputs.home-manager.darwinModules.default]
      else
      [inputs.home-manager.nixosModules.default]);
  in
  systemClosure {
    inherit lib system;
    specialArgs = {
      inherit (inputs) self;
      inherit machines machine inputs;
    };
    modules =
      modules
      ++
      home-manager
      ++ [
        ({pkgs, ...}: {
          nixpkgs.overlays = with lib; [
            (composeManyExtensions [
              (import ../overlays/vimPlugins.nix {inherit pkgs;})
              (import ../overlays/openpomodoro-cli.nix {inherit pkgs lib;})
              inputs.neovim-nightly-overlay.overlays.default
            ])
          ];
        })
      ];
  };

  mkSystemsFor = allSystems: systems:
    let
      machines = mkMachines (mapAttrs (name: value: value.opts) allSystems);
    in
      (mapAttrs (mkSystem machines) systems);

}