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
|
|
|
|
|
2023-09-14 06:00:14 +02:00
|
|
|
nixpkgs:
|
2023-04-09 02:56:06 +02:00
|
|
|
let
|
2023-08-25 06:14:55 +02:00
|
|
|
inherit (builtins) isAttrs isPath readDir;
|
2023-09-14 06:00:14 +02:00
|
|
|
inherit (nixpkgs.lib) attrNames composeManyExtensions
|
2023-09-14 07:39:20 +02:00
|
|
|
filter findFirst fix genAttrs getValues hasSuffix isFunction isList
|
2023-08-25 06:14:55 +02:00
|
|
|
mapAttrsToList pathExists pipe removePrefix removeSuffix evalModules
|
|
|
|
mkDefault mkOptionType singleton;
|
2023-09-14 06:00:14 +02:00
|
|
|
inherit (nixpkgs.lib.types) coercedTo functionTo listOf;
|
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; }
|
|
|
|
{ _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 = {
|
2023-08-27 09:01:48 +02:00
|
|
|
inherit mkFlake supportedSystem autoImport autoImportArgs;
|
2023-08-25 06:14:55 +02:00
|
|
|
|
|
|
|
types = {
|
|
|
|
overlay = mkOptionType {
|
|
|
|
name = "overlay";
|
|
|
|
description = "nixpkgs overlay";
|
|
|
|
descriptionClass = "noun";
|
|
|
|
check = isFunction;
|
|
|
|
merge = _: defs: composeManyExtensions (getValues defs);
|
|
|
|
};
|
2023-04-09 02:56:06 +02:00
|
|
|
|
2023-08-25 06:14:55 +02:00
|
|
|
packageDef = mkOptionType {
|
|
|
|
name = "packageDef";
|
|
|
|
description = "package definition";
|
|
|
|
descriptionClass = "noun";
|
|
|
|
check = isFunction;
|
2023-10-19 04:58:23 +02:00
|
|
|
merge = mergeOneOption;
|
2023-08-25 06:14:55 +02:00
|
|
|
};
|
2023-04-15 05:16:50 +02:00
|
|
|
|
2023-08-25 06:14:55 +02:00
|
|
|
path = mkOptionType {
|
|
|
|
name = "path";
|
|
|
|
description = "path";
|
|
|
|
descriptionClass = "noun";
|
|
|
|
check = isPath;
|
2023-10-19 04:58:23 +02:00
|
|
|
merge = mergeEqualOption;
|
2023-08-25 06:14:55 +02:00
|
|
|
};
|
|
|
|
|
Allow bundlers to be functions that take pkgs
In addition to a regular bundler of the form `x: x`, this allows setting
bundler options to a function of the form `pkgs: x: x` which is passed
the package set for the system (for example: `{ hello, ... }: x: hello`
to always return hello). This allows, in particular, an autoloaded
bundler in its own file to access the package set.
This is non-trivial as we must tell `x: x` and `pkgs: x: x` apart.
Fortunately, given some derivation `drv`, `pkgs // drv` is a valid
derivation and a set with attr names matching pkgs. When applying this
to the function, if it returns a derivation, it was of the `x: x` form,
and if it returns a function, it was of the `pkgs: x: x` form.
In order to prevent IFD when evaluating the flake if the bundler is of
form `x: x` and uses IFD, we determine the form and apply pkgs when
applying the bundler instead of during flake evaluation. This is done by
wrapping the bundler.
Of note, we cannot rely on `builtins.functionArgs`, since
`pkgs: { hello, ... }: (x: hello) pkgs` is the same as the inner
function but with args hidden.
2023-11-07 08:13:47 +01:00
|
|
|
function = mkOptionType {
|
|
|
|
name = "function";
|
|
|
|
description = "function";
|
|
|
|
descriptionClass = "noun";
|
|
|
|
check = isFunction;
|
|
|
|
merge = mergeOneOption;
|
|
|
|
};
|
|
|
|
|
2023-08-25 06:14:55 +02:00
|
|
|
module = mkOptionType {
|
|
|
|
name = "module";
|
|
|
|
description = "module";
|
|
|
|
descriptionClass = "noun";
|
|
|
|
check = x: isPath x || isFunction x || isAttrs x;
|
|
|
|
merge = _: defs: { imports = getValues defs; };
|
2023-04-17 01:31:17 +02:00
|
|
|
};
|
2023-08-25 06:14:55 +02:00
|
|
|
|
|
|
|
optListOf = elemType: coercedTo elemType singleton (listOf elemType);
|
|
|
|
|
|
|
|
optFunctionTo = elemType: coercedTo elemType (x: _: x)
|
|
|
|
(functionTo elemType);
|
2023-04-09 02:56:06 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-08-25 06:14:55 +02:00
|
|
|
supportedSystem = { lib, stdenv, ... }:
|
|
|
|
lib.meta.availableOn stdenv.hostPlatform;
|
2023-04-17 01:31:17 +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"))
|
|
|
|
(filter (hasSuffix ".nix"))
|
|
|
|
(map (removeSuffix ".nix"))
|
2023-04-15 20:02:06 +02:00
|
|
|
(map (removePrefix "_"))
|
2023-04-15 14:00:19 +02:00
|
|
|
])
|
|
|
|
(p: import (path + (if pathExists
|
2023-04-15 20:02:06 +02:00
|
|
|
(path + "/_${p}.nix") then "/_${p}.nix" else "/${p}.nix")));
|
2023-04-15 14:00:19 +02:00
|
|
|
|
2023-04-22 20:44:44 +02:00
|
|
|
autoImport = dir: name:
|
|
|
|
if isList name
|
|
|
|
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}")
|
|
|
|
else null;
|
2023-04-15 13:41:27 +02:00
|
|
|
|
2023-08-27 09:01:48 +02:00
|
|
|
autoImportArgs = dir: args: name:
|
|
|
|
let v = autoImport dir name; in
|
|
|
|
if isFunction v then v args else v;
|
2023-04-09 02:56:06 +02:00
|
|
|
in
|
2023-09-14 06:00:14 +02:00
|
|
|
flakelight
|