1 # Using resholve's Nix API
2 resholve replaces bare references (subject to a PATH search at runtime) to external commands and scripts with absolute paths.
4 This small super-power helps ensure script dependencies are declared, present, and don't unexpectedly shift when the PATH changes.
6 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.
8 <!-- generated from resholve's repo; best to suggest edits there (or at least notify me) -->
10 This will hopefully make its way into the Nixpkgs manual soon, but
11 until then I'll outline how to use the functions:
12 - `resholve.mkDerivation` (formerly `resholvePackage`)
13 - `resholve.writeScript` (formerly `resholveScript`)
14 - `resholve.writeScriptBin` (formerly `resholveScriptBin`)
15 - `resholve.phraseSolution` (new in resholve 0.8.0)
17 > Fair warning: resholve does *not* aspire to resolving all valid Shell
18 > scripts. It depends on the OSH/Oil parser, which aims to support most (but
19 > not all) Bash. resholve aims to be a ~90% sort of solution.
23 The main difference between `resholve.mkDerivation` and other builder functions
24 is the `solutions` attrset, which describes which scripts to resolve and how.
25 Each "solution" (k=v pair) in this attrset describes one resholve invocation.
27 > NOTE: For most shell packages, one invocation will probably be enough:
28 > - Packages with a single script will only need one solution.
29 > - Packages with multiple scripts can still use one solution if the scripts
30 > don't require conflicting directives.
31 > - Packages with scripts that require conflicting directives can use multiple
32 > solutions to resolve the scripts separately, but produce a single package.
34 `resholve.writeScript` and `resholve.writeScriptBin` support a _single_
35 `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.
37 ## Basic `resholve.mkDerivation` Example
39 Here's a simple example of how `resholve.mkDerivation` is already used in nixpkgs:
41 <!-- TODO: figure out how to pull this externally? -->
53 resholve.mkDerivation rec {
57 src = fetchFromGitHub {
60 rev = "refs/tags/v${version}";
61 hash = "sha256-FDn1OETkYIpMenk8QAAHvfNZcSzqGl5xrD0fAZPVmRM=";
68 sed -i '2i GOSS_PATH=${goss}/bin/goss' extras/dgoss/dgoss
69 install -D extras/dgoss/dgoss $out/bin/dgoss
74 scripts = [ "bin/dgoss" ];
75 interpreter = "${bash}/bin/bash";
76 inputs = [ coreutils which ];
78 "$CONTAINER_RUNTIME" = true;
84 homepage = "https://github.com/goss-org/goss/blob/v${version}/extras/dgoss/README.md";
85 changelog = "https://github.com/goss-org/goss/releases/tag/v${version}";
86 description = "Convenience wrapper around goss that aims to bring the simplicity of goss to docker containers";
87 license = licenses.asl20;
88 platforms = platforms.linux;
89 maintainers = with maintainers; [ hyzual anthonyroussel ];
90 mainProgram = "dgoss";
96 ## Basic `resholve.writeScript` and `resholve.writeScriptBin` examples
98 Both of these functions have the same basic API. The examples are a little
99 trivial, so I'll also link to some real-world examples:
100 - [shell.nix from abathur/tdverpy](https://github.com/abathur/tdverpy/blob/e1f956df3ed1c7097a5164e0c85b178772e277f5/shell.nix#L6-L13)
104 resholvedScript = resholve.writeScript "name" {
106 interpreter = "${bash}/bin/bash";
111 resholvedScriptBin = resholve.writeScriptBin "name" {
113 interpreter = "${bash}/bin/bash";
122 ## Basic `resholve.phraseSolution` example
124 This function has a similar API to `writeScript` and `writeScriptBin`, except it does require a `scripts` attr. It is intended to make resholve a little easier to mix into more types of build. This example is a little
125 trivial for now. If you have a real usage that you find helpful, please PR it.
128 { stdenv, resholve, module1 }:
130 stdenv.mkDerivation {
131 # pname = "testmod3";
132 # version = "unreleased";
137 install conjure.sh $out/bin/conjure.sh
138 ${resholve.phraseSolution "conjure" {
139 scripts = [ "bin/conjure.sh" ];
140 interpreter = "${bash}/bin/bash";
141 inputs = [ module1 ];
143 external = [ "jq" "openssl" ];
153 `resholve.mkDerivation` maps Nix types/idioms into the flags and environment variables
154 that the `resholve` CLI expects. Here's an overview:
156 | Option | Type | Containing |
157 |--------|------|------------|
158 | scripts | `<list>` | scripts to resolve (`$out`-relative paths) |
159 | interpreter | `"none"` `<path>` | The absolute interpreter `<path>` for the script's shebang. The special value `none` ensures there is no shebang. |
160 | inputs | `<packages>` `<paths>` | A list of packages and string paths to directories/files to resolve external dependencies from. |
161 | fake | `<directives>` | pretend some commands exist |
162 | fix | `<directives>` | fix things we can't auto-fix/ignore |
163 | keep | `<directives>` | keep things we can't auto-fix/ignore |
164 | lore | `<directory>` | control nested resolution |
165 | execer | `<statements>` | modify nested resolution |
166 | wrapper | `<statements>` | modify nested resolution |
167 | prologue | `<file>` | insert file before resolved script |
168 | epilogue | `<file>` | insert file after resolved script |
170 <!-- TODO: section below is largely custom for nixpkgs, but I would LIKE to wurst it. -->
172 ## Controlling resolution with directives
174 In order to resolve a script, resholve will make you disambiguate how it should
175 handle any potential problems it encounters with directives. There are currently
177 1. `fake` directives tell resholve to pretend it knows about an identifier
178 such as a function, builtin, external command, etc. if there's a good reason
179 it doesn't already know about it. Common examples:
180 - builtins for a non-bash shell
182 - platform-specific external commands in cross-platform conditionals
183 2. `fix` directives give resholve permission to fix something that it can't
184 safely fix automatically. Common examples:
185 - resolving commands in aliases (this is appropriate for standalone scripts
186 that use aliases non-interactively--but it would prevent profile/rc
187 scripts from using the latest current-system symlinks.)
188 - resolve commands in a variable definition
189 - resolve an absolute command path from inputs as if it were a bare reference
190 - force resholve to resolve known security wrappers
191 3. `keep` directives tell resholve not to raise an error (i.e., ignore)
192 something it would usually object to. Common examples:
193 - variables used as/within the first word of a command
194 - pre-existing absolute or user-relative (~) command paths
195 - dynamic (variable) arguments to commands known to accept/run other commands
197 > NOTE: resholve has a (growing) number of directives detailed in `man resholve`
198 > via `nixpkgs.resholve` (though protections against run-time use of python2 in nixpkgs mean you'll have to set `NIXPKGS_ALLOW_INSECURE=1` to pull resholve into nix-shell).
200 Each of these 3 types is represented by its own attrset, where you can think
201 of the key as a scope. The value should be:
202 - `true` for any directives that the resholve CLI accepts as a single word
203 - a list of strings for all other options
205 TODO: these should be fully-documented here, but I'm already maintaining
206 more copies of their specification/behavior than I like, and continuing to
207 add more at this early date will only ensure that I spend more time updating
208 docs and less time filling in feature gaps.
210 Full documentation may be greatly accelerated if someone can help me sort out
211 single-sourcing. See: https://github.com/abathur/resholve/issues/19
214 This will hopefully make more sense when you see it. Here are CLI examples
215 from the manpage, and the Nix equivalents:
219 # --fake 'f:setUp;tearDown builtin:setopt source:/etc/bashrc'
221 # fake accepts the initial of valid identifier types as a CLI convenience.
222 # Use full names in the Nix API.
223 function = [ "setUp" "tearDown" ];
224 builtin = [ "setopt" ];
225 source = [ "/etc/bashrc" ];
228 # --fix 'aliases $GIT:gix /bin/bash'
230 # all single-word directives use `true` as value
236 # --keep 'source:$HOME /etc/bashrc ~/.bashrc'
238 source = [ "$HOME" ];
239 "/etc/bashrc" = true;
246 > **Note:** For now, at least, you'll need to reference the manpage to completely understand these examples.
248 ## Controlling nested resolution with lore
250 Initially, resolution of commands in the arguments to command-executing
251 commands was limited to one level for a hard-coded list of builtins and
252 external commands. resholve can now resolve these recursively.
254 This feature combines information (_lore_) that the resholve Nix API
255 obtains via binlore ([nixpkgs](../../tools/analysis/binlore), [repo](https://github.com/abathur/resholve)),
256 with some rules (internal to resholve) for locating sub-executions in
257 some of the more common commands.
259 - "execer" lore identifies whether an executable can, cannot,
260 or might execute its arguments. Every "can" or "might" verdict requires:
261 - an update to the matching rules in [binlore](https://github.com/abathur/binlore)
262 if there's absolutely no exec in the executable and binlore just lacks
263 rules for understanding this
264 - an override in [binlore](https://github.com/abathur/binlore) if there is
265 exec but it isn't actually under user control
266 - a parser in [resholve](https://github.com/abathur/resholve) capable of
267 isolating the exec'd words if the command does have exec under user
269 - overriding the execer lore for the executable if manual triage indicates
270 that all of the invocations in the current package don't include any
271 commands that the executable would exec
272 - if manual triage turns up any commands that would be exec'd, use some
273 non-resholve tool to patch/substitute/replace them before or after you
274 run resholve on them (if before, you may need to also add keep directives
275 for these absolute paths)
277 - "wrapper" lore maps shell exec wrappers to the programs they exec so
278 that resholve can substitute an executable's verdict for its wrapper's.
280 > **Caution:** At least when it comes to common utilities, it's best to treat
281 > overrides as a stopgap until they can be properly handled in resholve and/or
282 > binlore. Please report things you have to override and, if possible, help
285 There will be more mechanisms for controlling this process in the future
286 (and your reports/experiences will play a role in shaping them...) For now,
287 the main lever is the ability to substitute your own lore. This is how you'd
292 # --execer 'cannot:${openssl.bin}/bin/openssl can:${openssl.bin}/bin/c_rehash'
295 This is the same verdict binlore will
296 come up with. It's a no-op just to demo
297 how to fiddle lore via the Nix API.
299 "cannot:${openssl.bin}/bin/openssl"
300 # different verdict, but not used
301 "can:${openssl.bin}/bin/c_rehash"
304 # --wrapper '${gnugrep}/bin/egrep:${gnugrep}/bin/grep'
307 This is the same verdict binlore will
308 come up with. It's a no-op just to demo
309 how to fiddle lore via the Nix API.
311 "${gnugrep}/bin/egrep:${gnugrep}/bin/grep"
317 The format is fairly simple to generate--you can script your own generator if
318 you need to modify the lore.