Skip to content

Instantly share code, notes, and snippets.

@tfc
Created May 20, 2021 14:50
Show Gist options
  • Save tfc/f0fa99f852ea7fd1cff58d2b2810b126 to your computer and use it in GitHub Desktop.
Save tfc/f0fa99f852ea7fd1cff58d2b2810b126 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#productization: don't use <nixpkgs>
IFS='' read -r -d '' expression <<"EOF"
{ inputPath }:
let
pkgs = import <nixpkgs> {};
input = import inputPath;
pathString = builtins.foldl'
(current: attr:
(if current == "" then "" else current + ".") + attr
) "";
isDerivation = x: x ? "type" && x.type == "derivation";
discoverBuildAttributes = currentPath: currentSet:
let
accumulate = accumList: name:
let
value = currentSet."${name}";
attrsInSet =
if isDerivation value then [ (pathString (currentPath ++ [ name ])) ]
else if builtins.typeOf value == "set" then discoverBuildAttributes (currentPath ++ [ name ]) value
else [ ];
in
accumList ++ attrsInSet;
in
builtins.foldl' accumulate [ ] (builtins.attrNames currentSet);
in
# Discover all build attributes starting from the root
discoverBuildAttributes [ ] input
EOF
inputPath=$(readlink -f $1)
# productization: check if this is an existing file
nix-instantiate --eval --json --strict -E "$expression" --argstr inputPath "$inputPath"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment