Home Explore Blog Models CI



nixpkgs

1st chunk of `pkgs/development/misc/resholve/README.md`
59ecf1b4847ade3061c16b31dea9bda656c73dc0ec7ecc5c0000000100000fa4
# Using resholve's Nix API
resholve replaces bare references (subject to a PATH search at runtime) to external commands and scripts with absolute paths.

This small super-power helps ensure script dependencies are declared, present, and don't unexpectedly shift when the PATH changes.

resholve is developed to enable the Nix package manager to package and integrate Shell projects, but its features are not Nix-specific and inevitably have other applications.

<!-- generated from resholve's repo; best to suggest edits there (or at least notify me) -->

This will hopefully make its way into the Nixpkgs manual soon, but
until then I'll outline how to use the functions:
- `resholve.mkDerivation` (formerly `resholvePackage`)
- `resholve.writeScript` (formerly `resholveScript`)
- `resholve.writeScriptBin` (formerly `resholveScriptBin`)
- `resholve.phraseSolution` (new in resholve 0.8.0)

> Fair warning: resholve does *not* aspire to resolving all valid Shell
> scripts. It depends on the OSH/Oil parser, which aims to support most (but
> not all) Bash. resholve aims to be a ~90% sort of solution.

## API Concepts

The main difference between `resholve.mkDerivation` and other builder functions
is the `solutions` attrset, which describes which scripts to resolve and how.
Each "solution" (k=v pair) in this attrset describes one resholve invocation.

> NOTE: For most shell packages, one invocation will probably be enough:
> - Packages with a single script will only need one solution.
> - Packages with multiple scripts can still use one solution if the scripts
>   don't require conflicting directives.
> - Packages with scripts that require conflicting directives can use multiple
>   solutions to resolve the scripts separately, but produce a single package.

`resholve.writeScript` and `resholve.writeScriptBin` support a _single_
`solution` attrset. This is basically the same as any single solution in `resholve.mkDerivation`, except that it doesn't need a `scripts` attr (it is automatically added). `resholve.phraseSolution` also only accepts a single solution--but it _does_ still require the `scripts` attr.

## Basic `resholve.mkDerivation` Example

Here's a simple example of how `resholve.mkDerivation` is already used in nixpkgs:

<!-- TODO: figure out how to pull this externally? -->

```nix
{
  bash,
  coreutils,
  gnused,
  goss,
  lib,
  resholve,
  which,
}:

resholve.mkDerivation rec {
  pname = "dgoss";
  version = goss.version;
  src = goss.src;

  dontConfigure = true;
  dontBuild = true;

  installPhase = ''
    sed -i '2i GOSS_PATH=${goss}/bin/goss' extras/dgoss/dgoss
    install -D extras/dgoss/dgoss $out/bin/dgoss
  '';

  solutions = {
    default = {
      scripts = [ "bin/dgoss" ];
      interpreter = "${bash}/bin/bash";
      inputs = [
        coreutils
        gnused
        which
      ];
      keep = {
        "$CONTAINER_RUNTIME" = true;
      };
    };
  };

  meta = {
    homepage = "https://github.com/goss-org/goss/blob/v${version}/extras/dgoss/README.md";
    changelog = "https://github.com/goss-org/goss/releases/tag/v${version}";
    description = "Convenience wrapper around goss that aims to bring the simplicity of goss to docker containers";
    license = lib.licenses.asl20;
    platforms = lib.platforms.linux;
    maintainers = with lib.maintainers; [
      hyzual
      anthonyroussel
    ];
    mainProgram = "dgoss";
  };
}
```


## Basic `resholve.writeScript` and `resholve.writeScriptBin` examples

Both of these functions have the same basic API. The examples are a little
trivial, so I'll also link to some real-world examples:
- [shell.nix from abathur/tdverpy](https://github.com/abathur/tdverpy/blob/e1f956df3ed1c7097a5164e0c85b178772e277f5/shell.nix#L6-L13)

```nix
{
  resholvedScript =
    resholve.writeScript "name"
      {
        inputs = [ file ];
        interpreter = "${bash}/bin/bash";
      }
      ''
        echo "Hello"
        file .
      '';
  resholvedScriptBin =
    resholve.writeScriptBin "name"

Title: Using resholve's Nix API for Shell Script Dependency Management
Summary
This document introduces `resholve`'s Nix API, a tool that replaces bare references to external commands in shell scripts with absolute paths to ensure dependencies are explicit and stable. While primarily designed for Nix, its features have broader applications. The API includes functions like `resholve.mkDerivation`, `resholve.writeScript`, `resholve.writeScriptBin`, and `resholve.phraseSolution`. A key concept is the `solutions` attrset, which defines how scripts are resolved, with `resholve.mkDerivation` supporting multiple solutions and `resholve.writeScript`/`writeScriptBin` a single one. It notes that `resholve` aims for approximately 90% compatibility with Bash scripts due to its reliance on the OSH/Oil parser, and provides examples of its implementation.