2023-08-27 07:48:57 +02:00
|
|
|
# flakelight -- Framework for simplifying flake setup
|
2023-08-25 06:14:55 +02:00
|
|
|
# Copyright (C) 2023 Archit Gupta <archit@accelbread.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2024-01-15 09:07:59 +01:00
|
|
|
{ config, options, src, lib, flakelight, ... }:
|
2023-08-25 06:14:55 +02:00
|
|
|
let
|
2024-01-15 09:07:59 +01:00
|
|
|
inherit (builtins) attrNames;
|
2024-01-15 10:07:55 +01:00
|
|
|
inherit (lib) findFirst genAttrs mkIf mkOption pathExists subtractLists;
|
2024-01-15 09:07:59 +01:00
|
|
|
inherit (lib.types) attrsOf listOf str;
|
2024-01-15 09:26:05 +01:00
|
|
|
inherit (flakelight) importDir;
|
2023-08-27 07:48:57 +02:00
|
|
|
inherit (flakelight.types) path;
|
2024-01-15 09:26:05 +01:00
|
|
|
|
2024-01-15 10:07:55 +01:00
|
|
|
inherit (config) nixDir;
|
|
|
|
|
|
|
|
importName = name:
|
|
|
|
if pathExists (nixDir + "/${name}.nix")
|
|
|
|
then { success = true; value = import (nixDir + "/${name}.nix"); }
|
|
|
|
else if pathExists (nixDir + "/${name}/default.nix")
|
|
|
|
then { success = true; value = import (nixDir + "/${name}"); }
|
|
|
|
else if pathExists (nixDir + "/${name}")
|
|
|
|
then { success = true; value = importDir (nixDir + "/${name}"); }
|
|
|
|
else { success = false; };
|
|
|
|
|
|
|
|
importNames = names:
|
|
|
|
findFirst (x: x.success) { success = false; } (map importName names);
|
2023-08-25 06:14:55 +02:00
|
|
|
in
|
|
|
|
{
|
2024-01-15 09:07:59 +01:00
|
|
|
options = {
|
|
|
|
nixDir = mkOption {
|
|
|
|
type = path;
|
|
|
|
default = src + /nix;
|
|
|
|
};
|
|
|
|
|
|
|
|
nixDirAliases = mkOption {
|
|
|
|
type = attrsOf (listOf str);
|
|
|
|
default = { };
|
|
|
|
};
|
2023-08-25 06:14:55 +02:00
|
|
|
};
|
|
|
|
|
2024-01-15 09:07:59 +01:00
|
|
|
config = genAttrs (subtractLists [ "_module" "nixDir" ] (attrNames options))
|
|
|
|
(name:
|
|
|
|
let
|
|
|
|
internal = options.${name}.internal or false;
|
2024-01-15 10:07:55 +01:00
|
|
|
val = importNames
|
|
|
|
(if name == "nixDirAliases" then [ name ] else
|
2024-01-15 09:07:59 +01:00
|
|
|
([ name ] ++ config.nixDirAliases.${name} or [ ]));
|
2024-01-15 10:07:55 +01:00
|
|
|
cond = !internal && val.success;
|
2024-01-15 09:07:59 +01:00
|
|
|
in
|
2024-01-15 10:07:55 +01:00
|
|
|
mkIf cond (if cond then val.value else { }));
|
2023-08-25 06:14:55 +02:00
|
|
|
}
|