1
1
forked from extern/flakelight
flakelight/misc/lock2inputs.nix
Archit Gupta cecfafe9a4 Set defaults for inputs from flake.lock
If the `src` passed to flakelight contains a flake.lock, it will now be
read and used to set defaults for `inputs`. Explicitly passed inputs
will override this.
2024-03-10 19:26:37 -07:00

47 lines
1.4 KiB
Nix

# flakelight -- Framework for simplifying flake setup
# Copyright (C) 2023 Archit Gupta <archit@accelbread.com>
# SPDX-License-Identifier: MIT
# Get a flakes inputs
{ lib, ... }:
src:
let
inherit (builtins) fromJSON head isString mapAttrs readFile tail;
inherit (lib) fix;
json = fromJSON (readFile (src + "/flake.lock"));
inherit (json) nodes;
rootNode = nodes.${json.root};
getInputName = base: ref:
let next = getInputName json.root nodes.${base}.inputs.${head ref}; in
if isString ref then ref
else if ref == [ ] then base
else getInputName next (tail ref);
getInput = ref: resolved.${getInputName json.root ref};
fetchNode = node: fetchTree (node.info or { } //
removeAttrs node.locked [ "dir" ]);
resolveFlakeNode = node: fix (self:
let
sourceInfo = fetchNode node;
outPath = sourceInfo +
(if node.locked ? dir then "/${node.locked.dir}" else "");
inputs = (mapAttrs (_: getInput) (node.inputs or { })) //
{ inherit self; };
outputs = (import (outPath + "/flake.nix")).outputs inputs;
in
outputs // sourceInfo // {
_type = "flake";
inherit outPath inputs outputs sourceInfo;
});
resolveNode = node:
if node.flake or true then resolveFlakeNode node else fetchNode node;
resolved = mapAttrs (_: resolveNode) nodes;
in
mapAttrs (_: v: resolved.${v}) rootNode.inputs