2022-05-19 10:57:42 +00:00
|
|
|
{ flake ? null }:
|
2021-08-17 08:55:08 +00:00
|
|
|
let
|
2022-05-19 10:57:42 +00:00
|
|
|
resolved = builtins.getFlake "input-flake";
|
2021-08-17 08:55:08 +00:00
|
|
|
|
|
|
|
nixpkgs = (import <nixpkgs> {});
|
|
|
|
lib = nixpkgs.lib;
|
|
|
|
|
|
|
|
# filter = lib.filterAttrs (key: _ : key == "apps" || key == "packages");
|
|
|
|
|
|
|
|
withSystem = fn: lib.mapAttrs (system: drvs: (fn system drvs));
|
|
|
|
isValid = d:
|
|
|
|
let
|
2022-01-06 14:42:31 +00:00
|
|
|
r = builtins.tryEval (lib.isDerivation d && ! (lib.attrByPath [ "meta" "broken" ] false d) &&
|
|
|
|
builtins.seq d.name true && d ? outputs);
|
2021-08-17 08:55:08 +00:00
|
|
|
in
|
|
|
|
r.success && r.value;
|
2022-01-06 14:42:31 +00:00
|
|
|
validPkgs = lib.filterAttrs (k: v: isValid v);
|
2021-08-17 08:55:08 +00:00
|
|
|
|
|
|
|
readPackages = system: drvs: lib.mapAttrsToList (
|
|
|
|
attribute_name: drv: (
|
|
|
|
{
|
2022-10-28 13:49:15 +00:00
|
|
|
entry_type = "package";
|
2021-08-17 08:55:08 +00:00
|
|
|
attribute_name = attribute_name;
|
|
|
|
system = system;
|
|
|
|
name = drv.name;
|
|
|
|
# TODO consider using `builtins.parseDrvName`
|
2022-01-06 14:42:31 +00:00
|
|
|
version = drv.version or "";
|
2021-08-17 08:55:08 +00:00
|
|
|
outputs = drv.outputs;
|
|
|
|
# paths = builtins.listToAttrs ( map (output: {name = output; value = drv.${output};}) drv.outputs );
|
2022-03-27 13:52:54 +00:00
|
|
|
default_output = drv.outputName;
|
2021-08-17 08:55:08 +00:00
|
|
|
}
|
2023-02-05 09:24:26 +00:00
|
|
|
// lib.optionalAttrs (drv ? meta.description) { inherit (drv.meta) description; }
|
|
|
|
// lib.optionalAttrs (drv ? meta.longDescription) { inherit (drv.meta) longDescription; }
|
|
|
|
// lib.optionalAttrs (drv ? meta.license) { inherit (drv.meta) license; }
|
2021-08-17 08:55:08 +00:00
|
|
|
)
|
2022-01-06 14:42:31 +00:00
|
|
|
) (validPkgs drvs);
|
2021-08-17 08:55:08 +00:00
|
|
|
readApps = system: apps: lib.mapAttrsToList (
|
|
|
|
attribute_name: app: (
|
|
|
|
{
|
2022-10-28 13:49:15 +00:00
|
|
|
entry_type = "app";
|
2021-08-17 08:55:08 +00:00
|
|
|
attribute_name = attribute_name;
|
|
|
|
system = system;
|
|
|
|
}
|
|
|
|
// lib.optionalAttrs (app ? outPath) { bin = app.outPath; }
|
|
|
|
// lib.optionalAttrs (app ? program) { bin = app.program; }
|
|
|
|
// lib.optionalAttrs (app ? type) { type = app.type; }
|
|
|
|
)
|
|
|
|
) apps;
|
|
|
|
|
2022-09-08 14:29:53 +00:00
|
|
|
readNixOSOptions = let
|
2021-08-17 08:55:08 +00:00
|
|
|
declarations = module: (
|
|
|
|
lib.evalModules {
|
|
|
|
modules = (if lib.isList module then module else [ module ]) ++ [
|
|
|
|
(
|
|
|
|
{ ... }: {
|
|
|
|
_module.check = false;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
];
|
2022-09-08 14:29:53 +00:00
|
|
|
specialArgs = {
|
|
|
|
# !!! NixOS-specific. Unfortunately, NixOS modules can rely on the `modulesPath`
|
|
|
|
# argument to import modules from the nixos tree. However, most of the time
|
|
|
|
# this is done to import *profiles* which do not declare any options, so we
|
|
|
|
# can allow it.
|
|
|
|
modulesPath = "${nixpkgs.path}/nixos/modules";
|
|
|
|
};
|
2021-08-17 08:55:08 +00:00
|
|
|
}
|
|
|
|
).options;
|
|
|
|
|
2022-01-06 14:42:31 +00:00
|
|
|
cleanUpOption = extraAttrs: opt:
|
2021-08-17 08:55:08 +00:00
|
|
|
let
|
2021-12-26 15:03:09 +00:00
|
|
|
applyOnAttr = n: f: lib.optionalAttrs (builtins.hasAttr n opt) { ${n} = f opt.${n}; };
|
2021-08-25 22:40:42 +00:00
|
|
|
mkDeclaration = decl:
|
|
|
|
let
|
2021-08-26 07:43:23 +00:00
|
|
|
discard = lib.concatStringsSep "/" (lib.take 4 (lib.splitString "/" decl)) + "/";
|
2021-08-25 22:40:42 +00:00
|
|
|
path = if lib.hasPrefix builtins.storeDir decl then lib.removePrefix discard decl else decl;
|
|
|
|
in
|
|
|
|
path;
|
|
|
|
|
2021-08-17 08:55:08 +00:00
|
|
|
# Replace functions by the string <function>
|
|
|
|
substFunction = x:
|
2021-12-26 15:03:09 +00:00
|
|
|
if builtins.isAttrs x then
|
|
|
|
lib.mapAttrs (_:substFunction ) x
|
2021-08-17 08:55:08 +00:00
|
|
|
else if builtins.isList x then
|
|
|
|
map substFunction x
|
|
|
|
else if lib.isFunction x then
|
2021-12-26 15:03:09 +00:00
|
|
|
"function"
|
2021-08-17 08:55:08 +00:00
|
|
|
else
|
2021-12-26 15:03:09 +00:00
|
|
|
x;
|
2021-08-17 08:55:08 +00:00
|
|
|
in
|
2021-08-25 22:40:42 +00:00
|
|
|
opt
|
2022-10-28 13:49:15 +00:00
|
|
|
// { entry_type = "option"; }
|
2021-08-17 08:55:08 +00:00
|
|
|
// applyOnAttr "default" substFunction
|
2021-12-26 15:03:09 +00:00
|
|
|
// applyOnAttr "example" substFunction # (_: { __type = "function"; })
|
2021-08-17 08:55:08 +00:00
|
|
|
// applyOnAttr "type" substFunction
|
2021-08-25 22:40:42 +00:00
|
|
|
// applyOnAttr "declarations" (map mkDeclaration)
|
2022-01-06 14:42:31 +00:00
|
|
|
// extraAttrs;
|
|
|
|
in
|
|
|
|
{ module, modulePath ? null }: let
|
|
|
|
opts = lib.optionAttrSetToDocList (declarations module);
|
|
|
|
extraAttrs = lib.optionalAttrs (modulePath != null) {
|
|
|
|
flake = modulePath;
|
|
|
|
};
|
|
|
|
in
|
2022-09-08 08:45:01 +00:00
|
|
|
map (cleanUpOption extraAttrs) (lib.filter (x: x.visible && !x.internal && lib.head x.loc != "_module") opts);
|
2021-08-17 08:55:08 +00:00
|
|
|
|
2022-01-06 14:42:31 +00:00
|
|
|
readFlakeOptions = let
|
|
|
|
nixosModulesOpts = builtins.concatLists (lib.mapAttrsToList (moduleName: module:
|
2022-09-08 14:29:53 +00:00
|
|
|
readNixOSOptions {
|
2022-01-06 14:42:31 +00:00
|
|
|
inherit module;
|
|
|
|
modulePath = [ flake moduleName ];
|
|
|
|
}
|
|
|
|
) (resolved.nixosModules or {}));
|
2021-08-17 08:55:08 +00:00
|
|
|
|
2022-01-06 14:42:31 +00:00
|
|
|
nixosModuleOpts = lib.optionals (resolved ? nixosModule) (
|
2022-09-08 14:29:53 +00:00
|
|
|
readNixOSOptions {
|
2022-01-06 14:42:31 +00:00
|
|
|
module = resolved.nixosModule;
|
|
|
|
modulePath = [ flake ];
|
|
|
|
}
|
|
|
|
);
|
2021-08-17 08:55:08 +00:00
|
|
|
in
|
2022-01-06 14:42:31 +00:00
|
|
|
# We assume that `nixosModules` includes `nixosModule` when there
|
|
|
|
# are multiple modules
|
|
|
|
if nixosModulesOpts != [] then nixosModulesOpts else nixosModuleOpts;
|
2021-08-17 08:55:08 +00:00
|
|
|
|
|
|
|
read = reader: set: lib.flatten (lib.attrValues (withSystem reader set));
|
|
|
|
|
2022-01-06 14:42:31 +00:00
|
|
|
legacyPackages' = read readPackages (resolved.legacyPackages or {});
|
|
|
|
packages' = read readPackages (resolved.packages or {});
|
2021-08-17 08:55:08 +00:00
|
|
|
|
2022-01-06 14:42:31 +00:00
|
|
|
apps' = read readApps (resolved.apps or {});
|
2021-08-17 08:55:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
collectSystems = lib.lists.foldr (
|
|
|
|
drv@{ attribute_name, system, ... }: set:
|
|
|
|
let
|
2022-01-06 14:42:31 +00:00
|
|
|
present = set."${attribute_name}" or ({ platforms = []; } // drv);
|
2021-08-17 08:55:08 +00:00
|
|
|
|
|
|
|
drv' = present // {
|
|
|
|
platforms = present.platforms ++ [ system ];
|
|
|
|
};
|
|
|
|
drv'' = removeAttrs drv' [ "system" ];
|
|
|
|
in
|
|
|
|
set // {
|
|
|
|
${attribute_name} = drv'';
|
|
|
|
}
|
|
|
|
) {};
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
rec {
|
|
|
|
legacyPackages = lib.attrValues (collectSystems legacyPackages');
|
|
|
|
packages = lib.attrValues (collectSystems packages');
|
|
|
|
apps = lib.attrValues (collectSystems apps');
|
2022-01-06 14:42:31 +00:00
|
|
|
options = readFlakeOptions;
|
2021-08-17 08:55:08 +00:00
|
|
|
all = packages ++ apps ++ options;
|
2022-05-19 10:57:42 +00:00
|
|
|
|
|
|
|
# nixpkgs-specific, doesn't use the flake argument
|
2023-06-28 15:44:44 +00:00
|
|
|
nixos-options = readNixOSOptions {
|
|
|
|
module = import <nixpkgs/nixos/modules/module-list.nix> ++ [
|
|
|
|
<nixpkgs/nixos/modules/virtualisation/qemu-vm.nix>
|
|
|
|
{ nixpkgs.hostPlatform = "x86_64-linux"; }
|
|
|
|
];
|
|
|
|
};
|
2021-08-17 08:55:08 +00:00
|
|
|
}
|