summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorKleidi Bujari <mail@4kb.net>2025-04-02 21:59:20 -0400
committerKleidi Bujari <mail@4kb.net>2025-04-02 21:59:20 -0400
commit09c2b6de63a6ff35348fcb2c2eb57b974f0a8a52 (patch)
treebd8f1d3a51f12cd98764ae572b00f0b32de62644 /lib
parentb1738c105de3bc0ba7cd27f68c9eb146439a0964 (diff)
downloaddepot-09c2b6de63a6ff35348fcb2c2eb57b974f0a8a52.tar.gz
depot-09c2b6de63a6ff35348fcb2c2eb57b974f0a8a52.tar.bz2
depot-09c2b6de63a6ff35348fcb2c2eb57b974f0a8a52.zip
automate project structure
Diffstat (limited to 'lib')
-rw-r--r--lib/depot-tools.nix84
1 files changed, 84 insertions, 0 deletions
diff --git a/lib/depot-tools.nix b/lib/depot-tools.nix
new file mode 100644
index 0000000..448055a
--- /dev/null
+++ b/lib/depot-tools.nix
@@ -0,0 +1,84 @@
+{ inputs
+, systems
+, nixpkgs ? inputs.nixpkgs
+, ...
+}:
+
+let
+ inherit (builtins)
+ match
+ head
+ readDir
+ ;
+
+ inherit (nixpkgs.lib)
+ callPackageWith
+ filterAttrs
+ genAttrs
+ makeScope
+ mapAttrs
+ mapAttrs'
+ ;
+
+ importDir =
+ path:
+ 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" ];
+ in
+ dirPaths // nixPaths;
+
+ # 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; })
+ );
+
+ eachSystem = f: genAttrs systems (system: f systemArgs.${system});
+in
+{ inherit importDir systemArgs eachSystem; }