summaryrefslogtreecommitdiff
path: root/lib/depot-tools.nix
blob: 7c2fc6b86dfad158c230ae8d000b64d3e6a1a893 (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
77
78
79
80
81
82
83
84
{ inputs
, systems
, nixpkgs ? inputs.nixpkgs
, ...
}:

let
  inherit (builtins)
    match
    head
    readDir
    ;

  inherit (nixpkgs.lib)
    callPackageWith
    filterAttrs
    genAttrs
    makeScope
    mapAttrs
    mapAttrs'
    ;

  importDir =
    path: fn:
    let
      entries = readDir path;

      # Get paths to directories
      onlyDirs = filterAttrs (_name: type: type == "directory") entries;
      dirPaths = mapAttrs
        (name: type: {
          path = path + "/${name}";
          inherit type;
        })
        onlyDirs;

      # Get paths to nix files, where the name is the basename of the file without the .nix extension
      nixPaths = removeAttrs
        (mapAttrs'
          (
            name: type:
              let
                nixName = match "(.*)\\.nix" name;
              in
              {
                name = if type == "directory" || nixName == null then "__junk" else (head nixName);
                value = {
                  path = path + "/${name}";
                  type = type;
                };
              }
          )
          entries) [ "__junk" ];

      combined = dirPaths // nixPaths;
    in
    fn combined;

  # Memoize the args per system
  systemArgs = genAttrs systems (
    system:
    let
      # Resolve the packages for each input.
      perSystem = mapAttrs
        (_: flake: flake.legacyPackages.${system} or { } // flake.packages.${system} or { })
        inputs;

      # Handle nixpkgs specially.
      pkgs =
        if (nixpkgs.config or { }) == { } && (nixpkgs.overlays or [ ]) == [ ] then
          perSystem.nixpkgs
        else
          import inputs.nixpkgs {
            inherit system;
            config = nixpkgs.config or { };
            overlays = nixpkgs.overlays or [ ];
          };

      flake = inputs.self;
    in
    makeScope callPackageWith (_: { inherit inputs perSystem flake pkgs system; })
  );
in
{ inherit importDir systemArgs; }