2021-08-17 08:55:08 +00:00
|
|
|
{ flake }:
|
|
|
|
let
|
|
|
|
resolved = builtins.getFlake (toString flake);
|
|
|
|
|
|
|
|
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: (
|
|
|
|
{
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
// lib.optionalAttrs (drv ? meta && drv.meta ? description) { inherit (drv.meta) description; }
|
|
|
|
// lib.optionalAttrs (drv ? meta && drv.meta ? license) { inherit (drv.meta) license; }
|
|
|
|
)
|
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: (
|
|
|
|
{
|
|
|
|
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-01-06 14:42:31 +00:00
|
|
|
readOptions = 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;
|
|
|
|
nixpkgs.system = lib.mkDefault "x86_64-linux";
|
|
|
|
nixpkgs.config.allowBroken = true;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
).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
|
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
|
|
|
|
map (cleanUpOption extraAttrs) (lib.filter (x: !x.internal) 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:
|
|
|
|
readOptions {
|
|
|
|
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) (
|
|
|
|
readOptions {
|
|
|
|
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;
|
|
|
|
nixos-options = readOptions {
|
|
|
|
module = import "${builtins.fetchTarball { url = flake; }}/nixos/modules/module-list.nix";
|
|
|
|
};
|
2021-08-17 08:55:08 +00:00
|
|
|
all = packages ++ apps ++ options;
|
|
|
|
}
|