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 04:17:50 +01:00
|
|
|
{ config, lib, inputs, flakelight, genSystems, moduleArgs, ... }:
|
2023-08-25 06:14:55 +02:00
|
|
|
let
|
2024-01-16 19:48:48 +01:00
|
|
|
inherit (builtins) hasAttr parseDrvName tryEval;
|
2024-03-24 22:23:41 +01:00
|
|
|
inherit (lib) findFirst functionArgs mapAttrs' mapAttrs mkIf mkMerge mkOption
|
|
|
|
nameValuePair optionalAttrs;
|
2024-02-07 09:54:03 +01:00
|
|
|
inherit (lib.types) lazyAttrsOf str uniq;
|
|
|
|
inherit (flakelight.types) nullable optCallWith overlay packageDef;
|
2023-08-25 06:14:55 +02:00
|
|
|
|
2024-01-16 19:48:48 +01:00
|
|
|
genPkg = final: prev: name: pkg:
|
|
|
|
let
|
2024-01-16 20:01:32 +01:00
|
|
|
args = functionArgs pkg;
|
|
|
|
noArgs = args == { };
|
|
|
|
pkg' = if noArgs then { pkgs }: pkg pkgs else pkg;
|
2024-01-16 19:48:48 +01:00
|
|
|
dependsOnSelf = hasAttr name (functionArgs pkg);
|
2024-01-16 20:01:32 +01:00
|
|
|
dependsOnPkgs = noArgs || (args ? pkgs);
|
2024-01-16 19:48:48 +01:00
|
|
|
selfOverride = {
|
|
|
|
${name} = prev.${name} or
|
|
|
|
(throw "${name} depends on ${name}, but no existing ${name}.");
|
|
|
|
};
|
|
|
|
overrides = optionalAttrs dependsOnSelf selfOverride
|
|
|
|
// optionalAttrs dependsOnPkgs { pkgs = final.pkgs // selfOverride; };
|
|
|
|
in
|
2024-01-16 20:01:32 +01:00
|
|
|
final.callPackage pkg' overrides;
|
2024-01-16 19:48:48 +01:00
|
|
|
genPkgs = final: prev: pkgs:
|
|
|
|
mapAttrs (name: genPkg final prev name) pkgs;
|
2023-08-25 06:14:55 +02:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
package = mkOption {
|
2024-02-07 09:54:03 +01:00
|
|
|
type = nullable packageDef;
|
2023-08-25 06:14:55 +02:00
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
packages = mkOption {
|
2024-01-15 04:17:50 +01:00
|
|
|
type = optCallWith moduleArgs (lazyAttrsOf packageDef);
|
2023-08-25 06:14:55 +02:00
|
|
|
default = { };
|
|
|
|
};
|
2023-08-27 22:57:16 +02:00
|
|
|
|
2024-01-12 02:27:34 +01:00
|
|
|
pname = mkOption {
|
2024-02-07 09:54:03 +01:00
|
|
|
type = nullable str;
|
2024-01-12 02:27:34 +01:00
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
|
2023-08-27 22:57:16 +02:00
|
|
|
packageOverlay = mkOption {
|
|
|
|
internal = true;
|
|
|
|
type = uniq overlay;
|
|
|
|
default = _: _: { };
|
|
|
|
};
|
2023-08-25 06:14:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkMerge [
|
|
|
|
(mkIf (config.package != null) {
|
|
|
|
packages.default = config.package;
|
|
|
|
})
|
|
|
|
|
|
|
|
(mkIf (config.packages != { }) {
|
2023-08-27 22:57:16 +02:00
|
|
|
packageOverlay = final: prev:
|
2023-08-25 06:14:55 +02:00
|
|
|
let
|
2023-08-27 22:57:16 +02:00
|
|
|
getName = pkg: pkg.pname or (parseDrvName pkg.name).name;
|
2024-01-12 08:21:49 +01:00
|
|
|
mockPkgs = import ../misc/nameMockedPkgs.nix prev;
|
2024-01-12 02:27:34 +01:00
|
|
|
|
|
|
|
defaultPkgName = findFirst (x: (tryEval x).success)
|
|
|
|
(throw ("Could not determine the name of the default package; " +
|
|
|
|
"please set the `pname` flakelight option to the intended name."))
|
|
|
|
[
|
|
|
|
(assert config.pname != null; config.pname)
|
2024-01-12 08:21:49 +01:00
|
|
|
(getName (mockPkgs.callPackage config.packages.default { }))
|
2024-01-12 02:27:34 +01:00
|
|
|
(getName (import inputs.nixpkgs {
|
|
|
|
inherit (prev.stdenv.hostPlatform) system;
|
|
|
|
inherit (config.nixpkgs) config;
|
2024-01-16 19:48:48 +01:00
|
|
|
overlays = config.withOverlays ++
|
|
|
|
[ (final: prev: genPkgs final prev config.packages) ];
|
2024-01-12 02:27:34 +01:00
|
|
|
}).default)
|
|
|
|
];
|
2023-08-25 06:14:55 +02:00
|
|
|
in
|
2024-01-16 19:48:48 +01:00
|
|
|
(optionalAttrs (config.packages ? default) rec {
|
|
|
|
default = genPkg final prev defaultPkgName
|
|
|
|
config.packages.default;
|
|
|
|
${defaultPkgName} = default;
|
|
|
|
}) // genPkgs final prev (removeAttrs config.packages [ "default" ]);
|
2023-08-25 06:14:55 +02:00
|
|
|
|
2023-08-27 22:57:16 +02:00
|
|
|
overlay = final: prev: removeAttrs
|
|
|
|
(config.packageOverlay (final.appendOverlays config.withOverlays) prev)
|
|
|
|
[ "default" ];
|
2023-08-25 06:14:55 +02:00
|
|
|
|
2024-01-10 10:16:24 +01:00
|
|
|
outputs = rec {
|
|
|
|
packages = genSystems (pkgs:
|
2024-03-24 22:23:41 +01:00
|
|
|
mapAttrs (k: _: pkgs.${k}) config.packages);
|
2023-08-25 06:14:55 +02:00
|
|
|
|
2024-01-10 10:16:24 +01:00
|
|
|
checks = mapAttrs
|
|
|
|
(_: mapAttrs' (n: nameValuePair ("packages-" + n)))
|
|
|
|
packages;
|
2023-08-25 06:14:55 +02:00
|
|
|
};
|
|
|
|
})
|
2024-02-12 03:09:55 +01:00
|
|
|
|
|
|
|
(mkIf (config.packages ? default) {
|
|
|
|
devShell.inputsFrom = pkgs: [ pkgs.default ];
|
|
|
|
})
|
2023-08-25 06:14:55 +02:00
|
|
|
];
|
|
|
|
}
|