2022-04-08 06:45:09 +00:00
|
|
|
{ lib, stdenv, resholve, binlore, writeTextFile }:
|
2021-09-23 22:40:35 +00:00
|
|
|
|
|
|
|
rec {
|
|
|
|
/* These functions break up the work of partially validating the
|
|
|
|
'solutions' attrset and massaging it into env/cli args.
|
|
|
|
|
|
|
|
Note: some of the left-most args do not *have* to be passed as
|
|
|
|
deep as they are, but I've done so to provide more error context
|
|
|
|
*/
|
|
|
|
|
|
|
|
# for brevity / line length
|
|
|
|
spaces = l: builtins.concatStringsSep " " l;
|
2022-04-08 06:45:09 +00:00
|
|
|
colons = l: builtins.concatStringsSep ":" l;
|
2021-09-23 22:40:35 +00:00
|
|
|
semicolons = l: builtins.concatStringsSep ";" l;
|
|
|
|
|
|
|
|
/* Throw a fit with dotted attr path context */
|
|
|
|
nope = path: msg:
|
|
|
|
throw "${builtins.concatStringsSep "." path}: ${msg}";
|
|
|
|
|
|
|
|
/* Special-case directive value representations by type */
|
2022-04-08 06:45:09 +00:00
|
|
|
phraseDirective = solution: env: name: val:
|
2021-09-23 22:40:35 +00:00
|
|
|
if builtins.isInt val then builtins.toString val
|
|
|
|
else if builtins.isString val then name
|
|
|
|
else if true == val then name
|
|
|
|
else if false == val then "" # omit!
|
|
|
|
else if null == val then "" # omit!
|
2022-04-08 06:45:09 +00:00
|
|
|
else if builtins.isList val then "${name}:${semicolons (map lib.escapeShellArg val)}"
|
2021-09-23 22:40:35 +00:00
|
|
|
else nope [ solution env name ] "unexpected type: ${builtins.typeOf val}";
|
|
|
|
|
|
|
|
/* Build fake/fix/keep directives from Nix types */
|
2022-04-08 06:45:09 +00:00
|
|
|
phraseDirectives = solution: env: val:
|
|
|
|
lib.mapAttrsToList (phraseDirective solution env) val;
|
|
|
|
|
|
|
|
/* Custom ~search-path routine to handle relative path strings */
|
|
|
|
relSafeBinPath = input:
|
|
|
|
if lib.isDerivation input then ((lib.getOutput "bin" input) + "/bin")
|
|
|
|
else if builtins.isString input then input
|
|
|
|
else throw "unexpected type for input: ${builtins.typeOf input}";
|
2021-09-23 22:40:35 +00:00
|
|
|
|
|
|
|
/* Special-case value representation by type/name */
|
2022-04-08 06:45:09 +00:00
|
|
|
phraseEnvVal = solution: env: val:
|
|
|
|
if env == "inputs" then (colons (map relSafeBinPath val))
|
2021-09-23 22:40:35 +00:00
|
|
|
else if builtins.isString val then val
|
|
|
|
else if builtins.isList val then spaces val
|
2022-04-08 06:45:09 +00:00
|
|
|
else if builtins.isAttrs val then spaces (phraseDirectives solution env val)
|
2021-09-23 22:40:35 +00:00
|
|
|
else nope [ solution env ] "unexpected type: ${builtins.typeOf val}";
|
|
|
|
|
|
|
|
/* Shell-format each env value */
|
|
|
|
shellEnv = solution: env: value:
|
2022-04-08 06:45:09 +00:00
|
|
|
lib.escapeShellArg (phraseEnvVal solution env value);
|
2021-09-23 22:40:35 +00:00
|
|
|
|
|
|
|
/* Build a single ENV=val pair */
|
2022-04-08 06:45:09 +00:00
|
|
|
phraseEnv = solution: env: value:
|
2021-09-23 22:40:35 +00:00
|
|
|
"RESHOLVE_${lib.toUpper env}=${shellEnv solution env value}";
|
|
|
|
|
2022-04-08 06:45:09 +00:00
|
|
|
/* Discard attrs:
|
|
|
|
- claimed by phraseArgs
|
|
|
|
- only needed for binlore.collect
|
|
|
|
*/
|
|
|
|
removeUnneededArgs = value:
|
|
|
|
removeAttrs value [ "scripts" "flags" "unresholved" ];
|
2021-09-23 22:40:35 +00:00
|
|
|
|
|
|
|
/* Verify required arguments are present */
|
|
|
|
validateSolution = { scripts, inputs, interpreter, ... }: true;
|
|
|
|
|
|
|
|
/* Pull out specific solution keys to build ENV=val pairs */
|
2022-04-08 06:45:09 +00:00
|
|
|
phraseEnvs = solution: value:
|
|
|
|
spaces (lib.mapAttrsToList (phraseEnv solution) (removeUnneededArgs value));
|
2021-09-23 22:40:35 +00:00
|
|
|
|
|
|
|
/* Pull out specific solution keys to build CLI argstring */
|
2022-04-08 06:45:09 +00:00
|
|
|
phraseArgs = { flags ? [ ], scripts, ... }:
|
2021-09-23 22:40:35 +00:00
|
|
|
spaces (flags ++ scripts);
|
|
|
|
|
2022-04-08 06:45:09 +00:00
|
|
|
phraseBinloreArgs = value:
|
|
|
|
let
|
|
|
|
hasUnresholved = builtins.hasAttr "unresholved" value;
|
|
|
|
in {
|
|
|
|
drvs = value.inputs ++
|
|
|
|
lib.optionals hasUnresholved [ value.unresholved ];
|
|
|
|
strip = if hasUnresholved then [ value.unresholved ] else [ ];
|
|
|
|
};
|
|
|
|
|
2021-09-23 22:40:35 +00:00
|
|
|
/* Build a single resholve invocation */
|
2022-04-08 06:45:09 +00:00
|
|
|
phraseInvocation = solution: value:
|
2021-09-23 22:40:35 +00:00
|
|
|
if validateSolution value then
|
|
|
|
# we pass resholve a directory
|
2022-04-08 06:45:09 +00:00
|
|
|
"RESHOLVE_LORE=${binlore.collect (phraseBinloreArgs value) } ${phraseEnvs solution value} ${resholve}/bin/resholve --overwrite ${phraseArgs value}"
|
2021-09-23 22:40:35 +00:00
|
|
|
else throw "invalid solution"; # shouldn't trigger for now
|
|
|
|
|
2022-04-08 06:45:09 +00:00
|
|
|
injectUnresholved = solutions: unresholved: (builtins.mapAttrs (name: value: value // { inherit unresholved; } ) solutions);
|
|
|
|
|
2021-09-23 22:40:35 +00:00
|
|
|
/* Build resholve invocation for each solution. */
|
2022-04-08 06:45:09 +00:00
|
|
|
phraseCommands = solutions: unresholved:
|
|
|
|
builtins.concatStringsSep "\n" (
|
|
|
|
lib.mapAttrsToList phraseInvocation (injectUnresholved solutions unresholved)
|
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
subshell/PS4/set -x and : command to output resholve envs
|
|
|
|
and invocation. Extra context makes it clearer what the
|
|
|
|
Nix API is doing, makes nix-shell debugging easier, etc.
|
|
|
|
*/
|
|
|
|
phraseContext = { invokable, prep ? ''cd "$out"'' }: ''
|
|
|
|
(
|
|
|
|
${prep}
|
|
|
|
PS4=$'\x1f'"\033[33m[resholve context]\033[0m "
|
|
|
|
set -x
|
|
|
|
: invoking resholve with PWD=$PWD
|
|
|
|
${invokable}
|
|
|
|
)
|
|
|
|
'';
|
|
|
|
phraseContextForPWD = invokable: phraseContext { inherit invokable; prep = ""; };
|
|
|
|
phraseContextForOut = invokable: phraseContext { inherit invokable; };
|
|
|
|
|
|
|
|
phraseSolution = name: solution: (phraseContextForOut (phraseInvocation name solution));
|
|
|
|
phraseSolutions = solutions: unresholved:
|
|
|
|
phraseContextForOut (phraseCommands solutions unresholved);
|
|
|
|
|
|
|
|
writeScript = name: partialSolution: text:
|
|
|
|
writeTextFile {
|
|
|
|
inherit name text;
|
|
|
|
executable = true;
|
|
|
|
checkPhase = ''
|
|
|
|
${(phraseContextForPWD (
|
|
|
|
phraseInvocation name (
|
|
|
|
partialSolution // {
|
|
|
|
scripts = [ "${placeholder "out"}" ];
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)}
|
2023-01-14 20:38:32 +00:00
|
|
|
'' + lib.optionalString (partialSolution.interpreter != "none") ''
|
2022-04-08 06:45:09 +00:00
|
|
|
${partialSolution.interpreter} -n $out
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
writeScriptBin = name: partialSolution: text:
|
|
|
|
writeTextFile rec {
|
|
|
|
inherit name text;
|
|
|
|
executable = true;
|
|
|
|
destination = "/bin/${name}";
|
|
|
|
checkPhase = ''
|
|
|
|
${phraseContextForOut (
|
|
|
|
phraseInvocation name (
|
|
|
|
partialSolution // {
|
|
|
|
scripts = [ "bin/${name}" ];
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
2023-01-14 20:38:32 +00:00
|
|
|
'' + lib.optionalString (partialSolution.interpreter != "none") ''
|
2022-04-08 06:45:09 +00:00
|
|
|
${partialSolution.interpreter} -n $out/bin/${name}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
mkDerivation = { pname
|
|
|
|
, src
|
|
|
|
, version
|
|
|
|
, passthru ? { }
|
|
|
|
, solutions
|
|
|
|
, ...
|
|
|
|
}@attrs:
|
|
|
|
let
|
|
|
|
inherit stdenv;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Knock out our special solutions arg, but otherwise
|
|
|
|
just build what the caller is giving us. We'll
|
|
|
|
actually resholve it separately below (after we
|
|
|
|
generate binlore for it).
|
|
|
|
*/
|
|
|
|
unresholved = (stdenv.mkDerivation ((removeAttrs attrs [ "solutions" ])
|
|
|
|
// {
|
2022-09-13 01:46:44 +00:00
|
|
|
inherit version src;
|
|
|
|
pname = "${pname}-unresholved";
|
2022-04-08 06:45:09 +00:00
|
|
|
}));
|
|
|
|
in
|
|
|
|
/*
|
|
|
|
resholve in a separate derivation; some concerns:
|
|
|
|
- we aren't keeping many of the user's args, so they
|
|
|
|
can't readily set LOGLEVEL and such...
|
|
|
|
- not sure how this affects multiple outputs
|
|
|
|
*/
|
|
|
|
lib.extendDerivation true passthru (stdenv.mkDerivation {
|
|
|
|
src = unresholved;
|
2022-09-13 01:46:44 +00:00
|
|
|
inherit version pname;
|
2022-04-08 06:45:09 +00:00
|
|
|
buildInputs = [ resholve ];
|
2023-01-10 18:53:05 +00:00
|
|
|
disallowedReferences = [ resholve ];
|
2022-04-08 06:45:09 +00:00
|
|
|
|
|
|
|
# retain a reference to the base
|
|
|
|
passthru = unresholved.passthru // {
|
|
|
|
unresholved = unresholved;
|
2022-09-15 16:10:29 +00:00
|
|
|
# fallback attr for update bot to query our src
|
|
|
|
originalSrc = unresholved.src;
|
2022-04-08 06:45:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
# do these imply that we should use NoCC or something?
|
|
|
|
dontConfigure = true;
|
|
|
|
dontBuild = true;
|
|
|
|
|
|
|
|
installPhase = ''
|
|
|
|
cp -R $src $out
|
|
|
|
'';
|
|
|
|
|
|
|
|
# enable below for verbose debug info if needed
|
|
|
|
# supports default python.logging levels
|
|
|
|
# LOGLEVEL="INFO";
|
|
|
|
preFixup = phraseSolutions solutions unresholved;
|
2022-09-13 01:46:44 +00:00
|
|
|
|
|
|
|
# don't break the metadata...
|
|
|
|
meta = unresholved.meta;
|
2022-04-08 06:45:09 +00:00
|
|
|
});
|
2021-09-23 22:40:35 +00:00
|
|
|
}
|