2023-08-27 07:48:57 +02:00
|
|
|
# flakelight -- Framework for simplifying flake setup
|
2023-04-09 02:56:06 +02:00
|
|
|
# Copyright (C) 2023 Archit Gupta <archit@accelbread.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2024-01-10 08:23:13 +01:00
|
|
|
inputs:
|
2023-04-09 02:56:06 +02:00
|
|
|
let
|
2024-01-10 08:23:13 +01:00
|
|
|
inherit (inputs) nixpkgs;
|
2023-08-25 06:14:55 +02:00
|
|
|
inherit (builtins) isAttrs isPath readDir;
|
2024-02-07 09:54:03 +01:00
|
|
|
inherit (nixpkgs.lib) all attrNames composeManyExtensions evalModules filter
|
2024-02-25 06:22:00 +01:00
|
|
|
findFirst fix genAttrs getValues hasSuffix isDerivation isFunction isList
|
|
|
|
isStringLike mapAttrs mapAttrsToList mkDefault mkOptionType pathExists pipe
|
|
|
|
removePrefix removeSuffix singleton warn;
|
2024-02-07 09:54:03 +01:00
|
|
|
inherit (nixpkgs.lib.types) coercedTo defaultFunctor functionTo listOf
|
|
|
|
optionDescriptionPhrase;
|
2023-10-19 04:58:23 +02:00
|
|
|
inherit (nixpkgs.lib.options) mergeEqualOption mergeOneOption;
|
2023-08-25 06:14:55 +02:00
|
|
|
|
|
|
|
builtinModules = mapAttrsToList (k: _: ./builtinModules + ("/" + k))
|
|
|
|
(readDir ./builtinModules);
|
|
|
|
|
2023-09-14 07:39:20 +02:00
|
|
|
mkFlake = {
|
|
|
|
__functor = self: src: root: (evalModules {
|
|
|
|
specialArgs.modulesPath = ./builtinModules;
|
|
|
|
modules = builtinModules ++ self.extraModules ++ [
|
|
|
|
{ inputs.nixpkgs = mkDefault nixpkgs; }
|
2024-01-10 08:23:13 +01:00
|
|
|
{ inputs.flakelight = mkDefault inputs.self; }
|
2023-09-14 07:39:20 +02:00
|
|
|
{ _module.args = { inherit src flakelight; }; }
|
|
|
|
root
|
|
|
|
];
|
|
|
|
}).config.outputs;
|
|
|
|
|
|
|
|
# Attributes to allow module flakes to extend mkFlake
|
|
|
|
extraModules = [ ];
|
|
|
|
extend = (fix (extend': mkFlake': modules: fix (self: mkFlake' // {
|
|
|
|
extraModules = mkFlake'.extraModules ++ modules;
|
|
|
|
extend = extend' self;
|
|
|
|
}))) mkFlake;
|
|
|
|
};
|
2023-08-25 06:14:55 +02:00
|
|
|
|
2023-08-27 07:48:57 +02:00
|
|
|
flakelight = {
|
2024-03-24 22:23:41 +01:00
|
|
|
inherit autoImport autoImportArgs importDir mkFlake selectAttr types;
|
2024-01-14 21:59:39 +01:00
|
|
|
};
|
|
|
|
|
2024-02-07 09:54:03 +01:00
|
|
|
types = rec {
|
2024-01-14 21:59:39 +01:00
|
|
|
overlay = mkOptionType {
|
|
|
|
name = "overlay";
|
|
|
|
description = "nixpkgs overlay";
|
|
|
|
descriptionClass = "noun";
|
|
|
|
check = isFunction;
|
|
|
|
merge = _: defs: composeManyExtensions (getValues defs);
|
|
|
|
};
|
|
|
|
|
|
|
|
packageDef = mkOptionType {
|
|
|
|
name = "packageDef";
|
|
|
|
description = "package definition";
|
|
|
|
descriptionClass = "noun";
|
|
|
|
check = isFunction;
|
|
|
|
merge = mergeOneOption;
|
|
|
|
};
|
|
|
|
|
|
|
|
path = mkOptionType {
|
|
|
|
name = "path";
|
|
|
|
description = "path";
|
|
|
|
descriptionClass = "noun";
|
|
|
|
check = isPath;
|
|
|
|
merge = mergeEqualOption;
|
|
|
|
};
|
|
|
|
|
|
|
|
function = mkOptionType {
|
|
|
|
name = "function";
|
|
|
|
description = "function";
|
|
|
|
descriptionClass = "noun";
|
|
|
|
check = isFunction;
|
|
|
|
merge = mergeOneOption;
|
2023-04-09 02:56:06 +02:00
|
|
|
};
|
2024-01-14 21:59:39 +01:00
|
|
|
|
2024-02-25 06:22:00 +01:00
|
|
|
drv = mkOptionType {
|
|
|
|
name = "drv";
|
|
|
|
description = "derivation";
|
|
|
|
descriptionClass = "noun";
|
|
|
|
check = isDerivation;
|
|
|
|
merge = mergeOneOption;
|
|
|
|
};
|
|
|
|
|
2024-02-22 03:43:05 +01:00
|
|
|
stringLike = mkOptionType {
|
|
|
|
name = "stringLike";
|
|
|
|
description = "string-convertible value";
|
|
|
|
descriptionClass = "noun";
|
|
|
|
check = isStringLike;
|
|
|
|
merge = mergeEqualOption;
|
|
|
|
};
|
|
|
|
|
2024-01-14 21:59:39 +01:00
|
|
|
module = mkOptionType {
|
|
|
|
name = "module";
|
|
|
|
description = "module";
|
|
|
|
descriptionClass = "noun";
|
|
|
|
check = x: isPath x || isFunction x || isAttrs x;
|
|
|
|
merge = _: defs: { imports = getValues defs; };
|
|
|
|
};
|
|
|
|
|
|
|
|
fileset = mkOptionType {
|
|
|
|
name = "fileset";
|
|
|
|
description = "fileset";
|
|
|
|
descriptionClass = "noun";
|
|
|
|
check = x: isPath x || x._type or null == "fileset";
|
|
|
|
};
|
|
|
|
|
|
|
|
optListOf = elemType: coercedTo elemType singleton (listOf elemType);
|
|
|
|
|
2024-02-25 06:22:00 +01:00
|
|
|
coercedTo' = coercedType: coerceFunc: finalType:
|
|
|
|
(coercedTo coercedType coerceFunc finalType) // {
|
|
|
|
merge = loc: defs:
|
|
|
|
let
|
|
|
|
coerceVal = val:
|
|
|
|
if finalType.check val then val
|
|
|
|
else coerceFunc val;
|
|
|
|
in
|
|
|
|
finalType.merge loc
|
|
|
|
(map (def: def // { value = coerceVal def.value; }) defs);
|
|
|
|
};
|
|
|
|
|
2024-02-22 03:40:32 +01:00
|
|
|
optFunctionTo =
|
|
|
|
let
|
|
|
|
nonFunction = mkOptionType {
|
|
|
|
name = "nonFunction";
|
|
|
|
description = "non-function";
|
|
|
|
descriptionClass = "noun";
|
|
|
|
check = x: ! isFunction x;
|
|
|
|
merge = mergeOneOption;
|
|
|
|
};
|
|
|
|
in
|
|
|
|
elemType: coercedTo nonFunction (x: _: x)
|
|
|
|
(functionTo elemType);
|
2024-01-15 04:05:39 +01:00
|
|
|
|
2024-02-07 10:15:56 +01:00
|
|
|
optCallWith = args: elemType: coercedTo function (x: x args) elemType;
|
2024-02-07 09:54:03 +01:00
|
|
|
|
|
|
|
nullable = elemType: mkOptionType {
|
|
|
|
name = "nullable";
|
|
|
|
description = "nullable ${optionDescriptionPhrase
|
|
|
|
(class: class == "noun" || class == "composite") elemType}";
|
|
|
|
descriptionClass = "noun";
|
|
|
|
check = x: x == null || elemType.check x;
|
|
|
|
merge = loc: defs:
|
|
|
|
if all (def: def.value == null) defs then null
|
|
|
|
else elemType.merge loc (filter (def: def.value != null) defs);
|
|
|
|
emptyValue.value = null;
|
|
|
|
inherit (elemType) getSubOptions getSubModules;
|
|
|
|
substSubModules = m: nullable (elemType.substSubModules m);
|
|
|
|
functor = (defaultFunctor "nullable") // {
|
|
|
|
type = nullable;
|
|
|
|
wrapped = elemType;
|
|
|
|
};
|
|
|
|
nestedTypes = { inherit elemType; };
|
|
|
|
};
|
2023-04-09 02:56:06 +02:00
|
|
|
};
|
|
|
|
|
2023-04-15 22:42:53 +02:00
|
|
|
importDir = path: genAttrs
|
2023-04-15 14:00:19 +02:00
|
|
|
(pipe (readDir path) [
|
|
|
|
attrNames
|
|
|
|
(filter (s: s != "default.nix"))
|
2024-01-14 10:24:52 +01:00
|
|
|
(filter (s: (hasSuffix ".nix" s)
|
|
|
|
|| pathExists (path + "/${s}/default.nix")))
|
2023-04-15 14:00:19 +02:00
|
|
|
(map (removeSuffix ".nix"))
|
2023-04-15 20:02:06 +02:00
|
|
|
(map (removePrefix "_"))
|
2023-04-15 14:00:19 +02:00
|
|
|
])
|
2024-01-14 10:24:52 +01:00
|
|
|
(p: import (path +
|
|
|
|
(if pathExists (path + "/_${p}.nix") then "/_${p}.nix"
|
|
|
|
else if pathExists (path + "/${p}.nix") then "/${p}.nix"
|
|
|
|
else "/${p}")));
|
2023-04-15 14:00:19 +02:00
|
|
|
|
2024-01-15 09:26:05 +01:00
|
|
|
autoImport = dir: name: warn
|
|
|
|
("The autoImport function is deprecated. " +
|
|
|
|
"All options are now automatically auto-loaded.")
|
|
|
|
(if isList name
|
2023-04-22 20:44:44 +02:00
|
|
|
then findFirst (x: x != null) null (map (autoImport dir) name)
|
|
|
|
else
|
|
|
|
if pathExists (dir + "/${name}.nix")
|
|
|
|
then import (dir + "/${name}.nix")
|
|
|
|
else if pathExists (dir + "/${name}/default.nix")
|
|
|
|
then import (dir + "/${name}")
|
|
|
|
else if pathExists (dir + "/${name}")
|
|
|
|
then importDir (dir + "/${name}")
|
2024-01-15 09:26:05 +01:00
|
|
|
else null);
|
2023-04-15 13:41:27 +02:00
|
|
|
|
2024-01-15 04:17:50 +01:00
|
|
|
autoImportArgs = dir: args: name: warn
|
|
|
|
("The autoImportArgs function is deprecated. " +
|
|
|
|
"Wrap the target type in flakelight.types.optCallWith instead.")
|
|
|
|
(
|
|
|
|
let v = autoImport dir name; in
|
|
|
|
if isFunction v then v args else v
|
|
|
|
);
|
2024-01-14 21:59:39 +01:00
|
|
|
|
|
|
|
selectAttr = attr: mapAttrs (_: v: v.${attr} or { });
|
2023-04-09 02:56:06 +02:00
|
|
|
in
|
2023-09-14 06:00:14 +02:00
|
|
|
flakelight
|